efc6d95a58fb5556813966d64c87f2e40e909caf
[ga4php.git] / authserver / lib / lib.php
1 <?php
2
3 if(!isset($MSG_QUEUE_KEY_ID_SERVER)) $MSG_QUEUE_KEY_ID_SERVER = "189751072";
4 if(!isset($MSG_QUEUE_KEY_ID_CLIENT)) $MSG_QUEUE_KEY_ID_CLIENT = "189751073";
5 global $MSG_QUEUE_KEY_ID_SERVER, $MSG_QUEUE_KEY_ID_CLIENT;
6
7 define("MSG_AUTH_USER", 1);
8 define("MSG_ADD_USER", 2);
9 define("MSG_DELETE_USER", 2);
10
11
12 if(file_exists("../../lib/ga4php.php")) require_once("../../lib/ga4php.php");
13 if(file_exists("../lib/ga4php.php")) require_once("../lib/ga4php.php");
14
15 function getDatabase() {
16         $dbobject = false;
17         if(file_exists("/tmp/gadata.sqlite")) {
18                 try {
19                         $dbobject = new PDO("sqlite:/tmp/gadata.sqlite");
20                 } catch(PDOException $exep) {
21                         error_log("execpt on db open");
22                 }
23         } else {
24                 try {
25                         $dbobject = new PDO("sqlite:/tmp/gadata.sqlite");
26                 } catch(PDOException $exep) {
27                         error_log("execpt on db open");
28                 }
29                 $sql = 'CREATE TABLE "users" ("users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT,"users_tokendata" TEXT);';
30                 $dbobject->query($sql);
31         }
32         
33         return $dbobject;
34 }
35
36 function closeDatabase($db) {
37         // doesnt do anything yet
38 }
39
40 class gaasGA extends GoogleAuthenticator {
41         function getData($username) {
42                 
43                 // get our database connection
44                 $dbObject = getDatabase();
45                 
46                 // set the sql for retreiving the data
47                 $sql = "select users_tokendata from users where users_username='$username'";
48                 
49                 // run the query
50                 $result = $dbObject->query($sql);
51                 
52                 // check the result
53                 if(!$result) return false;
54                 
55                 // now just retreieve all the data (there should only be one, but whatever)
56                 $tokendata = false;
57                 foreach($result as $row) {
58                         $tokendata = $row["users_tokendata"];
59                 }
60                 
61                 // now we have our data, we just return it. If we got no data
62                 // we'll just return false by default
63                 return $tokendata;
64                 
65                 // and there you have it, simple eh?
66         }
67         
68         
69         // now we need a function for putting the data back into our user table.
70         // in this example, we wont check anything, we'll just overwrite it.
71         function putData($username, $data) {
72                 // get our database connection
73                 $dbObject = getDatabase();
74                 
75                 // set the sql for updating the data
76                 // token data is stored as a base64 encoded string, it should
77                 // not need to be escaped in any way prior to storing in a database
78                 // but feel free to call your databases "addslashes" (or whatever)
79                 // function on $data prior to doing the SQL.
80                 $sql = "delete from users where users_username='$username'";
81                 $dbObject->query($sql);
82                 
83                 $sql = "insert into users values (NULL, '$username', '$data')";
84                 
85                 
86                 // now execute the sql and return straight away - you should probably
87                 // clean up after yourselves, but im going to assume pdo does this
88                 // for us anyway in this exmaple
89                 if($dbObject->query($sql)) {
90                         return true;
91                 } else {
92                         return false;
93                 }
94                 
95                 // even simpler!
96         }
97         
98         function getUsers() {
99                 // get our database connection
100                 $dbObject = getDatabase();
101                 
102                 // now the sql again
103                 $sql = "select users_username from users";
104                 
105                 // run the query
106                 $result = $dbObject->query($sql);
107                 
108                 // iterate over the results - we expect a simple array containing
109                 // a list of usernames
110                 $i = 0;
111                 $users = array();
112                 foreach($result as $row) {
113                         $users[$i] = $row["username"];
114                         $i++;
115                 }
116                 
117                 // now return the list
118                 return $users;
119         }       
120 }
121
122 ?>