Minigalnano/index.php

560 lines
20 KiB
PHP
Raw Permalink Normal View History

<?php
/*
* This file is part of MinigalNano: https://github.com/sebsauvage/MinigalNano
2015-07-07 14:01:47 +02:00
*
* MiniGal Nano is based on an original work by Thomas Rybak (© 2010)
*
* MinigalNano is licensed under the AGPL v3 (https://gnu.org/licenses/agpl-3.0.txt).
2015-07-02 00:08:46 +02:00
*/
2014-11-25 13:58:04 +01:00
error_reporting(-1);
2014-11-25 13:58:04 +01:00
// Do not edit below this section unless you know what you are doing!
2014-02-09 16:43:32 +01:00
header('Content-Type: text/html; charset=UTF-8'); // We use UTF-8 for proper international characters handling.
2015-07-07 14:03:36 +02:00
$version = "0.4.0";
2014-11-25 13:58:04 +01:00
ini_set("memory_limit", "256M");
2015-07-02 00:08:46 +02:00
require "config-default.php";
include "config.php";
2014-11-25 13:58:04 +01:00
//-----------------------
// DEFINE VARIABLES
//-----------------------
2019-11-13 12:08:51 +01:00
$page_navigation = "";
$breadcrumb_navigation = "";
2019-11-13 12:08:51 +01:00
$thumbnails = "";
$new = "";
$images = "";
$exif_data = "";
$messages = "";
$comment = "";
//-----------------------
// PHP ENVIRONMENT CHECK
//-----------------------
if (!function_exists('exif_read_data') && $display_exif == 1) {
2019-11-13 12:08:51 +01:00
$display_exif = 0;
$messages = "Error: PHP EXIF is not available. Set &#36;display_exif = 0; in config.php to remove this message";
}
//-----------------------
// FUNCTIONS
//-----------------------
2019-11-13 12:08:51 +01:00
function padstring($name, $length)
{
global $label_max_length;
if (!isset($length)) {
$length = $label_max_length;
}
if (strlen($name) > $length) {
return substr($name, 0, $length) . "...";
}
return $name;
}
2014-11-25 13:58:04 +01:00
2019-11-13 12:08:51 +01:00
function getfirstImage($dirname)
{
$imageName = false;
$extensions = array("jpg", "png", "jpeg", "gif");
if ($handle = opendir($dirname)) {
while (false !== ($file = readdir($handle))) {
if ($file[0] == '.') {
continue;
}
$pathinfo = pathinfo($file);
if (empty($pathinfo['extension'])) {
continue;
}
$ext = strtolower($pathinfo['extension']);
if (in_array($ext, $extensions)) {
$imageName = $file;
break;
}
}
closedir($handle);
}
return $imageName;
}
2014-11-25 13:58:04 +01:00
2019-11-13 12:08:51 +01:00
function parse_fraction($v, $round = 0)
{
list($x, $y) = array_map('intval', explode('/', $v));
if (empty($x) || empty($y)) {
return $v;
}
if ($x % $y == 0) {
return $x / $y;
}
if ($y % $x == 0) {
return "1/" . $y / $x;
}
return round($x / $y, $round);
2014-11-27 16:51:58 +01:00
}
2019-11-13 12:08:51 +01:00
function readEXIF($file)
{
$exif_arr = array();
$exif_data = exif_read_data($file);
2015-07-02 00:08:46 +02:00
2019-11-13 12:08:51 +01:00
$exif_val = @$exif_data['Model'];
if (!empty($exif_val)) {
$exif_arr[] = $exif_val;
}
2015-07-02 00:08:46 +02:00
2019-11-13 12:08:51 +01:00
$exif_val = @$exif_data['FocalLength'];
if (!empty($exif_val)) {
$exif_arr[] = parse_fraction($exif_val) . "mm";
}
2015-07-02 00:08:46 +02:00
2019-11-13 12:08:51 +01:00
$exif_val = @$exif_data['ExposureTime'];
if (!empty($exif_val)) {
$exif_arr[] = parse_fraction($exif_val, 2) . "s";
}
2015-07-02 00:08:46 +02:00
2019-11-13 12:08:51 +01:00
$exif_val = @$exif_data['FNumber'];
if (!empty($exif_val)) {
$exif_arr[] = "f" . parse_fraction($exif_val);
}
2015-07-02 00:08:46 +02:00
2019-11-13 12:08:51 +01:00
$exif_val = @$exif_data['ISOSpeedRatings'];
if (!empty($exif_val)) {
$exif_arr[] = "ISO " . $exif_val;
}
2015-07-02 00:08:46 +02:00
2019-11-13 12:08:51 +01:00
if (count($exif_arr) > 0) {
return "::" . implode(" | ", $exif_arr);
}
2016-06-24 17:07:13 +02:00
2019-11-13 12:08:51 +01:00
return $exif_arr;
}
2014-11-25 13:58:04 +01:00
2019-11-13 12:08:51 +01:00
function checkpermissions($file)
{
global $messages;
2014-11-26 18:43:29 +01:00
2019-11-13 12:08:51 +01:00
if (!is_readable($file)) {
$messages = "At least one file or folder has wrong permissions. "
. "Learn how to <a href='http://minigal.dk/faq-reader/items/"
. "how-do-i-change-file-permissions-chmod.html' target='_blank'>"
. "set file permissions</a>";
}
2015-07-02 00:08:46 +02:00
}
2019-11-13 12:08:51 +01:00
function guardAgainstDirectoryTraversal($path)
{
$pattern = "/^(.*\/)?(\.\.)(\/.*)?$/";
2016-06-24 17:07:13 +02:00
$directory_traversal = preg_match($pattern, $path);
2016-06-24 17:07:13 +02:00
if ($directory_traversal === 1) {
die("ERROR: Could not open " . htmlspecialchars(stripslashes($current_dir)) . " for reading!");
}
}
2015-07-02 00:08:46 +02:00
if (!defined("GALLERY_ROOT")) {
2019-11-13 12:08:51 +01:00
define("GALLERY_ROOT", "");
}
$requestedDir = '';
2015-07-02 00:08:46 +02:00
if (!empty($_GET['dir'])) {
2019-11-13 12:08:51 +01:00
$requestedDir = $_GET['dir'];
2015-07-02 00:08:46 +02:00
}
2019-11-13 12:08:51 +01:00
$photo_root = GALLERY_ROOT . 'photos/';
$thumbdir = rtrim('photos/' . $requestedDir, '/');
2016-06-24 17:07:13 +02:00
$current_dir = GALLERY_ROOT . $thumbdir;
2016-06-24 17:07:13 +02:00
guardAgainstDirectoryTraversal($current_dir);
2014-06-22 22:13:12 +02:00
//-----------------------
// READ FILES AND FOLDERS
//-----------------------
2019-11-13 12:08:51 +01:00
$files = array();
$dirs = array();
$img_captions = array();
2016-06-24 17:07:13 +02:00
if (is_dir($current_dir) && $handle = opendir($current_dir)) {
2019-11-13 12:08:51 +01:00
// 1. LOAD CAPTIONS
$caption_filename = "$current_dir/captions.txt";
if (is_readable($caption_filename)) {
$caption_handle = fopen($caption_filename, "rb");
while (!feof($caption_handle)) {
$caption_line = fgetss($caption_handle);
if (empty($caption_line)) {
continue;
}
list($img_file, $img_text) = explode('|', $caption_line);
$img_captions[$img_file] = trim($img_text);
}
fclose($caption_handle);
}
while (false !== ($file = readdir($handle))) {
if (in_array($file, $skip_objects)) {
continue;
}
// 2. LOAD FOLDERS
if (is_dir($current_dir . "/" . $file)) {
if ($file != "." && $file != "..") {
checkpermissions($current_dir . "/" . $file); // Check for correct file permission
// Set thumbnail to folder.jpg if found:
if (file_exists($current_dir . '/' . $file . '/folder.jpg')) {
$linkParams = http_build_query(
array('dir' => ltrim("$requestedDir/$file", '/')),
'',
'&amp;'
);
$linkUrl = "?$linkParams";
$imgParams = http_build_query(
array(
'filename' => "$current_dir/$file/folder.jpg",
'size' => $thumb_size,
),
'',
'&amp;'
);
$imgUrl = GALLERY_ROOT . "createthumb.php?$imgParams";
$dirs[] = array(
"name" => $file,
"date" => filemtime($current_dir . "/" . $file . "/folder.jpg"),
"html" => "<li><a href=\"{$linkUrl}\"><em>" . padstring($file, $label_max_length) . "</em><span></span><img src=\"{$imgUrl}\" alt=\"$label_loading\" /></a></li>",
);
} else {
// Set thumbnail to first image found (if any):
unset($firstimage);
$firstimage = getfirstImage("$current_dir/" . $file);
if ($firstimage != "") {
$linkParams = http_build_query(
array('dir' => ltrim("$requestedDir/$file", '/')),
'',
'&amp;'
);
$linkUrl = "?$linkParams";
$imgParams = http_build_query(
array(
'filename' => "$thumbdir/$file/$firstimage",
'size' => $thumb_size,
),
'',
'&amp;'
);
$imgUrl = GALLERY_ROOT . "createthumb.php?$imgParams";
$dirs[] = array(
"name" => $file,
"date" => filemtime($current_dir . "/" . $file),
"html" => "<li><a href=\"{$linkUrl}\"><em>" . padstring($file, $label_max_length) . "</em><span></span><img src=\"{$imgUrl}\" alt='$label_loading' /></a></li>",
);
} else {
// If no folder.jpg or image is found, then display default icon:
$linkParams = http_build_query(
array('dir' => ltrim("$requestedDir/$file", '/')),
'',
'&amp;'
);
$linkUrl = "?$linkParams";
$imgUrl = GALLERY_ROOT . 'images/folder_' . strtolower($folder_color) . '.png';
$dirs[] = array(
"name" => $file,
"date" => filemtime($current_dir . "/" . $file),
"html" => "<li><a href=\"{$linkUrl}\"><em>" . padstring($file, $label_max_length) . "</em><span></span><img src=\"{$imgUrl}\" width='$thumb_size' height='$thumb_size' alt='$label_loading' /></a></li>",
);
}
}
}
}
// 3. LOAD FILES
if ($file != "." && $file != ".." && $file != "folder.jpg") {
if ($display_filename) {
$filename_caption = "<em>" . padstring($file, $label_max_length) . "</em>";
} else {
$filename_caption = "";
}
// JPG, GIF and PNG
if (preg_match("/.jpg$|.gif$|.png$/i", $file)) {
//Read EXIF
if (!array_key_exists($file, $img_captions)) {
if ($display_exif == 1) {
$exifReaden = readEXIF($current_dir . "/" . $file);
//Add to the caption all the EXIF information
$img_captions[$file] = $file . $exifReaden;
} else {
//If no EXIF, just use the filename as caption
$img_captions[$file] = $file;
}
}
// Read the optionnal image title and caption in html file (image.jpg --> image.jpg.html)
// Format: title::caption
// Example: My cat::My cat like to <i>roll</i> on the floor.
// If file is not provided, image filename will be used instead.
checkpermissions($current_dir . "/" . $file);
if (is_file($current_dir . '/' . $file . '.html')) {
$img_captions[$file] = $file . '::' . htmlspecialchars(file_get_contents($current_dir . '/' . $file . '.html'), ENT_QUOTES);
}
$linkUrl = str_replace('%2F', '/', rawurlencode("$current_dir/$file"));
$imgParams = http_build_query(
array('filename' => "$thumbdir/$file", 'size' => $thumb_size),
'',
'&amp;');
$imgUrl = GALLERY_ROOT . "createthumb.php?$imgParams";
if ($lazyload) {
$imgopts = "class=\"b-lazy\" src=data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== data-src=\"$imgUrl\"";
} else {
$imgopts = "src=\"{$imgUrl}\"";
}
$files[] = array(
"name" => $file,
"date" => filemtime($current_dir . "/" . $file),
"size" => filesize($current_dir . "/" . $file),
"html" => "<li><a href=\"{$linkUrl}\" rel='lightbox[billeder]' title=\"" . htmlentities($img_captions[$file]) . "\"><img $imgopts alt='$label_loading' /></a>" . $filename_caption . "</li>");
}
// Other filetypes
$extension = "";
if (preg_match("/\.pdf$/i", $file)) {
$extension = "PDF";
}
// PDF
if (preg_match("/\.zip$/i", $file)) {
$extension = "ZIP";
}
// ZIP archive
if (preg_match("/\.rar$|\.r[0-9]{2,}/i", $file)) {
$extension = "RAR";
}
// RAR Archive
if (preg_match("/\.tar$/i", $file)) {
$extension = "TAR";
}
// TARball archive
if (preg_match("/\.gz$/i", $file)) {
$extension = "GZ";
}
// GZip archive
if (preg_match("/\.doc$|\.docx$/i", $file)) {
$extension = "DOCX";
}
// Word
if (preg_match("/\.ppt$|\.pptx$/i", $file)) {
$extension = "PPTX";
}
//Powerpoint
if (preg_match("/\.xls$|\.xlsx$/i", $file)) {
$extension = "XLXS";
}
// Excel
if (preg_match("/\.ogv$|\.mp4$|\.mpg$|\.mpeg$|\.mov$|\.avi$|\.wmv$|\.flv$|\.webm$/i", $file)) {
$extension = "VIDEO";
}
// video files
if (preg_match("/\.aiff$|\.aif$|\.wma$|\.aac$|\.flac$|\.mp3$|\.ogg$|\.m4a$/i", $file)) {
$extension = "AUDIO";
}
// audio files
if ($extension != "") {
$files[] = array(
"name" => $file,
"date" => filemtime($current_dir . "/" . $file),
"size" => filesize($current_dir . "/" . $file),
"html" => "<li><a href='$current_dir/$file' title='$file'><em-pdf>" . padstring($file, 20) . "</em-pdf><span></span><img src='" . GALLERY_ROOT . "images/filetype_" . $extension . ".png' width='$thumb_size' height='$thumb_size' alt='$file' /></a>$filename_caption</li>");
}
}
}
closedir($handle);
2014-11-25 13:58:04 +01:00
} else {
2019-11-13 12:08:51 +01:00
die("ERROR: Could not open " . htmlspecialchars(stripslashes($current_dir)) . " for reading!");
2014-11-25 13:58:04 +01:00
}
//-----------------------
// SORT FILES AND FOLDERS
//-----------------------
2014-11-25 13:58:04 +01:00
if (sizeof($dirs) > 0) {
2019-11-13 12:08:51 +01:00
foreach ($dirs as $key => $row) {
if ($row["name"] == "") {
unset($dirs[$key]); //Delete empty array entries
continue;
}
$name[$key] = strtolower($row['name']);
$date[$key] = strtolower($row['date']);
}
@array_multisort($$sorting_folders, $sortdir_folders, $name, $sortdir_folders, $dirs);
}
2014-11-25 13:58:04 +01:00
if (sizeof($files) > 0) {
2019-11-13 12:08:51 +01:00
foreach ($files as $key => $row) {
if ($row["name"] == "") {
unset($files[$key]); //Delete empty array entries
continue;
}
$name[$key] = strtolower($row['name']);
$date[$key] = strtolower($row['date']);
$size[$key] = strtolower($row['size']);
}
@array_multisort($$sorting_files, $sortdir_files, $name, SORT_ASC, $files);
}
//-----------------------
// OFFSET DETERMINATION
//-----------------------
2015-07-02 00:08:46 +02:00
if (!isset($_GET["page"])) {
2019-11-13 12:08:51 +01:00
$_GET["page"] = 1;
2015-07-02 00:08:46 +02:00
}
2014-11-25 13:58:04 +01:00
$offset_start = ($_GET["page"] * $thumbs_pr_page) - $thumbs_pr_page;
2019-11-13 12:08:51 +01:00
$offset_end = $offset_start + $thumbs_pr_page;
2015-07-02 00:08:46 +02:00
if ($offset_end > sizeof($dirs) + sizeof($files)) {
2019-11-13 12:08:51 +01:00
$offset_end = sizeof($dirs) + sizeof($files);
2015-07-02 00:08:46 +02:00
}
2014-11-25 13:58:04 +01:00
if ($_GET["page"] == "all" || $lazyload) {
2019-11-13 12:08:51 +01:00
$offset_start = 0;
$offset_end = sizeof($dirs) + sizeof($files);
2014-11-25 13:58:04 +01:00
}
//-----------------------
// PAGE NAVIGATION
//-----------------------
2014-11-25 13:58:04 +01:00
if (!$lazyload && sizeof($dirs) + sizeof($files) > $thumbs_pr_page) {
2019-11-13 12:08:51 +01:00
$page_navigation .= "$label_page ";
for ($i = 1; $i <= ceil((sizeof($files) + sizeof($dirs)) / $thumbs_pr_page); $i++) {
if ($_GET["page"] == $i) {
$page_navigation .= "$i";
} else {
$page_navigation .= "<a href='?dir=" . $requestedDir . "&amp;page=" . ($i) . "'>" . $i . "</a>";
}
if ($i != ceil((sizeof($files) + sizeof($dirs)) / $thumbs_pr_page)) {
$page_navigation .= " | ";
}
}
//Insert link to view all images
if ($_GET["page"] == "all") {
$page_navigation .= " | $label_all";
} else {
$page_navigation .= " | <a href='?dir=" . $requestedDir . "&amp;page=all'>$label_all</a>";
}
2015-07-02 00:08:46 +02:00
}
//-----------------------
// BREADCRUMB NAVIGATION
//-----------------------
2015-06-30 00:20:05 +02:00
if ($requestedDir != "" && $requestedDir != "photos") {
2019-11-13 12:08:51 +01:00
$breadcrumb_navigation = "<div class=\"NavWrapper\">";
$breadcrumb_navigation .= "<a href='?dir='>" . $label_home . "</a> $breadcrumb_separator ";
$navitems = explode("/", htmlspecialchars($_REQUEST['dir']));
for ($i = 0; $i < sizeof($navitems); $i++) {
if ($i == sizeof($navitems) - 1) {
$breadcrumb_navigation .= $navitems[$i];
} else {
$breadcrumb_navigation .= "<a href='?dir=";
for ($x = 0; $x <= $i; $x++) {
$breadcrumb_navigation .= $navitems[$x];
if ($x < $i) {
$breadcrumb_navigation .= "/";
}
}
$breadcrumb_navigation .= "'>" . $navitems[$i] . "</a> $breadcrumb_separator ";
}
}
$breadcrumb_navigation .= "</div>";
2014-11-25 13:58:04 +01:00
}
//Include hidden links for all images BEFORE current page so lightbox is able to browse images on different pages
2014-11-25 13:58:04 +01:00
for ($y = 0; $y < $offset_start - sizeof($dirs); $y++) {
2019-11-13 12:08:51 +01:00
$breadcrumb_navigation .= "<a href='" . $current_dir . "/" . $files[$y]["name"] . "' class='hidden' title='" . $img_captions[$files[$y]["name"]] . "'></a>";
}
//-----------------------
// DISPLAY FOLDERS
//-----------------------
if (count($dirs) + count($files) == 0) {
2019-11-13 12:08:51 +01:00
$thumbnails .= "<div class=\"Empty\">$label_noimages</div> <div class=\"EmptyAdvice\">$label_noimages_advice</div>"; //Display 'no images' text
if ($current_dir == "photos") {
$messages =
"It looks like you have just installed MiniGal Nano.
2015-06-29 23:48:14 +02:00
Please run the <a href='system_check.php'>system check tool</a>. <br>
And why not have a look to config.php and customize some values ?";
2019-11-13 12:08:51 +01:00
}
}
$offset_current = $offset_start;
2014-11-25 13:58:04 +01:00
for ($x = $offset_start; $x < sizeof($dirs) && $x < $offset_end; $x++) {
2019-11-13 12:08:51 +01:00
$offset_current++;
$thumbnails .= $dirs[$x]["html"];
}
//-----------------------
// DISPLAY FILES
//-----------------------
2014-11-25 13:58:04 +01:00
for ($i = $offset_start - sizeof($dirs); $i < $offset_end && $offset_current < $offset_end; $i++) {
2019-11-13 12:08:51 +01:00
if ($i >= 0) {
$offset_current++;
$thumbnails .= $files[$i]["html"];
}
}
//Include hidden links for all images AFTER current page so lightbox is able to browse images on different pages
2015-07-02 00:08:46 +02:00
if ($i < 0) {
2019-11-13 12:08:51 +01:00
$i = 1;
2015-07-02 00:08:46 +02:00
}
2014-11-25 13:58:04 +01:00
for ($y = $i; $y < sizeof($files); $y++) {
2019-11-13 12:08:51 +01:00
$page_navigation .= "<a href='" . $current_dir . "/" . $files[$y]["name"] . "' class='hidden' title='" . $img_captions[$files[$y]["name"]] . "'></a>";
}
//-----------------------
// OUTPUT MESSAGES
//-----------------------
if ($messages != "") {
2019-11-13 12:08:51 +01:00
$messages = $messages . "<div><a id=\"closeMessage\" class=\"closeMessage\" href=\"#\"><img src=\"images/close.png\" /></a><div>";
}
// Read folder comment.
2016-06-24 17:07:13 +02:00
$comment_filepath = $current_dir . $file . "/comment.html";
2014-11-25 13:58:04 +01:00
if (file_exists($comment_filepath)) {
2019-11-13 12:08:51 +01:00
$fd = fopen($comment_filepath, "r");
$comment = "<div class=\"Comment\">" . fread($fd, filesize($comment_filepath)) . "</div>";
fclose($fd);
}
2014-11-25 13:58:04 +01:00
//PROCESS TEMPLATE FILE
2015-07-02 00:08:46 +02:00
if (GALLERY_ROOT != "") {
2019-11-13 12:08:51 +01:00
$templatefile = GALLERY_ROOT . "templates/integrate.html";
2015-07-02 00:08:46 +02:00
} else {
2019-11-13 12:08:51 +01:00
$templatefile = "templates/" . $templatefile . ".html";
2015-07-02 00:08:46 +02:00
}
2014-11-25 13:58:04 +01:00
if (!$fd = fopen($templatefile, "r")) {
2019-11-13 12:08:51 +01:00
echo "Template " . htmlspecialchars(stripslashes($templatefile)) . " not found!";
exit();
2014-11-25 13:58:04 +01:00
} else {
2019-11-13 12:08:51 +01:00
$template = fread($fd, filesize($templatefile));
fclose($fd);
$template = stripslashes($template);
$template = preg_replace("/<% title %>/", $title, $template);
$template = preg_replace("/<% messages %>/", $messages, $template);
$template = preg_replace("/<% author %>/", $author, $template);
$template = preg_replace("/<% gallery_root %>/", GALLERY_ROOT, $template);
$template = preg_replace("/<% images %>/", "$images", $template);
$template = preg_replace("/<% thumbnails %>/", "$thumbnails", $template);
$template = preg_replace("/<% breadcrumb_navigation %>/", "$breadcrumb_navigation", $template);
$template = preg_replace("/<% page_navigation %>/", "$page_navigation", $template);
$template = preg_replace("/<% folder_comment %>/", "$comment", $template);
$template = preg_replace("/<% bgcolor %>/", "$backgroundcolor", $template);
$template = preg_replace("/<% gallery_width %>/", "$gallery_width", $template);
$template = preg_replace("/<% version %>/", "$version", $template);
echo "$template";
2014-11-25 13:58:04 +01:00
}