// i need to figure out how i do data encapsulation here. We'll support mysql and sqlite3 off the bat if we can - sqlite3 comes first tho
function gwvp_dbCreateMysqlStructure()
{
-
+
}
function gwvp_dbCreateSQLiteStructure($dbloc)
"user_desc" TEXT,
"user_status" INTEGER
)';
-
+
$groupsql = '
CREATE TABLE groups (
"groups_id" INTEGER PRIMARY KEY AUTOINCREMENT,
"groups_is_admin" INTEGER,
"groups_owner_id" INTEGER
)';
-
+
$reposql = '
CREATE TABLE "repos" (
"repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
"repos_description" TEXT,
"repos_owner" INTEGER
)';
-
+
+ // this looks like null, <repoid>, <read|visible|write>, user:<uid>|group:<gid>|authed|anon
+ // where authed = any authenticated user, anon = any unauthed user
+ // read|visible|write = can clone from repo|can see repo exists but not clone from it|can push to repo
+ // TODO: is this sufficient? i have to think about it
+ $repoperms = '
+ CREATE TABLE "repoperms" (
+ "repoperms_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+ "repo_id" INTEGER,
+ "repoperms_type" TEXT,
+ "repoperms_ref" TEXT
+ )';
+
$configsql = '
CREATE TABLE "config" (
"config_name" TEXT,
"config_value" TEXT
)';
-
+
$groupmemsql = '
CREATE TABLE "group_membership" (
"groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
"groupmember_groupid" INTEGER,
"groupmember_userid" INTEGER
)';
-
+
try {
$DB_CONNECTION = new PDO("sqlite:$dbloc");
} catch(PDOException $exep) {
error_log("execpt on db open");
return false;
}
-
-
+
+
$DB_CONNECTION->query($usersql);
$DB_CONNECTION->query($groupsql);
$DB_CONNECTION->query($reposql);
{
// for sqlite, we just check if the db exists, for everyone else, we check for a conneciton and go yay or nay
global $WEB_ROOT_FS, $BASE_URL, $repo_base, $data_directory, $db_type, $db_url;
-
+
if($db_type == "sqlite") {
if(file_exists($db_url)) return true;
else return false;
}
-
+
// TODO now for the connectables
// gwvp_ConnectDB();
}
function gwvp_ConnectDB()
{
global $WEB_ROOT_FS, $BASE_URL, $repo_base, $data_directory, $db_type, $db_name, $DB_CONNECTION;
-
+
// first check if $DB_CONNECTION IS live
if($DB_CONNECTION != false) return $DB_CONNECTION;
-
+
if($db_type == "sqlite") {
$db_url = $db_name;
if(!file_exists($db_name)) {
error_log("$db_name does not exist - problem");
- }
+ }
}
-
+
// and here we go with pdo.
error_log("attmpting to open db, $db_type:$db_url");
try {
error_log("execpt on db open");
return false;
}
-
+
return $DB_CONNECTION;
}
function gwvp_createUser($email, $fullname, $password, $username, $desc, $status)
{
$conn = gwvp_ConnectDB();
-
+
// TODO: change from sha1
$shapass = sha1($password);
//error_log("Create user called with $email");
$conn->query("insert into users values (null, '$fullname', '$shapass', '$username', '$email', '$desc', '$status')");
/*
* "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
- "user_full_name" TEXT,
- "user_password" TEXT,
- "user_username" TEXT,
- "user_email" TEXT,
- "user_desc" TEXT,
- "user_status" INTEGER
+ "user_full_name" TEXT,
+ "user_password" TEXT,
+ "user_username" TEXT,
+ "user_email" TEXT,
+ "user_desc" TEXT,
+ "user_status" INTEGER
*/
}
function gwvp_getUser($username=null, $email=null, $id=null)
{
$conn = gwvp_ConnectDB();
-
+
if($username != null) {
$res = $conn->query("select * from users where user_username='$username'");
} else if($email != null) {
$returns["desc"] = $u_res["user_desc"];
$returns["status"] = $u_res["user_status"];
}
-
+
return $returns;
-
+
}
function gwvp_getUsers()
{
$conn = gwvp_ConnectDB();
-
+
$res = $conn->query("select * from users");
-
+
$returns = false;
$rn = 0;
foreach($res as $u_res) {
$returns[$rn]["status"] = $u_res["user_status"];
$rn++;
}
-
+
return $returns;
}
function gwvp_deleteUser($email)
{
$conn = gwvp_ConnectDB();
-
+
$sql = "delete from users where user_email='$email'";
-
+
$conn->query($sql);
}
function gwvp_createGroup($group_name, $is_admin, $owner_id)
{
$conn = gwvp_ConnectDB();
-
+
/*
* CREATE TABLE groups (
- "groups_id" INTEGER,
- "groups_name" TEXT,
- "groups_is_admin" INTEGER,
+ "groups_id" INTEGER,
+ "groups_name" TEXT,
+ "groups_is_admin" INTEGER,
"groups_owner_id" INTEGER
)';
$is_admin_t = 0;
}
$sql = "insert into groups values( null, '$group_name', '$is_admin_t', '$owner_id')";
-
+
$conn->query($sql);
-
+
}
function gwvp_deleteGroup($groupname)
{
$conn = gwvp_ConnectDB();
-
+
$sql = "delete from groups where groups_name='$groupname'";
-
+
$conn->query($sql);
}
$conn = gwvp_ConnectDB();
/*
- select g.groups_name from
- group_membership gm, groups g, users u
- where
- gm.groupmember_userid=u.users_id and
- u.user_email='$email' and
- gm.groupmember_groupid=g.groups_id and
- g.groups_name='$groupname'
- */
-
+ select g.groups_name from
+ group_membership gm, groups g, users u
+ where
+ gm.groupmember_userid=u.users_id and
+ u.user_email='$email' and
+ gm.groupmember_groupid=g.groups_id and
+ g.groups_name='$groupname'
+ */
+
$sql = "
select g.groups_name from
group_membership gm, groups g, users u
u.user_email='$email' and
gm.groupmember_groupid=g.groups_id
";
-
+
$res = $conn->query($sql);
-
+
$return = false;
$rn = 0;
foreach($res as $u_res) {
$return[$rn] = $u_res[0];
$rn++;
}
-
+
return $return;
}
$conn = gwvp_ConnectDB();
/*
- select g.groups_name from
- group_membership gm, groups g, users u
- where
- gm.groupmember_userid=u.users_id and
- u.user_email='$email' and
- gm.groupmember_groupid=g.groups_id and
- g.groups_name='$groupname'
- */
-
+ select g.groups_name from
+ group_membership gm, groups g, users u
+ where
+ gm.groupmember_userid=u.users_id and
+ u.user_email='$email' and
+ gm.groupmember_groupid=g.groups_id and
+ g.groups_name='$groupname'
+ */
+
$sql = "
select g.groups_name from
groups g, users u
u.user_email='$email' and
u.users_id=g.groups_owner_id
";
-
+
$res = $conn->query($sql);
-
+
$return = false;
$rn = 0;
foreach($res as $u_res) {
$return[$rn] = $u_res[0];
$rn++;
}
-
+
return $return;
-
+
}
function gwvp_groupOwner($groupname)
{
$conn = gwvp_ConnectDB();
-
+
$sql = "select u.user_email from users u, groups g where g.groups_name='$groupname' and g.groups_owner_id=u.users_id";
$res = $conn->query($sql);
$return = false;
foreach($res as $u_res) {
- $return = $u_res[0];
+ $return = $u_res[0];
}
-
+
return $return;
}
function gwvp_getGroups()
{
$conn = gwvp_ConnectDB();
-
+
$res = $conn->query("select * from groups");
-
+
$returns = false;
$rn = 0;
foreach($res as $u_res) {
$returns[$rn]["ownerid"] = $u_res["groups_owner_id"];
$rn++;
}
-
+
return $returns;
}
function gwvp_getGroupId($groupname)
{
$conn = gwvp_ConnectDB();
-
+
$sql = "select groups_id from groups where groups_name='$groupname'";
-
+
$res = $conn->query($sql);
$return = false;
foreach($res as $u_res) {
$return = $u_res["groups_id"];
}
-
+
return $return;
}
function gwvp_getUserId($useremail=null, $username = null)
{
$conn = gwvp_ConnectDB();
-
+
if($useremail != null) {
$sql = "select users_id from users where user_email='$useremail'";
} else if($username != null) {
$sql = "select users_id from users where user_username='$username'";
} else return false;
-
+
$res = $conn->query($sql);
$return = false;
foreach($res as $u_res) {
$return = $u_res["users_id"];
}
-
+
return $return;
}
function gwvp_getUserEmail($id)
{
$conn = gwvp_ConnectDB();
-
+
$sql = "select user_email from users where users_id='$id'";
-
+
$res = $conn->query($sql);
$return = false;
foreach($res as $u_res) {
$return = $u_res["user_email"];
}
-
+
return $return;
}
function gwvp_addGroupMember($email, $groupname)
{
$conn = gwvp_ConnectDB();
-
+
$uid = gwvp_getUserId($email);
$gid = gwvp_getGroupId($groupname);
-
+
/*
* CREATE TABLE "group_membership" (
- "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
- "groupmember_groupid" INTEGER,
- "groupmember_userid" INTEGER
+ "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+ "groupmember_groupid" INTEGER,
+ "groupmember_userid" INTEGER
*/
if($uid!=false&&$gid!=false) $sql = "insert into group_membership values (null, '$gid', '$uid')";
else return false;
-
+
$conn->query($sql);
-
+
return true;
}
function gwvp_IsGroupMember($email, $groupname)
{
$conn = gwvp_ConnectDB();
-
+
// i think this is right
$sql = "
select count(*) from
gm.groupmember_groupid=g.groups_id and
g.groups_name='$groupname'
";
-
+
$res = $conn->query($sql);
$result = 0;
foreach($res as $u_res) {
$result = $u_res[0];
}
-
+
if($result == 0) return false;
if($result == 1) return true;
}
function gwvp_IsUserAdmin($email=null, $username = null)
{
$conn = gwvp_ConnectDB();
-
-
+
+
// TODO: clean this up, this should be a single query - idiot
if($email != null) {
$id = gwvp_getUserId($email);
$id = gwvp_getUserId(null, $username);
$sql = "select groupmember_groupid from group_membership where groupmember_userid='$id'";
} else return false;
-
+
$res = $conn->query($sql);
$rn = 0;
foreach($res as $u_res) {
- $gid[$rn] = $u_res["groupmember_groupid"];
+ $gid[$rn] = $u_res["groupmember_groupid"];
$rn++;
}
-
+
foreach($gid as $gid_t) {
- /*
- * CREATE TABLE groups (
- "groups_id" INTEGER,
- "groups_name" TEXT,
- "groups_is_admin" INTEGER,
- "groups_owner_id" INTEGER
- )';
+ /*
+ * CREATE TABLE groups (
+ "groups_id" INTEGER,
+ "groups_name" TEXT,
+ "groups_is_admin" INTEGER,
+ "groups_owner_id" INTEGER
+ )';
+
+ */
- */
-
$sql = "select groups_is_admin from groups where groups_id='$gid_t'";
$res = $conn->query($sql);
foreach($res as $u_res) {
if($u_res["groups_is_admin"] == "1") return true;
- }
+ }
}
-
+
return false;
}
{
/*
* "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
- "user_full_name" TEXT,
- "user_password" TEXT,
- "user_username" TEXT,
- "user_email" TEXT,
- "user_desc" TEXT,
- "user_status" INTEGER
+ "user_full_name" TEXT,
+ "user_password" TEXT,
+ "user_username" TEXT,
+ "user_email" TEXT,
+ "user_desc" TEXT,
+ "user_status" INTEGER
*/
-
+
$conn = gwvp_ConnectDB();
-
+
if($email != null) {
$sql = "update users set user_email='$email' where users_id='$userid'";
$conn->query($sql);
}
-
+
if($fullname != null) {
$sql = "update users set user_full_name='$fullname' where users_id='$userid'";
$conn->query($sql);
}
-
+
if($password != null) {
$shapass = sha1($password);
$sql = "update users set user_password='$shapass' where users_id='$userid'";
$sql = "update users set user_status='$status' where users_id='$userid'";
$conn->query($sql);
}
-
+
return true;
}
{
/*
* CREATE TABLE groups (
- "groups_id" INTEGER,
- "groups_name" TEXT,
- "groups_is_admin" INTEGER,
+ "groups_id" INTEGER,
+ "groups_name" TEXT,
+ "groups_is_admin" INTEGER,
"groups_owner_id" INTEGER
)';
*/
$conn = gwvp_ConnectDB();
-
+
if($groupname != null) {
$sql = "update groups set groups_name='$groupname' where groups_id='$groupid'";
$conn->query($sql);
$sql = "update groups set groups_is_admin='$group_is_admin' where groups_id='$groupid'";
$conn->query($sql);
}
-
+
if($groups_owner_id != null) {
$sql = "update groups set groups_owner_id='$groups_owner_id' where groups_id='$groupid'";
$conn->query($sql);
}
-
+
return true;
}
+
+function gwvp_AddRepo($reponame, $repodesc, $repoowner)
+{
+ $conn = gwvp_ConnectDB();
+
+ $sql = "insert into repos values (null, '$reponame', '$repodesc', '$repoowner')";
+
+ $conn->query($sql);
+}
+
+function gwvp_GetRepoList()
+{
+ $conn = gwvp_ConnectDB();
+
+ /*
+ * $reposql = '
+ CREATE TABLE "repos" (
+ "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+ "repos_name" TEXT,
+ "repos_description" TEXT,
+ "repos_owner" INTEGER
+ )';
+
+ */
+
+ $sql = "select * from repos";
+
+ $res = $conn->query($sql);
+
+ $return = false;
+ $rn = 0;
+ foreach($res as $u_res) {
+ $return[$rn]["id"] = $u_res["repos_id"];
+ $return[$rn]["name"] = $u_res["repos_name"];
+ $return[$rn]["description"] = $u_res["repos_description"];
+ $return[$rn]["owner"] = $u_res["repos_owner"];
+ $rn++;
+ }
+
+ return $return;
+}
/* functions we'll need to access data:
- *
+ *
* getUsers(pattern)
* getUserData(username)
* getGroups(pattern)
* modifyGroup(...)
* addGroupMember(...)
* deleteGroupMember(...)
- *
+ *
* createUser(...)
* deleteUser(...)
* modifyUser(...)