a9fd81da9cdb717707b2ea79e1b7da526f370d57
[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 = confGetVar("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 confGetVar($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 confPutVar($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 gaasGA extends GoogleAuthenticator
133 {
134         
135         function getData($username)
136         {
137         }
138         
139         
140         function putData($username, $data)
141         {
142         }
143         
144         
145         function getUsers()
146         {
147         }
148 }
149 ?>