From 631ec74b2bc7f5191b5a5a268b015e5847f55f51 Mon Sep 17 00:00:00 2001
From: Jan Dittberner <jan@dittberner.info>
Date: Sat, 11 Jul 2009 20:48:17 +0000
Subject: [PATCH] improve configuration (addresses #49)

---
 gallery.ini                   |  2 -
 gallery.ini.tmpl              |  6 +++
 includes/galleryfunctions.php | 76 +++++++++++++++++++++--------------
 index.php                     |  6 ++-
 4 files changed, 56 insertions(+), 34 deletions(-)
 delete mode 100644 gallery.ini
 create mode 100644 gallery.ini.tmpl

diff --git a/gallery.ini b/gallery.ini
deleted file mode 100644
index 9d8cff3..0000000
--- a/gallery.ini
+++ /dev/null
@@ -1,2 +0,0 @@
-logfile=gallery.log
-defaulttheme=default_horizontal
diff --git a/gallery.ini.tmpl b/gallery.ini.tmpl
new file mode 100644
index 0000000..71785c8
--- /dev/null
+++ b/gallery.ini.tmpl
@@ -0,0 +1,6 @@
+logfile=gallery.log
+defaulttheme=default_horizontal
+gallerydir=bilder
+gallerypath=/bilder
+themedir=themes
+themepath=/themes
diff --git a/includes/galleryfunctions.php b/includes/galleryfunctions.php
index 179af2d..36ca5e2 100644
--- a/includes/galleryfunctions.php
+++ b/includes/galleryfunctions.php
@@ -28,16 +28,17 @@
  * <http://www.gnu.org/licenses/>.
  */
 
-define(GALLERYPREFIX, "bilder");
-define(INFOFILE, "galleryinfo.ini");
 define(IMAGESEC, "images");
 define(GALLERYSEC, "gallery");
 define(GALLERY_RE, '/^[\w\d _-]+$/');
 
-if (!realpath('gallery.ini')) {
-  die('gallery.ini not found in ' . getcwd());
+$basedir = realpath(implode(DIRECTORY_SEPARATOR, array(dirname(__file__), '..')));
+$inifile = implode(DIRECTORY_SEPARATOR, array($basedir, 'gallery.ini'));
+
+if (!file_exists($inifile)) {
+  die("required $inifile not found.");
 }
-$configuration = parse_ini_file(realpath('gallery.ini'));
+$configuration = parse_ini_file($inifile);
 if (array_key_exists('logfile', $configuration)) {
   error_reporting(E_ALL);
   ini_set('display_errors', 0);
@@ -63,8 +64,10 @@ if (array_key_exists('theme', $_GET) &&
  * Verzeichnisnamen hat, sonst @c false
  */
 function galleryExists($galleryname) {
+  global $configuration;
+
   return preg_match(GALLERY_RE, $galleryname) &&
-    realpath(GALLERYPREFIX . DIRECTORY_SEPARATOR . $galleryname);
+    realpath($configuration['gallerydir'] . DIRECTORY_SEPARATOR . $galleryname);
 }
 
 /**
@@ -81,11 +84,15 @@ function galleryExists($galleryname) {
  * wenn keine Konfigurationsdatei vorhanden ist
  */
 function getGalleryConfig($galleryname = null) {
+  global $configuration;
+
   if ($galleryname) {
-    $filepath = realpath(GALLERYPREFIX . DIRECTORY_SEPARATOR .
-                         $galleryname . DIRECTORY_SEPARATOR . INFOFILE);
+    $filepath = realpath(implode(DIRECTORY_SEPARATOR,
+      array($configuration['gallerydir'], $galleryname,
+            'galleryinfo.ini')));
   } else {
-    $filepath = realpath(GALLERYPREFIX . DIRECTORY_SEPARATOR . INFOFILE);
+    $filepath = realpath(implode(DIRECTORY_SEPARATOR,
+      array($configuration['gallerydir'], 'galleryinfo.ini')));
   }
   if (is_file($filepath)) {
     return parse_ini_file($filepath, true);
@@ -107,17 +114,17 @@ function getGalleryConfig($galleryname = null) {
  * @li @a full relative URL des Vollbildes
  */
 function getImageInfo($galleryname, $imagename) {
-  global $theme;
+  global $theme, $configuration;
 
   $label = getImageLabel($galleryname, $imagename);
   $gallerylabel = getGalleryLabel($galleryname);
   return array("name" => $imagename,
                "label" => $label,
-               "preview" => GALLERYPREFIX . DIRECTORY_SEPARATOR .
+               "preview" => $configuration['gallerypath'] . '/' .
                getScaledImage($galleryname, $imagename,
                               $theme->previewsize, false),
-               "full" => GALLERYPREFIX . DIRECTORY_SEPARATOR . $galleryname .
-               DIRECTORY_SEPARATOR . $imagename,
+               "full" => implode('/', array($configuration['gallerypath'],
+                                            $galleryname, $imagename)),
                "title" => sprintf("%s :: %s", $gallerylabel, $label)
                );
 }
@@ -135,7 +142,9 @@ function getImageInfo($galleryname, $imagename) {
  * @return Label zu dem Bild
  */
 function getImageLabel($galleryname, $imagename) {
-  $gallerypath = realpath(GALLERYPREFIX . DIRECTORY_SEPARATOR . $galleryname);
+  global $configuration;
+
+  $gallerypath = realpath($configuration['gallerydir'] . DIRECTORY_SEPARATOR . $galleryname);
   if (empty($gallerypath) || !is_dir($gallerypath)) {
     return false;
   }
@@ -175,25 +184,26 @@ function getGalleryLabel($galleryname) {
  * Liefert die aktuelle Galerie. Die Galerie kann entweder im
  * GET-Parameter @c galleryname stehen, als Wert @a default in der
  * Sektion @a gallery der zentralen @c galleryinfo.ini angegeben
- * werden oder es wird das erste Unterverzeichnis von @c GALLERYPREFIX
+ * werden oder es wird das erste Unterverzeichnis von @c gallerydir
  * verwendet.
  *
  * @return Galeriename
  */
 function getCurrentGallery() {
+  global $configuration;
+
   if (array_key_exists('galleryname', $_GET) &&
       galleryExists($_GET["galleryname"])) {
     return $_GET["galleryname"];
   }
-  $filepath = realpath(GALLERYPREFIX . DIRECTORY_SEPARATOR . INFOFILE);
+  $filepath = realpath($configuration['gallerydir'] . DIRECTORY_SEPARATOR . 'galleryinfo.ini');
   if (!empty($filepath)) {
     $inidata = getGalleryConfig();
     if (galleryExists($inidata[GALLERYSEC]["default"])) {
       return $inidata[GALLERYSEC]["default"];
     }
   }
-  foreach (glob(realpath(GALLERYPREFIX) . DIRECTORY_SEPARATOR . '*',
-                GLOB_ONLYDIR) as $directory) {
+  foreach (glob(realpath($configuration['gallerydir']) . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR) as $directory) {
     $basename = basename($directory);
     if (galleryExists($basename)) {
       return $basename;
@@ -217,13 +227,15 @@ function getCurrentGallery() {
  * Maximalwert beachtet werden soll, wenn dieser Parameter @c true
  * ist, wird die Höhe auf @a $maxdim skaliert, ansonsten die Breite
  *
- * @return Pfad des skalierten Bildes relativ zu @a GALLERYPREFIX
+ * @return Pfad des skalierten Bildes relativ zu @a gallerydir
  */
 function getScaledImage($galleryname, $basename, $maxdim, $scaleheight=true) {
+  global $configuration;
+
   if ($maxdim == 0) {
     debug_print_backtrace();
   }
-  $gallerydir = realpath(GALLERYPREFIX . DIRECTORY_SEPARATOR . $galleryname);
+  $gallerydir = realpath($configuration['gallerydir'] . DIRECTORY_SEPARATOR . $galleryname);
   if ($scaleheight) {
     $scaleddir = sprintf("%s%sscaled_x%d", $galleryname,
                          DIRECTORY_SEPARATOR, $maxdim);
@@ -231,7 +243,7 @@ function getScaledImage($galleryname, $basename, $maxdim, $scaleheight=true) {
     $scaleddir = sprintf("%s%sscaled%dx_", $galleryname,
                          DIRECTORY_SEPARATOR, $maxdim);
   }
-  $scaleddirpath = GALLERYPREFIX . DIRECTORY_SEPARATOR . $scaleddir;
+  $scaleddirpath = $configuration['gallerydir'] . DIRECTORY_SEPARATOR . $scaleddir;
   if (!is_dir($scaleddirpath)) {
     // versuchen das Thumbnail-Verzeichnis anzulegen
     $mkdir = @mkdir($scaleddirpath, 0755);
@@ -278,11 +290,11 @@ function getScaledImage($galleryname, $basename, $maxdim, $scaleheight=true) {
  * und dem Ergebnis von getimagesize() als Werten ist.
  */
 function getThumbNailInfo($galleryname) {
-  global $theme;
+  global $theme, $configuration;
 
   $thumbsizes = array();
   $thumbdimsum = 2;
-  foreach (glob(realpath(GALLERYPREFIX . DIRECTORY_SEPARATOR .
+  foreach (glob(realpath($configuration['gallerydir'] . DIRECTORY_SEPARATOR .
                          $galleryname) . DIRECTORY_SEPARATOR .
                 '*.jp{e,}g', GLOB_BRACE) as $filename) {
     $basename = basename($filename);
@@ -293,7 +305,7 @@ function getThumbNailInfo($galleryname) {
       $thumbfile = getScaledImage($galleryname, $basename, $theme->thumbsize,
                                   false);
     }
-    if ($thumbsize = getimagesize(realpath(GALLERYPREFIX .
+    if ($thumbsize = getimagesize(realpath($configuration['gallerydir'] .
                                            DIRECTORY_SEPARATOR .
                                            $thumbfile))) {
       $thumbsizes[$basename] = array($thumbfile, $thumbsize);
@@ -320,8 +332,10 @@ function getThumbNailInfo($galleryname) {
  * @see getGalleryConfig()
  */
 function getGalleryLinks() {
+  global $configuration;
+
   $retval = array();
-  foreach (glob(realpath(GALLERYPREFIX) . DIRECTORY_SEPARATOR . '*',
+  foreach (glob(realpath($configuration['gallerydir']) . DIRECTORY_SEPARATOR . '*',
                 GLOB_ONLYDIR) as $directory) {
     $basename = basename($directory);
     if (galleryExists($basename)) {
@@ -359,9 +373,11 @@ function getGalleryLinks() {
  * @see getThumbNailInfo()
  */
 function getAllThumbnails(&$thumbinfo) {
+  global $configuration;
+
   $retval = array();
   foreach ($thumbinfo[2] as $basename => $data) {
-    $retval[] = array('src' => GALLERYPREFIX . DIRECTORY_SEPARATOR . $data[0],
+    $retval[] = array('src' => $configuration['gallerydir'] . DIRECTORY_SEPARATOR . $data[0],
                       'sizes' => $data[1][3],
                       'alt' => getImageLabel($thumbinfo[1], $basename));
   }
@@ -383,23 +399,23 @@ function getAllThumbnails(&$thumbinfo) {
  * @see getThumbNailInfo()
  */
 function getFirstPreview(&$thumbinfo) {
-  global $theme;
+  global $theme, $configuration;
 
   reset($thumbinfo[2]);
   $basename = key($thumbinfo[2]);
   $data = current($thumbinfo[2]);
   $galleryname = $thumbinfo[1];
-  $fullname = GALLERYPREFIX . DIRECTORY_SEPARATOR . $galleryname .
+  $fullname = $configuration['gallerypath'] . DIRECTORY_SEPARATOR . $galleryname .
     DIRECTORY_SEPARATOR . $basename;
   $scaledimage = getScaledImage($galleryname, $basename,
                                 $theme->previewsize, false);
-  $scaledimagesize = getimagesize(realpath(GALLERYPREFIX .
+  $scaledimagesize = getimagesize(realpath($configuration['gallerydir'] .
                                            DIRECTORY_SEPARATOR .
                                            $scaledimage));
   $label = getImageLabel($galleryname, $basename);
   return array('title' => $label,
                'full'  => $fullname,
-               'src'   => GALLERYPREFIX . DIRECTORY_SEPARATOR . $scaledimage,
+               'src'   => $configuration['gallerypath'] . DIRECTORY_SEPARATOR . $scaledimage,
                'alt'   => $label,
                'sizes' => $scaledimagesize[3]);
 }
diff --git a/index.php b/index.php
index 644a3a5..9633429 100644
--- a/index.php
+++ b/index.php
@@ -32,10 +32,12 @@
  * <http://www.gnu.org/licenses/>.
  */
 
+$basedir = dirname(__file__);
+
 /**
  * Inkludiert die Funktionsbibliothek.
  */
-require('includes/galleryfunctions.php');
+require($basedir . '/includes/galleryfunctions.php');
 
 /**
  * Name der aktuellen Galerie.
@@ -51,7 +53,7 @@ $scripts = array('js/jquery.js',
                  'js/jquery.colorBlend.js',
                  'js/jquery.lightbox.js',
                  'scripts/ourhandlers.js');
-$styles  = array('css/jquery.lightbox.css');
+$styles  = array();
 
 $template = $theme->getTemplate();