PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/index.php

https://github.com/fbjerggaard/Minigal-Nano
PHP | 409 lines | 290 code | 32 blank | 87 comment | 108 complexity | 9062b8c349d6273efeee7906cfb69c2e MD5 | raw file
  1. <?php
  2. /*
  3. MINIGAL NANO
  4. - A PHP/HTML/CSS based image gallery script
  5. This script and included files are subject to licensing from Creative Commons (http://creativecommons.org/licenses/by-sa/2.5/)
  6. You may use, edit and redistribute this script, as long as you pay tribute to the original author by NOT removing the linkback to www.minigal.dk ("Powered by MiniGal Nano x.x.x")
  7. MiniGal Nano is created by Thomas Rybak
  8. Copyright 2010 by Thomas Rybak
  9. Support: www.minigal.dk
  10. Community: www.minigal.dk/forum
  11. Please enjoy this free script!
  12. */
  13. // Do not edit below this section unless you know what you are doing!
  14. //-----------------------
  15. // Debug stuff
  16. //-----------------------
  17. error_reporting(E_ERROR);
  18. // error_reporting(E_ALL);
  19. // error_reporting(0);
  20. /*
  21. $mtime = microtime();
  22. $mtime = explode(" ",$mtime);
  23. $mtime = $mtime[1] + $mtime[0];
  24. $starttime = $mtime;
  25. */
  26. $version = "0.3.5";
  27. ini_set("memory_limit","256M");
  28. require("config_default.php");
  29. include("config.php");
  30. //-----------------------
  31. // DEFINE VARIABLES
  32. //-----------------------
  33. $page_navigation = "";
  34. $breadcrumb_navigation = "";
  35. $thumbnails = "";
  36. $new = "";
  37. $images = "";
  38. $exif_data = "";
  39. $messages = "";
  40. //-----------------------
  41. // PHP ENVIRONMENT CHECK
  42. //-----------------------
  43. if (!function_exists('exif_read_data') && $display_exif == 1) {
  44. $display_exif = 0;
  45. $messages = "Error: PHP EXIF is not available. Set &#36;display_exif = 0; in config.php to remove this message";
  46. }
  47. //-----------------------
  48. // FUNCTIONS
  49. //-----------------------
  50. function is_directory($filepath) {
  51. // $filepath must be the entire system path to the file
  52. if (!@opendir($filepath)) return FALSE;
  53. else {
  54. return TRUE;
  55. closedir($filepath);
  56. }
  57. }
  58. function padstring($name, $length) {
  59. global $label_max_length;
  60. if (!isset($length)) $length = $label_max_length;
  61. if (strlen($name) > $length) {
  62. return substr($name,0,$length) . "...";
  63. } else return $name;
  64. }
  65. function getfirstImage($dirname) {
  66. $imageName = false;
  67. $ext = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "GIF", "JPEG");
  68. if($handle = opendir($dirname))
  69. {
  70. while(false !== ($file = readdir($handle)))
  71. {
  72. $lastdot = strrpos($file, '.');
  73. $extension = substr($file, $lastdot + 1);
  74. if ($file[0] != '.' && in_array($extension, $ext)) break;
  75. }
  76. $imageName = $file;
  77. closedir($handle);
  78. }
  79. return($imageName);
  80. }
  81. function readEXIF($file) {
  82. $exif_data = "";
  83. $exif_idf0 = exif_read_data ($file,'IFD0' ,0 );
  84. $emodel = $exif_idf0['Model'];
  85. $efocal = $exif_idf0['FocalLength'];
  86. list($x,$y) = split('/', $efocal);
  87. $efocal = round($x/$y,0);
  88. $exif_exif = exif_read_data ($file,'EXIF' ,0 );
  89. $eexposuretime = $exif_exif['ExposureTime'];
  90. $efnumber = $exif_exif['FNumber'];
  91. list($x,$y) = split('/', $efnumber);
  92. $efnumber = round($x/$y,0);
  93. $eiso = $exif_exif['ISOSpeedRatings'];
  94. $exif_date = exif_read_data ($file,'IFD0' ,0 );
  95. $edate = $exif_date['DateTime'];
  96. if (strlen($emodel) > 0 OR strlen($efocal) > 0 OR strlen($eexposuretime) > 0 OR strlen($efnumber) > 0 OR strlen($eiso) > 0) $exif_data .= "::";
  97. if (strlen($emodel) > 0) $exif_data .= "$emodel";
  98. if ($efocal > 0) $exif_data .= " | $efocal" . "mm";
  99. if (strlen($eexposuretime) > 0) $exif_data .= " | $eexposuretime" . "s";
  100. if ($efnumber > 0) $exif_data .= " | f$efnumber";
  101. if (strlen($eiso) > 0) $exif_data .= " | ISO $eiso";
  102. return($exif_data);
  103. }
  104. function checkpermissions($file) {
  105. global $messages;
  106. if (substr(decoct(fileperms($file)), -1, strlen(fileperms($file))) < 4 OR substr(decoct(fileperms($file)), -3,1) < 4) $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>";
  107. }
  108. //-----------------------
  109. // CHECK FOR NEW VERSION
  110. //-----------------------
  111. if (ini_get('allow_url_fopen') == "1") {
  112. $file = @fopen ("http://www.minigal.dk/minigalnano_version.php", "r");
  113. $server_version = fgets ($file, 1024);
  114. if (strlen($server_version) == 5 ) { //If string retrieved is exactly 5 chars then continue
  115. if (version_compare($server_version, $version, '>')) $messages = "MiniGal Nano $server_version is available! <a href='http://www.minigal.dk/minigal-nano.html' target='_blank'>Get it now</a>";
  116. }
  117. fclose($file);
  118. }
  119. if (!defined("GALLERY_ROOT")) define("GALLERY_ROOT", "");
  120. $thumbdir = rtrim('photos' . "/" .$_REQUEST["dir"],"/");
  121. $thumbdir = str_replace("/..", "", $thumbdir); // Prevent looking at any up-level folders
  122. $currentdir = GALLERY_ROOT . $thumbdir;
  123. //-----------------------
  124. // READ FILES AND FOLDERS
  125. //-----------------------
  126. $files = array();
  127. $dirs = array();
  128. if ($handle = opendir($currentdir))
  129. {
  130. while (false !== ($file = readdir($handle)))
  131. {
  132. // 1. LOAD FOLDERS
  133. if (is_directory($currentdir . "/" . $file))
  134. {
  135. if ($file != "." && $file != ".." )
  136. {
  137. checkpermissions($currentdir . "/" . $file); // Check for correct file permission
  138. // Set thumbnail to folder.jpg if found:
  139. if (file_exists("$currentdir/" . $file . "/folder.jpg"))
  140. {
  141. $dirs[] = array(
  142. "name" => $file,
  143. "date" => filemtime($currentdir . "/" . $file . "/folder.jpg"),
  144. "html" => "<li><a href='?dir=" .ltrim($_GET['dir'] . "/" . $file, "/") . "'><em>" . padstring($file, $label_max_length) . "</em><span></span><img src='" . GALLERY_ROOT . "createthumb.php?filename=$currentdir/" . $file . "/folder.jpg&amp;size=$thumb_size' alt='$label_loading' /></a></li>");
  145. } else
  146. {
  147. // Set thumbnail to first image found (if any):
  148. unset ($firstimage);
  149. $firstimage = getfirstImage("$currentdir/" . $file);
  150. if ($firstimage != "") {
  151. $dirs[] = array(
  152. "name" => $file,
  153. "date" => filemtime($currentdir . "/" . $file),
  154. "html" => "<li><a href='?dir=" . ltrim($_GET['dir'] . "/" . $file, "/") . "'><em>" . padstring($file, $label_max_length) . "</em><span></span><img src='" . GALLERY_ROOT . "createthumb.php?filename=$thumbdir/" . $file . "/" . $firstimage . "&amp;size=$thumb_size' alt='$label_loading' /></a></li>");
  155. } else {
  156. // If no folder.jpg or image is found, then display default icon:
  157. $dirs[] = array(
  158. "name" => $file,
  159. "date" => filemtime($currentdir . "/" . $file),
  160. "html" => "<li><a href='?dir=" . ltrim($_GET['dir'] . "/" . $file, "/") . "'><em>" . padstring($file) . "</em><span></span><img src='" . GALLERY_ROOT . "images/folder_" . strtolower($folder_color) . ".png' width='$thumb_size' height='$thumb_size' alt='$label_loading' /></a></li>");
  161. }
  162. }
  163. }
  164. }
  165. // 2. LOAD CAPTIONS
  166. if (file_exists($currentdir ."/captions.txt"))
  167. {
  168. $file_handle = fopen($currentdir ."/captions.txt", "rb");
  169. while (!feof($file_handle) )
  170. {
  171. $line_of_text = fgets($file_handle);
  172. $parts = explode('/n', $line_of_text);
  173. foreach($parts as $img_capts)
  174. {
  175. list($img_filename, $img_caption) = explode('|', $img_capts);
  176. $img_captions[$img_filename] = $img_caption;
  177. }
  178. }
  179. fclose($file_handle);
  180. }
  181. // 3. LOAD FILES
  182. if ($file != "." && $file != ".." && $file != "folder.jpg")
  183. {
  184. // JPG, GIF and PNG
  185. if (preg_match("/.jpg$|.gif$|.png$/i", $file))
  186. {
  187. //Read EXIF
  188. if ($display_exif == 1) $img_captions[$file] .= readEXIF($currentdir . "/" . $file);
  189. checkpermissions($currentdir . "/" . $file);
  190. $files[] = array (
  191. "name" => $file,
  192. "date" => filemtime($currentdir . "/" . $file),
  193. "size" => filesize($currentdir . "/" . $file),
  194. "html" => "<li><a href='" . $currentdir . "/" . $file . "' rel='lightbox[billeder]' title='$img_captions[$file]'><span></span><img src='" . GALLERY_ROOT . "createthumb.php?filename=" . $thumbdir . "/" . $file . "&amp;size=$thumb_size' alt='$label_loading' /></a></li>");
  195. }
  196. // Other filetypes
  197. $extension = "";
  198. if (preg_match("/.pdf$/i", $file)) $extension = "PDF"; // PDF
  199. if (preg_match("/.zip$/i", $file)) $extension = "ZIP"; // ZIP archive
  200. if (preg_match("/.rar$|.r[0-9]{2,}/i", $file)) $extension = "RAR"; // RAR Archive
  201. if (preg_match("/.tar$/i", $file)) $extension = "TAR"; // TARball archive
  202. if (preg_match("/.gz$/i", $file)) $extension = "GZ"; // GZip archive
  203. if (preg_match("/.doc$|.docx$/i", $file)) $extension = "DOCX"; // Word
  204. if (preg_match("/.ppt$|.pptx$/i", $file)) $extension = "PPTX"; //Powerpoint
  205. if (preg_match("/.xls$|.xlsx$/i", $file)) $extension = "XLXS"; // Excel
  206. if ($extension != "")
  207. {
  208. $files[] = array (
  209. "name" => $file,
  210. "date" => filemtime($currentdir . "/" . $file),
  211. "size" => filesize($currentdir . "/" . $file),
  212. "html" => "<li><a href='" . $currentdir . "/" . $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></li>");
  213. }
  214. }
  215. }
  216. closedir($handle);
  217. } else die("ERROR: Could not open $currentdir for reading!");
  218. //-----------------------
  219. // SORT FILES AND FOLDERS
  220. //-----------------------
  221. if (sizeof($dirs) > 0)
  222. {
  223. foreach ($dirs as $key => $row)
  224. {
  225. if($row["name"] == "") unset($dirs[$key]); //Delete empty array entries
  226. $name[$key] = strtolower($row['name']);
  227. $date[$key] = strtolower($row['date']);
  228. }
  229. if (strtoupper($sortdir_folders) == "DESC") array_multisort($$sorting_folders, SORT_DESC, $name, SORT_DESC, $dirs);
  230. else array_multisort($$sorting_folders, SORT_ASC, $name, SORT_ASC, $dirs);
  231. }
  232. if (sizeof($files) > 0)
  233. {
  234. foreach ($files as $key => $row)
  235. {
  236. if($row["name"] == "") unset($files[$key]); //Delete empty array entries
  237. $name[$key] = strtolower($row['name']);
  238. $date[$key] = strtolower($row['date']);
  239. $size[$key] = strtolower($row['size']);
  240. }
  241. if (strtoupper($sortdir_files) == "DESC") array_multisort($$sorting_files, SORT_DESC, $name, SORT_ASC, $files);
  242. else array_multisort($$sorting_files, SORT_ASC, $name, SORT_ASC, $files);
  243. }
  244. //-----------------------
  245. // OFFSET DETERMINATION
  246. //-----------------------
  247. $offset_start = ($_GET["page"] * $thumbs_pr_page) - $thumbs_pr_page;
  248. if (!isset($_GET["page"])) $offset_start = 0;
  249. $offset_end = $offset_start + $thumbs_pr_page;
  250. if ($offset_end > sizeof($dirs) + sizeof($files)) $offset_end = sizeof($dirs) + sizeof($files);
  251. if ($_GET["page"] == "all")
  252. {
  253. $offset_start = 0;
  254. $offset_end = sizeof($dirs) + sizeof($files);
  255. }
  256. //-----------------------
  257. // PAGE NAVIGATION
  258. //-----------------------
  259. if (!isset($_GET["page"])) $_GET["page"] = 1;
  260. if (sizeof($dirs) + sizeof($files) > $thumbs_pr_page)
  261. {
  262. $page_navigation .= "$label_page ";
  263. for ($i=1; $i <= ceil((sizeof($files) + sizeof($dirs)) / $thumbs_pr_page); $i++)
  264. {
  265. if ($_GET["page"] == $i)
  266. $page_navigation .= "$i";
  267. else
  268. $page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&amp;page=" . ($i) . "'>" . $i . "</a>";
  269. if ($i != ceil((sizeof($files) + sizeof($dirs)) / $thumbs_pr_page)) $page_navigation .= " | ";
  270. }
  271. //Insert link to view all images
  272. if ($_GET["page"] == "all") $page_navigation .= " | $label_all";
  273. else $page_navigation .= " | <a href='?dir=" . $_GET["dir"] . "&amp;page=all'>$label_all</a>";
  274. }
  275. //-----------------------
  276. // BREADCRUMB NAVIGATION
  277. //-----------------------
  278. if ($_GET['dir'] != "")
  279. {
  280. $breadcrumb_navigation .= "<a href='?dir='>" . $label_home . "</a> > ";
  281. $navitems = explode("/", $_REQUEST['dir']);
  282. for($i = 0; $i < sizeof($navitems); $i++)
  283. {
  284. if ($i == sizeof($navitems)-1) $breadcrumb_navigation .= $navitems[$i];
  285. else
  286. {
  287. $breadcrumb_navigation .= "<a href='?dir=";
  288. for ($x = 0; $x <= $i; $x++)
  289. {
  290. $breadcrumb_navigation .= $navitems[$x];
  291. if ($x < $i) $breadcrumb_navigation .= "/";
  292. }
  293. $breadcrumb_navigation .= "'>" . $navitems[$i] . "</a> > ";
  294. }
  295. }
  296. } else $breadcrumb_navigation .= $label_home;
  297. //Include hidden links for all images BEFORE current page so lightbox is able to browse images on different pages
  298. for ($y = 0; $y < $offset_start - sizeof($dirs); $y++)
  299. {
  300. $breadcrumb_navigation .= "<a href='" . $currentdir . "/" . $files[$y]["name"] . "' rel='lightbox[billeder]' class='hidden' title='" . $img_captions[$files[$y]["name"]] . "'></a>";
  301. }
  302. //-----------------------
  303. // DISPLAY FOLDERS
  304. //-----------------------
  305. if (count($dirs) + count($files) == 0) {
  306. $thumbnails .= "<li>$label_noimages</li>"; //Display 'no images' text
  307. if($currentdir == "photos") $messages = "It looks like you have just installed MiniGal Nano. Please run the <a href='system_check.php'>system check tool</a>";
  308. }
  309. $offset_current = $offset_start;
  310. for ($x = $offset_start; $x < sizeof($dirs) && $x < $offset_end; $x++)
  311. {
  312. $offset_current++;
  313. $thumbnails .= $dirs[$x]["html"];
  314. }
  315. //-----------------------
  316. // DISPLAY FILES
  317. //-----------------------
  318. for ($i = $offset_start - sizeof($dirs); $i < $offset_end && $offset_current < $offset_end; $i++)
  319. {
  320. if ($i >= 0)
  321. {
  322. $offset_current++;
  323. $thumbnails .= $files[$i]["html"];
  324. }
  325. }
  326. //Include hidden links for all images AFTER current page so lightbox is able to browse images on different pages
  327. for ($y = $i; $y < sizeof($files); $y++)
  328. {
  329. $page_navigation .= "<a href='" . $currentdir . "/" . $files[$y]["name"] . "' rel='lightbox[billeder]' class='hidden' title='" . $img_captions[$files[$y]["name"]] . "'></a>";
  330. }
  331. //-----------------------
  332. // OUTPUT MESSAGES
  333. //-----------------------
  334. if ($messages != "") {
  335. $messages = "<div id=\"topbar\">" . $messages . " <a href=\"#\" onclick=\"document.getElementById('topbar').style.display = 'none';\";><img src=\"images/close.png\" /></a></div>";
  336. }
  337. //PROCESS TEMPLATE FILE
  338. if(GALLERY_ROOT != "") $templatefile = GALLERY_ROOT . "templates/integrate.html";
  339. else $templatefile = "templates/" . $templatefile . ".html";
  340. if(!$fd = fopen($templatefile, "r"))
  341. {
  342. echo "Template $templatefile not found!";
  343. exit();
  344. }
  345. else
  346. {
  347. $template = fread ($fd, filesize ($templatefile));
  348. fclose ($fd);
  349. $template = stripslashes($template);
  350. $template = preg_replace("/<% title %>/", $title, $template);
  351. $template = preg_replace("/<% messages %>/", $messages, $template);
  352. $template = preg_replace("/<% author %>/", $author, $template);
  353. $template = preg_replace("/<% gallery_root %>/", GALLERY_ROOT, $template);
  354. $template = preg_replace("/<% images %>/", "$images", $template);
  355. $template = preg_replace("/<% thumbnails %>/", "$thumbnails", $template);
  356. $template = preg_replace("/<% breadcrumb_navigation %>/", "$breadcrumb_navigation", $template);
  357. $template = preg_replace("/<% page_navigation %>/", "$page_navigation", $template);
  358. $template = preg_replace("/<% bgcolor %>/", "$backgroundcolor", $template);
  359. $template = preg_replace("/<% gallery_width %>/", "$gallery_width", $template);
  360. $template = preg_replace("/<% version %>/", "$version", $template);
  361. echo "$template";
  362. }
  363. //-----------------------
  364. //Debug stuff
  365. //-----------------------
  366. /* $mtime = microtime();
  367. $mtime = explode(" ",$mtime);
  368. $mtime = $mtime[1] + $mtime[0];
  369. $endtime = $mtime;
  370. $totaltime = ($endtime - $starttime);
  371. echo "This page was created in ".$totaltime." seconds";
  372. */
  373. ?>