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