moved to an sqlite database for config rather then flat text file
[glcas.git] / libglcas / config_new.php
1 <?php
2
3 class GLCASConfig {
4         
5         function __construct()
6         {
7                 $this->configPath = "";
8                 $this->config = "";
9                 $this->dbobject = false;
10         }
11         
12         function loadConfig($configpath)
13         {
14                 error_log("loadConfig $configpath");
15                 $this->configPath = $configpath.".db";
16                 
17                 try {
18                         $this->dbobject = new PDO("sqlite:".$this->configPath);
19                 } catch(PDOException¬†$exep) {
20                         error_log("execpt on db open");
21                         return false;
22                 }
23                 
24                 $this->setupTables();
25         }
26         
27         function setConfig($configname, $configval)
28         {
29                 $sql = "delete from config where configname='$configname'";
30                 $this->dbobject->query($sql);
31                 $sql = "insert into config values ('$configname', '$configval')";
32                 $this->dbobject->query($sql);
33         }
34         
35         function getConfig($configname)
36         {
37                 $sql = "select configvar from config where configname='$configname'";
38                 $res = $this->dbobject->query($sql);
39                 $val = null;
40                 foreach($res as $row) {
41                         $val = $row[0];
42                         error_log("foreach, $val\n");
43                 }
44                 
45                 return $val;
46         }
47         
48         function delConfig($configname)
49         {
50                 $sql = "delete from config where configname='$configname'";
51                 $this->dbobject->query($sql);
52                 
53                 return true;
54         }
55         
56         function addData($configType, $configName, $configVal)
57         {
58                 $sql = "insert into datatable values (NULL, '$configType', '$configName', '$configVal')";
59                 $this->dbobject->query($sql);
60                 
61         }
62         
63         function getData($configType, $configName="")
64         {
65                 $ret = null;
66                 $nret = 0;
67                 if($configName == "") {
68                         $sql = "select data_val,data_name from datatable where data_type='$configType'";
69                 } else {
70                         $sql = "select data_val from datatable where data_type='$configType' and data_name='$configName'";
71                 }
72                 $res = $this->dbobject->query($sql);
73                 
74                 foreach($res as $row) {
75                         $ret[$nret]["val"] = $row[0];
76                         if($configName == "") $ret[$nret]["name"] = $row[1];
77                         $nret++;
78                 }
79                 
80                 return $ret;
81         }
82         
83         function delData($configType, $configName, $configVal)
84         {
85                 $sql = "delete from datatable where data_type='$configType' and data_name='$configName' and data_val='$configVal')";
86                 $this->dbobject->query($sql);
87         }
88         
89         function delAllDAta($configType, $configName)
90         {
91                 $sql = "delete from datatable where data_type='$configType' and data_name='$configName'";
92                 //echo "sql is $sql\n";
93                 $this->dbobject->query($sql);
94         }
95         
96         function setupTables()
97         {
98                 $sql = "SELECT count(name) FROM sqlite_master WHERE type='table' AND name='config';";
99                 $res = $this->dbobject->query($sql);
100                 
101                 foreach($res as $row) {
102                         if($row[0] > 0) {
103                                 echo "Tables exist\n";
104                                 return;
105                         }
106                 }
107                 
108                 
109                 $sql = 'CREATE TABLE "config" ( "configname" TEXT,"configvar" TEXT);';
110                 $this->dbobject->query($sql);
111                 
112                 $sql = 'CREATE TABLE "datatable" ("data_id" INTEGER PRIMARY KEY AUTOINCREMENT,"data_type" TEXT,"data_name" TEXT,"data_val" TEXT);';
113                 $this->dbobject->query($sql);
114
115                 $sql = 'CREATE TABLE sqlite_sequence(name,seq);';
116                 $this->dbobject->query($sql);
117         }
118         
119         private $configPath;
120         private $dbobject;
121         
122 }
123                 
124 function glcas_getWebConfigPath()
125 {
126         global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
127
128         // if you wish to add more places to find webconfig, add them here.
129         $configpath = false;
130         if(file_exists($WEB_ROOT_FS."/../var/glcas/webconfig")) return realpath($WEB_ROOT_FS."/../var/glcas/webconfig");
131         if(file_exists("/var/lib/glcas/webconfig")) return realpath("/var/lib/glcas/webconfig");
132         if(file_exists("/var/run/glcas/webconfig")) return realpath("/var/run/glcas/webconfig");
133         
134         return $configpath;
135         //return false; 
136 }
137
138                 
139
140 ?>