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