Location: PHPKode > scripts > Photos > photos/engine.inc.php
<?php

// This file is part of Photos
// Copyright (c) 2001-2004 Alex King
// see LICENSE.txt for more information

$image_entry = new image;

// put vars into object
if (isset($_REQUEST["id"])) {
	$image_entry->id = intval($_REQUEST["id"]);
}
if (isset($_REQUEST["title"])) {
	$image_entry->title = $_REQUEST["title"];
}
if (isset($_REQUEST["description"])) {
	$image_entry->description = $_REQUEST["description"];
}
if (isset($_REQUEST["keywords"])) {
	$image_entry->keywords = $_REQUEST["keywords"];
}
if (isset($_REQUEST["people"])) {
	$image_entry->people = $_REQUEST["people"];
}
if (isset($_REQUEST["event"])) {
	$image_entry->event = $_REQUEST["event"];
}
if (isset($_REQUEST["photographer"])) {
	$image_entry->photographer = $_REQUEST["photographer"];
}
if (isset($_REQUEST["location"])) {
	$image_entry->location = $_REQUEST["location"];
}
if (isset($_REQUEST["state"])) {
	$image_entry->state = $_REQUEST["state"];
}
if (isset($_REQUEST["country"])) {
	$image_entry->country = $_REQUEST["country"];
}
if (isset($_REQUEST["indoors"])) {
	$image_entry->indoors = $_REQUEST["indoors"];
}
if (isset($_REQUEST["URL"])) {
	$image_entry->URL = stripslashes($_REQUEST["URL"]);
}
if (isset($_REQUEST["datetaken"])) {
	$image_entry->datetaken = $_REQUEST["datetaken"];
}
if (isset($_REQUEST["dateentered"])) {
	$image_entry->dateentered = $_REQUEST["dateentered"];
}
if (isset($_REQUEST["portfolio"])) {
	$image_entry->portfolio = $_REQUEST["portfolio"];
}
$image_entry->needdetails = 0;

// check to make sure the date is valid
if (!empty($image_entry->datetaken) && checkdate(substr($image_entry->datetaken, 5, 2), substr($image_entry->datetaken, 8, 2), substr($image_entry->datetaken, 0, 4)) == false) {
	$screen = "entry";
	$page->msgs[] = new msg('error'
	                       ,$lang->str("err_invalid_date")
	                       );
	$page->add_flag("dont_save");
}
// check to make sure the entry hasn't been updated by another user 
if (!empty($image_entry->dateentered)) {
	$old_image = new image;
	$old_image = $old_image->retrieve($image_entry->id);

	if ($image_entry->dateentered != $old_image->dateentered) {
		if ($image_entry->country != $old_image->country || 
			$image_entry->datetaken != $old_image->datetaken || 
			$image_entry->description != $old_image->description || 
			$image_entry->event != $old_image->event || 
			$image_entry->indoors != $old_image->indoors || 
			$image_entry->location != $old_image->location || 
			$image_entry->keywords != $old_image->keywords || 
			$image_entry->people != $old_image->people || 
			$image_entry->photographer != $old_image->photographer || 
			$image_entry->state != $old_image->state ||
			$image_entry->title != $old_image->title) {
			$page->msgs[] = new msg('error'
			                       ,$lang->str("err_conflict"
			                                  ,array(get_friendly_timestamp($image_entry->dateentered)
			                                        ,get_friendly_timestamp($old_image->dateentered)
			                                        )
			                                  )
			                       );
			$page->add_flag("dont_save");
			$screen = "resolve";
		}
	}
}

if ($action == "save" && !in_array("dont_save", $page->flags)) {
// check what to do - insert or update	
	if (empty($image_entry->id)) {
		// check to see if URL is already in the database
		if (is_in_database('URL',$image_entry->URL) == true) {
			$page->msgs[] = new msg('warning'
			                       ,$lang->str("warn_already_in_db"
			                                  ,array($image_entry->URL)
			                                  )
			                       );
		}
// create thumbnail to put in the database
		$image_entry->thumbnail = $image_entry->create_thumbnail();
// get orientation of photo
		$image_entry->orientation = $image_entry->get_orientation();
		$image_entry->get_EXIF();
// put new image in the database
		$image_entry->store();
// get ID of inserted photo
		$image_entry->id = mysql_insert_id();
// remove slashes before displaying
		$image_entry = $image_entry->clean();		
		$page->msgs[] = new msg('info'
		                       ,$lang->str("info_saved"
		                                  ,array($image_entry->id
		                                        ,html(stripslashes($image_entry->title))
		                                        ,date("g:i:s a, F j, Y")
		                                        )
		                                  )
		                       );
	}
	else {
		$update_thumb = 0;
		if (isset($_REQUEST["update_thumbnail"]) && $_REQUEST["update_thumbnail"] == 1) {
			@unlink("temp/".$_SESSION["load_thumbs"][$image_entry->URL]);
			@unlink("temp/temp_display_img_".$image_entry->id.".jpg");
			$image_entry->thumbnail = $image_entry->create_thumbnail($image_entry->URL, 160, 1);
			$update_thumb = 1;
		}
// get orientation of photo
		$image_entry->orientation = $image_entry->get_orientation();
		$image_entry->get_EXIF();
// update image in the database
		$image_entry->update($update_thumb);
		$image_entry = $image_entry->clean();		
		$page->msgs[] = new msg('info'
		                       ,$lang->str("info_saved"
		                                  ,array($image_entry->id
		                                        ,html(stripslashes($image_entry->title))
		                                        ,date("g:i:s a, F j, Y")
		                                        )
		                                  )
		                       );
		if (isset($_REQUEST["post_action"]) && $_REQUEST["post_action"] == "add_details") {
			$screen = "entry";
			$action = "add_details";
		}
	}
	$image_entry->dateentered = date("YmdHis");
}

switch ($screen) {
	case "view":
// ---------------------------------------------------------------------------
// the view screen will either store a new entry or retrieve an existing entry

		if (!is_in_database('ID', $image_entry->id)) {
			$page->msgs[] = new msg('error'
								   ,$lang->str("err_no_photo"
											  ,array($image_entry->id)
											  )
								   );
			$show_body = 0;
			$show_image = 0;
		}
		else {
			if ($action != "save") {
				$image_entry = $image_entry->retrieve($image_entry->id);
			}
			if ($custom->minimize_data_transfer == 1) {
				$temp_file_name = 'temp_display_img_'.$image_entry->id.'.jpg';
				if (file_exists('temp/'.$temp_file_name)) {
					$image_entry->display_URL = 'temp/'.$temp_file_name;
				}
				else if (isset($_SESSION["temp_images"]) && in_array($image_entry->URL, $_SESSION["temp_images"])) {
					$image_entry->display_URL = $_SESSION["temp_images"][$image_entry->URL];
				}
				else {
					$image_entry->display_URL = save_JPEG_file_from_JPEG($image_entry->URL, 800, "temp/", $temp_file_name, 70);
				}
			}
			else {
				$image_entry->display_URL = str_replace(" ", "%20", $image_entry->URL);
			}
			$preview_dimensions = get_preview_dimensions($image_entry->display_URL);
		}
// set the screen type
		$page->body = "view.inc.php";
		$page->title = $lang->str("pt_title", array(html(stripslashes($image_entry->title))));
		break;
	case "search":
// ---------------------------------------------------------------------------
// search form for entering search criteria
		$page->init_string .= " setFocus();";
// delete temp images
		clean_up_temp_dir();
// set the screen type
		$page->body = "search.inc.php";
		$show_image = 0;
		$page->title = $lang->str("pt_title", array($lang->str("pt_search")));
		break;
	case "transaction":
// ---------------------------------------------------------------------------
		if (isset($_SESSION["saved"]) && count($_SESSION["saved"]) > 0) {
			$page->body = "transaction.inc.php";
			$show_image = 0;
			$page->title = $lang->str("pt_photos");
		}
		else {
			header("Location: index.php?screen=load_folder");
		}
		break;
	case "list":
// ---------------------------------------------------------------------------
// return a list of images matching a query

		$images = new image_list;
		
		if (isset($_REQUEST["items_per_page"])) {
			$images->show_n = $_REQUEST["items_per_page"];
		}
		if (isset($_REQUEST["result_view"])) {
			$result_view = $_REQUEST["result_view"];
		}
		else {
			$result_view = "thumbnail";
		}
		
		if (empty($nav_command)) {
// we need to find matching images
// build query string criteria
			$query = "";
			$query = query_add_property($query, 'orientation', $_REQUEST["orientation"], '=', '');
			$query = query_add_property($query, 'needdetails', $_REQUEST["needdetails"], '=', '');
			$query = query_add_property($query, 'portfolio', $_REQUEST["portfolio"], '=', '');
			$query = query_add_property($query, 'ID', $_REQUEST["id"], '=', '');
			$query = query_add_multi_property_array($query, array('title','description'), $_REQUEST["title"], 'AND');
			$query = query_add_property_array($query, 'keywords', $_REQUEST["keywords"], 'AND');
			$query = query_add_property_array($query, 'people', $_REQUEST["people"], 'AND');
			$query = query_add_property_array($query, 'photographer', $_REQUEST["photographer"], 'AND');
			$query = query_add_property_array($query, 'event', $_REQUEST["event"], 'AND');
			$query = query_add_property_array($query, 'location', $_REQUEST["location"], 'AND');
			$query = query_add_property($query, 'state', $_REQUEST["state"]);
			$query = query_add_property($query, 'country', $_REQUEST["country"]);
			$query = query_add_property($query, 'indoors', $_REQUEST["indoors"]);
			$query = query_add_property_array($query, 'URL', $_REQUEST["URL"], 'AND');
			$query = query_add_date_range_property($query, 'datetaken', $_REQUEST["datetaken_from_month"], $_REQUEST["datetaken_from_day"], $_REQUEST["datetaken_from_year"], ">=");
			$query = query_add_date_range_property($query, 'datetaken', $_REQUEST["datetaken_to_month"], $_REQUEST["datetaken_to_day"], $_REQUEST["datetaken_to_year"], "<=");
			$query = query_add_property($query, 'camera_make', $_REQUEST["camera_make"]);
			$query = query_add_property($query, 'camera_model', $_REQUEST["camera_model"]);
			$query = query_add_property($query, 'flash', $_REQUEST["flash"]);
			$query = query_add_property($query, 'fnumber', $_REQUEST["fnumber_more"], '>=', '');
			$query = query_add_property($query, 'fnumber', $_REQUEST["fnumber_less"], '<=', '');
			$query = query_add_property($query, 'focal_length', $_REQUEST["focal_length_more"], '>=', '');
			$query = query_add_property($query, 'focal_length', $_REQUEST["focal_length_less"], '<=', '');
			$query = query_add_property($query, 'ISO', $_REQUEST["ISO_more"], '>=', '');
			$query = query_add_property($query, 'ISO', $_REQUEST["ISO_less"], '<=', '');
			$query = query_add_property($query, 'shutter', $_REQUEST["shutter_more"], '>=', '');
			$query = query_add_property($query, 'shutter', $_REQUEST["shutter_less"], '<=', '');
			if (!empty($_REQUEST["dateentered_from_year"]) &&
				!empty($_REQUEST["dateentered_from_month"]) &&
				!empty($_REQUEST["dateentered_from_day"])) {
				$query = query_add_property($query, 'dateentered', $_REQUEST["dateentered_from_year"].$_REQUEST["dateentered_from_month"].$_REQUEST["dateentered_from_day"].'000000', ">=", '');
			}
			if (!empty($_REQUEST["dateentered_to_year"]) &&
				!empty($_REQUEST["dateentered_to_month"]) &&
				!empty($_REQUEST["dateentered_to_day"])) {
				$query = query_add_property($query, 'dateentered', $_REQUEST["dateentered_to_year"].$_REQUEST["dateentered_to_month"].$_REQUEST["dateentered_to_day"].'235959', "<=", '');
			}
	
// get all photos if no query data entered
			if ($query == "") {
				$query = query_add_property_array($query, 'title', "%", 'AND');
			}
			
			$query = "SELECT ID FROM $database->table_name WHERE ( ".$query." ) ORDER BY datetaken, ID";
	
			if ($debug == 1) { 
				print("<p>Select by Search Criteria Query: <span class=\"code\">".$query."</span>"); 
			}
	
			$results = mysql_query($query)
					or die("error: ".mysql_error());
	
			$images->items = array();
			$images->items_string = "";
			
			while (list($id) = mysql_fetch_array($results)) {
				$images->items[] = $id;
				$images->items_string = $images->items_string.$id.",";
			}
			
			$images->items_string = substr($images->items_string, 0, -1);
			$images->count = count($images->items);
	
			if ($debug == 1) { 
				print("<p>\$images->items_string: <span class=\"code\">".$images->items_string."</span>"); 
				print("<p>\$images->count: <span class=\"code\">".$images->count."</span>"); 
				print("<p>\$images->items: <span class=\"code\">");
					print_r(array_values($images->items));
					print("</span>"); 
				print("<p>\$images->position: <span class=\"code\">".$images->position."</span>"); 
			}
		}
		elseif (!empty($nav_command)) {
// we already have our list of images, need to move around the list
			$images = $images->set_defaults($_REQUEST["list_items"]);
			$images->position = $_REQUEST["position"];
			
			$images = $images->navigate($nav_command);
		}
	
		$images->items_shown = $images->get_items_shown();
	
// set the screen type
		$page->body = "list.inc.php";
		$page->title = $lang->str("pt_title", array($lang->str("pt_search_results")));
		break;
	case "history":
// ---------------------------------------------------------------------------
// return a list of images matching a query

		$images = new image_list;
		
		$result_view = "thumbnail";
		$images->show_n = 15;
		
		$query = "SELECT ID FROM $database->table_name ORDER BY dateentered DESC LIMIT 15"; 
		$result = mysql_query($query)
				or die("error: ".mysql_error());
	
		$images->items = array();
		$images->items_string = "";
		
		while (list($id) = mysql_fetch_array($result)) {
			$images->items[] = $id;
		}
// get last 15, or fewer if not 15 photos
		$max = count($images->items);
		for ($i = 0; $i < $max; $i++) {
			$images->items_string = $images->items_string.$images->items[$i].",";
		}			
		$images->items_shown = $images->get_items_shown();
		$images->count = count($images->items);

// set the screen type
		$page->body = "history.inc.php";
		$page->title = $lang->str("pt_title", array($lang->str("pt_history")));
		break;
	case "portfolio":
// ---------------------------------------------------------------------------
// return a list of images flagged as "portfolio" shots

		$images = new image_list;
		$images->show_n = 9;
		if (empty($_REQUEST["result_view"])) {
			$result_view = "contact_sheet";
		}
		else {
			$result_view = $_REQUEST["result_view"];
		}
	
		if (empty($nav_command)) {
			$query = "SELECT ID FROM $database->table_name WHERE ( portfolio = '1' ) ORDER BY datetaken";
	
			$results = mysql_query($query)
					or die("error: ".mysql_error());
	
			$images->items = array();
			$images->items_string = "";
			
			while (list($id) = mysql_fetch_array($results)) {
				$images->items[] = $id;
				$images->items_string = $images->items_string.$id.",";
			}
			$images->items_string = substr($images->items_string, 0, -1);
			$images->count = count($images->items);
	
			if ($debug == 1) { 
				print("<p>Select by Search Criteria Query: <span class=\"code\">".$query."</span>"); 
				print("<p>\$images->items_string: <span class=\"code\">".$images->items_string."</span>"); 
				print("<p>\$images->count: <span class=\"code\">".$images->count."</span>"); 
				print("<p>\$images->items: <span class=\"code\">");
					print_r(array_values($images->items));
					print("</span>"); 
				print("<p>\$images->position: <span class=\"code\">".$images->position."</span>"); 
			}
		}
		else if (!empty($nav_command)) {
// we already have our list of images, need to move around the list
			$images = $images->set_defaults($_REQUEST["list_items"]);
			$images->position = $_REQUEST["position"];
			
			$images = $images->navigate($nav_command);
		}
	
		$images->items_shown = $images->get_items_shown();
	
// set the screen type
		$page->body = "list.inc.php";
		$page->title = $lang->str("pt_title", array($lang->str("pt_portfolio")));
		break;
	case "load_folder":
// ---------------------------------------------------------------------------
// the load_folder screen will allow you to specify a folder to grab images from

		$show_image = 0;
		if (!empty($_REQUEST["path"])) {
			$path = $_REQUEST["path"];
		}
		else {
			$page->init_string .= " document.checkPathForm.path.focus();";
		}
		
		$photo_URLs = array();
		if (isset($path) && is_dir($path)) {
			$photo_URLs = load_photos($path);
	
			if (isset($path) && $action == "check_path") {
				if (count($photo_URLs) > 200 && $override != 1) {
					$page->add_flag("load_check");
					$page->msgs[] = new msg('info'
					                       ,$lang->str("info_count_images"
					                                  ,array(count($photo_URLs))
					                                  )
					                       );
					$page->msgs[] = new msg('info'
					                       ,$lang->str("info_still_add"
					                                  ,array('index.php?action=check_path&screen=load_folder&path='.$path.'&override=1')
					                                  )
					                       );
				}
				else {
// add to session
					$test = folder_in_database($path);
					if ($test > 0) {
						$page->msgs[] = new msg('warning'
											   ,$lang->str("warn_photos_in_db"
														  ,array($test
														        ,html($path)
														        ,'index.php?screen=list&URL='.urlencode($path).'&result_view=thumbnail&items_per_page=10'
														        )
														  )
											   );
					}
					$_SESSION["load_thumbs"] = array();
					$_SESSION["load_id"] = get_unique_id();
					for ($i = 0; $i < count($photo_URLs); $i++) {
						$_SESSION["load_thumbs"][$photo_URLs[$i]] = $_SESSION["load_id"]."_".$i.".jpg";
					}
				}
				$page->init_string = "showLoadFolderThumb('temp/".$_SESSION["load_thumbs"][$photo_URLs[0]]."');";
			}
			elseif (isset($path) && $action == "store_images") {
				$_SESSION["saved"] = array();
	
				$image_entry->needdetails = 0;
				if (isset($_REQUEST["needdetails"])) {
					$image_entry->needdetails = $_REQUEST["needdetails"];
				}
				$image_entry->portfolio = 0;
			}
		}	

// set the screen type
		$page->body = "load_folder.inc.php";
		$page->title = $lang->str("pt_title", array($lang->str("pt_add_photos")));
		break;
	case "confirm":
// ---------------------------------------------------------------------------
// the confirm screen will either delete an entry
		if (!empty($id) && ($action == "delete" || $action == "delete_file")) {
			if ($action == "delete_file") { // delete file
				$temp_image = new image;
				$temp_image = $temp_image->retrieve($id);
// delete specified image file
				if (@unlink($temp_image->URL)) {
					$page->msgs[] = new msg('info'
					                       ,$lang->str("info_photo_deleted"
					                                  ,array(html($temp_image->URL))
					                                  )
					                       );
				}
				else {
					$page->msgs[] = new msg('error'
					                       ,$lang->str("err_photo_not_deleted"
					                                  ,array(html($temp_image->URL))
					                                  )
					                       );
				}
			}
// delete image from database
			$query = "DELETE FROM $database->table_name WHERE ID = '$id'";
			mysql_query($query)
				or die("error: ".mysql_error());
// set confirm message
			$page->msgs[] = new msg('info'
								   ,$lang->str("info_photo_id_deleted"
											  ,array($id)
											  )
								   );
		}
		else if ($action == "undo_saved") {
			if (isset($_SESSION["saved"]) && count($_SESSION["saved"]) > 0) {
				$temp = implode(',', $_SESSION["saved"]);
				mysql_query("DELETE FROM $database->table_name WHERE ID IN ($temp)")
					or die("error: ".mysql_error());
				$page->msgs[] = new msg('info'
									   ,$lang->str("info_photos_deleted"
												  ,array(count($_SESSION["saved"]))
												  )
									   );
				$_SESSION["saved"] = array();
			}
		}
		else {
			$page->msgs[] = new msg('info'
								   ,$lang->str("info_did_nothing")
								   );
		}
// set the screen type
		$show_image = 0;
		$page->body = "confirm.inc.php";
		$page->title = $lang->str("pt_photos");
		break;
	case "about":
// ---------------------------------------------------------------------------
// the about screen shows info about this application
// set the screen type
		$show_image = 0;
		$page->body = "about.inc.php";
		$page->title = $lang->str("pt_title", array($lang->str("pt_about")));
		break;
	case "resolve":
// ---------------------------------------------------------------------------
// show resolution form
		$show_image = 1;
		$page->body = "resolve.inc.php";
		$page->title = $lang->str("pt_title", array($lang->str("pt_resolve")));
		break;
	case "entry":
// ---------------------------------------------------------------------------
// show entry/edit form
	
		$image_entry = new image;
		$show_image = 1;
	
		if (!empty($entry_shortcut_id)) {
			$image_entry->URL = $_REQUEST["shortcut_cleanURL"];
			$image_entry->datetaken = $_REQUEST["shortcut_datetaken"];
			$page->init_string .= " copyEntryData('retrieved',0);";
		}
	
		if (isset($action) && $action == "add_details") {
			$id = "";
			$query = "SELECT ID FROM $database->table_name WHERE needdetails = 1 ORDER BY datetaken, ID LIMIT 1";
			$temp = mysql_query($query);
			if ($temp != false) {
				$i = 0;
				while (list($fetched_id) = mysql_fetch_row($temp)) {
					if ($i < 1) {
						$id = $fetched_id;
					}
					$i++;
				}
			}
			if (empty($id)) {
				$page->msgs[] = new msg('info'
				                       ,$lang->str("info_none_need_details")
				                       );
			}
		}
	
		if (empty($id)) {
			if (empty($image_entry->URL)) {
				$image_entry->URL = $cleanURL;
			}
// set default varibles
			$image_entry->country = $custom->entry_country;
			$image_entry->state = $custom->entry_state;
			$image_entry->photographer = $custom->default_photographer;
// load image and set up variables that can automatically be entered
			if (isset($action) && $action == "load_image") {
				$image_entry->get_EXIF();
				if (empty($image_entry->height) || empty($image_entry->width)) {
					$image_entry->orientation = $image_entry->get_orientation();
				}
				if (is_in_database('URL',$image_entry->URL) == true) {
					$page->msgs[] = new msg('warning'
					                       ,$lang->str("warn_already_in_db_link"
					                                  ,array(html($image_entry->URL)
					                                        ,'index.php?screen=list&URL='.urlencode($image_entry->URL).'&result_view=thumbnail&items_per_page=10'
                                                            )
					                                  )
					                       );
				}
			}
			if ($custom->import_IPTC_data == 1) {
				$image_entry->append_IPTC();
			}
			$temp_file_name = get_unique_id().".jpg";
			if (!isset($_SESSION["temp_images"])) {
				$_SESSION["temp_images"] = array();
			}
			$_SESSION["temp_images"][$image_entry->URL] = "temp/".$temp_file_name;
		}
		else {
// get existing image from the database
			$image_exists = is_in_database('ID', $id);
			if ($image_exists == true) {
				$image_entry = $image_entry->retrieve($id);
				$temp_file_name = "temp_display_img_".$image_entry->id.".jpg";
			}
			else {
				$page->msgs[] = new msg('error'
									   ,$lang->str("err_no_photo"
												  ,array($id)
												  )
									   );
				$show_body = 0;
				$show_image = 0;
			}
		}
		if ($custom->minimize_data_transfer == 1) {
			if (file_exists('temp/'.$temp_file_name)) {
				$image_entry->display_URL = "temp/".$temp_file_name;
			}
			else if (isset($_SESSION["temp_images"]) && in_array($image_entry->URL, $_SESSION["temp_images"])) {
				$image_entry->display_URL = $_SESSION["temp_images"][$image_entry->URL];
			}
			else {
				$image_entry->display_URL = save_JPEG_file_from_JPEG($image_entry->URL, 800, "temp/", $temp_file_name, 70);
			}
		}
		else {
			$image_entry->display_URL = urlencode($image_entry->URL);
		}
// get preview dimensions
		if (!empty($image_entry->display_URL)) {
			$preview_dimensions = get_preview_dimensions($image_entry->display_URL);
		}
// set the screen type
		$page->body = "entry.inc.php";
		$page->init_string .= " setFocus();";
		$page->title = $lang->str("pt_title", array($lang->str("pt_entry")));
		break;
}
if (!empty($image_entry->title) && $screen != 'load_folder') {
	$page->title = $lang->str("pt_title", array(html($image_entry->title)));
}

?>
Return current item: Photos