9af4dfb728781876edaa96cdc693302c6def59eb
[ga4php.git] / gaas / lib / gaasdLib.php
1 <?php 
2
3 require_once("globalLib.php");
4 require_once("gaasdMessages.php");
5
6 // messy
7 require_once(dirname(__FILE__)."/../../lib/ga4php.php");
8
9 // first we check if our db exists, if not, we're not inited
10 $initState = false;
11 $backEnd = "";
12 global $initState, $backEnd;
13 if(file_exists($BASE_DIR."/gaas/gaasd/gaasd.sqlite")) {
14         // then we check if the config vars we need exist in the db
15         $backEndType = confGetVal("backend");
16         
17         if($backEndType == "AD") {
18                 $backEnd = "AD";
19                 
20                 // TODO: we should now check all vars are set, but for now this will surfice
21                 $initState = true;
22         }
23
24         if($backEndType == "internal") {
25                 $backEnd = "IN";
26                 $initState = true;
27         }
28 }
29
30 // have a gloval db handle so we dont have to keep opening the db all the time
31 // this may go away when we consider the implications for a parallel gaasd
32 $DB_HANDLE = false;
33 global $DB_HANDLE;
34
35
36 // a function to create our db
37 // TODO: error checking
38 function createDB()
39 {
40         $dbobject = false;
41         global $BASE_DIR, $initState, $backEnd;
42         try {
43                 $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
44         } catch(PDOException $exep) {
45                 error_log("execpt on db open");
46                 return false;
47         }
48         
49         if($backEnd == "IN") {
50                 $sql = 'CREATE TABLE "users" ("users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT, "users_realname" TEXT, "users_password" TEXT, "users_tokendata" TEXT, "users_otk" TEXT);';
51                 $dbobject->query($sql);
52         }
53         
54         $sql = 'CREATE TABLE "config" ("conf_id" INTEGER PRIMARY KEY AUTOINCREMENT,"conf_name" TEXT, "conf_value" TEXT);';
55         $dbobject->query($sql);
56         $sql = 'CREATE TABLE "radclients" ("rad_id" INTEGER PRIMARY KEY AUTOINCREMENT,"rad_name" TEXT, "rad_ip" TEXT, "rad_secret" TEXT, "rad_desc" TEXT);';
57         $dbobject->query($sql);
58         $sql = 'CREATE TABLE "hardwaretokens" ("tok_id" INTEGER PRIMARY KEY AUTOINCREMENT,"tok_name" TEXT, "tok_key" TEXT, "tok_type" TEXT);';
59         $dbobject->query($sql);
60         
61         return true;
62 }
63
64 // a function to get the database
65 function getDB()
66 {
67         $dbobject = false;
68         global $BASE_DIR, $DB_HANDLE;
69         if($DB_HANDLE != false) return $DB_HANDLE;
70         if(file_exists("$BASE_DIR/gaas/gaasd/gaasd.sqlite")) {
71                 try {
72                         $dbobject = new PDO("sqlite:$BASE_DIR/gaas/gaasd/gaasd.sqlite");
73                 } catch(PDOException $exep) {
74                         error_log("execpt on db open");
75                         return false;
76                 }
77         } else {
78                 return false;
79         }
80         
81         $DB_HANDLE = $dbobject;
82         return $dbobject;
83 }
84
85
86 function confDelVar($varname)
87 {
88         $db = getDB();
89         
90         $sql = "delete from config where conf_name='$varname'";
91         $db->query($sql);
92         
93         return true;
94 }
95
96 // a funciton to deal with Config Vars
97 function confGetVal($varname)
98 {
99         $db = getDB();
100         
101         $sql = "select conf_value from config where conf_name='$varname'";
102         
103         $result = $db->query($sql);
104         
105         if(!$result) return false;
106         
107         $val = "";
108         foreach($result as $row) {
109                 $val = $row["conf_value"];
110         }
111
112         // TOTALLY GUNNA WORK!
113         return $val;
114 }
115
116 // and a function to put vars
117 function confSetVal($varname, $value)
118 {
119         $db = getDB();
120         
121         $sql = "delete from config where conf_name='$varname'";
122         $db->query($sql);
123         
124         $sql = "insert into config values (NULL, '$varname','$value')";
125         $db->query($sql);
126         
127         // TODO: do all this better
128         return true;
129 }
130
131 // now we define our extended class
132 class gaasdGA extends GoogleAuthenticator
133 {
134         
135         function getData($username) {
136                 //echo "called into getdata\n";
137                 
138                 // get our database connection
139                 $dbObject = getDB();
140                 
141                 // set the sql for retreiving the data
142                 $sql = "select users_tokendata from users where users_username='$username'";
143                 
144                 // run the query
145                 $result = $dbObject->query($sql);
146                 
147                 // check the result
148                 //echo "next1\n";
149                 if(!$result) return false;
150                 
151                 // now just retreieve all the data (there should only be one, but whatever)
152                 //echo "next2\n";
153                 $tokendata = false;
154                 foreach($result as $row) {
155                         $tokendata = $row["users_tokendata"];
156                 }
157
158                 //echo "next3, $username, $tokendata\n";
159                 // now we have our data, we just return it. If we got no data
160                 // we'll just return false by default
161                 return $tokendata;
162                 
163                 // and there you have it, simple eh?
164         }
165         
166         
167         function putData($username, $data) {
168                 // get our database connection
169                 $dbObject = getDB();
170                 
171                 // we need to check if the user exists, and if so put the data, if not create the data
172                 $sql = "select * from users where users_username='$username'";
173                 $res = $dbObject->query($sql);
174                 if($res->fetchColumn() > 0) {
175                         // do update
176                         //error_log("doing userdata update");
177                         $sql = "update users set users_tokendata='$data' where users_username='$username'";
178                 } else {
179                         // do insert
180                         //error_log("doing user data create");
181                         $sql = "insert into users values (NULL, '$username', '', '', '$data', '')";
182                 }
183                 
184                 if($dbObject->query($sql)) {
185                         return true;
186                 } else {
187                         return false;
188                 }
189
190         }
191         
192         function getUsers() {
193                 // get our database connection
194                 $dbObject = getDB();
195                 
196                 // now the sql again
197                 $sql = "select users_username from users";
198                 
199                 // run the query
200                 $result = $dbObject->query($sql);
201                 
202                 // iterate over the results - we expect a simple array containing
203                 // a list of usernames
204                 $i = 0;
205                 $users = array();
206                 foreach($result as $row) {
207                         $users[$i] = $row["username"];
208                         $i++;
209                 }
210                 
211                 // now return the list
212                 return $users;
213         }
214 }
215 ?>