comms work between authd and authclient
[ga4php.git] / authserver / lib / lib.php
1 <?php
2
3 if(!isset($MSG_QUEUE_KEY_ID_SERVER)) $MSG_QUEUE_KEY_ID_SERVER = "189751072"; // i would use ftok, but its crap
4 if(!isset($MSG_QUEUE_KEY_ID_CLIENT)) $MSG_QUEUE_KEY_ID_CLIENT = "189751073"; // ftok is not ok!
5 global $MSG_QUEUE_KEY_ID_SERVER, $MSG_QUEUE_KEY_ID_CLIENT;
6
7 define("MSG_AUTH_USER_TOKEN", 1);
8 define("MSG_ADD_USER_TOKEN", 2);
9 define("MSG_DELETE_USER", 3);
10 define("MSG_AUTH_USER_PASSWORD", 4);
11 define("MSG_SET_USER_PASSWORD", 5);
12 define("MSG_SET_USER_REALNAME", 6);
13 define("MSG_SET_USER_TOKEN", 7);
14 define("MSG_SET_USER_TOKEN_TYPE", 8);
15 define("MSG_GET_USERS", 9);
16
17 if(file_exists("../../lib/ga4php.php")) require_once("../../lib/ga4php.php");
18 if(file_exists("../lib/ga4php.php")) require_once("../lib/ga4php.php");
19
20 function getDatabase() {
21         $dbobject = false;
22         if(file_exists("gaasdata.sqlite")) {
23                 try {
24                         $dbobject = new PDO("sqlite:gaasdata.sqlite");
25                 } catch(PDOException $exep) {
26                         error_log("execpt on db open");
27                 }
28         } else {
29                 try {
30                         $dbobject = new PDO("sqlite:gaasdata.sqlite");
31                 } catch(PDOException $exep) {
32                         error_log("execpt on db open");
33                 }
34                 $sql = 'CREATE TABLE "users" ("users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT, "users_realname" TEXT, "users_password" TEXT, "users_tokendata" TEXT);';
35                 $dbobject->query($sql);
36         }
37         
38         return $dbobject;
39 }
40
41 function closeDatabase($db) {
42         // doesnt do anything yet
43 }
44
45 class gaasGA extends GoogleAuthenticator {
46         function getData($username) {
47                 
48                 // get our database connection
49                 $dbObject = getDatabase();
50                 
51                 // set the sql for retreiving the data
52                 $sql = "select users_tokendata from users where users_username='$username'";
53                 
54                 // run the query
55                 $result = $dbObject->query($sql);
56                 
57                 // check the result
58                 if(!$result) return false;
59                 
60                 // now just retreieve all the data (there should only be one, but whatever)
61                 $tokendata = false;
62                 foreach($result as $row) {
63                         $tokendata = $row["users_tokendata"];
64                 }
65                 
66                 // now we have our data, we just return it. If we got no data
67                 // we'll just return false by default
68                 return $tokendata;
69                 
70                 // and there you have it, simple eh?
71         }
72         
73         
74         function putData($username, $data) {
75                 // get our database connection
76                 $dbObject = getDatabase();
77                 
78                 // we need to check if the user exists, and if so put the data, if not create the data
79                 $sql = "select * from users where users_username='$username'";
80                 $res = $dbObject->query($sql);
81                 if($res->fetchColumn() > 0) {
82                         // do update
83                         $sql = "update users set users_tokendata='$data' where users_username='$username'";
84                 } else {
85                         // do insert
86                         $sql = "insert into users values (NULL, '$username', '', '', '$data')";
87                 }
88                 
89                 if($dbObject->query($sql)) {
90                         return true;
91                 } else {
92                         return false;
93                 }
94
95         }
96         
97         function getUsers() {
98                 // get our database connection
99                 $dbObject = getDatabase();
100                 
101                 // now the sql again
102                 $sql = "select users_username from users";
103                 
104                 // run the query
105                 $result = $dbObject->query($sql);
106                 
107                 // iterate over the results - we expect a simple array containing
108                 // a list of usernames
109                 $i = 0;
110                 $users = array();
111                 foreach($result as $row) {
112                         $users[$i] = $row["username"];
113                         $i++;
114                 }
115                 
116                 // now return the list
117                 return $users;
118         }       
119 }
120
121 ?>