user admin/status change management
[gwvp-mini.git] / gwvpmini / gwvpmini_db.php
index b74a334..bb679b8 100644 (file)
@@ -252,6 +252,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
@@ -261,6 +305,19 @@ function gwvpmini_DisableUser($uid)
        $sql = "update users set user_status=1 where user_id='$uid'";\r
 \r
        return $conn->query($sql);\r
+}
+
+function gwvpmini_UpdateRepoDescription($rid, $desc)
+{
+       $conn = gwvpmini_ConnectDB();\r
+       
+       // remove any tag elements
+       $newdesc_r = preg_replace("/[\<\>\;]+/", "", $desc);
+       $newdesc = preg_replace("/\n/", "<br>", $newdesc_r);
+       
+       $sql = "update repos set repos_description='$newdesc' where repos_id='$rid'";
+       
+       return $conn->query($sql);
 }\r
 \r
 function gwvpmini_EnableUser($uid)\r
@@ -302,7 +359,8 @@ 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
@@ -317,7 +375,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
@@ -373,6 +431,59 @@ function gwvpmini_AddUser($username, $password, $fullname, $email, $desc, $level
        return $retval;
 }\r
 
+function gwvpmini_GetActivityLog($nentries = 100)
+{
+       $conn = gwvpmini_ConnectDB();\r
+       
+       $sql = "select * from activity order by activity_date desc limit 100";
+
+       $res = $conn->query($sql);
+       
+       $logs = null;
+       $i = 0;
+       foreach($res as $row) {
+               $logs[$i]["type"] = $row["activity_type"];
+               $logs[$i]["date"] = $row["activity_date"];
+               $logs[$i]["userid"] = $row["activity_user"];
+               $logs[$i]["repoid"] = $row["activity_repo"];
+               $logs[$i]["commitid"] = $row["activity_commitid"];
+               $logs[$i]["commitlog"] = $row["activity_commitlog"];
+               $logs[$i]["visibleto"] = $row["activity_visibleto"];
+               $i++;
+       }
+       
+       return $logs;
+}
+
+function gwvpmini_AddActivityLog($type, $userid, $repoid, $commitid, $commitlog)
+{
+       /*
+        *      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
@@ -411,7 +522,27 @@ function gwvpmini_dbCreateSQLiteStructure($dbloc)
        // this looks like null, <repoid>, <read|visible|write>, user:<uid>|group:<gid>|authed|anon\r
        // where authed = any authenticated user, anon = everyone (logged in, not logged in, etc)\r
        // read|visible|write = can clone from repo|can see repo exists and see description but not clone from it|can push to repo\r
-       // TODO: is this sufficient? i have to think about it\r
+       // TODO: is this sufficient? i have to think about it
+       
+       
+       // type for now will only be "git"
+       // date is obvious
+       // desc is a desction of the activity
+       // link is of the form "repo,reponame,repoid:user,username,useremail,userid" so it
+       // can be attached simply
+       // viewby is a simple list of uid's that should be able to see it... "r" for all registered
+       // "a" for everyone
+       $activitysql = '
+       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
+       )';\r
 \r
        $configsql = '\r
        CREATE TABLE "config" (\r
@@ -430,7 +561,8 @@ function gwvpmini_dbCreateSQLiteStructure($dbloc)
        $DB_CONNECTION->query($initialuser_admin);
        $DB_CONNECTION->query($initialuser_user);
        $DB_CONNECTION->query($reposql);\r
-       $DB_CONNECTION->query($configsql);\r
+       $DB_CONNECTION->query($configsql);
+       $DB_CONNECTION->query($activitysql);\r
 }
 
 function gwvpmini_getConfigVal($confname)\r
@@ -654,6 +786,7 @@ function gwvpmini_GetUserNameFromEmail($email)
 \r
        return $retval;\r
 }
+
 function gwvpmini_GetOwnedRepos($username)\r
 {
        /*
@@ -680,6 +813,7 @@ function gwvpmini_GetOwnedRepos($username)
                $retval[$id]["name"] = $row["repos_name"];
                $retval[$id]["desc"] = $row["repos_description"];
                $retval[$id]["id"] = $row["repos_id"];
+               $retval[$id]["status"] = $row["repos_status"];
                error_log(print_r($row, true));
        }
        
@@ -687,6 +821,51 @@ function gwvpmini_GetOwnedRepos($username)
        return $retval;\r
 }
 
+function gwvpmini_GetContributedRepos($username)\r
+{\r
+       /*\r
+        *      CREATE TABLE "repos" (\r
+                       "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,\r
+                       "repos_name" TEXT,\r
+                       "repos_description" TEXT,\r
+                       "repos_owner" INTEGER\r
+        )';\r
+\r
+       */\r
+       $conn = gwvpmini_ConnectDB();\r
+\r
+\r
+       $uid = gwvpmini_GetUserId($username);\r
+       $sql = "select repos_id,repos_description,repos_perms,repos_owner,repos_status,repos_name from repos";\r
+       $res = $conn->query($sql);\r
+       if($username == "") return false;\r
+\r
+       $retval = false;
+       $rids = null;
+       $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));
+               if(isset($perms["$uid"])) if($perms["$uid"] > 1) {
+                       $rids[$i]["id"] = $row["repos_id"];
+                       $rids[$i]["desc"] = $row["repos_description"];
+                       $rids[$i]["owner"] = $row["repos_owner"];
+                       $rids[$i]["status"] = $row["repos_status"];
+                       $rids[$i]["name"] = $row["repos_name"];
+                       $i++;
+               }
+       }
+       $retval = $rids;
+       
+       if($i == 0) {
+               error_log("CONTRIBREPOS: no repos found?");
+               return false;
+       }\r
+\r
+       error_log("CONTRIBREPOS: ".print_r($retval, true));\r
+       return $retval;\r
+}
+
 function gwvpmini_userLevel($id)
 {
        $conn = gwvpmini_ConnectDB();