<?php
/**
 * basic functions used by zenphoto
 * Headers not sent yet!
 * @package functions
 *
 */

// force UTF-8 Ø

require_once(dirname(__FILE__).'/functions-basic.php');

if(!function_exists("gettext")) {
	// load the drop-in replacement library
	require_once(dirname(__FILE__).'/lib-gettext/gettext.inc');
}

// allow reading of old Option tables--should be needed only during upgrade
$result = query_full_array("SHOW COLUMNS FROM ".prefix('options').' LIKE "%ownerid%"', true);
if (is_array($result)) {
	foreach ($result as $row) {
		if ($row['Field'] == 'ownerid') {
			$_zp_optionDB_hasownerid = true;
			break;
		}
	}
}

if (getOption('album_session') && OFFSET_PATH==0) {
	session_start();
}

require_once(dirname(__FILE__).'/functions-i18n.php');

setMainDomain();

/**
 * parses the allowed HTML tags for use by htmLawed
 *
 *@param string &$source by name, contains the string with the tag options
 *@return array the allowed_tags array.
 *@since 1.1.3
 **/
function parseAllowedTags(&$source) {
	$source = trim($source);
	if (substr($source, 0, 1) != "(") { return false; }
	$source = substr($source, 1); //strip off the open paren
	$a = array();
	while ((strlen($source) > 1) && (substr($source, 0, 1) != ")")) {
		$i = strpos($source, '=>');
		if ($i === false) { return false; }
		$tag = trim(substr($source, 0, $i));
		$source = trim(substr($source, $i+2));
		if (substr($source, 0, 1) != "(") { return false; }
		$x = parseAllowedTags($source);
		if ($x === false) { return false; }
		$a[$tag] = $x;
	}
	if (substr($source, 0, 1) != ')') { return false; }
	$source = trim(substr($source, 1)); //strip the close paren
	return $a;
}

	// Note: The database setup/upgrade uses this list, so if fields are added or deleted, setup.php should be
	//   run or the new data won't be stored (but existing fields will still work; nothing breaks).
	$_zp_exifvars = array(
		// Database Field       	=> array('IFDX', 'ExifKey',          'ZP Display Text',        				 	 Display?)
		'EXIFOrientation'       => array('IFD0',   'Orientation',       gettext('Orientation'),            false),
		'EXIFMake'              => array('IFD0',   'Make',              gettext('Camera Maker'),           true),
		'EXIFModel'             => array('IFD0',   'Model',             gettext('Camera Model'),           true),
		'EXIFExposureTime'      => array('SubIFD', 'ExposureTime',      gettext('Shutter Speed'),          true),
		'EXIFFNumber'           => array('SubIFD', 'FNumber',           gettext('Aperture'),               true),
		'EXIFFocalLength'       => array('SubIFD', 'FocalLength',       gettext('Focal Length'),           true),
		'EXIFFocalLength35mm'   => array('SubIFD', 'FocalLength35mmEquiv', gettext('35mm Equivalent Focal Length'), false),
		'EXIFISOSpeedRatings'   => array('SubIFD', 'ISOSpeedRatings',   gettext('ISO Sensitivity'),        true),
		'EXIFDateTimeOriginal'  => array('SubIFD', 'DateTimeOriginal',  gettext('Time Taken'),             true),
		'EXIFExposureBiasValue' => array('SubIFD', 'ExposureBiasValue', gettext('Exposure Compensation'),  true),
		'EXIFMeteringMode'      => array('SubIFD', 'MeteringMode',      gettext('Metering Mode'),          true),
		'EXIFFlash'             => array('SubIFD', 'Flash',             gettext('Flash Fired'),            true),
		'EXIFImageWidth'        => array('SubIFD', 'ExifImageWidth',    gettext('Original Width'),         false),
		'EXIFImageHeight'       => array('SubIFD', 'ExifImageHeight',   gettext('Original Height'),        false),
		'EXIFContrast'          => array('SubIFD', 'Contrast',          gettext('Contrast Setting'),       false),
		'EXIFSharpness'         => array('SubIFD', 'Sharpness',         gettext('Sharpness Setting'),      false),
		'EXIFSaturation'        => array('SubIFD', 'Saturation',        gettext('Saturation Setting'),     false),
		'EXIFWhiteBalance'			=> array('SubIFD', 'WhiteBalance',			gettext('White Balance'),					 false),
		'EXIFSubjectDistance'		=> array('SubIFD', 'SubjectDistance',		gettext('Subject Distance'),			 false),
		'EXIFGPSLatitude'       => array('GPS',    'Latitude',          gettext('Latitude'),               false),
		'EXIFGPSLatitudeRef'    => array('GPS',    'Latitude Reference',gettext('Latitude Reference'),     false),
		'EXIFGPSLongitude'      => array('GPS',    'Longitude',         gettext('Longitude'),              false),
		'EXIFGPSLongitudeRef'   => array('GPS',    'Longitude Reference',gettext('Longitude Reference'),   false),
		'EXIFGPSAltitude'       => array('GPS',    'Altitude',          gettext('Altitude'),               false),
		'EXIFGPSAltitudeRef'    => array('GPS',    'Altitude Reference',gettext('Altitude Reference'),     false)
		);

/**
 * initializes the $_zp_exifvars array display state
 *
 */
function setexifvars() {
	global $_zp_exifvars;
	foreach ($_zp_exifvars as $key=>$item) {
		$_zp_exifvars[$key][3] = getOption($key);
	}
}

$_zp_supported_images = array('jpg','jpeg','gif','png');
// Image utility functions
/**
 * Returns true if the file is an image
 *
 * @param string $filename the name of the target
 * @return bool
 */
function is_valid_image($filename) {
	global $_zp_supported_images;
	$ext = strtolower(substr(strrchr($filename, "."), 1));
	return in_array($ext, $_zp_supported_images);
}

//ZenVideo: Video utility functions

/**
 * Check if the image is a video thumb
 *
 * @param string $album folder path for the album
 * @param string $filename name of the target
 * @return bool
 *
 * Note: this function is inefficient and slows down the image file loop a lot.
 * Don't use it in a loop!
 */
function is_objectsThumb($album, $filename){
	global $_zp_extra_filetypes;
	$types = array_keys($_zp_extra_filetypes);
	$ext = strtolower(substr($fext = strrchr($filename, "."), 1));
	if (in_array($ext, $types)) {
		return str_replace($fext, '', $filename);
	}
	return false;
}

/**
 * Search for a thumbnail for the image
 *
 * @param string $album folder path of the album
 * @param string $video name of the target
 * @return string
 */
function checkObjectsThumb($album, $video){
	global $_zp_supported_images;
	$video = is_objectsThumb($album, $video);
	if($video) {
		foreach($_zp_supported_images as $ext) {
			if(file_exists(UTF8ToFilesystem($album."/".$video.'.'.$ext))) {
				return $video.'.'.$ext;
			}
		}
	}
	return NULL;
}

/**
 * Search for a high quality version of the video
 *
 * @param string $album folder path of the album
 * @param string $video name of the target
 * @return string
 */
function checkVideoOriginal($album, $video){
	$video = is_objectsThumb($album, $video);
	if ($video) {
		$extTab = array(".ogg",".OGG",".avi",".AVI",".wmv",".WMV");
		foreach($extTab as $ext) {
			if(file_exists(UTF8ToFilesystem($album."/".$video.$ext))) {
				return $video.$ext;
			}
		}
	}
	return NULL;
}

/**
 * Returns a truncated string
 *
 * @param string $string souirce string
 * @param int $length how long it should be
 * @return string
 */
function truncate_string($string, $length) {
	if (strlen($string) > $length) {
		$pos = strpos($string, ' ', $length);
		if ($pos === FALSE) return substr($string, 0, $length) . '...';
		return substr($string, 0, $pos) . '...';
	}
	return $string;
}

/**
 * Returns the oldest ancestor of an alubm;
 *
 * @param string $album an album object
 * @return object
 */
function getUrAlbum($album) {
	if (!is_object($album)) return NULL;
	while (true) {
		$parent = $album->getParent();
		if (is_null($parent)) { return $album; }
		$album = $parent;
	}
}

/**
 * Returns the DB sort key for an album sort type
 *
 * @param string $sorttype The sort type option
 * @return string
 */
function albumSortKey($sorttype) {
	switch ($sorttype) {
		case "Title":
			return 'title';
		case "Manual":
			return 'sort_order';
		case "Date":
			return 'date';
		case "ID":
			return 'id';
		case 'mtime':
			return 'mtime';
	}
	return 'filename';
}
/**
 * Returns the DB key associated with the subalbum sort type
 *
 * @param string $sorttype subalbum sort type
 * @return string
 */
function subalbumSortKey($sorttype) {
	switch ($sorttype) {
		case "Title":
			return 'title';
		case "Manual":
			return 'sort_order';
		case "Filename":
			return 'folder';
		case "Date":
			return 'date';
		case "ID":
			return 'id';
		case 'mtime':
			return 'mtime';
	}
	return 'sort_order';
}

/**
 * Returns a formated date for output
 *
 * @param string $format the "strftime" format string
 * @param date $dt the date to be output
 * @return string
 */
function zpFormattedDate($format, $dt) {
	$fdate = strftime($format, $dt);
	$charset = 'ISO-8859-1';
	if (function_exists('mb_internal_encoding')) {
		if (($charset = mb_internal_encoding()) == 'UTF-8') {
			return $fdate;
		}
	}
	return utf8::convert($fdate, $charset);
}

/**
 * Simple mySQL timestamp formatting function.
 *
 * @param string $format formatting template
 * @param int $mytimestamp timestamp
 * @return string
 */
function myts_date($format,$mytimestamp)
{
	// If your server is in a different time zone than you, set this.
	$timezoneadjust = getOption('time_offset');

	$month  = substr($mytimestamp,4,2);
	$day    = substr($mytimestamp,6,2);
	$year   = substr($mytimestamp,0,4);

	$hour   = substr($mytimestamp,8,2);
	$min    = substr($mytimestamp,10,2);
	$sec    = substr($mytimestamp,12,2);

	$epoch  = mktime($hour+$timezoneadjust,$min,$sec,$month,$day,$year);
	$date   = zpFormattedDate($format, $epoch);
	return $date;
}

/**
 * Get the size of a directory.
 * From: http://aidan.dotgeek.org/lib/
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.0.0
 * @param       string $directory   Path to directory
 */
function dirsize($directory)
{
	$size = 0;
	if (substr($directory, -1, 1) !== DIRECTORY_SEPARATOR) {
		$directory .= DIRECTORY_SEPARATOR;
	}
	$stack = array($directory);
	for ($i = 0, $j = count($stack); $i < $j; ++$i) {
		if (is_file($stack[$i])) {
			$size += filesize($stack[$i]);
		} else if (is_dir($stack[$i])) {
			$dir = dir($stack[$i]);
			while (false !== ($entry = $dir->read())) {
				if ($entry == '.' || $entry == '..') continue;
				$add = $stack[$i] . $entry;
				if (is_dir($stack[$i] . $entry)) $add .= DIRECTORY_SEPARATOR;
				$stack[] = $add;
			}
			$dir->close();
		}
		$j = count($stack);
	}
	return $size;
}

// Text formatting and checking functions

/**
 * Determines if the input is an e-mail address. Adapted from WordPress.
 * Name changed to avoid conflicts in WP integrations.
 *
 * @param string $input_email email address?
 * @return bool
 */
function is_valid_email_zp($input_email) {
	$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
	if(strstr($input_email, '@') && strstr($input_email, '.')) {
		if (preg_match($chars, $input_email)) {
			return true;
		}
	}
	return false;
}


/**
 * Send an mail to the admin user(s). We also attempt to intercept any form injection
 * attacks by slime ball spammers.
 *
 * @param string $subject  The subject of the email.
 * @param string $message  The message contents of the email.
 * @param string $headers  Optional headers for the email.
 * @param array $admin_emails a list of email addresses
 *
 * @author Todd Papaioannou (lucky@luckyspin.org)
 * @since  1.0.0
 */
function zp_mail($subject, $message, $headers = '', $admin_emails=null) {
	if (is_null($admin_emails)) { $admin_emails = getAdminEmail(); }
	if (count($admin_emails) > 0) {
		// Make sure no one is trying to use our forms to send Spam
		// Stolen from Hosting Place:
		//   http://support.hostingplace.co.uk/knowledgebase.php?action=displayarticle&cat=0000000039&id=0000000040
		$badStrings = array("Content-Type:", "MIME-Version:",	"Content-Transfer-Encoding:",	"bcc:",	"cc:");
		foreach($_POST as $k => $v) {
			foreach($badStrings as $v2) {
				if (strpos($v, $v2) !== false) {
					header("HTTP/1.0 403 Forbidden");
					die("Forbidden");
					exit();
				}
			}
		}

		foreach($_GET as $k => $v){
			foreach($badStrings as $v2){
				if (strpos($v, $v2) !== false){
					header("HTTP/1.0 403 Forbidden");
					die("Forbidden");
					exit();
				}
			}
		}

		if( $headers == '' ) {
			$headers = "From: " . get_language_string(getOption('gallery_title'), getOption('locale')) . "<zenphoto@" . $_SERVER['SERVER_NAME'] . ">";
		}

		// Convert to UTF-8
		if (getOption('charset') != 'UTF-8') {
			$subject = utf8::convert($subject, getOption('charset'));
			$message = utf8::convert($message, getOption('charset'));
		}

		// Send the mail
		foreach ($admin_emails as $email) {
			UTF8::send_mail($email, $subject, $message, $headers);
		}
	}
}

/**
 * Sorts the results of a DB search by the current locale title string
 *
 * @param array $dbresult the result of the DB query
 * @param string $field the field name to sort on
 * @param bool $descending the direction of the sort
 * @return array the sorted result
 */
function sortByMultilingual($dbresult, $field, $descending) {
	$temp = array();
	foreach ($dbresult as $row) {
		$temp[] = get_language_string($row[$field]);
	}
	natcasesort($temp);
	$result = array();
	foreach ($temp as $key=>$title) {
		if ($descending) {
			array_unshift($result, $dbresult[$key]);
		} else {
			$result[] = $dbresult[$key];
		}
	}
	return $result;
}

/**
 * Sort the album array based on either according to the sort key.
 * Default is to sort on the `sort_order` field.
 *
 * Returns an array with the albums in the desired sort order
 *
 * @param  array $albums array of album names
 * @param  string $sortkey the sorting scheme
 * @return array
 *
 * @author Todd Papaioannou (lucky@luckyspin.org)
 * @since  1.0.0
 */
function sortAlbumArray($parentalbum, $albums, $sortkey='`sort_order`', $recursed=false) {
	global $_zp_loggedin, $_zp_gallery;
	$hidden = array();
	if (is_null($parentalbum)) {
		$albumid = ' IS NULL';
	} else {
		$albumid = '='.$parentalbum->id;
	}
	$result = query('SELECT `folder`, `sort_order`, `title`, `show`, `dynamic`, `search_params` FROM ' .
						prefix("albums") . ' WHERE `parentid`'.$albumid.' ORDER BY ' . $sortkey);
	$results = array();
	while ($row = mysql_fetch_assoc($result)) {
		$results[] = $row;
	}
	if (strpos($sortkey,'title') !== false) {
		$results = sortByMultilingual($results, 'title', strpos($sortkey,'DESC') !== false);
	} else if (strpos($sortkey, 'folder') !== false) {
		if (strpos($sortkey,'DESC') !== false) $order = 'dsc'; else $order = 'asc';
		$results = sortMultiArray($results, 'folder', $order, true, false);
	}
	$i = 0;
	$albums_r = array_flip($albums);
	$albums_touched = array();
	foreach ($results as $row) {
		$folder = $row['folder'];
		if (array_key_exists($folder, $albums_r)) {
			$albums_r[$folder] = $i;
			$albums_touched[] = $folder;
			if (!$_zp_loggedin && !$row['show']) { $hidden[] = $folder; }
		}
		$i++;
	}

	$albums_untouched = array_diff($albums, $albums_touched);
	if (count($albums_untouched) > 0) { //found new albums
		foreach($albums_untouched as $alb) {
			$albobj = new Album($_zp_gallery, $alb); // force load to DB
			$albums_r[$alb] = -$i;  // place them in the front of the list
			$i++;
		}
		if (!$recursed) return sortAlbumArray($parentalbum, $albums, $sortkey, true);
	}

	foreach($hidden as $alb) {
		unset($albums_r[$alb]);
	}

	$albums = array_flip($albums_r);
	ksort($albums);

	$albums_ordered = array();
	foreach($albums as $album) {
		$albums_ordered[] = $album;
	}

	return $albums_ordered;
}

/**
 * Checks to see access is allowed to an album
 * Returns true if access is allowed.
 * There is no password dialog--you must have already had authorization via a cookie.
 *
 * @param string $albumname the album
 * @param string &$hint becomes populated with the password hint.
 * @return bool
 */
function checkAlbumPassword($albumname, &$hint) {
	global $_zp_pre_authorization, $_zp_loggedin;
	if (zp_loggedin(ADMIN_RIGHTS | VIEWALL_RIGHTS | ALL_ALBUMS_RIGHTS)) { return true; }
	if ($_zp_loggedin) {
		if (isMyAlbum($albumname, ALL_RIGHTS)) { return true; }  // he is allowed to see it.
	}
	if (isset($_zp_pre_authorization[$albumname])) {
		return true;
	}
	$album = new album($_zp_gallery, $albumname);
	$hash = $album->getPassword();
	if (empty($hash)) {
		$album = $album->getParent();
		while (!is_null($album)) {
			$hash = $album->getPassword();
			$authType = "zp_album_auth_" . cookiecode($album->name);
			$saved_auth = zp_getCookie($authType);

			if (!empty($hash)) {
				if ($saved_auth != $hash) {
					$hint = $album->getPasswordHint();
					return false;
				}
			}
			$album = $album->getParent();
		}
		// revert all tlhe way to the gallery
		$hash = getOption('gallery_password');
		$authType = 'zp_gallery_auth';
		$saved_auth = zp_getCookie($authType);
		if (!empty($hash)) {
			if ($saved_auth != $hash) {
				$hint = get_language_string(getOption('gallery_hint'));
				return false;
			}
		}
	} else {
		$authType = "zp_album_auth_" . cookiecode($album->name);
		$saved_auth = zp_getCookie($authType);
		if ($saved_auth != $hash) {
			$hint = $album->getPasswordHint();
			return false;
		}
	}
	$_zp_pre_authorization[$albumname] = true;
	return true;
}

/**
 * Adds a subalbum to the zipfile being created
 *
 * @param string $base the directory of the base album
 * @param string $offset the from $base to the subalbum
 * @param string $subalbum the subalbum file name
 */
function zipAddSubalbum($base, $offset, $subalbum) {
	global $_zp_zip_list;
	$leadin = str_replace(getAlbumFolder(), '', $base);
	if (checkAlbumPassword($leadin.$offset.$subalbum, $hint)) {
		$new_offset = $offset.$subalbum.'/';
		$rp = $base.$new_offset;
		$cwd = getcwd();
		chdir($rp);
		if ($dh = opendir($rp)) {
			$_zp_zip_list[] = "./".$new_offset.'*.*';
			while (($file = readdir($dh)) !== false) {
				if($file != '.' && $file != '..'){
					if (is_dir($rp.$file)) {
						zipAddSubalbum($base, $new_offset, $file, $zip);
					}
				}
			}
			closedir($dh);
		}
		chdir($cwd);
	}
}

/**
 * Creates a zip file of the album
 *
 * @param string $album album folder
 */
function createAlbumZip($album){
	global $_zp_zip_list;
	if (!checkAlbumPassword($album, $hint)) {
		pageError();
		exit();
	}
	$album = UTF8ToFilesystem($album);
	$rp = realpath(getAlbumFolder() . $album) . '/';
	$p = $album . '/';
	include_once('archive.php');
	$dest = realpath(getAlbumFolder()) . '/' . urlencode($album) . ".zip";
	$persist = getOption('persistent_archive');
	if (!$persist  || !file_exists($dest)) {
		if (file_exists($dest)) unlink($dest);
		$z = new zip_file($dest);
		$z->set_options(array('basedir' => $rp, 'inmemory' => 0, 'recurse' => 0, 'storepaths' => 1));
		if ($dh = opendir($rp)) {
			$_zp_zip_list[] = '*.*';

			while (($file = readdir($dh)) !== false) {
				if($file != '.' && $file != '..'){
					if (is_dir($rp.$file)) {
						$base_a = explode("/", $album);
						unset($base_a[count($base_a)-1]);
						$base = implode('/', $base_a);
						zipAddSubalbum($rp, $base, $file, $z);
					}
				}
			}
			closedir($dh);
		}
		$z->add_files($_zp_zip_list);
		$z->create_archive();
	}
	header('Content-Type: application/zip');
	header('Content-Disposition: attachment; filename="' . urlencode($album) . '.zip"');
	echo file_get_contents($dest);
	if (!$persist) { unlink($dest); }
}

/**
 * Returns the fully qualified "require" file name of the plugin file.
 *
 * @param  string $plugin is the name of the plugin file, typically something.php
 * @param  bool $inTheme tells where to find the plugin.
 *   true means look in the current theme
 *   false means look in the zp-core/plugins folder.
 *
 * @return string
 */
function getPlugin($plugin, $inTheme) {
	global $_zp_themeroot;
	$_zp_themeroot = WEBPATH . "/themes/$inTheme";
	if ($inTheme) {
		$pluginFile = $_zp_themeroot . '/' . $plugin;
		$pluginFile = SERVERPATH . '/' . str_replace(WEBPATH, '', UTF8ToFilesystem($pluginFile));
	} else {
		$pluginFile = SERVERPATH . '/' . ZENFOLDER . '/plugins/' . UTF8ToFilesystem($plugin);
	}
	if (file_exists($pluginFile)) {
		return $pluginFile;
	} else {
		return false;
	}
}

/**
 * Returns an array of the currently enabled plugins
 *
 * @return array
 */
function getEnabledPlugins() {
	$pluginlist = array();
	$curdir = getcwd();
	chdir(SERVERPATH . "/" . ZENFOLDER . PLUGIN_FOLDER);
	$filelist = safe_glob('*'.'php');
	chdir($curdir);
	foreach ($filelist as $extension) {
		$extension = FilesystemToUTF8($extension);
		$opt = 'zp_plugin_'.substr($extension, 0, strlen($extension)-4);
		if (getOption($opt)) {
			$pluginlist[] = $extension;
		}
	}
	return $pluginlist;
}

/**
 * For internal use--fetches a single tag from IPTC data
 *
 * @param string $tag the metadata tag sought
 * @return string
 */
function getIPTCTag($tag) {
	global $iptc;
	if (isset($iptc[$tag])) {
		$iptcTag = $iptc[$tag];
		$r = "";
		$ct = count($iptcTag);
		for ($i=0; $i<$ct; $i++) {
			$w = $iptcTag[$i];
			if (!empty($r)) { $r .= ", "; }
			$r .= $w;
		}
		return $r;
	}
	return '';
}

/**
 * For internal use--fetches the IPTC array for a single tag.
 *
 * @param string $tag the metadata tag sought
 * @return array
 */
function getIPTCTagArray($tag) {
	global $iptc;
	if (array_key_exists($tag, $iptc)) {
		return $iptc[$tag];
	}
	return NULL;
}

/**
 * Returns the IPTC data converted into UTF8
 *
 * @param string $iptcstring the IPTC data
 * @return string
 */
function prepIPTCString($iptcstring) {
	$iptcstring = utf8::convert($iptcstring, 'ISO-8859-1');
	// Remove null byte at the end of the string if it exists.
	if (substr($iptcstring, -1) === 0x0) {
		$iptcstring = substr($iptcstring, 0, -1);
	}
	return $iptcstring;
}

/**
 * Parces IPTC data and returns those tags zenphoto is interested in
 * folds multiple tags into single zp data items based on precedence.
 *
 * @param string $imageName the name of the image
 * @return array
 */
function getImageMetadata($imageName) {
	require_once(dirname(__FILE__).'/exif/exif.php');
	global $iptc;

	$result = array();
	getimagesize($imageName, $imageInfo);
	if (is_array($imageInfo)) {
		$exifraw = read_exif_data_raw($imageName, false);
		if (isset($exifraw['SubIFD'])) {
			$subIFD = $exifraw['SubIFD'];
		} else {
			$subIFD = array();
		}
		/* EXIF date */
		if (isset($subIFD['DateTime'])) {
			$date = $subIFD['DateTime'];
		} else {
			$date = '';
		}
		if (empty($date) && isset($subIFD['DateTimeOriginal'])) {
			$date = $subIFD['DateTimeOriginal'];
		}
		if (empty($date)  && isset($subIFD['DateTimeDigitized'])) {
			$date = $subIFD['DateTimeDigitized'];
		}
		if (!empty($date)) {
			$result['date'] = $date;
		}

		/* check IPTC data */
		if (isset($imageInfo["APP13"])) {
			$iptc = iptcparse($imageInfo["APP13"]);
			if ($iptc) {
				/* iptc date */
				$date = getIPTCTag('2#055');
				if (!empty($date)) {
					$result['date'] = substr($date, 0, 4).'-'.substr($date, 4, 2).'-'.substr($date, 6, 2);
				}
				/* iptc title */
				$title = getIPTCTag('2#005');   /* Option Name */
				if (empty($title)) {
					$title = getIPTCTag('2#105'); /* Headline */
				}
				if (!empty($title)) {
					$result['title'] = prepIPTCString($title);
				}

				/* iptc description */
				$caption= getIPTCTag('2#120');
				if (!empty($caption)) {
					$result['desc'] = prepIPTCString($caption);
				}

				/* iptc location, state, country */
				$location = getIPTCTag('2#092');
				if (!empty($location)) {
					$result['location'] = prepIPTCString($location);
				}
				$city = getIPTCTag('2#090');
				if (!empty($city)) {
					$result['city'] = prepIPTCString($city);
				}
				$state = getIPTCTag('2#095');
				if (!empty($state)) {
					$result['state'] = prepIPTCString($state);
				}
				$country = getIPTCTag('2#101');
				if (!empty($country)) {
					$result['country'] = prepIPTCString($country);
				}
				/* iptc credit */
				$credit= getIPTCTag('2#080'); /* by-line */
				if (empty($credit)) {
					$credit = getIPTCTag('2#110'); /* credit */
				}
				if (empty($credit)) {
					$credit = getIPTCTag('2#115'); /* source */
				}
				if (!empty($credit)) {
					$result['credit'] = prepIPTCString($credit);
				}

				/* iptc copyright */
				$copyright= getIPTCTag('2#116');
				if (!empty($copyright)) {
					$result['copyright'] = prepIPTCString($copyright);
				}

				/* iptc keywords (tags) */
				$keywords = getIPTCTagArray('2#025');
				if (is_array($keywords)) {
					$taglist = array();
					foreach($keywords as $keyword) {
						$taglist[] = prepIPTCString($keyword);
					}
					$result['tags'] = $taglist;
				}
			}
		}
	}

	return $result;
}

/**
 * Generic comment adding routine. Called by album objects or image objects
 * to add comments.
 *
 * Returns a code for the success of the comment add:
 *    0: Bad entry
 *    1: Marked for moderation
 *    2: Successfully posted
 *
 * @param string $name Comment author name
 * @param string $email Comment author email
 * @param string $website Comment author website
 * @param string $comment body of the comment
 * @param string $code Captcha code entered
 * @param string $code_ok Captcha md5 expected
 * @param string $type 'albums' if it is an album or 'images' if it is an image comment
 * @param object $receiver the object (image or album) to which to post the comment
 * @param string $ip the IP address of the comment poster
 * @param bool $private set to true if the comment is for the admin only
 * @param bool $anon set to true if the poster wishes to remain anonymous
 * @return int
 */
function postComment($name, $email, $website, $comment, $code, $code_ok, $receiver, $ip, $private, $anon) {
	// added for zenpage support
	$class = strtolower(get_class($receiver));
	if ($class == "zenpage") {
		if($receiver->isPage(ZENPAGE_NEWS)) {
			$type = "news";
		} else if($receiver->isPage(ZENPAGE_PAGES)) {
			$type = "pages";
		}
	} else {
		$type = $class.'s'; // Historically we have stored "images" or "albums" as the type
	}
	$receiver->getComments();
	$name = trim($name);
	$email = trim($email);
	$website = trim($website);
	$admins = getAdministrators();
	$admin = array_shift($admins);
	$key = $admin['pass'];
	// Let the comment have trailing line breaks and space? Nah...
	// Also (in)validate HTML here, and in $name.
	$comment = trim($comment);
	if (getOption('comment_email_required') && (empty($email) || !is_valid_email_zp($email))) { return -2; }
	if (getOption('comment_name_required') && empty($name)) { return -3; }
	if (getOption('comment_web_required') && (empty($website) || !isValidURL($website))) { return -4; }
	if (getOption('Use_Captcha')) {
		$code_cypher = md5(bin2hex(rc4($key, trim($code))));
		$code_ok = trim($code_ok);
		if ($code_cypher != $code_ok || strlen($code) != CAPTCHA_LENGTH) { return -5; }
		query('DELETE FROM '.prefix('captcha').' WHERE `ptime`<'.(time()-3600)); // expired tickets
		$result = query('DELETE FROM '.prefix('captcha').' WHERE `hash`="'.$code_cypher.'"');
		$count = mysql_affected_rows();
		if ($count != 1) { return -5; } // no ticket
	}
	if (empty($comment)) {
		return -6;
	}
	if (!empty($website) && substr($website, 0, 7) != "http://") {
		$website = "http://" . $website;
	}
	$goodMessage = 2;
	$gallery = new gallery();
	if (!(false === ($requirePath = getPlugin('spamfilters/'.UTF8ToFileSystem(getOption('spam_filter')).".php", false)))) {
		require_once($requirePath);
		$spamfilter = new SpamFilter();
		$goodMessage = $spamfilter->filterMessage($name, $email, $website, $comment, $type=='images'?$receiver->getFullImage():NULL, $ip);
	}
	if ($goodMessage) {
		if ($goodMessage == 1) {
			$moderate = 1;
		} else {
			$moderate = 0;
		}
		if ($private) $private = 1; else $private = 0;
		if ($anon) $anon = 1; else $anon = 0;
		// added for zenpage support - $receiver->id in the query below changed to $receiverid
		if ($class === "zenpage") {
			$receiverid = $receiver->zenpages['id'];
		} else {
			$receiverid = $receiver->id;
		}
		// Update the database entry with the new comment
		query("INSERT INTO " . prefix("comments") . " (`ownerid`, `name`, `email`, `website`, `comment`, `inmoderation`, `date`, `type`, `ip`, `private`, `anon`) VALUES " .
						' ("' . $receiverid .
						'", "' . mysql_real_escape_string($name) .
						'", "' . mysql_real_escape_string($email) .
						'", "' . mysql_real_escape_string($website) .
						'", "' . mysql_real_escape_string ($comment) .
						'", "' . $moderate .
						'", NOW()'.
						', "'.$type.
						'", "'.$ip .
						'", "'.$private.
						'", "'.$anon.'")');
		if ($moderate) {
			$action = "placed in moderation";
		} else {
			//  add to comments array and notify the admin user
			$newcomment = array();
			$newcomment['name'] = $name;
			$newcomment['email'] = $email;
			$newcomment['website'] = $website;
			$newcomment['comment'] = $comment;
			$newcomment['date'] = time();
			$receiver->comments[] = $newcomment;
			$action = "posted";
		}
	// switch added for zenpage support
		switch ($type) {
			case "images":
				$on = $receiver->getAlbumName() . " about " . $receiver->getTitle();
				$url = "album=" . urlencode($receiver->album->name) . "&image=" . urlencode($receiver->filename);
				$album = $receiver->getAlbum();
				$ur_album = getUrAlbum($album);
				break;
			case "albums":
				$on = $receiver->name;
				$url = "album=" . urlencode($receiver->name);
				$ur_album = getUrAlbum($receiver);
				break;
			case "news":
				$on = get_language_string($receiver->zenpages['title']);
				$url = "p=".ZENPAGE_NEWS."&title=" . urlencode($receiver->zenpages['titlelink']);
				//$album = $receiver->getAlbum();
				//$ur_album = getUrAlbum($album);
				break;
			case "pages":
				$on = get_language_string($receiver->zenpages['title']);
				$url = "p=".ZENPAGE_PAGES."&title=" . urlencode($receiver->zenpages['titlelink']);
				//$album = $receiver->getAlbum();
				//$ur_album = getUrAlbum($album);
				break;
		}
		if (getOption('email_new_comments')) {
			$message = gettext("A comment has been $action in your album")." $on\n" .
 										"\n" .
 										"Author: " . $name . "\n" .
 										"Email: " . $email . "\n" .
 										"Website: " . $website . "\n" .
 										"Comment:\n" . $comment . "\n" .
 										"\n" .
 										"You can view all comments about this image here:\n" .
 										"http://" . $_SERVER['SERVER_NAME'] . WEBPATH . "/index.php?$url\n" .
 										"\n" .
 										"You can edit the comment here:\n" .
 										"http://" . $_SERVER['SERVER_NAME'] . WEBPATH . "/" . ZENFOLDER . "/admin-comments.php\n";
			$emails = array();
			$admin_users = getAdministrators();
			foreach ($admin_users as $admin) {  // mail anyone else with full rights
				if (($admin['rights'] & ADMIN_RIGHTS) && ($admin['rights'] & COMMENT_RIGHTS) && !empty($admin['email'])) {
					$emails[] = $admin['email'];
					unset($admin_users[$admin['id']]);
				}
			}
			// take out for zenpage comments since there are no album admins
			if($type === "images" OR $type === "albums") {
				$id = $ur_album->getAlbumID();
				$sql = "SELECT `adminid` FROM ".prefix('admintoalbum')." WHERE `albumid`=$id";
				$result = query_full_array($sql);
				foreach ($result as $anadmin) {
					$admin = $admin_users[$anadmin['adminid']];
					if (!empty($admin['email'])) {
						$emails[] = $admin['email'];
					}
				}
			}
			zp_mail("[" . get_language_string(getOption('gallery_title'), getOption('locale')) . "] Comment posted on $on", $message, "", $emails);
		}
	}
	return $goodMessage;
}

//admin user handling

define('NO_RIGHTS', 2);
define('MAIN_RIGHTS', 4);
define('VIEWALL_RIGHTS', 8);
define('UPLOAD_RIGHTS', 16);
define('COMMENT_RIGHTS', 64);
define('EDIT_RIGHTS', 256);
define('ALL_ALBUMS_RIGHTS', 512);
define('THEMES_RIGHTS', 1024);
define('OPTIONS_RIGHTS', 8192);
define('ZENPAGE_RIGHTS',2048);
define('ADMIN_RIGHTS', 65536);
define('ALL_RIGHTS', 07777777777);

$_zp_current_admin = null;
$_zp_admin_users = null;

/**
 * Saves an admin user's settings
 *
 * @param string $user The username of the admin
 * @param string $pass The password associated with the user name (md5)
 * @param string $name The display name of the admin
 * @param string $email The email address of the admin
 * @param bit $rights The administrating rites for the admin
 * @param array $albums an array of albums that the admin can access. (If empty, access is to all albums)
 */
function saveAdmin($user, $pass, $name, $email, $rights, $albums) {

	if (DEBUG_LOGIN) { debugLog("saveAdmin($user, $pass, $name, $email, $rights, $albums)"); }

	$sql = "SELECT `name`, `id` FROM " . prefix('administrators') . " WHERE `user` = '$user'";
	$result = query_single_row($sql);
	if ($result) {
		$id = $result['id'];
		if (is_null($pass)) {
			$password = '';
		} else {
			$password = "' ,`password`='" . escape($pass);
		}
		if (is_null($rights)) {
			$rights = '';
		} else {
			$rightsset = "', `rights`='" . escape($rights);
		}
		$sql = "UPDATE " . prefix('administrators') . "SET `name`='" . escape($name) . $password .
 					"', `email`='" . escape($email) . $rightsset . "' WHERE `id`='" . $id ."'";
		$result = query($sql);

		if (DEBUG_LOGIN) { debugLog("updating[$id]:$result");	}

	} else {
		if (is_null($pass)) $pass = md5($user);
		$sql = "INSERT INTO " . prefix('administrators') . " (user, password, name, email, rights) VALUES ('" .
		escape($user) . "','" . escape($pass) . "','" . escape($name) . "','" . escape($email) . "','" . $rights . "')";
		$result = query($sql);
		$sql = "SELECT `name`, `id` FROM " . prefix('administrators') . " WHERE `user` = '$user'";
		$result = query_single_row($sql);
		$id = $result['id'];

		if (DEBUG_LOGIN) { debugLog("inserting[$id]:$result"); }

	}
	$gallery = new Gallery();
	if (is_array($albums)) {
		$sql = "DELETE FROM ".prefix('admintoalbum')." WHERE `adminid`=$id";
		$result = query($sql);
		foreach ($albums as $albumname) {
			$album = new Album($gallery, $albumname);
			$albumid = $album->getAlbumID();
			$sql = "INSERT INTO ".prefix('admintoalbum')." (adminid, albumid) VALUES ($id, $albumid)";
			$result = query($sql);
		}
	}
}

/**
 * Returns an array of admin users, indexed by the userid
 *
 * The array contains the md5 password, user's name, email, and admin priviledges
 *
 * @return array
 */
function getAdministrators() {
	global $_zp_admin_users;
	if (is_null($_zp_admin_users)) {
		$_zp_admin_users = array();
		$sql = "SELECT `user`, `password`, `name`, `email`, `rights`, `id` FROM ".prefix('administrators')."ORDER BY `rights` DESC, `id`";
		$admins = query_full_array($sql, true);
		if ($admins !== false) {
			foreach($admins as $user) {
				if (NO_RIGHTS == 2) {
					if (($rights = $user['rights']) & 1) { // old compressed rights
						$newrights = MAIN_RIGHTS;
						if ($rights & 2) $newrights = $newrights | UPLOAD_RIGHTS;
						if ($rights & 4) $newrights = $newrights | COMMENT_RIGHTS;
						if ($rights & 8) $newrights = $newrights | EDIT_RIGHTS;
						if ($rights & 16) $newrights = $newrights | THEMES_RIGHTS;
						if ($rights & 32) $newrights = $newrights | OPTIONS_RIGHTS;
						if ($rights & 16384) $newrights = $newrights | ADMIN_RIGHTS;
						$user['rights'] = $newrights;
					}
				} else {
					if (!(($rights = $user['rights']) & 1)) { // new expanded rights
						$newrights = MAIN_RIGHTS;
						if ($rights & 16) $newrights = $newrights | UPLOAD_RIGHTS;
						if ($rights & 64) $newrights = $newrights | COMMENT_RIGHTS;
						if ($rights & 256) $newrights = $newrights | EDIT_RIGHTS;
						if ($rights & 1024) $newrights = $newrights | THEMES_RIGHTS;
						if ($rights & 8192) $newrights = $newrights | OPTIONS_RIGHTS;
						if ($rights & 65536) $newrights = $newrights | ADMIN_RIGHTS;
						$user['rights'] = $newrights;
					}
				}
				$_zp_admin_users[$user['id']] = array('user' => $user['user'], 'pass' => $user['password'],
 												'name' => $user['name'], 'email' => $user['email'], 'rights' => $user['rights'],
 												'id' => $user['id']);
			}
		}
	}
	return $_zp_admin_users;
}

/**
 * Retuns the administration rights of a saved authorization code
 *
 * @param string $authCode the md5 code to check
 *
 * @return bit
 */
function checkAuthorization($authCode) {

	if (DEBUG_LOGIN) { debugLogBacktrace("checkAuthorization($authCode)");	}

	global $_zp_current_admin;
	$admins = getAdministrators();
	$reset_date = getOption('admin_reset_date');
	if ((count($admins) == 0) || empty($reset_date)) {
		$_zp_current_admin = null;

		if (DEBUG_LOGIN) { debugLog("no admin or reset request"); }

		return ADMIN_RIGHTS; //no admins or reset request
	}
	if (empty($authCode)) return 0; //  so we don't "match" with an empty password
	$i = 0;
	foreach($admins as $user) {

		if (DEBUG_LOGIN) { debugLogArray("checking",$user);	}

		if ($user['pass'] == $authCode) {
			$_zp_current_admin = $user;
			$result = $user['rights'];
			if ($i == 0) { // the first admin is the master.
				$result = $result | ADMIN_RIGHTS;
			}

			if (DEBUG_LOGIN) { debugLog("match");	}

			return $result;
		}
		$i++;
	}
	$_zp_current_admin = null;
	return 0; // no rights
}

/**
 * Checks a logon user/password against the list of admins
 *
 * Returns true if there is a match
 *
 * @param string $user
 * @param string $pass
 * @return bool
 */
function checkLogon($user, $pass) {
	$admins = getAdministrators();
	foreach ($admins as $admin) {
		if ($admin['user'] == $user) {
			$md5 = md5($user.$pass);
			if ($admin['pass'] == $md5) {
				return checkAuthorization($md5);
			}
		}
	}
	return false;
}

/**
 * Returns the email addresses of the Admin with ADMIN_USERS rights
 *
 * @param bit $rights what kind of admins to retrieve
 * @return array
 */
function getAdminEmail($rights=ADMIN_RIGHTS) {
	$emails = array();
	$admins = getAdministrators();
	$user = array_shift($admins);
	if (!empty($user['email'])) {
		$emails[] = $user['email'];
	}
	foreach ($admins as $user) {
		if (($user['rights'] & $rights)  && !empty($user['email'])) {
			$emails[] = $user['email'];
		}
	}
	return $emails;
}

/**
 * Populates and returns the $_zp_admin_album_list array
 *
 * @return array
 */
function getManagedAlbumList() {
	global $_zp_admin_album_list, $_zp_current_admin;
	$_zp_admin_album_list = array();
	$sql = "SELECT ".prefix('albums').".`folder` FROM ".prefix('albums').", ".
	prefix('admintoalbum')." WHERE ".prefix('admintoalbum').".adminid=".
	$_zp_current_admin['id']." AND ".prefix('albums').".id=".prefix('admintoalbum').".albumid";
	$albums = query_full_array($sql);
	foreach($albums as $album) {
		$_zp_admin_album_list[] =$album['folder'];
	}
	return $_zp_admin_album_list;
}

/**
 * Checks to see if the loggedin Admin has rights to the album
 *
 * @param string $albumfolder the album to be checked
 */
function isMyAlbum($albumfolder, $action) {
	global $_zp_loggedin, $_zp_admin_album_list, $_zp_current_admin;
	if ($_zp_loggedin & (ADMIN_RIGHTS | ALL_ALBUMS_RIGHTS)) { return true; }
	if (empty($albumfolder)) { return false; }
	if ($_zp_loggedin & $action) {
		if (is_null($_zp_admin_album_list)) {
			getManagedAlbumList();
		}
		if (count($_zp_admin_album_list) == 0) { return false; }
		foreach ($_zp_admin_album_list as $key => $adminalbum) { // see if it is one of the managed folders or a subfolder there of
			if (substr($albumfolder, 0, strlen($adminalbum)) == $adminalbum) { return true; }
		}
		return false;
	} else {
		return false;
	}
}
/**
 * Returns  an array of album ids whose parent is the folder
 * @param string $albumfolder folder name if you want a album different >>from the current album
 * @return array
 */
function getAllSubAlbumIDs($albumfolder='') {
	global $_zp_current_album;
	if (empty($albumfolder)) {
		if (isset($_zp_current_album)) {
			$albumfolder = $_zp_current_album->getFolder();
		} else {
			return null;
		}
	}
	$query = "SELECT `id`,`folder`, `show` FROM " . prefix('albums') . " WHERE `folder` LIKE '" . mysql_real_escape_string($albumfolder) . "%'";
	$subIDs = query_full_array($query);
	return $subIDs;
}

/**
 * recovers search parameters from stored cookie, clears the cookie
 *
 * @param string $album Name of the album
 * @param string $image Name of the image
 */
function handleSearchParms($album='', $image='') {
	global $_zp_current_search, $zp_request;
	$cookiepath = WEBPATH;
	if (WEBPATH == '') { $cookiepath = '/'; }
	if ($zp_request && empty($album)) { // clear the cookie if we are loading the index page
		zp_setcookie("zenphoto_image_search_params", "", time()-368000, $cookiepath);
		return;
	}
	$context = get_context();
	$params = zp_getCookie('zenphoto_image_search_params');
	if (!empty($params)) {
		$_zp_current_search = new SearchEngine();
		$_zp_current_search->setSearchParams($params);
		// check to see if we are still "in the search context"
		if (!empty($image)) {
			if ($_zp_current_search->getImageIndex($album, $image) !== false) {
				$context = $context | ZP_SEARCH_LINKED;
			}
		}
		if (!empty($album)) {
			$albumlist = $_zp_current_search->getAlbums(0);
			foreach ($albumlist as $searchalbum) {
				if (strpos($album, $searchalbum) !== false) {
					$context = $context | ZP_ALBUM_LINKED | ZP_SEARCH_LINKED;
					break;
				}
			}
		}
		if (($context & ZP_SEARCH_LINKED)) {
			set_context($context);
		} else {
			$_zp_current_search = null;
		}
	}
}

/**
 * Returns the number of album thumbs that go on a gallery page
 *
 * @return int
 */
function galleryAlbumsPerPage() {
	return max(1, getOption('albums_per_page'));
}

/**
 * Returns the theme folder
 * If there is an album theme, loads the theme options.
 *
 * @return string
 */
function setupTheme() {
	global $_zp_gallery, $_zp_current_album, $_zp_current_search, $_zp_options, $_zp_themeroot;
	$albumtheme = '';
	if (in_context(ZP_SEARCH_LINKED)) {
		$name = $_zp_current_search->dynalbumname;
		if (!empty($name)) {
			$album = new Album($_zp_gallery, $name);
		} else {
			$album = NULL;
		}
	} else {
		$album = $_zp_current_album;
	}
	$theme = $_zp_gallery->getCurrentTheme();
	if (!is_null($album)) {
		$parent = getUrAlbum($album);
		$albumtheme = $parent->getAlbumTheme();
	}
	if (!(false === ($requirePath = getPlugin('themeoptions.php', $theme)))) {
		require_once($requirePath);
		$optionHandler = new ThemeOptions(); /* prime the theme options */
	}
	if (!empty($albumtheme)) {
		$theme = $albumtheme;
		$tbl = prefix('options').' WHERE `ownerid`='.$parent->id;
		//load the album theme options
		$sql = "SELECT `name`, `value` FROM ".$tbl;
		$optionlist = query_full_array($sql, true);
		if ($optionlist !== false) {
			foreach($optionlist as $option) {
				$_zp_options[$option['name']] = $option['value'];
			}
		}
	}
	$_zp_themeroot = WEBPATH . "/".THEMEFOLDER."/$theme";
	return $theme;
}

/**
 * Returns true if the file has the dynamic album suffix
 *
 * @param string $path
 * @return bool
 */
function hasDyanmicAlbumSuffix($path) {
	return strtolower(substr(strrchr($path, "."), 1)) == 'alb';
}

/**
 * Count Binary Ones
 *
 * Returns the number of bits set in $bits
 *
 * @param bit $bits the bit mask to count
 * @param int $limit the upper limit on the numer of bits;
 * @return int
 */
function cbone($bits, $limit) {
	$c = 0;
	for ($i=0; $i<$limit; $i++) {
		$x = pow(2, $i);
		if ($bits & $x) $c++;
	}
	return $c;
}

/**
 * Allows plugins to add to the scripts output by zenJavascript()
 *
 * @param string $script the text to be added.
 */
function addPluginScript($script) {
	global $_zp_plugin_scripts;
	$_zp_plugin_scripts[] = $script;
}

/**
 * Registers a plugin as handler for a file extension
 *
 * @param string $suffix the file extension
 * @param string $objectName the name of the object that handles this extension
 */
function addPluginType($suffix, $objectName) {
	global $_zp_extra_filetypes;
	$_zp_extra_filetypes[strtolower($suffix)] = $objectName;
}

/**
 * Returns true if the file is handled by a plugin object
 *
 * @param string $filename
 * @return bool
 */
function is_valid_other_type($filename) {
	global $_zp_extra_filetypes;
	$ext = strtolower(substr(strrchr($filename, "."), 1));
	if (array_key_exists($ext, $_zp_extra_filetypes)) {
		return $ext;
	} else {
		return false;
	}
}

/**
 * generates a simple captcha for comments
 *
 * Thanks to gregb34 who posted the original code
 *
 * Returns the captcha code string and image URL (via the $image parameter).
 *
 * @return string;
 */
function generateCaptcha(&$image) {

	$lettre='abcdefghijkmnpqrstuvwxyz23456789';

	$string = '';
	for ($i=0; $i < CAPTCHA_LENGTH; $i++) {
		$string .= $lettre[rand(0,31)];
	}
	$admins = getAdministrators();
	$admin = array_shift($admins);
	$key = $admin['pass'];
	$cypher = bin2hex(rc4($key, $string));
	$code=md5($cypher);
	query('DELETE FROM '.prefix('captcha').' WHERE `ptime`<'.(time()-3600), true);  // expired tickets
	query("INSERT INTO " . prefix('captcha') . " (ptime, hash) VALUES ('" . escape(time()) . "','" . escape($code) . "')", true);
	$image = WEBPATH . '/' . ZENFOLDER . "/c.php?i=$cypher";

	return $code;
}

// multidimensional array column sort - source: http://codingforums.com/showthread.php?t=71904
/**
 * Trims the tag values and eliminates duplicates.
 * Tags are case insensitive so only the first of 'Tag' and 'tag' will be preserved
 *
 * Returns the filtered tag array.
 *
 * @param array $tags
 * @return array
 */
function filterTags($tags) {
	$lc_tags = array();
	$filtered_tags = array();
	foreach ($tags as $key=>$tag) {
		$tag = trim($tag);
		if (!empty($tag)) {
			$lc_tag = strtolower($tag);
			if (!in_array($lc_tag, $lc_tags)) {
				$lc_tags[] = $lc_tag;
				$filtered_tags[] = $tag;
			}
		}
	}
	return $filtered_tags;
}

$_zp_unique_tags = NULL;
/**
 * Returns an array of unique tag names
 *
 * @return unknown
 */
function getAllTagsUnique() {
	global $_zp_unique_tags;
	if (!is_null($_zp_unique_tags)) return $_zp_unique_tags;  // cache them.
	$sql = "SELECT `name` FROM ".prefix('tags').' ORDER BY `name`';
	$result = query_full_array($sql);
	if (is_array($result)) {
		$_zp_unique_tags = array();
		foreach ($result as $row) {
			$_zp_unique_tags[] = $row['name'];
		}
		return $_zp_unique_tags;
	} else {
		return array();
	}
}

$_zp_count_tags = NULL;
/**
 * Returns an array indexed by 'tag' with the element value the count of the tag
 *
 * @return array
 */
function getAllTagsCount() {
	global $_zp_count_tags;
	if (!is_null($_zp_count_tags)) return $_zp_count_tags;
	$_zp_count_tags = array();
	$sql = "SELECT `name`, `id` from ".prefix('tags').' ORDER BY `name`';
	$tagresult = query_full_array($sql);
	if (is_array($tagresult)) {
		$sql = 'SELECT `tagid`, `objectid` FROM '.prefix('obj_to_tag').' ORDER BY `tagid';
		$countresult = query_full_array($sql);
		if (is_array($countresult)) {
			$id = 0;
			$tagcounts = array();
			foreach ($countresult as $row) {
				if ($id != $row['tagid']) {
					$tagcounts[$id = $row['tagid']] = 0;
				}
				$tagcounts[$id] ++;
			}
			foreach ($tagresult as $row) {
				if (isset($tagcounts[$row['id']])) {
					$_zp_count_tags[$row['name']] = $tagcounts[$row['id']];
				} else {
					$_zp_count_tags[$row['name']] = 0;
				}
			}
		} else {
			foreach ($tagresult as $tag) {
				$_zp_count_tags[$tag] = 0;
			}
		}
	}
	return $_zp_count_tags;
}

/**
 * Stores tags for an album/image
 *
 * @param array $tags the tag values
 * @param int $id the record id of the album/image
 * @param string $tbl 'albums' or 'images'
 */
function storeTags($tags, $id, $tbl) {
	$tags = filterTags($tags);
	$tagsLC = array();
	foreach ($tags as $tag) {
		$tagsLC[$tag] = utf8::strtolower($tag);
	}
	$sql = "SELECT `id`, `tagid` from ".prefix('obj_to_tag')." WHERE `objectid`='".$id."' AND `type`='".$tbl."'";
	$result = query_full_array($sql);
	$existing = array();
	if (is_array($result)) {
		foreach ($result as $row) {
			$dbtag = query_single_row("SELECT `name` FROM ".prefix('tags')." WHERE `id`='".$row['tagid']."'");
			$existingLC = utf8::strtolower($dbtag['name']);
			if (in_array($existingLC, $tagsLC)) { // tag already set no action needed
				$existing[] = $existingLC;
			} else { // tag no longer set, remove it
				query("DELETE FROM ".prefix('obj_to_tag')." WHERE `id`='".$row['id']."'");
			}
		}
	}
	$tags = array_flip(array_diff($tagsLC, $existing)); // new tags for the object
	foreach ($tags as $tag) {
		$dbtag = query_single_row("SELECT `id` FROM ".prefix('tags')." WHERE `name`='".mysql_real_escape_string($tag)."'");
		if (!is_array($dbtag)) { // tag does not exist
			query("INSERT INTO " . prefix('tags') . " (name) VALUES ('" . mysql_real_escape_string($tag) . "')");
			$dbtag = query_single_row("SELECT `id` FROM ".prefix('tags')." WHERE `name`='".mysql_real_escape_string($tag)."'");
		}
		query("INSERT INTO ".prefix('obj_to_tag'). "(`objectid`, `tagid`, `type`) VALUES (".$id.",".$dbtag['id'].",'".$tbl."')");
	}
}

/**
 * Retrieves the tags for an album/image
 * Returns them in an array
 *
 * @param int $id the record id of the album/image
 * @param string $tbl 'albums' or 'images'
 * @return unknown
 */
function readTags($id, $tbl) {
	$tags = array();
	$result = query_full_array("SELECT `tagid` FROM ".prefix('obj_to_tag')." WHERE `type`='".$tbl."' AND `objectid`='".$id."'");
	if (is_array($result)) {
		foreach ($result as $row) {
			$dbtag = query_single_row("SELECT `name` FROM".prefix('tags')." WHERE `id`='".$row['tagid']."'");
			$tags[] = $dbtag['name'];
		}
	}
	natcasesort($tags);
	return $tags;
}

/**
 * Creates the body of a select list
 *
 * @param array $currentValue list of items to be flagged as checked
 * @param array $list the elements of the select list
 * @param bool $descending set true for a reverse order sort
 */
function generateListFromArray($currentValue, $list, $descending, $localize) {
	if ($localize) {
		$list = array_flip($list);
		if ($descending) {
			arsort($list);
		} else {
			natcasesort($list);
		}
		$list = array_flip($list);
	} else {
		if ($descending) {
			rsort($list);
		} else {
			natcasesort($list);
		}
	}
	foreach($list as $key=>$item) {
		echo '<option value="' . $item . '"';		
		if (in_array($item, $currentValue)) {
			echo ' selected="selected"';
		}
		if ($localize) $display = $key; else $display = $item;
		echo '>' . $display . "</option>"."\n";
	}
}

/**
 * Generates a selection list from files found on disk
 *
 * @param strig $currentValue the current value of the selector
 * @param string $root directory path to search
 * @param string $suffix suffix to select for
 * @param bool $descending set true to get a reverse order sort
 */
function generateListFromFiles($currentValue, $root, $suffix, $descending=false) {
	$curdir = getcwd();
	chdir($root);
	$filelist = safe_glob('*'.$suffix);
	$list = array();
	foreach($filelist as $file) {
		$file = str_replace($suffix, '', $file);
		$list[] = FilesystemToUTF8($file);
	}
	generateListFromArray(array($currentValue), $list, $descending, false);
	chdir($curdir);
}

/**
 * General link printing function
 * @param string $url The link URL
 * @param string $text The text to go with the link
 * @param string $title Text for the title tag
 * @param string $class optional class
 * @param string $id optional id
 */
function printLink($url, $text, $title=NULL, $class=NULL, $id=NULL) {
	echo "<a href=\"" . htmlspecialchars($url) . "\"" .
	(($title) ? " title=\"" . html_encode($title) . "\"" : "") .
	(($class) ? " class=\"$class\"" : "") .
	(($id) ? " id=\"$id\"" : "") . ">" .
	$text . "</a>";
}

/**
 * multidimensional array column sort
 *
 * @param array $array The multidimensional array to be sorted
 * @param string $index Which key should be sorted by
 * @param string $order The order to sort by, "asc" or "desc"
 * @param bool $natsort If natural order should be used
 * @param bool $case_sensitive If the sort should be case sensitive
 * @return array
 *
 * @author redoc (http://codingforums.com/showthread.php?t=71904)
 */
function sortMultiArray($array, $index, $order='asc', $natsort=FALSE, $case_sensitive=FALSE) {
	if(is_array($array) && count($array)>0) {
		foreach(array_keys($array) as $key) {
			$temp[$key]=$array[$key][$index];
			if(!$natsort) {
				($order=='asc')? asort($temp) : arsort($temp);
			} else {
				($case_sensitive)? natsort($temp) : natcasesort($temp);
				if($order!='asc')  {
					$temp=array_reverse($temp,TRUE);
				}
			}
		}
		foreach(array_keys($temp) as $key) {
			if(is_numeric($key)) {
				$sorted[]=$array[$key];
			} else {
				$sorted[$key]=$array[$key];
			}
		}
		return $sorted;
	}
	return $array;
}

$_zp_not_viewable_album_list = NULL;
/**
 * Returns a list of album IDs that the current viewer is allowed to see
 *
 * @return array
 */
function getNotViewableAlbums() {
	if (zp_loggedin(ADMIN_RIGHTS)) return array(); //admins can see all
	$hint = '';
	global $_zp_not_viewable_album_list;
	if (is_null($_zp_not_viewable_album_list)) {
		$sql = 'SELECT `folder`, `id`, `password` FROM '.prefix('albums').' WHERE `show`=0 OR `password`!=""';
		$result = query_full_array($sql);
		if (is_array($result)) {
			$_zp_not_viewable_album_list = array();
			foreach ($result as $row) {
				if (!checkAlbumPassword($row['folder'], $hint)) {
					$_zp_not_viewable_album_list[] = $row['id'];
				} else {
					if (!zp_loggedin() || !isMyAlbum($row['folder'])) {
						$_zp_not_viewable_album_list[] = $row['id'];
					}
				}
			}
		}
	}
	return $_zp_not_viewable_album_list;
}

/**
 * Parses and sanitizes Theme definition text
 *
 * @param file $file theme file
 * @return string
 */
function parseThemeDef($file) {
	$file = UTF8ToFilesystem($file);
	$themeinfo = array();
	if (is_readable($file) && $fp = @fopen($file, "r")) {
		while($line = fgets($fp)) {
			if (substr(trim($line), 0, 1) != "#") {
				$item = explode("::", $line);
				$themeinfo[trim($item[0])] = sanitize(trim($item[1]), 1);
			}
		}
		return $themeinfo;
	} else {
		return false;
	}
}

/**
 * Emits a page error. Used for attempts to bypass password protection
 *
 */
function pageError($err,$text) {
	header("HTTP/1.0 ".$err.' '.$text);
	echo "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head>	<title>".$err." - ".$text."</TITLE>	<META NAME=\"ROBOTS\" CONTENT=\"NOINDEX, FOLLOW\"></head>";
	echo "<BODY bgcolor=\"#ffffff\" text=\"#000000\" link=\"#0000ff\" vlink=\"#0000ff\" alink=\"#0000ff\">";
	echo "<FONT face=\"Helvitica,Arial,Sans-serif\" size=\"2\">";
	echo "<b>".sprintf(gettext('Page access %2$s (%1$s)'),$err, $text)."</b><br/><br/>";
	echo "</body></html>";
}

/**
 * Checks to see if a URL is valid
 *
 * @param string $url the URL being checked
 * @return bool
 */
function isValidURL($url) {
	return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

/**
 * Provide an alternative to glob which does not return filenames with accented charactes in them
 *
 * @param string $pattern the 'pattern' for matching files
 * @param bit $flags glob 'flags'
 */
function safe_glob($pattern, $flags=0) {
	$split=explode('/',$pattern);
	$match=array_pop($split);
	$path_return = $path = implode('/',$split);
	if (empty($path)) {
		$path = '.';
	} else {
		$path_return = $path_return . '/';
	}
	if (!is_dir($path)) return array();
	if (($dir=opendir($path))!==false) {
		$glob=array();
		while(($file=readdir($dir))!==false) {
			if (safe_fnmatch($match,$file)) {
				if ((is_dir("$path/$file"))||(!($flags&GLOB_ONLYDIR))) {
					if ($flags&GLOB_MARK) $file.='/';
					$glob[]=$path_return.$file;
				}
			}
		}
		closedir($dir);
		if (!($flags&GLOB_NOSORT)) sort($glob);
		return $glob;
	} else {
		return array();
	}
}

/**
 * pattern match function Works with accented characters where the PHP one does not.
 *
 * @param string $pattern pattern
 * @param string $string haystack
 * @return bool
 */
function safe_fnmatch($pattern, $string) {
	return @preg_match('/^' . strtr(addcslashes($pattern, '\\.+^$(){}=!<>|'), array('*' => '.*', '?' => '.?')) . '$/i', $string);
}

/**
 * Returns the value of a cookie from either the cookies or from $_SESSION[]
 *
 * @param string $name the name of the cookie
 */
function zp_getCookie($name) {
	if (DEBUG_LOGIN) {
		if (isset($_SESSION[$name])) {
			$sessionv = $_SESSION[$name];
		} else {
			$sessionv = '';
		}
		if (isset($_COOKIE[$name])) {
			$cookiev = $_COOKIE[$name];
		} else {
			$cookiev = '';
		}
		debugLog("zp_getCookie($name): SESSION[".session_id()."]=".$sessionv.", COOKIE=".$cookiev);
	}
	if (isset($_COOKIE[$name]) && !empty($_COOKIE[$name])) {
		return $_COOKIE[$name];
	}
	if (isset($_SESSION[$name])) {
		return $_SESSION[$name];
	}
	return false;
}

/**
 * Sets a cookie both in the browser cookies and in $_SESSION[]
 *
 * @param string $name The 'cookie' name
 * @param string $value The value to be stored
 * @param timestamp $time The time the cookie expires
 * @param string $path The path on the server in which the cookie will be available on
 */
function zp_setCookie($name, $value, $time=0, $path='/') {
	if (!getOption('album_session')) {
		if (DEBUG_LOGIN) {
			debugLog("zp_setCookie($name, $value, $time, $path)");
		}
		setcookie($name, $value, $time, $path);
	}
	if ($time < time()) {
		if (isset($_SESSION))	unset($_SESSION[$name]);
		if (isset($_COOKIE)) unset($_COOKIE[$name]);
	} else {
		$_SESSION[$name] = $value;
		$_COOKIE[$name] = $value;
	}
}

/**
 * Returns a new "image" object based on the file extension
 *
 * @param object $album the owner album
 * @param string $filename the filename
 * @return object
 */
function newImage(&$album, $filename) {
	global $_zp_extra_filetypes;
	if ($ext = is_valid_other_type($filename)) {
		$object = $_zp_extra_filetypes[$ext];
		return new $object($album, $filename);
	} else {
		return New Image($album, $filename);
	}
}

/**
 * Copies a directory recursively
 * @param string $srcdir the source directory.
 * @param string $dstdir the destination directory.
 * @return the total number of files copied.
 */
function dircopy($srcdir, $dstdir) {
	$num = 0;
	if(!is_dir($dstdir)) mkdir($dstdir);
	if($curdir = opendir($srcdir)) {
		while($file = readdir($curdir)) {
			if($file != '.' && $file != '..') {
				$srcfile = $srcdir . '/' . $file;
				$dstfile = $dstdir . '/' . $file;
				if(is_file($srcfile)) {
					if(is_file($dstfile)) $ow = filemtime($srcfile) - filemtime($dstfile); else $ow = 1;
					if($ow > 0) {
						if(copy($srcfile, $dstfile)) {
							touch($dstfile, filemtime($srcfile)); $num++;
						}
					}
				}
				else if(is_dir($srcfile)) {
					$num += dircopy($srcfile, $dstfile, $verbose);
				}
			}
		}
		closedir($curdir);
	}
	return $num;
}

setexifvars();
?>