<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 1/5/2016	ELL

include ("check_session.php");
include ("../lib/image_processing.php");

define("MAX_EXEC_TIME", 600);	// in seconds
ini_set("max_execution_time", MAX_EXEC_TIME);

$full_width = $_REQUEST['full_width'];
$full_height = $_REQUEST['full_height'];
$thumb_width = $_REQUEST['thumb_width'];
$thumb_height = $_REQUEST['thumb_height'];
$preserve_thumb_aspect = $_REQUEST['preserve_thumb_aspect'];
$show_titles = $_REQUEST['show_titles'];
$disable_sort = $_REQUEST['disable_sort'];

$rslt = 1;

$query = "TRUNCATE TABLE gallery_params";
$cnt = $bwb_dbh->exec($query);

$query = "INSERT INTO gallery_params SET full_width = '" . $full_width . "', full_height = '" . $full_height . "', thumb_width = '" . $thumb_width . "', thumb_height = '" . $thumb_height . "', preserve_thumb_aspect = '" . $preserve_thumb_aspect . "', show_titles = '" . $show_titles . "', disable_sort = '" . $disable_sort . "'";
$cnt = $bwb_dbh->exec($query);

$doc_root = getenv('DOCUMENT_ROOT');
if (BWB_BASE_DIR == '')
	$userfiles = '';
else
	$userfiles = "/" . BWB_BASE_DIR;
$orig_image_path = $doc_root . $userfiles . "/" . BWB_IMAGE_DIR . "/gallery/orig";
$fs_image_path = $doc_root . $userfiles . "/" . BWB_IMAGE_DIR . "/gallery/fs";
$tn_image_path = $doc_root . $userfiles . "/" . BWB_IMAGE_DIR . "/gallery/tn";

if (is_dir($orig_image_path)) {
	if ($dh = opendir($orig_image_path)) {
		while (($file = readdir($dh)) !== false) {
			if ((filetype($orig_image_path . "/" . $file) != "dir") && ($file != ".") && ($file != ".."))	{
				if (!copy($orig_image_path . "/" . $file, $fs_image_path . "/" . $file)) {
//					echo "failed to copy $orig_image_path/$file to $fs_image_path...\n";
					$rslt = 2;
				}
				if (!copy($orig_image_path . "/" . $file, $tn_image_path . "/" . $file)) {
//					echo "failed to copy $orig_image_path/$file to $tn_image_path...\n";
					$rslt = 3;
				}
			}
		}
		closedir($dh);
	}
}

$fs_images = array();
if (is_dir($fs_image_path)) {
    if ($dh = opendir($fs_image_path)) {
        while (($file = readdir($dh)) !== false) {
			if ((filetype($fs_image_path . "/" . $file) != "dir") && ($file != ".") && ($file != ".."))
            	$fs_images[] = $file;
        }
        closedir($dh);
    }
}
else
	$rslt = -1;

$tn_images = array();
if (is_dir($tn_image_path)) {
    if ($dh = opendir($tn_image_path)) {
        while (($file = readdir($dh)) !== false) {
			if ((filetype($tn_image_path . "/" . $file) != "dir") && ($file != ".") && ($file != ".."))
            	$tn_images[] = $file;
        }
        closedir($dh);
    }
}
else
	$rslt = -2;

foreach ($fs_images as $file)	{
	$result = resize_image($fs_image_path . "/" . $file, $full_width, $full_height, $fs_image_path . "/" . $file, 1);
}

foreach ($tn_images as $file)	{
	$result = resize_image($tn_image_path . "/" . $file, $thumb_width, $thumb_height, $tn_image_path . "/" . $file, 1, $preserve_thumb_aspect);
}

print $rslt;
	
?>
