4 $DB_CONNECTION = false;
6 // 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
7 function gwvp_dbCreateMysqlStructure()
12 function gwvp_dbCreateSQLiteStructure($dbloc)
15 CREATE TABLE "users" (
16 "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
17 "user_full_name" TEXT,
27 "groups_id" INTEGER PRIMARY KEY AUTOINCREMENT,
29 "groups_description" TEXT,
30 "groups_is_admin" INTEGER,
31 "groups_owner_id" INTEGER
35 CREATE TABLE "repos" (
36 "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
38 "repos_description" TEXT,
42 // this looks like null, <repoid>, <read|visible|write>, user:<uid>|group:<gid>|authed|anon
43 // where authed = any authenticated user, anon = everyone (logged in, not logged in, etc)
44 // read|visible|write = can clone from repo|can see repo exists and see description but not clone from it|can push to repo
45 // TODO: is this sufficient? i have to think about it
47 CREATE TABLE "repoperms" (
48 "repoperms_id" INTEGER PRIMARY KEY AUTOINCREMENT,
50 "repoperms_type" TEXT,
55 CREATE TABLE "config" (
61 CREATE TABLE "group_membership" (
62 "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
63 "groupmember_groupid" INTEGER,
64 "groupmember_userid" INTEGER
68 $DB_CONNECTION = new PDO("sqlite:$dbloc");
69 } catch(PDOException $exep) {
70 error_log("execpt on db open");
75 $DB_CONNECTION->query($usersql);
76 $DB_CONNECTION->query($groupsql);
77 $DB_CONNECTION->query($reposql);
78 $DB_CONNECTION->query($configsql);
79 $DB_CONNECTION->query($groupmemsql);
82 function gwvp_isDBSetup()
84 // for sqlite, we just check if the db exists, for everyone else, we check for a conneciton and go yay or nay
85 global $WEB_ROOT_FS, $BASE_URL, $repo_base, $data_directory, $db_type, $db_url;
87 if($db_type == "sqlite") {
88 if(file_exists($db_url)) return true;
92 // TODO now for the connectables
96 function gwvp_ConnectDB()
98 global $WEB_ROOT_FS, $BASE_URL, $repo_base, $data_directory, $db_type, $db_name, $DB_CONNECTION;
100 // first check if $DB_CONNECTION IS live
101 if($DB_CONNECTION != false) return $DB_CONNECTION;
103 if($db_type == "sqlite") {
105 if(!file_exists($db_name)) {
106 error_log("$db_name does not exist - problem");
110 // and here we go with pdo.
111 error_log("attmpting to open db, $db_type:$db_url");
113 $DB_CONNECTION = new PDO("$db_type:$db_url");
114 } catch(PDOException $exep) {
115 error_log("execpt on db open");
119 return $DB_CONNECTION;
122 // TODO: we have to define what "Status" is
123 function gwvp_createUser($email, $fullname, $password, $username, $desc, $status)
125 $conn = gwvp_ConnectDB();
127 // TODO: change from sha1
128 $shapass = sha1($password);
129 //error_log("Create user called with $email");
130 $sql = "insert into users values (null, '$fullname', '$shapass', '$username', '$email', '$desc', '$status')";
131 error_log("Creating user, $sql");
132 return $conn->query($sql);
134 * "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
135 "user_full_name" TEXT,
136 "user_password" TEXT,
137 "user_username" TEXT,
140 "user_status" INTEGER
145 function gwvp_getUser($username=null, $email=null, $id=null)
147 $conn = gwvp_ConnectDB();
149 if($username != null) {
150 $res = $conn->query("select * from users where user_username='$username'");
151 } else if($email != null) {
152 $res = $conn->query("select * from users where user_email='$email'");
153 } else if($id != null) {
154 $res = $conn->query("select * from users where users_id='$id'");
158 foreach($res as $u_res) {
159 $returns["id"] = $u_res["users_id"];
160 $returns["fullname"] = $u_res["user_full_name"];
161 $returns["password"] = $u_res["user_password"];
162 $returns["username"] = $u_res["user_username"];
163 $returns["email"] = $u_res["user_email"];
164 $returns["desc"] = $u_res["user_desc"];
165 $returns["status"] = $u_res["user_status"];
172 function gwvp_getUsers()
174 $conn = gwvp_ConnectDB();
176 $res = $conn->query("select * from users");
180 foreach($res as $u_res) {
181 $returns[$rn]["id"] = $u_res["users_id"];
182 $returns[$rn]["fullname"] = $u_res["user_full_name"];
183 $returns[$rn]["password"] = $u_res["user_password"];
184 $returns[$rn]["username"] = $u_res["user_username"];
185 $returns[$rn]["email"] = $u_res["user_email"];
186 $returns[$rn]["desc"] = $u_res["user_desc"];
187 $returns[$rn]["status"] = $u_res["user_status"];
194 function gwvp_deleteUser($email)
196 $conn = gwvp_ConnectDB();
198 $sql = "delete from users where user_email='$email'";
203 function gwvp_createGroup($group_name, $group_desc, $is_admin, $owner_id)
205 $conn = gwvp_ConnectDB();
208 * CREATE TABLE groups (
211 "groups_is_admin" INTEGER,
212 "groups_owner_id" INTEGER
221 $sql = "insert into groups values( null, '$group_name', '$group_desc', '$is_admin_t', '$owner_id')";
228 function gwvp_deleteGroup($groupname)
230 $conn = gwvp_ConnectDB();
232 $sql = "delete from groups where groups_name='$groupname'";
237 function gwvp_getGroupsForUser($email)
239 $conn = gwvp_ConnectDB();
242 select g.groups_name from
243 group_membership gm, groups g, users u
245 gm.groupmember_userid=u.users_id and
246 u.user_email='$email' and
247 gm.groupmember_groupid=g.groups_id and
248 g.groups_name='$groupname'
252 select g.groups_name from
253 group_membership gm, groups g, users u
255 gm.groupmember_userid=u.users_id and
256 u.user_email='$email' and
257 gm.groupmember_groupid=g.groups_id
260 $res = $conn->query($sql);
264 foreach($res as $u_res) {
265 $return[$rn] = $u_res[0];
272 function gwvp_getGroupsOwnedByUser($email)
274 $conn = gwvp_ConnectDB();
277 select g.groups_name from
278 group_membership gm, groups g, users u
280 gm.groupmember_userid=u.users_id and
281 u.user_email='$email' and
282 gm.groupmember_groupid=g.groups_id and
283 g.groups_name='$groupname'
287 select g.groups_name from
290 u.user_email='$email' and
291 u.users_id=g.groups_owner_id
294 $res = $conn->query($sql);
298 foreach($res as $u_res) {
299 $return[$rn] = $u_res[0];
307 function gwvp_groupOwner($groupname)
309 $conn = gwvp_ConnectDB();
311 $sql = "select u.user_email from users u, groups g where g.groups_name='$groupname' and g.groups_owner_id=u.users_id";
313 $res = $conn->query($sql);
315 foreach($res as $u_res) {
322 function gwvp_getGroups()
324 $conn = gwvp_ConnectDB();
326 $res = $conn->query("select * from groups");
330 foreach($res as $u_res) {
331 $returns[$rn]["id"] = $u_res["groups_id"];
332 $returns[$rn]["name"] = $u_res["groups_name"];
333 if($u_res["groups_is_admin"]=="1") $return[$rn]["admin"] = true;
334 else $return[$rn]["admin"] = false;
335 $returns[$rn]["admin"] = $u_res["groups_is_admin"];
336 $returns[$rn]["ownerid"] = $u_res["groups_owner_id"];
343 function gwvp_getGroupId($groupname)
345 $conn = gwvp_ConnectDB();
347 $sql = "select groups_id from groups where groups_name='$groupname'";
349 $res = $conn->query($sql);
351 foreach($res as $u_res) {
352 $return = $u_res["groups_id"];
358 function gwvp_getGroup($gid)
362 CREATE TABLE groups (
363 "groups_id" INTEGER PRIMARY KEY AUTOINCREMENT,
365 "groups_is_admin" INTEGER,
366 "groups_owner_id" INTEGER
370 $conn = gwvp_ConnectDB();
372 $sql = "select * from groups where groups_id='$gid'";
374 $res = $conn->query($sql);
376 foreach($res as $u_res) {
377 $return["id"] = $u_res["groups_id"];
378 $return["name"] = $u_res["groups_name"];
379 if($u_res["groups_is_admin"] == 1) {
380 $return["isadmin"] = true;
382 $return["isadmin"] = false;
384 $return["ownerid"] = $u_res["groups_owner_id"];
385 $return["description"] = $u_res["groups_description"];
391 function gwvp_getUserId($useremail=null, $username = null)
393 $conn = gwvp_ConnectDB();
395 if($useremail != null) {
396 $sql = "select users_id from users where user_email='$useremail'";
397 } else if($username != null) {
398 $sql = "select users_id from users where user_username='$username'";
401 $res = $conn->query($sql);
403 foreach($res as $u_res) {
404 $return = $u_res["users_id"];
410 function gwvp_getUserName($id = null, $email=null)
412 $conn = gwvp_ConnectDB();
415 $sql = "select user_username from users where user_email='$email'";
416 } else if($id != null) {
417 $sql = "select user_username from users where users_id='$id'";
420 $res = $conn->query($sql);
422 foreach($res as $u_res) {
423 $return = $u_res["user_username"];
430 function gwvp_getUserEmail($id)
432 $conn = gwvp_ConnectDB();
434 $sql = "select user_email from users where users_id='$id'";
436 $res = $conn->query($sql);
438 foreach($res as $u_res) {
439 $return = $u_res["user_email"];
445 function gwvp_addGroupMember($email, $groupname)
447 $conn = gwvp_ConnectDB();
449 $uid = gwvp_getUserId($email);
450 $gid = gwvp_getGroupId($groupname);
453 * CREATE TABLE "group_membership" (
454 "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
455 "groupmember_groupid" INTEGER,
456 "groupmember_userid" INTEGER
459 if($uid!=false&&$gid!=false) $sql = "insert into group_membership values (null, '$gid', '$uid')";
467 function gwvp_IsGroupMember($email, $groupname)
469 $conn = gwvp_ConnectDB();
471 // i think this is right
474 group_membership gm, groups g, users u
476 gm.groupmember_userid=u.users_id and
477 u.user_email='$email' and
478 gm.groupmember_groupid=g.groups_id and
479 g.groups_name='$groupname'
482 $res = $conn->query($sql);
484 foreach($res as $u_res) {
488 if($result == 0) return false;
489 if($result == 1) return true;
492 function gwvp_IsUserAdmin($email=null, $username = null)
494 $conn = gwvp_ConnectDB();
497 // TODO: clean this up, this should be a single query - idiot
499 $id = gwvp_getUserId($email);
500 $sql = "select groupmember_groupid from group_membership where groupmember_userid='$id'";
501 } else if($username != null) {
502 $id = gwvp_getUserId(null, $username);
503 $sql = "select groupmember_groupid from group_membership where groupmember_userid='$id'";
506 $res = $conn->query($sql);
509 foreach($res as $u_res) {
510 $gid[$rn] = $u_res["groupmember_groupid"];
514 foreach($gid as $gid_t) {
516 * CREATE TABLE groups (
519 "groups_is_admin" INTEGER,
520 "groups_owner_id" INTEGER
525 $sql = "select groups_is_admin from groups where groups_id='$gid_t'";
526 $res = $conn->query($sql);
527 foreach($res as $u_res) {
528 if($u_res["groups_is_admin"] == "1") return true;
535 function gwvp_ModifyUser($userid, $email=null, $fullname=null, $password=null, $username=null, $desc=null, $status=null)
538 * "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
539 "user_full_name" TEXT,
540 "user_password" TEXT,
541 "user_username" TEXT,
544 "user_status" INTEGER
548 $conn = gwvp_ConnectDB();
551 $sql = "update users set user_email='$email' where users_id='$userid'";
555 if($fullname != null) {
556 $sql = "update users set user_full_name='$fullname' where users_id='$userid'";
560 if($password != null) {
561 $shapass = sha1($password);
562 $sql = "update users set user_password='$shapass' where users_id='$userid'";
566 if($username != null) {
567 $sql = "update users set user_username='$username' where users_id='$userid'";
572 $sql = "update users set user_desc='$desc' where users_id='$userid'";
576 if($status != null) {
577 $sql = "update users set user_status='$status' where users_id='$userid'";
585 function gwvp_ModifyGroup($groupid, $groupname = null, $group_is_admin = null, $groups_owner_id = null)
588 * CREATE TABLE groups (
591 "groups_is_admin" INTEGER,
592 "groups_owner_id" INTEGER
596 $conn = gwvp_ConnectDB();
598 if($groupname != null) {
599 $sql = "update groups set groups_name='$groupname' where groups_id='$groupid'";
603 if($group_is_admin != null) {
604 $sql = "update groups set groups_is_admin='$group_is_admin' where groups_id='$groupid'";
608 if($groups_owner_id != null) {
609 $sql = "update groups set groups_owner_id='$groups_owner_id' where groups_id='$groupid'";
616 function gwvp_AddRepo($reponame, $repodesc, $repoowner)
618 $conn = gwvp_ConnectDB();
620 $sql = "insert into repos values (null, '$reponame', '$repodesc', '$repoowner')";
625 function gwvp_GetRepoList()
627 $conn = gwvp_ConnectDB();
631 CREATE TABLE "repos" (
632 "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
634 "repos_description" TEXT,
635 "repos_owner" INTEGER
640 $sql = "select * from repos";
642 $res = $conn->query($sql);
646 foreach($res as $u_res) {
647 $return[$rn]["id"] = $u_res["repos_id"];
648 $return[$rn]["name"] = $u_res["repos_name"];
649 $return[$rn]["description"] = $u_res["repos_description"];
650 $return[$rn]["owner"] = $u_res["repos_owner"];
656 /* functions we'll need to access data:
659 * getUserData(username)
661 * getGroupData(groupname)
663 * addGroupMember(...)
664 * deleteGroupMember(...)