not sure what i did, but you can bet it was AWESOME
[glcas.git] / libglcas / config.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;
16                 
17                 try {
18                         $this->dbobject = new PDO("sqlite:$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 = false;
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, $configCat, $configName, $configVal)
57         {
58                 $sql = "insert into datatable values (NULL, '$configType', '$configCat', '$configName', '$configVal')";
59                 $this->dbobject->query($sql);
60                 //error_log("CONFIG: adddata as $sql");
61         }
62         
63         function saveConfig()
64         {
65                 // stub function for old config method reverse compatability
66         }
67         
68         function getData($configType, $configCat="", $configName="")
69         {
70                 $ret = null;
71                 $nret = 0;
72                 $haveWhere = false;
73
74                 $wheredata = "where data_type='$configType'";
75                 if($configCat != "") {
76                         $wheredata .= " and data_category='$configCat'";
77                 }
78                 
79                 if($configName != "") {
80                         $wheredata .= " and data_name='$configName'";
81                 }
82                 
83                 $sql = "select data_category,data_name,data_val from datatable $wheredata";
84                 //error_log("CONFIG: get via $sql");
85                 
86                 $res = $this->dbobject->query($sql);
87                 
88                 foreach($res as $row) {
89                         $ret[$nret]["val"] = $row[2];
90                         $ret[$nret]["category"] = $row[0];
91                         $ret[$nret]["name"] = $row[1];
92                         $nret++;
93                 }
94                 if($nret == 0) return false;
95                 
96                 return $ret;
97         }
98         
99         function delData($configType, $configCat, $configName="", $configVal="")
100         {
101                 $extrawhere = "";
102                 if($configName != "") {
103                         $extrawhere = " and data_name='$configName'";
104                 }
105                 if($configVal != "") {
106                         $extrawhere .= " and data_val='$configVal'";
107                 }
108                 $sql = "delete from datatable where data_type='$configType' and data_category='$configCat' $extrawhere";
109                 $this->dbobject->query($sql);
110                 
111                 //error_log("del all data was $sql");
112         }
113         
114         function delAllDAta($configType, $configCat)
115         {
116                 $sql = "delete from datatable where data_type='$configType' and data_category='$configCat'";
117                 //echo "sql is $sql\n";
118                 $this->dbobject->query($sql);
119                 //error_log("del all data was $sql");
120         }
121         
122         function setupTables()
123         {
124                 $sql = "SELECT count(name) FROM sqlite_master WHERE type='table' AND name='config';";
125                 $res = $this->dbobject->query($sql);
126                 
127                 foreach($res as $row) {
128                         if($row[0] > 0) {
129                                 //echo "Tables exist\n";
130                                 return;
131                         }
132                 }
133                 
134                 
135                 $sql = 'CREATE TABLE "config" ( "configname" TEXT,"configvar" TEXT);';
136                 $this->dbobject->query($sql);
137                 
138                 $sql = 'CREATE TABLE "datatable" ("data_id" INTEGER PRIMARY KEY AUTOINCREMENT,"data_type" TEXT,"data_category" TEXT,"data_name" TEXT,"data_val" TEXT);';
139                 $this->dbobject->query($sql);
140
141                 $sql = 'CREATE TABLE sqlite_sequence(name,seq);';
142                 $this->dbobject->query($sql);
143         }
144         
145         private $configPath;
146         private $dbobject;
147         
148 }
149                 
150 function glcas_getWebConfigPath()
151 {
152         global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
153
154         // if you wish to add more places to find webconfig, add them here.
155         $configpath = false;
156         if(file_exists($WEB_ROOT_FS."/../var/glcas/webconfig")) return realpath($WEB_ROOT_FS."/../var/glcas/webconfig");
157         if(file_exists("/var/lib/glcas/webconfig")) return realpath("/var/lib/glcas/webconfig");
158         if(file_exists("/var/run/glcas/webconfig")) return realpath("/var/run/glcas/webconfig");
159         
160         return $configpath;
161         //return false; 
162 }
163
164                 
165
166 ?>