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