changeset 324:d598b2320878

Improvements and fixes to configuration handling.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 09 Apr 2020 13:09:01 +0300
parents 23625c53e62d
children 9a4297e59a40
files mgallery.inc.php
diffstat 1 files changed, 48 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/mgallery.inc.php	Thu Apr 09 12:23:41 2020 +0300
+++ b/mgallery.inc.php	Thu Apr 09 13:09:01 2020 +0300
@@ -67,7 +67,7 @@
   "base_path"        => [MG_STR, mgPathName(mgRealPath($_SERVER["SCRIPT_FILENAME"]))],
   "base_url"         => [MG_STR, mgPathName($_SERVER["PHP_SELF"])],
   "image_url"        => [MG_STR, mgPathName($_SERVER["PHP_SELF"])],
-  "mgallery_php"     => [MG_STR, "mgallery.php"],
+  "mgallery_php"     => [MG_STR, ""],
   "format_exts"      => [MG_STR, "\.jpg|\.png|\.gif|\.jpeg|\.webp"],
   "captions_file"    => [MG_STR, "captions.txt"],
   "header_file"      => [MG_STR, "header.txt"],
@@ -75,7 +75,7 @@
   "cache_file"       => [MG_STR, ".mgallery.cache"],
 
   "cover_images"     => [MG_BOOL, TRUE],
-  "album_icon"       => [MG_STR, "album_sm.png"],
+  "album_icon"       => [MG_STR, NULL],
 
   "title_prefix"     => [MG_STR, ""],
   "title_sep"        => [MG_STR, " - "],
@@ -98,12 +98,12 @@
   "tn_quality"       => [MG_INT, 90],   // JPEG/WEBP quality percent
 
   "med_format"       => [MG_STR_LC, "jpeg"],
-  "med_width"        => [MG_INT, 960],
-  "med_height"       => [MG_INT, 640],
+  "med_width"        => [MG_INT, 1200],
+  "med_height"       => [MG_INT, 900],
   "med_quality"      => [MG_INT, 90],
 
   "backend"          => [MG_STR_LC, "php"],
-  "sql_db"           => [MG_STR, NULL],
+  "sql_db"           => [MG_STR, FALSE],
   "sql_username"     => [MG_STR, ""],
   "sql_password"     => [MG_STR, ""],
   "sql_options"      => [MG_STR_ARRAY, []],
@@ -234,12 +234,47 @@
 }
 
 
-function mgReadSettings(&$spaths)
+function mgReadOneConfig(&$searchPaths, $pathList)
 {
-  global $mgSettings, $mgDefaults;
+  global $mgSettings, $mgProgConfigFile;
+  $found = FALSE;
+
+  foreach (array_unique($pathList) as $path)
+  {
+    $filename = $path.$mgProgConfigFile;
+    $searchPaths[] = $filename;
+
+    if (!$found)
+    {
+      mgDebug("Checking '".$filename."' for configuration ..\n");
+
+      if (file_exists($filename) &&
+        ($data = parse_ini_file($filename, FALSE)) !== FALSE)
+      {
+        mgDebug("Found '".$filename."' config.\n");
+        foreach ($data as $dkey => &$dval)
+          $mgSettings[$dkey] = $dval;
 
+        $found = TRUE;
+      }
+    }
+  }
+
+  return $found;
+}
+
+
+function mgReadSettings(&$searchPaths)
+{
+  global $mgSettings;
+  $mgSettings = [];
+  $searchPaths = [];
+
+  // System-wide
+  $ok = mgReadOneConfig($searchPaths, ["/etc/", "/usr/local/etc/"]);
+
+  // User-specific
   $spaths = [];
-  $spaths[] = getcwd()."/";
   if (($tmp = getenv("HOME")) !== FALSE && strlen($tmp) > 0)
   {
     $spaths[] = $tmp."/.config/mgallery/";
@@ -255,27 +290,13 @@
       $spaths[] = $tmp."/.";
     }
   }
-  $spaths[] = dirname(__FILE__)."/";
-
-  $spaths = array_map(function ($path)
-  {
-    global $mgProgConfigFile;
-    return $path.$mgProgConfigFile;
-  }, $spaths);
+  $ok |= mgReadOneConfig($searchPaths, $spaths);
 
-  foreach (array_unique($spaths) as $file)
-  {
-    mgDebug("Checking '".$file."' for configuration ..\n");
-    if (file_exists($file) &&
-      ($mgSettings = parse_ini_file($file, FALSE)) !== FALSE)
-    {
-      mgDebug("Found '".$file."' config.\n");
-      return TRUE;
-    }
-  }
+  // Album/directory-local
+  $ok |= mgReadOneConfig($searchPaths, [ dirname(__FILE__)."/", getcwd()."/" ]);
 
-  $mgSettings = [];
-  return FALSE;
+  $searchPaths = array_unique($searchPaths);
+  return $ok;
 }