adding some framework bits.
[anyhammer.git] / libanyhammer / config.php
diff --git a/libanyhammer/config.php b/libanyhammer/config.php
new file mode 100644 (file)
index 0000000..330190d
--- /dev/null
@@ -0,0 +1,166 @@
+<?php
+
+class AHConfig {
+       
+       function __construct()
+       {
+               $this->configPath = "";
+               $this->config = "";
+               $this->dbobject = false;
+       }
+       
+       function loadConfig($configpath)
+       {
+               error_log("loadConfig $configpath");
+               $this->configPath = $configpath;
+               
+               try {
+                       $this->dbobject = new PDO("sqlite:$configpath");
+               } catch(PDOException $exep) {
+                       error_log("execpt on db open");
+                       return false;
+               }
+               
+               $this->setupTables();
+       }
+       
+       function setConfig($configname, $configval)
+       {
+               $sql = "delete from config where configname='$configname'";
+               $this->dbobject->query($sql);
+               $sql = "insert into config values ('$configname', '$configval')";
+               $this->dbobject->query($sql);
+       }
+       
+       function getConfig($configname)
+       {
+               $sql = "select configvar from config where configname='$configname'";
+               $res = $this->dbobject->query($sql);
+               $val = false;
+               foreach($res as $row) {
+                       $val = $row[0];
+                       error_log("foreach, $val\n");
+               }
+               
+               return $val;
+       }
+       
+       function delConfig($configname)
+       {
+               $sql = "delete from config where configname='$configname'";
+               $this->dbobject->query($sql);
+               
+               return true;
+       }
+       
+       function addData($configType, $configCat, $configName, $configVal)
+       {
+               $sql = "insert into datatable values (NULL, '$configType', '$configCat', '$configName', '$configVal')";
+               $this->dbobject->query($sql);
+               //error_log("CONFIG: adddata as $sql");
+       }
+       
+       function saveConfig()
+       {
+               // stub function for old config method reverse compatability
+       }
+       
+       function getData($configType, $configCat="", $configName="")
+       {
+               $ret = null;
+               $nret = 0;
+               $haveWhere = false;
+
+               $wheredata = "where data_type='$configType'";
+               if($configCat != "") {
+                       $wheredata .= " and data_category='$configCat'";
+               }
+               
+               if($configName != "") {
+                       $wheredata .= " and data_name='$configName'";
+               }
+               
+               $sql = "select data_category,data_name,data_val from datatable $wheredata";
+               //error_log("CONFIG: get via $sql");
+               
+               $res = $this->dbobject->query($sql);
+               
+               foreach($res as $row) {
+                       $ret[$nret]["val"] = $row[2];
+                       $ret[$nret]["category"] = $row[0];
+                       $ret[$nret]["name"] = $row[1];
+                       $nret++;
+               }
+               if($nret == 0) return false;
+               
+               return $ret;
+       }
+       
+       function delData($configType, $configCat, $configName="", $configVal="")
+       {
+               $extrawhere = "";
+               if($configName != "") {
+                       $extrawhere = " and data_name='$configName'";
+               }
+               if($configVal != "") {
+                       $extrawhere .= " and data_val='$configVal'";
+               }
+               $sql = "delete from datatable where data_type='$configType' and data_category='$configCat' $extrawhere";
+               $this->dbobject->query($sql);
+               
+               //error_log("del all data was $sql");
+       }
+       
+       function delAllDAta($configType, $configCat)
+       {
+               $sql = "delete from datatable where data_type='$configType' and data_category='$configCat'";
+               //echo "sql is $sql\n";
+               $this->dbobject->query($sql);
+               //error_log("del all data was $sql");
+       }
+       
+       function setupTables()
+       {
+               $sql = "SELECT count(name) FROM sqlite_master WHERE type='table' AND name='config';";
+               $res = $this->dbobject->query($sql);
+               
+               foreach($res as $row) {
+                       if($row[0] > 0) {
+                               //echo "Tables exist\n";
+                               return;
+                       }
+               }
+               
+               
+               $sql = 'CREATE TABLE "config" ( "configname" TEXT,"configvar" TEXT);';
+               $this->dbobject->query($sql);
+               
+               $sql = 'CREATE TABLE "datatable" ("data_id" INTEGER PRIMARY KEY AUTOINCREMENT,"data_type" TEXT,"data_category" TEXT,"data_name" TEXT,"data_val" TEXT);';
+               $this->dbobject->query($sql);
+
+               $sql = 'CREATE TABLE sqlite_sequence(name,seq);';
+               $this->dbobject->query($sql);
+       }
+       
+       private $configPath;
+       private $dbobject;
+       
+}
+               
+function ah_getConfigPath()
+{
+       global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+
+       // if you wish to add more places to find webconfig, add them here.
+       $configpath = false;
+       if(file_exists($WEB_ROOT_FS."/../var/anyhammer/config.db")) return realpath($WEB_ROOT_FS."/../var/anyhammer/config.db");
+       if(file_exists("/var/lib/anyhammer/config.db")) return realpath("/var/lib/anyhammer/config.db");
+       if(file_exists("/var/run/anyhammer/config.db")) return realpath("/var/run/anyhammer/config.db");
+       
+       return $configpath;
+       //return false; 
+}
+
+               
+
+?>