689389a5079dcd8668bb111dc4e910df942aebb0
[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 define("MSG_GET_OTK_PNG", 10);
17 define("MSG_GET_OTK_ID", 11);
18
19 if(file_exists("../../lib/ga4php.php")) require_once("../../lib/ga4php.php");
20 if(file_exists("../lib/ga4php.php")) require_once("../lib/ga4php.php");
21
22
23 function generateRandomString()
24 {
25         $str = "";
26         $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
27         
28         for($i=0; $i<128; $i++) {
29                 $str .= $strpos[rand(0, strlen($strpos)-1)];
30         }
31         
32         return $str;
33 }
34
35
36 function getDatabase() {
37         $dbobject = false;
38         if(file_exists("gaasdata.sqlite")) {
39                 try {
40                         $dbobject = new PDO("sqlite:gaasdata.sqlite");
41                 } catch(PDOException $exep) {
42                         error_log("execpt on db open");
43                 }
44         } else {
45                 try {
46                         $dbobject = new PDO("sqlite:gaasdata.sqlite");
47                 } catch(PDOException $exep) {
48                         error_log("execpt on db open");
49                 }
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         return $dbobject;
55 }
56
57 function closeDatabase($db) {
58         // doesnt do anything yet
59 }
60
61 class gaasGA extends GoogleAuthenticator {
62         function getData($username) {
63                 echo "called into getdata\n";
64                 
65                 // get our database connection
66                 $dbObject = getDatabase();
67                 
68                 // set the sql for retreiving the data
69                 $sql = "select users_tokendata from users where users_username='$username'";
70                 
71                 // run the query
72                 $result = $dbObject->query($sql);
73                 
74                 // check the result
75                 echo "next1\n";
76                 if(!$result) return false;
77                 
78                 // now just retreieve all the data (there should only be one, but whatever)
79                 echo "next2\n";
80                 $tokendata = false;
81                 foreach($result as $row) {
82                         $tokendata = $row["users_tokendata"];
83                 }
84
85                 echo "next3, $username, $tokendata\n";
86                 // now we have our data, we just return it. If we got no data
87                 // we'll just return false by default
88                 return $tokendata;
89                 
90                 // and there you have it, simple eh?
91         }
92         
93         
94         function putData($username, $data) {
95                 // get our database connection
96                 $dbObject = getDatabase();
97                 
98                 // we need to check if the user exists, and if so put the data, if not create the data
99                 $sql = "select * from users where users_username='$username'";
100                 $res = $dbObject->query($sql);
101                 if($res->fetchColumn() > 0) {
102                         // do update
103                         $sql = "update users set users_tokendata='$data' where users_username='$username'";
104                 } else {
105                         // do insert
106                         $sql = "insert into users values (NULL, '$username', '', '', '$data', '')";
107                 }
108                 
109                 if($dbObject->query($sql)) {
110                         return true;
111                 } else {
112                         return false;
113                 }
114
115         }
116         
117         function getUsers() {
118                 // get our database connection
119                 $dbObject = getDatabase();
120                 
121                 // now the sql again
122                 $sql = "select users_username from users";
123                 
124                 // run the query
125                 $result = $dbObject->query($sql);
126                 
127                 // iterate over the results - we expect a simple array containing
128                 // a list of usernames
129                 $i = 0;
130                 $users = array();
131                 foreach($result as $row) {
132                         $users[$i] = $row["username"];
133                         $i++;
134                 }
135                 
136                 // now return the list
137                 return $users;
138         }       
139 }
140
141 ?>