diff mgallery.php @ 0:ac688606ec4b

Initial import of code.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 13 May 2015 07:27:40 +0300
parents
children c85f630a4198
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mgallery.php	Wed May 13 07:27:40 2015 +0300
@@ -0,0 +1,408 @@
+<?php
+//
+// Yet Another Image Gallery
+// (C) Copyright 2015 Tecnic Software productions (TNSP)
+//
+require "msitegen.inc.php";
+require "mgallery.inc.php";
+
+
+//
+// Various utility functions
+//
+function mgGetImageURL()
+{
+  global $galImageURL, $galPath;
+  return $galImageURL.$galPath."/".implode("", func_get_args());
+}
+
+
+function mgGetURL($path, $image, $entities = TRUE)
+{
+  global $galBaseURL, $galCleanURLS;
+  $amp = $entities ? "&amp;" : "&";
+
+  if ($galCleanURLS)
+  {
+    return $galBaseURL.$path."/".($image !== FALSE ? $image : "");
+  }
+  else
+  {
+    return
+      $galBaseURL.mgGetSetting("mgallery_php")."?path=".
+      $path.($image !== FALSE ? $amp."image=".$image : "");
+  }
+}
+
+
+function mgGetNaviActive(&$galIndex, $index, $delta, &$res, &$url, $entities)
+{
+  global $galPath;
+  $res = $index + $delta;
+  if ($res >= 0 && $res <= sizeof($galIndex) - 1)
+  {
+    $url = mgGetURL($galPath, $galIndex[$res], $entities);
+    return TRUE;
+  }
+  else
+    return FALSE;
+}
+
+
+function mgGetNaviControlImage(&$galIndex, $index, $class, $url)
+{
+  global $galTNPath;
+
+  $img = "<div class=\"imageCtrl ".$class."\">";
+
+  if ($url !== FALSE)
+  {
+    $img .=
+    "<a href=\"".$url."\"><img src=\"".
+    mgGetImageURL($galTNPath, $galIndex[$index]).
+    "\" alt=\"".$galIndex[$index]."\" /></a>";
+  }
+
+  return $img."</div>\n";
+}
+
+
+function mgGetNaviControlImageBox(&$galIndex, $index, $class, $delta)
+{
+  if (!mgGetNaviActive($galIndex, $index, $delta, $res, $url, TRUE))
+    $url = FALSE;
+
+  return mgGetNaviControlImage($galIndex, $res, $class, $url);
+}
+
+
+function mgGetControl($str, $class, &$galIndex, $index, $delta, $naviFlags)
+{
+  $active = mgGetNaviActive($galIndex, $index, $delta, $res, $url, TRUE);
+  if ($active && ($naviFlags & GNAV_IMG))
+    $img = mgGetNaviControlImage($galIndex, $res, $class, $url);
+  else
+    $img = "";
+
+  if ($naviFlags & GNAV_TEXT)
+    $str = "<span class=\"naviControl ".$class."\">[".($active ? "<a href=\"".$url."\">".$str."</a>" : $str)."]</span>";
+  else
+    $str = "";
+
+  if ($delta < 0)
+    return $img.$str;
+  else
+    return $str.$img;
+}
+
+
+function mgGetNaviControls(&$galIndex, $index, $naviFlags)
+{
+  global $galPath;
+
+  return
+    "<div class=\"naviControls\">".
+    mgGetControl("&lt;&lt;", "prev", $galIndex, $index, -1, $naviFlags).
+    "[<a href=\"".mgGetURL($galPath, FALSE)."\">^^</a>]".
+    mgGetControl("&gt;&gt;", "next", $galIndex, $index,  1, $naviFlags).
+    "</div>\n";
+}
+
+
+function mgPrintTable($class, &$galEntries, &$galIndex, $start, $limit)
+{
+  global $galAlbumIcon, $galPath, $galTNPath;
+
+  $galCount = count($galIndex);
+  if ($start >= $galCount)
+    return $start;
+
+  $end = ($limit === FALSE) ? $galCount : $start + $limit;
+  if ($end > $galCount) $end = $galCount;
+
+  $rowLimit = mgGetSetting("album_row_limit");
+  $n = 0;
+
+  echo "<table class=\"".$class."\">\n";
+  for ($index = $start; $index < $end; $index++)
+  {
+    $filename = &$galIndex[$index];
+    $data = &$galEntries[$filename];
+
+    if ($n == 0) echo " <tr>\n";
+
+    echo
+      "  <td id=\"cd".$data["base"]."\">\n";
+
+    if ($data["type"] == 0)
+    {
+      echo
+      "<div class=\"imageBox\"><a href=\"".mgGetURL($galPath, $filename)."\">".
+      "<img src=\"".mgGetImageURL($galTNPath, $filename)."\" alt=\"".
+      chentities($filename)."\"></a>".
+      "</div>".
+      mgGetArr($data, "caption", "<div class=\"imageCaption\">%1</div>", "", "chentities");
+/*
+      if ($mode == "")
+      {
+      echo
+        "  <select class=\"dropdown\" id=\"dd".$data["base"]."\" name=\"dd".$data["base"].
+        "\" onchange=\"galPhotoDataChanged('".$data["base"]."');\">\n";
+
+      foreach ($picChoices as $name => $value)
+      {
+        echo "   <option value=\"$value\"".($value == $data["id"] ? " selected=\"selected\"" : "").">".chentities($name)."</option>\n";
+      }
+      echo
+        "  </select>\n";
+      }
+*/
+    }
+    else
+    {
+      echo
+      " <a href=\"".mgGetURL(mgCleanPath(TRUE, $galPath, $data["base"]), FALSE)."\">".
+      "<img class=\"albumIcon\" src=\"".$galAlbumIcon."\" alt=\"".chentities($data["caption"])."\" />\n".
+      "<div class=\"albumTitle\">".chentities($data["caption"])."</div></a>\n";
+    }
+
+    echo
+      "  </td>\n";
+
+    if (++$n >= $rowLimit)
+    {
+      echo " </tr>\n";
+      $n = 0;
+    }
+  }
+  if ($n > 0)
+  {
+    while ($n++ < $rowLimit)
+      echo "  <td></td>\n";
+    echo " </tr>\n";
+  }
+  echo "</table>\n";
+  return $index;
+}
+
+
+function mgTimeStr($str)
+{
+  $tmp = date_create_from_format("Y:m:d H:i:s", $str);
+  return date_format($tmp, "d M Y (H:i)");
+}
+
+
+function mgPrintPageInfoFooter()
+{
+  if (($str = mgGetSetting("page_info")) !== FALSE)
+    echo "<div class=\"pageInfoFooter\">".$str."</div>";
+}
+
+
+function mgPrintBreadCrumbs($galData)
+{
+  $res = array();
+  if ($galData["caption"])
+    $res[] = chentities($galData["caption"]);
+
+  $tmp = $galData;
+  while (isset($tmp["parent"]))
+  {
+    $pdata = $tmp["parent"];
+    $res[] = "<a href=\"".mgGetURL($pdata["path"], FALSE)."\">".chentities($pdata["caption"])."</a>";
+    $tmp = $tmp["parent"];
+  }
+
+  if (count($res) > 1)
+  {
+    $res = array_map(function ($a) { return "<span class=\"naviBreadCrumbItem\">".$a."</span>"; }, $res);
+    echo
+      "<div class=\"naviBreadCrumbs\">".
+      implode("<span class=\"naviBreadCrumbSep\"></span>", array_reverse($res)).
+      "</div>\n";
+  }
+}
+
+
+//
+// Get gallery settings
+//
+mgReadSettings();
+
+$pageCSS = mgGetSetting("css");
+$pageCSSSelect = mgGetSetting("css_select");
+$galBasePath = mgGetSetting("base_path");
+$galBaseURL = mgGetSetting("base_url");
+$galImageURL = mgGetSetting("image_url", mgGetSetting("base_url"));
+
+$galAlbumIcon = mgGetSetting("album_icon");
+$galCleanURLS = mgGetSetting("clean_urls");
+$galTNPath = mgGetSetting("tn_path");
+$galMedSuffix = mgGetSetting("med_suffix");
+$galTitlePrefix = mgGetSetting("title_prefix");
+$galTitleSep = mgGetSetting("title_sep");
+
+$galMode = stGetRequestItem("mode", "view", TRUE);
+$galPath = stGetRequestItem("path", ".", TRUE);
+$galPageIndex = intval(stGetRequestItem("index", 0, TRUE));
+$galImage = stGetRequestItem("image", FALSE, TRUE);
+
+if (is_string($galImage))
+  $galImage = basename($galImage);
+
+
+//
+// Attempt to read the data cache file
+//
+$filename = mgGetPath(mgCleanPath(TRUE, $galBasePath, $galPath), "cache_file");
+$filename2 = mgGetPath(mgCleanPath(FALSE, $galBasePath, $galPath), "cache_file");
+if ($filename == $filename2 && file_exists($filename) && ($fp = @fopen($filename, "rb")) !== FALSE)
+{
+  if (flock($fp, LOCK_SH))
+  {
+    require($filename);
+    flock($fp, LOCK_UN);
+  }
+  fclose($fp);
+}
+
+
+// If no data available, show an error page
+if (!isset($galData) || !isset($galEntries) ||
+    !isset($galAlbumsIndex) || !isset($galImagesIndex))
+{
+  cmPrintPageHeader(mgGetVal(array("title_prefix", "title_sep"), "%1%2")."ERROR!");
+
+  echo
+    "<h1>Gallery error</h1>\n".
+    "<p>Gallery path <b>".chentities($galPath)."</b> does not exist or is invalid.</p>\n";
+
+  //echo "<p>".$filename."</p><p>".$filename2."</p>";
+
+  mgPrintPageInfoFooter();
+  cmPrintPageFooter(TRUE);
+  exit;
+}
+
+
+//
+// Print page header, etc.
+//
+if (($index = array_search($galImage, $galImagesIndex)) !== FALSE)
+{
+  //
+  // Single image mode
+  //
+  $naviFlags = mgGetSetting("image_navigation");
+  $data = $galEntries[$galImage];
+
+  $pageTitle = $galTitlePrefix.$galTitleSep.$galData["caption"]." - ".$galImage;
+  cmPrintPageHeader($pageTitle);
+  echo "<h1>".chentities($pageTitle)."</h1>\n";
+
+  if ($naviFlags & GNAV_BREADCRUMBS)
+    mgPrintBreadCrumbs($galData);
+
+  if ($naviFlags & GNAV_TOP)
+    echo mgGetNaviControls($galImagesIndex, $index, $naviFlags);
+
+  echo
+  "<div class=\"imageCBox\">\n".
+  mgGetNaviControlImageBox($galImagesIndex, $index, "prev", -1).
+  "<div class=\"imageBox\">\n".
+  "<a target=\"_blank\" href=\"".$galImageURL.$galPath."/".$galImage."\">".
+  "<img src=\"".mgGetImageURL($galTNPath, $data["base"].$galMedSuffix.$data["ext"])."\" alt=\"".
+  chentities($data["base"].$galMedSuffix.$data["ext"])."\"></a>\n".
+  "</div>\n".
+  mgGetNaviControlImageBox($galImagesIndex, $index, "next", 1).
+  "</div>\n".
+  "<div class=\"imageCaption\">".mgGetArr($data, "caption", "%1", "")."</div>\n";
+
+  $list = array(
+    mgGetArr($data, array("width", "height"), "<span class=\"infoDimensions\"><b>%1</b> x <b>%2</b> px</span>", NULL),
+    mgGetArr($data, "model", "<span class=\"infoModel\"><b>%1</b></span>", NULL),
+    mgGetArr($data, "fnumber", "<span class=\"infoFNumber\"><b>f/%1</b></span>", NULL),
+    mgGetArr($data, "exposure", "<span class=\"infoExposure\"><b>%1</b> sec</span>", NULL, NULL),
+    mgGetArr($data, "iso", "<span class=\"infoISO\">ISO <b>%1</b></span>", NULL),
+  );
+
+  echo
+    "<div class=\"infoBox\">\n".
+    mgGetArr($data, "datetime", "<span class=\"infoDateTime\">%1</span>", "", "mgTimeStr").
+    implode(", ", array_filter($list, function($a) { return $a !== NULL; })).
+    "</div>\n";
+
+  if ($naviFlags & GNAV_BOTTOM)
+    echo mgGetNaviControls($galImagesIndex, $index, $naviFlags);
+
+  // Javascript navigation
+  if ($naviFlags & GNAV_JAVASCRIPT)
+  {
+    $prevActive = mgGetNaviActive($galImagesIndex, $index, -1, $res, $prevURL, FALSE);
+    $nextActive = mgGetNaviActive($galImagesIndex, $index, 1, $res, $nextURL, FALSE);
+    echo
+      "<script type=\"text/javascript\">\n".
+      "var mgalPrevURL = \"".($prevActive ? $prevURL : "")."\";\n".
+      "var mgalNextURL = \"".($nextActive ? $nextURL : "")."\";\n".
+      "\n";
+?>
+function mgalNavigateTo(url)
+{
+    if (url != "")
+        window.location = url;
+}
+
+
+function mgalProcessKeyPress(ev)
+{
+    ev = ev || window.event;
+    var key = ev.keyCode ? ev.keyCode : ev.which;
+    switch (key)
+    {
+        case 37:
+        case 65:
+            // left
+            mgalNavigateTo(mgalPrevURL);
+            break;
+
+        case 39:
+        case 68:
+            // right
+            mgalNavigateTo(mgalNextURL);
+            break;
+    }
+}
+
+document.onkeypress = mgalProcessKeyPress;
+<?
+    echo
+      "</script>\n";
+  }
+}
+else
+{
+  //
+  // Gallery mode
+  //
+  // - needs sub-modes / handling of order shit
+  // - Javascript stuff for picture data updates
+  //
+  $pageTitle = $galTitlePrefix.mgGetArr($galData, "caption", " - %1", "", "chentities");
+  cmPrintPageHeader($pageTitle);
+  echo "<h1>".$pageTitle."</h1>\n";
+
+  $naviFlags = mgGetSetting("album_navigation");
+  if ($naviFlags & GNAV_BREADCRUMBS)
+    mgPrintBreadCrumbs($galData);
+
+  if (isset($galData["header"]) && strlen($galData["header"]) > 0)
+    echo "<div class=\"albumHeaderText\">".$galData["header"]."</div>\n";
+
+  mgPrintTable("albumTable", $galEntries, $galAlbumsIndex, 0, FALSE);
+  mgPrintTable("imageTable", $galEntries, $galImagesIndex, 0, FALSE);
+}
+
+mgPrintPageInfoFooter();
+cmPrintPageFooter(TRUE);
+?>
\ No newline at end of file