changeset 322:2f4e3e458714

Improve configuration handling, and add "string array" configuration item type.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 09 Apr 2020 11:55:08 +0300
parents f07e3d79c421
children 23625c53e62d
files mgallery.inc.php mgallery.php mgtool.php
diffstat 3 files changed, 136 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/mgallery.inc.php	Thu Apr 09 10:13:45 2020 +0300
+++ b/mgallery.inc.php	Thu Apr 09 11:55:08 2020 +0300
@@ -28,11 +28,12 @@
 //
 define("MG_STR"              , 1);
 define("MG_STR_LC"           , 2);
-define("MG_INT"              , 3);
-define("MG_DVA"              , 4);
-define("MG_BOOL"             , 5);
-define("MG_FLAGS"            , 6);
-define("MG_DATE"             , 7);
+define("MG_STR_ARRAY"        , 3);
+define("MG_INT"              , 4);
+define("MG_DVA"              , 5);
+define("MG_BOOL"             , 6);
+define("MG_FLAGS"            , 7);
+define("MG_DATE"             , 8);
 
 
 define("yes"                 , 1);
@@ -55,6 +56,7 @@
     return $path;
 }
 
+
 //
 // Configuration settings and their default values
 //
@@ -79,9 +81,9 @@
   "title_sep"        => [MG_STR, " - "],
   "page_info"        => [MG_STR, "Powered by <b>MGallery ".$mgProgVersion."</b> &copy; Copyright ".$mgProgCopyright],
 
-  "css"              => [MG_STR, NULL],
-  "js_file"          => [MG_STR, NULL],
-  "urchin_file"      => [MG_STR, FALSE],
+  "css"              => [MG_STR_ARRAY, NULL],
+  "js_file"          => [MG_STR_ARRAY, NULL],
+  "urchin_file"      => [MG_STR_ARRAY, FALSE],
 
   "global_flags"     => [MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS | MGF_CAPTIONS, &$mgGFlags],
   "image_flags"      => [MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS, &$mgGFlags],
@@ -104,7 +106,7 @@
   "sql_db"           => [MG_STR, NULL],
   "sql_username"     => [MG_STR, ""],
   "sql_password"     => [MG_STR, ""],
-  "sql_options"      => [MG_STR, []],
+  "sql_options"      => [MG_STR_ARRAY, []],
 ];
 
 
@@ -357,41 +359,6 @@
 }
 
 
-function mgGetDValStr($mdef, $val)
-{
-  switch ($mdef[0])
-  {
-    case MG_STR:
-      if (is_array($val))
-        return implode(", ", $val);
-      else
-        return "\"".$val."\"";
-
-    case MG_STR_LC:
-      if (is_array($val))
-        return implode(", ", array_map('strtolower', $val));
-      else
-        return "\"".strtolower($val)."\"";
-
-    case MG_BOOL:
-      return $val ? "yes" : "no";
-
-    case MG_FLAGS:
-      {
-        $mstr = [];
-        foreach ($mdef[2] as $vkey => $vval)
-        {
-          if ($val & $vval)
-            $mstr[] = $vkey;
-        }
-        return implode($mstr, " | ");
-      }
-    case MG_INT  :
-    default      : return (string) $val;
-  }
-}
-
-
 function mgLogSQLError($dbh, $sql)
 {
   return mgError("SQL error '".implode("; ", $dbh->errorInfo())."' in statement: \"".$sql."\"");
--- a/mgallery.php	Thu Apr 09 10:13:45 2020 +0300
+++ b/mgallery.php	Thu Apr 09 11:55:08 2020 +0300
@@ -140,9 +140,23 @@
 }
 
 
-function mgPrintPageHeader($pageTitle, $pageClass)
+function mgGetJSLine($filename)
+{
+  return
+    "  <script src=\"".mgMangleURI($filename)."\"></script>\n";
+}
+
+
+function mgIncludeUrchin($filename)
 {
-  global $pageCSS, $pageCharset, $pageUrchin, $pageJSFile, $pageLang;
+  if (file_exists($filename))
+    require_once $filename;
+}
+
+
+function mgPrintPageHeader($pageTitle, $pageDivID)
+{
+  global $pageCSS, $pageCharset, $pageUrchin, $pageJS, $pageLang;
 
   echo
     "<!DOCTYPE html>\n".
@@ -151,18 +165,27 @@
     "  <meta charset=\"".$pageCharset."\">\n".
     // Setting the viewport makes things break especially with the
     // text-size-adjust CSS settings... so we don't use it.
+    // XXX: We do now. For now.
     "  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n".
-    "  <script src=\"".mgMangleURI($pageJSFile)."\"></script>\n".
     "  <title>".strip_tags($pageTitle)."</title>\n".
-    mgGetCSSLine($pageCSS, FALSE).
+    (is_array($pageJS) ? array_map('mgGetJSLine', $pageJS) : mgGetJSLine($pageJS)).
+    (is_array($pageCSS) ? array_map('mgGetCSSLine', $pageCSS) : mgGetCSSLine($pageCSS, FALSE)).
     "</head>\n".
     "<body>\n";
 
-  if ($pageUrchin !== FALSE && file_exists($pageUrchin))
-    require_once $pageUrchin;
+  if ($pageUrchin !== NULL)
+  {
+    if (is_array($pageUrchin))
+    {
+      foreach ($pageUrchin as $filename)
+        mgIncludeUrchin($filename);
+    }
+    else
+      mgIncludeUrchin($pageUrchin);
+  }
 
   echo
-    "<div id=\"".$pageClass."\">\n";
+    "<div id=\"".$pageDivID."\">\n";
 }
 
 
@@ -460,7 +483,7 @@
 }
 
 $pageCSS = mgGetSetting("css");
-$pageJSFile = mgGetSetting("js_file");
+$pageJS = mgGetSetting("js_file");
 $pageUrchin = mgGetSetting("urchin_file");
 $galBasePath = mgGetSetting("base_path");
 $galBaseURL = mgGetSetting("base_url");
--- a/mgtool.php	Thu Apr 09 10:13:45 2020 +0300
+++ b/mgtool.php	Thu Apr 09 11:55:08 2020 +0300
@@ -1337,6 +1337,93 @@
 }
 
 
+function mgGetDValStr($key, $mdata, $val, $format, $multi)
+{
+  $vfmt = "%s";
+
+  switch ($mdata[0])
+  {
+    case MG_STR_ARRAY:
+      if ($val === FALSE)
+        $val = [];
+      else
+      if (is_array($val) || is_string($val))
+      {
+        $vfmt = "\"%s\"";
+        if (is_string($val))
+          $val = [ $val ];
+      }
+      else
+        $val = "ERROR1";
+      break;
+
+    case MG_STR:
+    case MG_STR_LC:
+      if (is_string($val))
+      {
+        $vfmt = "\"%s\"";
+        if ($mdata[0] == MG_STR_LC)
+          $val = strtolower($val);
+      }
+      else
+        $val = "ERROR2";
+      break;
+
+    case MG_BOOL:
+      $val = $val ? "yes" : "no";
+      break;
+
+    case MG_FLAGS:
+      {
+        $mstr = [];
+        foreach ($mdata[2] as $vkey => $vval)
+        {
+          if ($val & $vval)
+            $mstr[] = $vkey;
+        }
+
+        $val = "\"".implode(" | ", $mstr)."\"";
+      }
+      break;
+
+    case MG_INT:
+      $val = (string) $val;
+      break;
+
+    default:
+      $val = "ERROR";
+  }
+
+  if (!is_array($val))
+  {
+    if ($val === NULL || $val === FALSE)
+      $val = [];
+    else
+      $val = [ $val ];
+  }
+
+  $res = (count($val) == 0) ? "# " : "";
+
+  if ($multi)
+  {
+    if (count($val) > 0)
+    {
+      foreach ($val as $vv)
+        $res .= sprintf($format, $key, sprintf($vfmt, $vv));
+    }
+    else
+      $res .= sprintf($format, $key, "");
+  }
+  else
+  {
+    $res .= sprintf($format, $key,
+        implode(", ", array_map(function($vv) use ($vfmt) { return sprintf($vfmt, $vv); }, $val)));
+  }
+
+  return $res;
+}
+
+
 function mgShowCopyright()
 {
   global $mgProgVersion, $mgProgCopyright,
@@ -1545,22 +1632,19 @@
 
   case "config":
   case "dump":
+    foreach ($mgDefaults as $ckey => $cdata)
+    {
+      $sval = mgGetSetting($ckey);
 
-    foreach ($mgDefaults as $key => $dval)
-    {
-      $sval = mgGetSetting($key);
       if ($cmd == "dump")
       {
-        printf("%-20s = %s\n",
-          $key,
-          mgGetDValStr($dval, $sval));
+        echo mgGetDValStr($ckey, $cdata, $sval, "%1\$-15s = %2\$s\n", FALSE);
       }
       else
       {
-        printf("%-20s = %s%s\n",
-          $key,
-          mgGetDValStr($dval, $sval),
-          ($dval[1] !== NULL && $sval !== $dval[1]) ? " (default: ".mgGetDValStr($dval, $dval[1]).")" : "");
+        echo mgGetDValStr($ckey, $cdata, $sval, "%1\$-15s = %2\$s", TRUE).
+            (($cdata[1] !== NULL && $sval !== $cdata[1]) ?
+            " (default: ".mgGetDValStr($ckey, $cdata, $cdata[1], "%2\$s", FALSE).")" : "")."\n";
       }
     }
     break;