\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()
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;
}
}
} else if($email != null) {\r
$res = $conn->query("select * from users where user_email='$email'");\r
} else if($id != null) {\r
- $res = $conn->query("select * from users where users_id='$id'");\r
+ $res = $conn->query("select * from users where user_id='$id'");\r
} else return false;\r
\r
$returns = false;\r
\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
+ $conn = gwvpmini_ConnectDB();
+
+ /*
+ * $reposql = '
+ CREATE TABLE "repos" (
+ "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+ "repos_name" TEXT,
+ "repos_description" TEXT,
+ "repos_owner" INTEGER,
+ "repos_readperms" TEXT,
+ UNIQUE(repos_name)
+ )';
+
+ */\r
+\r
+ if($ownerid != null) {\r
+ $res = $conn->query("select * from repos where repos_owner='$ownerid'");\r
+ } else if($name != null) {\r
+ $res = $conn->query("select * from repos where repos_name='$name'");\r
+ } else if($id != null) {\r
+ $res = $conn->query("select * from repos where repos_id='$id'");\r
+ } else return false;\r
+\r
+ $returns = false;
+ if(!$res) return false;\r
+ foreach($res as $u_res) {\r
+ $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"];
+ $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: with call for $rid and user $uid ".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
+ \r
+ if($id < 0) return;\r
+ \r
+ $sql = "delete from repos where repos_id='$id'";\r
+ \r
+ return $conn->query($sql);\r
+
+}
+
function gwvpmini_RemoveUser($uid)
{
$conn = gwvpmini_ConnectDB();\r
$sql = "delete from users where user_id='$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
+\r
+ if($uid < 0) return;\r
+\r
+ $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
+{\r
+ $conn = gwvpmini_ConnectDB();\r
+\r
+ if($uid < 0) return;\r
+\r
+ $sql = "update users set user_status=0 where user_id='$uid'";\r
+\r
+ 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");
+ $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
}\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
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
"repos_name" TEXT,\r
"repos_description" TEXT,\r
"repos_owner" INTEGER,
- "repos_readperms" TEXT,
+ "repos_perms" TEXT,
+ "repos_status" TEXT,
+ "repos_origin" TEXT,
UNIQUE(repos_name)\r
)';\r
\r
// 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
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
$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
$retval = -1;
if(!$res) return -1;
foreach($res as $row) {
- $reval = (int)$row[0];
+ $retval = (int)$row[0];
}
return $retval;
$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
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"];
return $conn->query($sql);\r
}
-function gwvpmini_AddRepo($name, $desc, $ownerid, $perms = "perms-public")
+function gwvpmini_AddRepo($name, $desc, $ownerid, $defperms, $clonefrom)
{
- error_log("addrepo in db for $name, $desc, $ownerid");
+ // error_log("addrepo in db for $name, $desc, $ownerid");
$conn = gwvpmini_ConnectDB();\r
+
+ $perms["b"] = $defperms;
+
+ $encperms = base64_encode(serialize($perms));
+
+ if($clonefrom === false) $clonefrom = "";
\r
- $sql = "insert into repos values (null, '$name', '$desc', '$ownerid', '$perms')";\r
+ $sql = "insert into repos values (null, '$name', '$desc', '$ownerid', '$encperms', 0, '$clonefrom')";\r
\r
$conn->query($sql);\r
}
$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);
\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
\r
return $retval;\r
}
+
function gwvpmini_GetOwnedRepos($username)\r
{
/*
*/
$conn = gwvpmini_ConnectDB();
+
$uid = gwvpmini_GetUserId($username);
$sql = "select * from repos where repos_owner='$uid'";
- error_log("owned repos sql $sql");
+ // error_log("owned repos sql $sql for username $username");
$res = $conn->query($sql);
+ if($username == "") return false;\r
$retval = false;
foreach($res as $row) {
$id = $row["repos_id"];
$retval[$id]["name"] = $row["repos_name"];
$retval[$id]["desc"] = $row["repos_description"];
- error_log(print_r($row, true));
+ $retval[$id]["id"] = $row["repos_id"];
+ $retval[$id]["status"] = $row["repos_status"];
+ // error_log(print_r($row, true));
+ }
+
+ // error_log(print_r($retval, true));\r
+ 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;
- error_log(print_r($retval, true));\r
+ if($i == 0) {
+ // error_log("CONTRIBREPOS: no repos found?");
+ return false;
+ }\r
+\r
+ // error_log("CONTRIBREPOS: ".print_r($retval, true));\r
return $retval;\r
}
$retval[$id]["desc"] = $row["user_desc"];
$retval[$id]["level"] = $row["user_level"];
$retval[$id]["status"] = $row["user_status"];
+ $retval[$id]["id"] = $row["user_id"];
}
return $retval;
}\r
+function gwvpmini_findPeopleLike($search)\r
+{\r
+ $conn = gwvpmini_ConnectDB();
+
+ $sql = "select * from users where user_username like '%$search%' or user_full_name like '%$search%'";\r
+ \r
+ $res = $conn->query($sql);
+
+ if(!$res) {
+ // error_log("SERACHUSER: $sql returned false");
+ return false;
+ } else {
+ // error_log("SERACHUSER: $sql returned true");
+ }\r
+ \r
+ $retval = false;\r
+ foreach($res as $row) {\r
+ $id = $row["user_id"];\r
+ $retval[$id]["fullname"] = $row["user_full_name"];\r
+ $retval[$id]["username"] = $row["user_username"];\r
+ $retval[$id]["email"] = $row["user_email"];\r
+ $retval[$id]["desc"] = $row["user_desc"];\r
+ $retval[$id]["level"] = $row["user_level"];\r
+ $retval[$id]["status"] = $row["user_status"];\r
+ $retval[$id]["id"] = $row["user_id"];\r
+ }\r
+
+ // error_log("SEARCHUSER: array is ".print_r($retval, true));\r
+ return $retval;
+}
+
function gwvpmini_GetRepos($startat=0, $num=200)\r
{\r
$conn = gwvpmini_ConnectDB();\r
$retval[$id]["name"] = $row["repos_name"];\r
$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
\r
}\r
+function gwvpmini_findReposLike($search)\r
+{\r
+ $conn = gwvpmini_ConnectDB();
+
+ /*
+ * CREATE TABLE "repos" (
+ "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+ "repos_name" TEXT,
+ "repos_description" TEXT,
+ "repos_owner" INTEGER
+ )';
+
+ */
+
+ $sql = "select * from repos where repos_name like '%$search%' or repos_description like '%$search%'";
+
+ $res = $conn->query($sql);
+
+ if(!$res) return false;
+
+ $retval = false;
+ foreach($res as $row) {
+ $id = $row["repos_id"];
+ $retval[$id]["name"] = $row["repos_name"];
+ $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;
+ \r
+}\r
+\r
function gwvpmini_GetNRepos()
{
$conn = gwvpmini_ConnectDB();