set token types
[ga4php.git] / authserver / lib / lib.php
index eb4fc64..83d7ced 100644 (file)
@@ -12,25 +12,49 @@ define("MSG_SET_USER_PASSWORD", 5);
 define("MSG_SET_USER_REALNAME", 6);
 define("MSG_SET_USER_TOKEN", 7);
 define("MSG_SET_USER_TOKEN_TYPE", 8);
+define("MSG_GET_USERS", 9);
+define("MSG_GET_OTK_PNG", 10);
+define("MSG_GET_OTK_ID", 11);
+define("MSG_DELETE_USER_TOKEN", 12);
+define("MSG_SYNC_TOKEN", 13);
+define("MSG_GET_TOKEN_TYPE", 14);
+
+// BASE_DIR = 
+$BASE_DIR = realpath(dirname(__FILE__)."/../../");
+global $BASE_DIR;
+
+// messy
+require_once(dirname(__FILE__)."/../../lib/ga4php.php");
+
+function generateRandomString()
+{
+       $str = "";
+       $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+       
+       for($i=0; $i<128; $i++) {
+               $str .= $strpos[rand(0, strlen($strpos)-1)];
+       }
+       
+       return $str;
+}
 
-if(file_exists("../../lib/ga4php.php")) require_once("../../lib/ga4php.php");
-if(file_exists("../lib/ga4php.php")) require_once("../lib/ga4php.php");
 
 function getDatabase() {
        $dbobject = false;
-       if(file_exists("gaasdata.sqlite")) {
+       global $BASE_DIR;
+       if(file_exists("$BASE_DIR/authserver/authd/gaasdata.sqlite")) {
                try {
-                       $dbobject = new PDO("sqlite:gaasdata.sqlite");
+                       $dbobject = new PDO("sqlite:$BASE_DIR/authserver/authd/gaasdata.sqlite");
                } catch(PDOException $exep) {
                        error_log("execpt on db open");
                }
        } else {
                try {
-                       $dbobject = new PDO("sqlite:gaasdata.sqlite");
+                       $dbobject = new PDO("sqlite:$BASE_DIR/authserver/authd/gaasdata.sqlite");
                } catch(PDOException $exep) {
                        error_log("execpt on db open");
                }
-               $sql = 'CREATE TABLE "users" ("users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT, "users_realname" TEXT, "users_password" TEXT, "users_tokendata" TEXT);';
+               $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);';
                $dbobject->query($sql);
        }
        
@@ -43,6 +67,7 @@ function closeDatabase($db) {
 
 class gaasGA extends GoogleAuthenticator {
        function getData($username) {
+               echo "called into getdata\n";
                
                // get our database connection
                $dbObject = getDatabase();
@@ -54,14 +79,17 @@ class gaasGA extends GoogleAuthenticator {
                $result = $dbObject->query($sql);
                
                // check the result
+               echo "next1\n";
                if(!$result) return false;
                
                // now just retreieve all the data (there should only be one, but whatever)
+               echo "next2\n";
                $tokendata = false;
                foreach($result as $row) {
                        $tokendata = $row["users_tokendata"];
                }
-               
+
+               echo "next3, $username, $tokendata\n";
                // now we have our data, we just return it. If we got no data
                // we'll just return false by default
                return $tokendata;
@@ -79,10 +107,12 @@ class gaasGA extends GoogleAuthenticator {
                $res = $dbObject->query($sql);
                if($res->fetchColumn() > 0) {
                        // do update
+                       error_log("doing userdata update");
                        $sql = "update users set users_tokendata='$data' where users_username='$username'";
                } else {
                        // do insert
-                       $sql = "insert into users values (NULL, '$username', '', '', '$data')";
+                       error_log("doing user data create");
+                       $sql = "insert into users values (NULL, '$username', '', '', '$data', '')";
                }
                
                if($dbObject->query($sql)) {
@@ -117,4 +147,4 @@ class gaasGA extends GoogleAuthenticator {
        }       
 }
 
-?>
\ No newline at end of file
+?>