include('admin-config.php');
//check cookie in the case the session data is lost
cookie_check();
if (empty($_SESSION['GalleryUser']))
{
login();
exit;
}
// "photos" folder must exists and be writable
if (!file_exists($BASE_IMG_PATH))
{
echo 'Folder "photos" doesn\'t exist';
exit;
}
function folders_main($errors='')
{
global $BASE_IMG_PATH;
global $HIDE_FOLDERS_AR;
$dir = $BASE_IMG_PATH;
?>
Folders:
if ($errors)
{
/* ?>
echo $errors; ?>
*/
?>
}
$files_ar = array();
if (is_dir($dir))
{
$handle = opendir($dir);
if ($handle)
{
while (($file = readdir($handle)) !== false)
{
if (is_dir($dir . $file) and $file!='.' and $file!='..' and !in_array($file, $HIDE_FOLDERS_AR))
{
$files_ar[] = $file;
}
}
closedir($handle);
}
}
sort($files_ar);
foreach ($files_ar as $file)
{
?>
'."\n"; }
else if ($show=="html") { $str_preview.=htmlentities('')." \n"; }
if ($show=="")
{
$template_preview = file_get_contents('admin-preview.html');
$str_preview = str_replace(array('[folder_url]','[folder_name]','[content_preview]'), array($url, $folder, $str_preview), $template_preview);
}
else if ($show=="html")
{
$str_preview = htmlentities('
')." \n".$str_preview.htmlentities('
');
}
echo $str_preview;
}
function check_thumbnail_exist($folder, $file, $create=false)
{
global $BASE_IMG_PATH, $BASE_IMG_URL;
$dir = $BASE_IMG_PATH.$folder.'/';
$originals_dir = $dir.'originals/';
$thumbs_dir = $dir.'thumbs-125/';
// thumbnail size image doesn't exist
if (!file_exists($thumbs_dir.$file))
{
if ($create)
a_makethumb($originals_dir.$file,$thumbs_dir.$file,$height=125,$width=125);
else
return false;
}
// thumbnail size image is out dated
if (file_exists($thumbs_dir.$file))
{
$stat = stat($originals_dir.$file);
$thumbnail_stat = stat($thumbs_dir.$file);
if ($thumbnail_stat['mtime'] < $stat['mtime'])
{
if ($create) {
unlink($thumbs_dir.$file);
a_makethumb($originals_dir.$file,$thumbs_dir.$file,$height=125,$width=125);
} else
return false;
}
}
return true;
}
function check_regular_exist($folder, $file, $create=false)
{
global $BASE_IMG_PATH, $BASE_IMG_URL;
$dir = $BASE_IMG_PATH.$folder.'/';
$originals_dir = $dir.'originals/';
$images_dir = $dir.'images/';
// regular size image doesn't exist
if (!file_exists($images_dir.$file))
{
if ($create)
a_makethumb($originals_dir.$file,$images_dir.$file,$height=650,$width=650);
else
return false;
}
// thumbnail is out dated
if (file_exists($images_dir.$file))
{
$stat = stat($originals_dir.$file);
$image_stat = stat($images_dir.$file);
if ($image_stat['mtime'] < $stat['mtime'])
{
if ($create) {
unlink($images_dir.$file);
a_makethumb($originals_dir.$file,$images_dir.$file,$height=650,$width=650);
} else
return false;
}
}
return true;
}
function image_create()
{
global $BASE_IMG_PATH, $BASE_IMG_URL;
$folder = $_GET['f'];
$file = $_GET['image'];
$size = $_GET['size'];
$dir = $BASE_IMG_PATH.$folder.'/';
$originals_dir = $dir.'originals/';
$thumbs_dir = $dir.'thumbs-125/';
$images_dir = $dir.'images/';
$url = $BASE_IMG_URL.$folder.'/';
$originals_url = $url.'originals/';
$thumbs_url = $url.'thumbs-125/';
$images_url = $url.'images/';
header("Content-Type: image/jpeg");
if ($size == 'thumbnail')
{
check_thumbnail_exist($folder, $file, $create=true);
readfile($thumbs_dir.$file);
}
if ($size == 'regular')
{
check_regular_exist($folder, $file, $create=true);
readfile($images_dir.$file);
}
exit;
}
//function images_update()
//{
// $folder = $_GET['f'];
//
// $image_name_ar = $_POST['image_name'];
// $image_caption_ar = $_POST['image_caption'];
//
// csv_file_write($folder, $image_name_ar, $image_caption_ar);
//
// header('Location: admin-gallery.php?method=folder_view&f='.$folder);
// exit;
//}
function csv_file_write($folder, $image_name_ar, $image_caption_ar)
{
global $BASE_IMG_PATH, $BASE_IMG_URL;
$csv_file = $BASE_IMG_PATH.$folder.'/'.$folder.'.csv';
$set_permissions = false;
if (!file_exists($csv_file))
{
$set_permissions = true;
}
if (file_exists($csv_file) and !is_writable($csv_file))
{
chmod($csv_file, 0664);
}
if (file_exists($csv_file) and !is_writable($csv_file))
{
echo '.csv file is not writable';
exit;
}
$fp = fopen($csv_file, 'w');
for ($i=0; $i0)
{
return false;
}
return true;
}
function a_makethumb($origimg,$newimg,$height=200,$width=200,$quality=85)
{
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($origimg);
$size = getimagesize($origimg);
if ($size)
{
$width_orig = $size[0];
$height_orig = $size[1];
}
else
{
// could get the image dimmensions
return;
}
if ($height_orig<=$height && $width_orig<=$width)
{ // already small enough
$width=$width_orig;
$height=$height_orig;
}
else
{ // shrink
if (($width/$width_orig)>($height/$height_orig))
{
$height=round($height);
$width=round($width_orig*($height/$height_orig));
}
else
{
$width=round($width);
$height=round($height_orig*($width/$width_orig));
}
}
$image_p = imagecreatetruecolor($width, $height);
$ext = strtolower(pathinfo($origimg, PATHINFO_EXTENSION));
if ($ext == 'gif')
{
$image = imagecreatefromgif($origimg);
}
elseif ($ext == 'png')
{
$image = imagecreatefrompng($origimg);
}
else
{
$image = imagecreatefromjpeg($origimg);
}
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
if ($ext == 'gif')
{
imagegif($image_p, $newimg);
}
elseif ($ext == 'png')
{
imagepng($image_p, $newimg);
}
else
{
// rotate
$exif = exif_read_data($origimg);
if (is_array($exif))
{
if (!empty($exif['Orientation']) and $exif['Orientation']==6)
{
$image_p = imagerotate($image_p, $degrees=270, 0);
}
if (!empty($exif['Orientation']) and $exif['Orientation']==8)
{
$image_p = imagerotate($image_p, $degrees=90, 0);
}
}
imagejpeg($image_p, $newimg, $quality);
}
imagedestroy($image_p);
}
function login()
{
global $GALLERY_USERS;
$error = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST' and !empty($_POST['submit']) and $_POST['submit'] == 'Login')
{
$login = trim($_POST['login']);
$pass = trim($_POST['pass']);
if (!empty($login) and !empty($pass))
{
if (!empty($GALLERY_USERS[$login]) and $GALLERY_USERS[$login] == md5($pass))
{
$_SESSION['GalleryUser'] = $login;
cookie_set($login);
header('Location: admin-gallery.php');
exit;
}
else
{
$error = 'Incorrect Login/Password';
}
}
else
{
$error = 'Login or Password are empty';
}
}
template_header();
?>
echo $error; ?>
template_footer();
}
function cookie_set($user)
{
$data = $user;
//encrypt the user data (id and email)
$key = 'IslandTechnologies.Net';
$iv = '20120606';
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH,'','cbc','');
mcrypt_generic_init($cipher, $key, $iv);
$encrypted = mcrypt_generic($cipher, $data);
mcrypt_generic_deinit($cipher);
//set cookies (admin gallery user)
setcookie('AdminGalleryUser', md5($user), 0, '/');
setcookie('AdminGalleryData', $encrypted, 0, '/');
}
function cookie_unset()
{
setcookie('AdminGalleryUser', "", time()-60*60, '/');
setcookie('AdminGalleryData', "", time()-60*60, '/');
}
function cookie_check()
{
//restore session user from cookie
if (empty($_SESSION['GalleryUser']) and !empty($_COOKIE['AdminGalleryUser']) and !empty($_COOKIE['AdminGalleryData']))
{
//decrypt the user data (id and email)
$key = 'IslandTechnologies.Net';
$iv = '20120606';
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH,'','cbc','');
mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher, $_COOKIE['AdminGalleryData']);
mcrypt_generic_deinit($cipher);
//check you have a valid cookie ... user_id and email stored in cookie should match the info in the database
$user = trim($decrypted);
$restore_session = true;
if (md5($user) == $_COOKIE['AdminGalleryUser'])
{
}
else
{
$restore_session = false;
}
if ($restore_session)
{
$_SESSION['GalleryUser']=$user;
}
}
}
function template_header()
{
global $HIDE_TEMPLATE;
if (!$HIDE_TEMPLATE)
{
echo '
Gallery';
?>
global $method;
if ($method == 'folder_view') {
?>
}
echo '
';
}
}
function template_footer()
{
global $HIDE_TEMPLATE;
if (!$HIDE_TEMPLATE)
{
echo '
';
}
}
$method = @$_GET['method'];
if ($method == 'folder_preview' or $method == 'folder_html' or $method == 'folder_html_mobile' or $method == 'image_create')
{
$HIDE_TEMPLATE=1;
}
template_header();
switch ($method)
{
case 'folders_main':
folders_main();
break;
case 'folder_add':
folder_add();
break;
case 'folder_view':
folder_view();
break;
case 'folder_preview':
folder_preview();
break;
case 'folder_html':
folder_preview($show="html");
break;
case 'folder_html_mobile':
folder_preview($show="html_mobile", 'admin-image-mobile.html');
break;
case 'image_create':
image_create();
break;
// case 'images_update':
// images_update();
// break;
case 'captions_update':
captions_update();
break;
default:
folders_main();
}
template_footer();
?>