Activity log is now working
[gwvp-mini.git] / gwvpmini / gwvpmini_db.php
index 972746e..82abfda 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;
                }
        }
@@ -55,6 +55,46 @@ function gwvpmini_getUser($username=null, $email=null, $id=null)
 \r
 }
 
+function gwvpmini_GetActivityLog($nentries = 20, $forid=-1)
+{
+       /*
+        *              "activity_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+               "activity_type" TEXT,
+               "activity_date" TEXT,
+               "activity_user" TEXT,
+               "activity_repo" TEXT,
+               "activity_commitid" TEXT,
+               "activity_commitlog" TEXT,
+               "activity_visibleto" TEXT
+
+        */
+       $conn = gwvpmini_ConnectDB();\r
+       
+       if($forid == -1) {
+               // we're searching for anonymous ones
+               $sql = "select * from activity where activity_visibleto='a' order by activity_date desc limit $nentries";
+       } else {
+               $sql = "select * from activity where activity_visibleto='a' or activity_visibleto='r' or activity_visibleto like '%:$forid:%' order by activity_date desc limit $nentries";
+       }
+       
+       $vls = $conn->query($sql);
+       
+       $nent = 0;
+       $ret = array();
+       if($vls !== false) foreach($vls as $vals) {
+               $ret[$nent]["type"] = $vals["activity_type"];
+               $ret[$nent]["date"] = $vals["activity_date"];
+               $ret[$nent]["userid"] = $vals["activity_user"];
+               $ret[$nent]["repoid"] = $vals["activity_repo"];
+               $ret[$nent]["commitid"] = $vals["activity_commitid"];
+               $ret[$nent]["commitlog"] = $vals["activity_commitlog"];
+               $nent++;
+       }
+       
+       if($nent == 0) return false;
+       else return $ret;
+}
+
 
 function gwvpmini_getRepo($ownerid=null, $name=null, $id=null)\r
 {\r
@@ -108,7 +148,7 @@ function gwvpmini_ChangeRepoPerm($rid, $uid, $acc)
        
        $res = $conn->query($sql);
        
-       error_log("CHANGEREPOPERMS: call with $rid, $uid, $acc");
+       // error_log("CHANGEREPOPERMS: call with $rid, $uid, $acc");
        
        $cperms_t = "";
        foreach($res as $row) {
@@ -125,12 +165,12 @@ function gwvpmini_ChangeRepoPerm($rid, $uid, $acc)
                        $permsarray = unserialize(base64_decode($cperms_t));
                        $permsarray[$uid] = $acc;
                        if($acc == 0) {
-                               error_log("PERMSUPDATE: REMOVE $uid");
+                               // error_log("PERMSUPDATE: REMOVE $uid");
                                unset($permsarray[$uid]);
                        }
                }
        } else {
-               error_log("CHANGEREPOPERMS for b of $acc");
+               // error_log("CHANGEREPOPERMS for b of $acc");
                $permsarray["b"] = $acc;
        }
        
@@ -138,7 +178,7 @@ function gwvpmini_ChangeRepoPerm($rid, $uid, $acc)
        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");
+                               // error_log("CHANGEREPOPERMS removed $key $val for base perm change");
                                unset($permsarray[$key]);
                        }
                }
@@ -159,7 +199,7 @@ function gwvpmini_ChangeRepoPerm($rid, $uid, $acc)
        
        $sql = "update repos set repos_perms='$encperms' where repos_id='$rid'";\r
 
-       error_log("PERMSARRAYNOW $sql ".print_r($permsarray,true));\r
+       // error_log("PERMSARRAYNOW $sql ".print_r($permsarray,true));\r
        
        $conn->query($sql);
        
@@ -203,7 +243,7 @@ function gwvpmini_GetRepoPerm($rid, $uid)
        \r
        if($cperms_t === false) return 0;
        
-       error_log("PERMSCHECK $rid, $uid:".print_r($dets, true));\r
+       // error_log("PERMSCHECK $rid, $uid:".print_r($dets, true));\r
        \r
        if($dets === false) return 0;
        
@@ -211,7 +251,7 @@ function gwvpmini_GetRepoPerm($rid, $uid)
        \r
        $permsarray = unserialize(base64_decode($cperms_t));
        
-       error_log("PERMSARRAY: ".print_r($permsarray,true));
+       // error_log("PERMSARRAY: ".print_r($permsarray,true));
        
        
        $perm = 0;
@@ -252,6 +292,50 @@ function gwvpmini_RemoveUser($uid)
        return $conn->query($sql);
 }
 
+function gwvpmini_UpdateUserEmail($uid, $email)
+{
+       $conn = gwvpmini_ConnectDB();\r
+       \r
+       if($uid < 0) return;\r
+       \r
+       $sql = "update users set user_email='$email' where user_id='$uid'";\r
+       \r
+       return $conn->query($sql);
+}
+
+function gwvpmini_UpdateUserDesc($uid, $desc)\r
+{\r
+       $conn = gwvpmini_ConnectDB();\r
+\r
+       if($uid < 0) return;\r
+\r
+       $sql = "update users set user_desc='$desc' where user_id='$uid'";\r
+\r
+       return $conn->query($sql);\r
+}\r
+
+function gwvpmini_SetUserStatusAdmin($uid, $adminstat)
+{
+       $conn = gwvpmini_ConnectDB();\r
+       \r
+       if($uid < 0) return;\r
+       \r
+       $sql = "update users set user_level='$adminstat' where user_id='$uid'";\r
+       \r
+       return $conn->query($sql);
+}
+
+function gwvpmini_UpdateUserPassword($uid, $pass)
+{
+       $conn = gwvpmini_ConnectDB();
+
+       if($uid < 0) return;
+
+       $sql = "update users set user_password='".sha1($pass)."' where user_id='$uid'";
+
+       return $conn->query($sql);
+}
+
 function gwvpmini_DisableUser($uid)\r
 {\r
        $conn = gwvpmini_ConnectDB();\r
@@ -315,14 +399,15 @@ function gwvpmini_ConnectDB()
        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");
+       $db_url = false;\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
@@ -330,11 +415,11 @@ 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
-               error_log("execpt on db open");\r
+               // error_log("execpt on db open");\r
                return false;\r
        }\r
 \r
@@ -386,6 +471,35 @@ function gwvpmini_AddUser($username, $password, $fullname, $email, $desc, $level
        return $retval;
 }\r
 
+function gwvpmini_AddActivityLog($type, $userid, $repoid, $commitid, $commitlog, $visibleto="a")
+{
+       /*
+        *      CREATE TABLE "activity" (
+               "activity_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+               "activity_type" TEXT,
+               "activity_date" TEXT,
+               "activity_user" TEXT,
+               "activity_repo" TEXT,
+               "activity_commitid" TEXT,
+               "activity_commitlog" TEXT,
+               "activity_visibleto" TEXT
+       )';
+
+        */
+       
+       $conn = gwvpmini_ConnectDB();
+       
+       // TODO: implement visibility
+       //$visibleto = "a";\r
+       \r
+       $sql = "insert into 'activity' values ( null, '$type', '".time()."', '$userid', '$repoid', '$commitid', '$commitlog', '$visibleto')";
+       
+       // error_log("SQL IS $sql");\r
+       \r
+       $res = $conn->query($sql);\r
+       if(!$res) return -1;
+       return true;
+}
 
 function gwvpmini_dbCreateSQLiteStructure($dbloc)\r
 {\r
@@ -436,11 +550,14 @@ function gwvpmini_dbCreateSQLiteStructure($dbloc)
        // "a" for everyone
        $activitysql = '
        CREATE TABLE "activity" (
+               "activity_id" INTEGER PRIMARY KEY AUTOINCREMENT,
                "activity_type" TEXT,
                "activity_date" TEXT,
-               "activity_desc" TEXT,
-               "activity_link" TEXT,
-               "activity_viewby" TEXT
+               "activity_user" TEXT,
+               "activity_repo" TEXT,
+               "activity_commitid" TEXT,
+               "activity_commitlog" TEXT,
+               "activity_visibleto" TEXT
        )';\r
 \r
        $configsql = '\r
@@ -452,7 +569,7 @@ function gwvpmini_dbCreateSQLiteStructure($dbloc)
        try {\r
                $DB_CONNECTION = new PDO("sqlite:$dbloc");\r
        } catch(PDOException $exep) {\r
-               error_log("execpt on db open");\r
+               // error_log("execpt on db open");\r
                return false;\r
        }\r
 
@@ -553,7 +670,7 @@ function gwvpmini_GetRepoDescFromName($reponame)
        $conn = gwvpmini_ConnectDB();\r
 \r
        $sql = "select repos_description from repos where repos_name='$reponame'";
-       error_log("desc for name sql: $sql");\r
+       // error_log("desc for name sql: $sql");\r
 \r
        $res = $conn->query($sql);\r
 \r
@@ -600,7 +717,7 @@ function gwvpmini_GetRepoOwnerDetailsFromName($reponame)
        if(!$res) return -1;\r
        foreach($res as $row) {
                $retval = array();
-               error_log("STUFF2: ".print_r($row,true));\r
+               // error_log("STUFF2: ".print_r($row,true));\r
                $retval["id"] = $row["user_id"];
                $retval["fullname"] = $row["user_full_name"];
                $retval["username"] = $row["user_username"];
@@ -638,7 +755,7 @@ function gwvpmini_setConfigVal($confname, $confval)
 function gwvpmini_AddRepo($name, $desc, $ownerid)
 {
        
-       error_log("addrepo in db for $name, $desc, $ownerid");
+       // error_log("addrepo in db for $name, $desc, $ownerid");
        $conn = gwvpmini_ConnectDB();\r
        
        $perms["b"] = "a";
@@ -656,7 +773,7 @@ function gwvpmini_GetUserId($username)
        
        $sql = "select user_id from users where user_username='$username'";
 
-       error_log("userid sql $sql");\r
+       // error_log("userid sql $sql");\r
        
        $res = $conn->query($sql);
        
@@ -674,7 +791,7 @@ function gwvpmini_GetUserNameFromEmail($email)
 \r
        $sql = "select user_username from users where user_email='$email'";\r
 \r
-       error_log("username sql $sql");\r
+       // error_log("username sql $sql");\r
 \r
        $res = $conn->query($sql);\r
 \r
@@ -702,7 +819,7 @@ function gwvpmini_GetOwnedRepos($username)
        
        $uid = gwvpmini_GetUserId($username);
        $sql = "select * from repos where repos_owner='$uid'";
-       error_log("owned repos sql $sql for username $username");
+       // error_log("owned repos sql $sql for username $username");
        $res = $conn->query($sql);
        if($username == "") return false;\r
        
@@ -713,10 +830,10 @@ function gwvpmini_GetOwnedRepos($username)
                $retval[$id]["desc"] = $row["repos_description"];
                $retval[$id]["id"] = $row["repos_id"];
                $retval[$id]["status"] = $row["repos_status"];
-               error_log(print_r($row, true));
+               // error_log(print_r($row, true));
        }
        
-       error_log(print_r($retval, true));\r
+       // error_log(print_r($retval, true));\r
        return $retval;\r
 }
 
@@ -744,7 +861,7 @@ function gwvpmini_GetContributedRepos($username)
        $i = 0;\r
        foreach($res as $row) {
                $perms = unserialize(base64_decode($row["repos_perms"]));
-               error_log("CONTRIB: $uid for ".$row["repos_id"]." - ".print_r($perms,true));
+               // error_log("CONTRIB: $uid for ".$row["repos_id"]." - ".print_r($perms,true));
                if(isset($perms["$uid"])) if($perms["$uid"] > 1) {
                        $rids[$i]["id"] = $row["repos_id"];
                        $rids[$i]["desc"] = $row["repos_description"];
@@ -757,11 +874,11 @@ function gwvpmini_GetContributedRepos($username)
        $retval = $rids;
        
        if($i == 0) {
-               error_log("CONTRIBREPOS: no repos found?");
+               // error_log("CONTRIBREPOS: no repos found?");
                return false;
        }\r
 \r
-       error_log("CONTRIBREPOS: ".print_r($retval, true));\r
+       // error_log("CONTRIBREPOS: ".print_r($retval, true));\r
        return $retval;\r
 }
 
@@ -827,10 +944,10 @@ function gwvpmini_findPeopleLike($search)
        $res = $conn->query($sql);
        
        if(!$res) {
-               error_log("SERACHUSER: $sql returned false");
+               // error_log("SERACHUSER: $sql returned false");
                return false;
        } else {
-               error_log("SERACHUSER: $sql returned true");
+               // error_log("SERACHUSER: $sql returned true");
        }\r
        \r
        $retval = false;\r
@@ -845,7 +962,7 @@ function gwvpmini_findPeopleLike($search)
                $retval[$id]["id"] = $row["user_id"];\r
        }\r
        
-       error_log("SEARCHUSER: array is ".print_r($retval, true));\r
+       // error_log("SEARCHUSER: array is ".print_r($retval, true));\r
        return $retval;
 }