some auth debugging nonsense
[gwvp-mini.git] / gwvpmini / gwvpmini_db.php
index 1995e7d..b74a334 100644 (file)
@@ -6,7 +6,7 @@ $DB_CONNECTION = false;
 \r
 \r
 global $db_url, $db_type;\r
-error_log("in include for database, $db_type, $db_name");
+//error_log("in include for database, $db_type, $db_name");
 
 
 function gwvpmini_DBExists()
@@ -14,15 +14,15 @@ function gwvpmini_DBExists()
        global $WEB_ROOT_FS, $BASE_URL, $data_directory, $db_type, $db_name;
        
        // oh this isnt working. poo.
-       error_log("checking for $db_name, $db_type");
+       //error_log("checking for $db_name, $db_type");
        
        if($db_type == "sqlite") {
                if(file_exists($db_name)) {
-                       error_log("Exists");
+                       //error_log("Exists");
                        return true;
                }
                else {
-                       error_log("no exists");
+                       //error_log("no exists");
                        return false;
                }
        }
@@ -87,14 +87,148 @@ function gwvpmini_getRepo($ownerid=null, $name=null, $id=null)
                $returns["id"] = $u_res["repos_id"];\r
                $returns["name"] = $u_res["repos_name"];\r
                $returns["desc"] = $u_res["repos_description"];\r
-               $returns["ownerid"] = $u_res["repos_owner"];\r
-               $returns["perms"] = $u_res["repos_readperms"];\r
+               $returns["ownerid"] = $u_res["repos_owner"];
+               $returns["status"] = $u_res["repos_status"];\r
        }\r
 \r
        return $returns;\r
 \r
 }
 
+// $rid = repo id
+// $uid = user id (a for "anyone", r for "registered")
+// $acc = 0 or 1, 0 = no access, 1 = read access, 2 = write
+// first part of ths is the "base" repo permissions
+// this is spec'd as b:t where t = a (anyone can read), r (only registered can read) or x (explicit read perms)
+function gwvpmini_ChangeRepoPerm($rid, $uid, $acc)
+{
+       $conn = gwvpmini_ConnectDB();
+       
+       $sql = "select repos_perms from repos where repos_id='$rid'";
+       
+       $res = $conn->query($sql);
+       
+       error_log("CHANGEREPOPERMS: call with $rid, $uid, $acc");
+       
+       $cperms_t = "";
+       foreach($res as $row) {
+               $cperms_t = $row[0];
+       }
+       
+       if($cperms_t === false) return false;
+       
+       $permsarray = array();
+       if($uid != "b") {
+               if($cperms_t == "") {
+                       $permsarray[$uid] = $acc;
+               } else {
+                       $permsarray = unserialize(base64_decode($cperms_t));
+                       $permsarray[$uid] = $acc;
+                       if($acc == 0) {
+                               error_log("PERMSUPDATE: REMOVE $uid");
+                               unset($permsarray[$uid]);
+                       }
+               }
+       } else {
+               error_log("CHANGEREPOPERMS for b of $acc");
+               $permsarray["b"] = $acc;
+       }
+       
+       // check if base is now r or a, we can drop any 1's
+       if($permsarray["b"] == "a" || $permsarray["b"] == "r") {
+               foreach($permsarray as $key => $val) {
+                       if($val == 1) {
+                               error_log("CHANGEREPOPERMS removed $key $val for base perm change");
+                               unset($permsarray[$key]);
+                       }
+               }
+       }
+       
+       if(is_array($permsarray)) {
+               if(!isset($permsarray["b"])) {
+                       // something went wrong, repalce b bit
+                       $permsarray["b"] = "a";
+               }
+       } else {
+               // something went even wronger
+               $permsarray["b"] = "a";
+       }
+       
+
+       $encperms = base64_encode(serialize($permsarray));
+       
+       $sql = "update repos set repos_perms='$encperms' where repos_id='$rid'";\r
+
+       error_log("PERMSARRAYNOW $sql ".print_r($permsarray,true));\r
+       
+       $conn->query($sql);
+       
+}
+
+function gwvpmini_GetRepoPerms($rid)
+{
+       $conn = gwvpmini_ConnectDB();\r
+       \r
+       $sql = "select repos_perms from repos where repos_id='$rid'";\r
+       \r
+       $res = $conn->query($sql);\r
+       \r
+       $cperms_t = false;\r
+       if($res !== false) foreach($res as $row) {\r
+               $cperms_t = $row[0];\r
+       }\r
+       \r
+       if($cperms_t === false) return false;\r
+       \r
+       $permsarray = unserialize(base64_decode($cperms_t));
+       
+       return $permsarray;
+}
+
+//returns 0 for none, 1 for read, 2 for writes
+function gwvpmini_GetRepoPerm($rid, $uid)
+{
+       $conn = gwvpmini_ConnectDB();\r
+       \r
+       $dets = gwvpmini_getRepo(null, null, $rid);
+       
+       $sql = "select repos_perms from repos where repos_id='$rid'";\r
+       \r
+       $res = $conn->query($sql);
+       
+       $cperms_t = false;\r
+       if($res !== false) foreach($res as $row) {\r
+               $cperms_t = $row[0];\r
+       }\r
+       \r
+       if($cperms_t === false) return 0;
+       
+       error_log("PERMSCHECK $rid, $uid:".print_r($dets, true));\r
+       \r
+       if($dets === false) return 0;
+       
+       if($dets["ownerid"] == $uid) return 2;\r
+       \r
+       $permsarray = unserialize(base64_decode($cperms_t));
+       
+       error_log("PERMSARRAY: ".print_r($permsarray,true));
+       
+       
+       $perm = 0;
+       if($uid != "a") {
+               if(isset($permsarray[$uid])) {
+                       $perm = $permsarray[$uid];
+               } else if($permsarray["b"] == "a" ||$permsarray["b"] == "r") {
+                       $perm = 1;
+               }
+       } else {
+               if($permsarray["b"] == "a") $perm = 1;
+       }
+       
+       return $perm;\r
+
+}
+
 function gwvpmini_RemoveRepoDB($id)
 {
        $conn = gwvpmini_ConnectDB();\r
@@ -118,17 +252,17 @@ function gwvpmini_RemoveUser($uid)
        return $conn->query($sql);
 }
 
-function gwvpmini_DisableUser($uid)
-{
+function gwvpmini_DisableUser($uid)\r
+{\r
        $conn = gwvpmini_ConnectDB();\r
-       \r
+\r
        if($uid < 0) return;\r
-       \r
+\r
        $sql = "update users set user_status=1 where user_id='$uid'";\r
-       \r
-       return $conn->query($sql);
+\r
+       return $conn->query($sql);\r
 }\r
-
+\r
 function gwvpmini_EnableUser($uid)\r
 {\r
        $conn = gwvpmini_ConnectDB();\r
@@ -140,20 +274,42 @@ function gwvpmini_EnableUser($uid)
        return $conn->query($sql);\r
 }\r
 \r
+function gwvpmini_DisableRepo($rid)
+{
+       $conn = gwvpmini_ConnectDB();\r
+       \r
+       if($rid < 0) return;\r
+       \r
+       $sql = "update repos set repos_status=1 where repos_id='$rid'";\r
+       \r
+       return $conn->query($sql);
+}\r
+
+function gwvpmini_EnableRepo($rid)\r
+{\r
+       $conn = gwvpmini_ConnectDB();\r
+\r
+       if($rid < 0) return;
+       
+       $sql = "update repos set repos_status=0 where repos_id='$rid'";
+       \r
+       return $conn->query($sql);\r
+}\r
+\r
 
 function gwvpmini_ConnectDB()\r
 {\r
        global $WEB_ROOT_FS, $BASE_URL, $data_directory, $db_type, $db_name, $DB_CONNECTION;\r
 \r
        // first check if $DB_CONNECTION IS live\r
-       error_log("in connection $db_type, $db_name");\r
+       //error_log("in connection $db_type, $db_name");\r
 \r
        if($DB_CONNECTION != false) return $DB_CONNECTION;\r
 \r
        if($db_type == "sqlite") {\r
                $db_url = $db_name;\r
                if(!file_exists($db_name)) {\r
-                       error_log("$db_name does not exist - problem");
+                       //error_log("$db_name does not exist - problem");
                        // TODO: NEED A SETUP AGENT!
                        gwvpmini_dbCreateSQLiteStructure($db_name);
                        gwvpmini_setConfigVal("repodir", "$data_directory/repos");\r
@@ -161,7 +317,7 @@ function gwvpmini_ConnectDB()
        }\r
 \r
        // and here we go with pdo.\r
-       error_log("attmpting to open db, $db_type:$db_url");\r
+       //error_log("attmpting to open db, $db_type:$db_url");\r
        try {\r
                $DB_CONNECTION = new PDO("$db_type:$db_url");\r
        } catch(PDOException $exep) {\r
@@ -247,7 +403,8 @@ function gwvpmini_dbCreateSQLiteStructure($dbloc)
        "repos_name" TEXT,\r
        "repos_description" TEXT,\r
        "repos_owner" INTEGER,
-       "repos_readperms" TEXT,
+       "repos_perms" TEXT,
+       "repos_status" TEXT,
        UNIQUE(repos_name)\r
        )';\r
 \r
@@ -342,7 +499,7 @@ function gwvpmini_GetRepoId($reponame)
        $retval = -1;
        if(!$res) return -1;
        foreach($res as $row) {
-               $reval = (int)$row[0];
+               $retval = (int)$row[0];
        }
        
        return $retval;
@@ -447,13 +604,17 @@ function gwvpmini_setConfigVal($confname, $confval)
        return $conn->query($sql);\r
 }
 
-function gwvpmini_AddRepo($name, $desc, $ownerid, $perms = "perms-public")
+function gwvpmini_AddRepo($name, $desc, $ownerid)
 {
        
        error_log("addrepo in db for $name, $desc, $ownerid");
        $conn = gwvpmini_ConnectDB();\r
+       
+       $perms["b"] = "a";
+       
+       $encperms = base64_encode(serialize($perms));
        \r
-       $sql = "insert into repos values (null, '$name', '$desc', '$ownerid', '$perms')";\r
+       $sql = "insert into repos values (null, '$name', '$desc', '$ownerid', '$encperms', 0)";\r
        \r
        $conn->query($sql);\r
 }
@@ -579,7 +740,7 @@ function gwvpmini_GetUsers($startat = 0, $num = 10)
        return $retval;
 }\r
 
-function gwvp_findPeopleLike($search)\r
+function gwvpmini_findPeopleLike($search)\r
 {\r
        $conn = gwvpmini_ConnectDB();
        
@@ -635,6 +796,7 @@ function gwvpmini_GetRepos($startat=0, $num=200)
                $retval[$id]["desc"] = $row["repos_description"];\r
                $retval[$id]["owner"] = $row["repos_owner"];\r
                $retval[$id]["id"] = $row["repos_id"];
+               $retval[$id]["status"] = $row["repos_status"];          
        }\r
        \r
        return $retval;\r
@@ -642,7 +804,7 @@ function gwvpmini_GetRepos($startat=0, $num=200)
        \r
 }\r
 
-function gwvp_findReposLike($search)\r
+function gwvpmini_findReposLike($search)\r
 {\r
        $conn = gwvpmini_ConnectDB();
        
@@ -669,6 +831,7 @@ function gwvp_findReposLike($search)
                $retval[$id]["desc"] = $row["repos_description"];
                $retval[$id]["owner"] = $row["repos_owner"];
                $retval[$id]["id"] = $row["repos_id"];
+               $retval[$id]["status"] = $row["repos_status"];
        }
        
        return $retval;