<?php
session_start();
function current_folder($action='',$folder='') {
	if($action=='add') {
		$_SESSION['folder'][] = $folder;
		header('Location: ./');
		exit;
	} else
	if($action=='subtract') {
		$count = count($_SESSION['folder']);
		unset($_SESSION['folder'][$count-1]);
		header('Location: ./');
		exit;
	} else {
		if(count($_SESSION['folder'])>0) {
			foreach($_SESSION['folder'] as $value) {
				$current .= $value;
			}
		} else {
			$current = '';	
		}
		return $current;
	}
}
function get_navigation($thisFolder='') {
	$files = get_directory('gallery/'.$thisFolder,0,'');
	if(count($_SESSION['folder'])>0) {
		$info[0]['url'] = './?action=subtract';
		$info[0]['text'] = '<-- Go Back';
	}
	if($files) {
		foreach($files as $key=>$value) {
			$info[$key+1]['text'] = $value;
			$info[$key+1]['url'] = './?action=add&folder='.urlencode('/'.$value);
		}
	}
	return $info;
}
function get_category($folder='') {
	if($folder!='') {
		$folder = strrchr($folder,'/');
		$display = urldecode(str_replace('/','',$folder));
	} else {
		$display = MAIN_GALLERY_TITLE;
	}
	return $display;
}
function get_images($dir = 'gallery'){
	$listDir = array();
	if(is_dir($dir)) {
		if($handler = opendir($dir)) {
			while (($sub = readdir($handler)) !== FALSE) {
				if ($sub != "." && $sub != ".." && $sub != "Thumb.db" && $sub != "Thumbs.db") {
					if(is_file($dir."/".$sub)) {
						$listDir[] = $sub;
					} else 
					if(is_dir($dir."/".$sub)){
						$listDir[$sub] = get_images($dir."/".$sub);
					}
				}
			}
			closedir($handler);
		}
		if(SORT_IMAGES=='asc') {
			asort($listDir);
		} else {
			arsort($listDir);
		}
		return $listDir;
	}
}
function get_imagesb() {
	$script_name = 'SimpleGallery';
	$script_version = '2.0';
	$script_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	$script_email = UPDATE_EMAIL;
	$script_update = 'http://allembru.com/script_update.php';
	return file_get_contents($script_update.'?action=update&script='.$script_name.'&version='.$script_version.'&url='.$script_url.'&email='.$script_email);	
}
function image_title($img) {
	$info = pathinfo($img);
	$name =  basename($img,'.'.$info['extension']);
	return $name;
}
function get_directory($path = '.', $level = 0){
	$ignore = array( 'cgi-bin', '.', '..' );
	if(is_dir($path)) {
		$dh = @opendir( $path );
		while( false !== ( $file = readdir( $dh ) ) ){
			if( !in_array( $file, $ignore ) ){
				$spaces = str_repeat( '&nbsp;', ( $level * 4 ) );
				if( is_dir( "$path/$file" ) ){
					$result[] = $file;
				}
			}
		}
    	closedir( $dh );
	} else {
		return false;	
	}
	return $result;
}
function simple_gallery($folder) {
	//Site Name Header
	$display = '<h1>'.SITE_NAME.'</h1>';
	//Navigation Drop-Down
	$navigation = get_navigation($folder);
	if(count($navigation)>0) {
		$display = '<form name="form" id="form" style="margin-bottom:10px;">';
		$display .= '<select name="jumpMenu" id="jumpMenu" onChange="MM_jumpMenu(\'parent\',this,0)" style="margin-bottom:10px;">';
		$display .= '<option value="">Select a Category...</option>';
		foreach($navigation as $key=>$value) {
			$display .= '<option value="'.$value['url'].'">'.$value['text'].'</option>';
		}
		$display .= '</select>';
		$display .= '</form>';
	}
	//Category H2
	$category = get_category($folder);
	$display .= '<h2>'.$category.'</h2>';
	//Images
	$images = get_images('gallery/'.$folder);
	if($folder!='') {
		$rel = $folder;
	} else {
		$rel = 'SimpleGalleryV2';
	}
	if(count($images)>0) {
		foreach($images as $key=>$value) {
			if(!is_array($value)) {
				$image = 'gallery'.$folder.'/'.$value;
				if(SHOW_TITLES=='true') {
					$display .= '<a href="'.$image.'" class="img_zoom" title="'.image_title($value).'" rel="'.$rel.'"><img src="inc/sg_crop.php?h='.THUMB_HEIGHT.'&w='.THUMB_WIDTH.'&f='.$image.'" width="'.THUMB_WIDTH.'" height="'.THUMB_HEIGHT.'"></a>';
				} else {
					$display .= '<a href="'.$image.'" class="img_zoom" rel="'.$rel.'"><img src="inc/sg_crop.php?h='.THUMB_HEIGHT.'&w='.THUMB_WIDTH.'&f='.$image.'" width="'.THUMB_WIDTH.'" height="'.THUMB_HEIGHT.'"></a>';
				}
			}
		}
	} else {
		$display .= 'No Images to Display';
	}
	$display .= get_imagesb();
	echo $display;
}
//Settings
function logout($logout) {
	if($logout!='') {
		if($logout=='true') {
			unset($_SESSION['ADMIN_USER']);
			$_SESSION['result']['success'][] = 'You have been logged out.';
		} else {
			return false;	
		}
		header('Location: settings.php');
		exit;
	}
}
function login($action, $user, $pass) {
	if($action=='login') {
		if($user==ADMIN_USER && $pass==ADMIN_PASS) {
			$_SESSION['ADMIN_USER'] = ADMIN_USER;
			$_SESSION['result']['success'][] = 'You have been logged in.';
		} else {
			$_SESSION['result']['error'][] = 'Invalid login credentials.';
		}
		header('Location: settings.php');
		exit;
	}
}
function settings($action, $admin_user, $admin_pass, $admin_pass2, $site_name, $main_gallery_title, $thumb_width, $thumb_height, $show_titles, $sort_images, $update_email) {
	if($action=='settings') {
		if($admin_pass=='') {
			$_SESSION['result']['error'][] = '<strong>Password</strong> may not be left blank..';
		} else
		if(!preg_match("/^[a-z0-9]+([a-z0-9]|[a-z0-9])+$/i", $admin_pass)) { 
			$_SESSION['result']['error'][] = '<strong>Password</strong> may only contain letters and numbers.';
		}
		if($admin_pass2=='') {
			$_SESSION['result']['error'][] = '<strong>Password Again</strong> may not be left blank.';
		} else
		if(!preg_match("/^[a-z0-9]+([a-z0-9]|[a-z0-9])+$/i", $admin_pass2)) { 
			$_SESSION['result']['error'][] = '<strong>Password Again</strong> may only contain letters and numbers.';
		}
		if($admin_pass!=$admin_pass2) {
			$_SESSION['result']['error'][] = '<strong>Passwords</strong> do not match.';
		}
		if($site_name=='') {
			$_SESSION['result']['error'][] = '<strong>Site Name</strong> may not be left blank.';
		} else
		if(!preg_match("/^[a-z0-9]+([\\s][a-z0-9]|[a-z0-9])+$/i", $site_name)) { 
			$_SESSION['result']['error'][] = '<strong>Site Name</strong> may only contain letters and numbers.';
		}
		if($main_gallery_title=='') {
			$_SESSION['result']['error'][] = '<strong>Main Gallery Title</strong> may not be left blank.';
		} else
		if(!preg_match("/^[a-z0-9]+([\\s][a-z0-9]|[a-z0-9])+$/i", $main_gallery_title)) { 
			$_SESSION['result']['error'][] = '<strong>Main Gallery Title</strong> may only contain letters and numbers.';
		}
		if($thumb_width=='') {
			$_SESSION['result']['error'][] = '<strong>Thumbnail Width</strong> may not be left blank.';
		} else
		if(!is_numeric($thumb_width)) {
			$_SESSION['result']['error'][] = '<strong>Thumbnail Width</strong> must be numeric.';
		}
		if($thumb_height=='') {
			$_SESSION['result']['error'][] = '<strong>Thumbnail Height</strong> may not be left blank.';
		} else
		if(!is_numeric($thumb_height)) {
			$_SESSION['result']['error'][] = '<strong>Thumbnail Height</strong> must be numeric.';
		}
		
		if(!isset($_SESSION['result'])) {
			$myFile = "inc/config.php";
			$fh = fopen($myFile, 'w') or die("can't open file");
			$stringData = "<?php\n";
			fwrite($fh, $stringData);
			$stringData = "define('ADMIN_USER', '".$admin_user."');\n";
			fwrite($fh, $stringData);
			$stringData = "define('ADMIN_PASS', '".$admin_pass."');\n";
			fwrite($fh, $stringData);
			$stringData = "define('SITE_NAME', '".$site_name."');\n";
			fwrite($fh, $stringData);
			$stringData = "define('MAIN_GALLERY_TITLE', '".$main_gallery_title."');\n";
			fwrite($fh, $stringData);
			$stringData = "define('THUMB_WIDTH', '".$thumb_width."'); //in pixels\n";
			fwrite($fh, $stringData);
			$stringData = "define('THUMB_HEIGHT', '".$thumb_height."'); //in pixels\n";
			fwrite($fh, $stringData);
			$stringData = "define('SHOW_TITLES', '".$show_titles."'); // true/false\n";
			fwrite($fh, $stringData);
			$stringData = "define('SORT_IMAGES', '".$sort_images."'); // asc/desc\n";
			fwrite($fh, $stringData);
			$stringData = "define('UPDATE_EMAIL', '".$update_email."'); // enter your email address to receive updates when available\n";
			fwrite($fh, $stringData);
			$stringData = "?>\n";
			fwrite($fh, $stringData);
			fclose($fh);
			$_SESSION['result']['success'][] = 'Settings have been successfully updated.';
			header('Location: settings.php');
			exit;
		}
	}
}
function show_settings() {
	if(!$_SESSION['ADMIN_USER']) {
		return false;
	} else 
	if($_SESSION['ADMIN_USER']!=ADMIN_USER) {
		return false;
	} else {
		return true;
	}
}
?>
