diff msite.inc.php @ 8:4c5f651aa107

Migrate certain settings to SQL database, cleanups, etc.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 06 Dec 2012 13:30:46 +0200
parents d76020022881
children fa9b66f596bb
line wrap: on
line diff
--- a/msite.inc.php	Wed Dec 05 09:35:30 2012 +0200
+++ b/msite.inc.php	Thu Dec 06 13:30:46 2012 +0200
@@ -5,6 +5,13 @@
 $errorSet = FALSE;
 $errorMsg = "";
 
+
+define("VT_STR", 1);
+define("VT_INT", 2);
+define("VT_BOOL", 3);
+define("VT_TEXT", 4);
+
+
 function stError($msg)
 {
   global $errorSet, $errorMsg;
@@ -21,9 +28,8 @@
 
 function stSetupCacheControl()
 {
-  header("Cache-Control: private");
-  header("Cache-Control: must-revalidate");
-  header("Cache-Control: no-store");
+  header("Cache-Control: must-revalidate, no-store, private");
+  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
 }
 
 
@@ -125,16 +131,36 @@
 }
 
 
+function stReloadSettings()
+{
+  global $siteSettings;
+  $res = stExecSQL("SELECT * FROM settings");
+  if ($res !== FALSE)
+  {
+    foreach ($res as $row)
+    {
+      switch ($row["vtype"])
+      {
+        case VT_INT:  $val = intval($row["vint"]); break;
+        case VT_BOOL: $val = intval($row["vint"]) ? true : false; break;
+        case VT_STR:  $val = $row["vstr"]; break;
+        case VT_TEXT: $val = $row["vtext"]; break;
+      }
+      $siteSettings[$row["key"]] = $val;
+    }
+  }
+  else
+    die("Error fetching site settings.");
+}
+
+
 function stGetSetting($name)
 {
-  global $siteSettings, $siteDefaults;
+  global $siteSettings;
   if (isset($siteSettings[$name]))
     return $siteSettings[$name];
   else
-  if (isset($siteDefaults[$name]))
-    return $siteDefaults[$name];
-  else
-    die("No config value or default for '".$name."'.\n");
+    die("No config value for '".$name."'.\n");
 }
 
 
@@ -321,7 +347,7 @@
 {
   global $db;
   try {
-    $db = new PDO("sqlite:".stGetSetting("sqlDBfilename"));
+    $db = new PDO(stGetSetting("sqlDB"));
   }
   catch (PDOException $e) {
     error_log("Could not connect to SQL database: ".$e->getMessage().".");
@@ -477,50 +503,43 @@
 }
 
 
-function stConvSwitchMode(&$str, &$mode, $newMode)
+//
+// CLI utility helper functions
+//
+function stCArg($index)
 {
-  if ($newMode != $mode)
-  {
-    if ($mode != "")
-      $str .= "\n</".$mode.">\n";
+  global $argc, $argv;
+  if ($index < $argc)
+    return $argv[$index];
+  else
+    return FALSE;
+}
 
-    $mode = $newMode;
-
-    if ($mode != "")
-      $str .= "<".$mode.">\n";
-  }
+function stCArgLC($index)
+{
+  global $argc, $argv;
+  if ($index < $argc)
+    return strtolower($argv[$index]);
+  else
+    return FALSE;
 }
 
 
-function stConvertCompoDesc($desc)
+function stCSQLError($sql)
 {
-  global $stDescConversion;
-  $str = "";
-  $mode = "";
+  global $db;
+  die("Error executing SQL query: ".implode("; ", $db->errorInfo())." in statement \"".$sql."\"\n");
+  exit;
+}
 
-  foreach (explode("\n", $desc) as $line)
+
+function stCheckCLIOrDie()
+{
+  if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"]))
   {
-    if (preg_match("/^\s*\s*\*(.+)$/", $line, $m))
-    {
-      stConvSwitchMode($str, $mode, "ol");
-      $str .= "<li>".$m[1]."</li>\n";
-    }
-    else
-    if (preg_match("/^\s*-\s*(.+)$/", $line, $m))
-    {
-      stConvSwitchMode($str, $mode, "ul");
-      $str .= "<li>".$m[1]."</li>\n";
-    }
-    else
-    {
-      stConvSwitchMode($str, $mode, "p");
-      $str .= $line;
-    }
+    header("Status: 404 Not Found");
+    die();
   }
-
-  stConvSwitchMode($str, $mode, "");
-
-  return $str;
 }
 
 ?>
\ No newline at end of file