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_GetUserStatus($userid)
84 $conn = gwvp_ConnectDB();
86 $sql = "select user_status from users where users_id='$userid'";
88 $res = $conn->query($sql);
91 foreach($res as $val) {
92 $spl = explode(";", $val);
94 $return["statusid"] = $spl[0];
95 $return["extstatus"] = $spl[1];
100 function gwvp_SetUserStatus($userid, $status, $extendedstatus=null)
106 * 2 - password locked
107 * 3 - awaiting registration completion
108 * 4 - awaiting password reset
109 * where use status = 3,4 the key for unlock is set as the extended status
110 * i.e. if a user goes thru registration, when the validation email gets to
111 * them they'll have a key in their email (128 or 256 bit), thats what
112 * the extended status field is used for
115 $conn = gwvp_ConnectDB();
117 if($extendedstatus != null) {
118 $sql = "update users set user_status='$status;$extendedstatus' where users_id='$userid'";
120 $sql = "update users set user_status='$status;0' where users_id='$userid'";
123 return $conn->query($sql);
127 function gwvp_forceDisconnect()
130 global $DB_CONNECTION;
132 $DB_CONNECTION = false;
136 function gwvp_getConfigVal($confname)
140 CREATE TABLE "config" (
147 $conn = gwvp_ConnectDB();
149 $sql = "select * from config where config_name='$confname'";
151 $res = $conn->query($sql);
154 foreach($res as $val) {
160 function gwvp_eraseConfigVal($confname)
164 CREATE TABLE "config" (
171 $conn = gwvp_ConnectDB();
173 $sql = "delete from config where config_name='$confname'";
175 return $conn->query($sql);
178 function gwvp_setConfigVal($confname, $confval)
182 CREATE TABLE "config" (
188 gwvp_eraseConfigVal($confname);
190 $conn = gwvp_ConnectDB();
192 $sql = "insert into config values('$confname', '$confval')";
194 return $conn->query($sql);
198 function gwvp_isDBSetup()
200 // for sqlite, we just check if the db exists, for everyone else, we check for a conneciton and go yay or nay
201 global $WEB_ROOT_FS, $BASE_URL, $repo_base, $data_directory, $db_type, $db_url;
203 if($db_type == "sqlite") {
204 if(file_exists($db_url)) return true;
208 // TODO now for the connectables
212 function gwvp_ConnectDB()
214 global $WEB_ROOT_FS, $BASE_URL, $repo_base, $data_directory, $db_type, $db_name, $DB_CONNECTION;
216 // first check if $DB_CONNECTION IS live
217 if($DB_CONNECTION != false) return $DB_CONNECTION;
219 if($db_type == "sqlite") {
221 if(!file_exists($db_name)) {
222 error_log("$db_name does not exist - problem");
226 // and here we go with pdo.
227 error_log("attmpting to open db, $db_type:$db_url");
229 $DB_CONNECTION = new PDO("$db_type:$db_url");
230 } catch(PDOException $exep) {
231 error_log("execpt on db open");
235 return $DB_CONNECTION;
238 // TODO: we have to define what "Status" is
239 function gwvp_createUser($email, $fullname, $password, $username, $desc, $status)
241 $conn = gwvp_ConnectDB();
243 // TODO: change from sha1
244 $shapass = sha1($password);
245 //error_log("Create user called with $email");
246 $sql = "insert into users values (null, '$fullname', '$shapass', '$username', '$email', '$desc', '$status')";
247 error_log("Creating user, $sql");
248 return $conn->query($sql);
250 * "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
251 "user_full_name" TEXT,
252 "user_password" TEXT,
253 "user_username" TEXT,
256 "user_status" INTEGER
261 function gwvp_getUser($username=null, $email=null, $id=null)
263 $conn = gwvp_ConnectDB();
265 if($username != null) {
266 $res = $conn->query("select * from users where user_username='$username'");
267 } else if($email != null) {
268 $res = $conn->query("select * from users where user_email='$email'");
269 } else if($id != null) {
270 $res = $conn->query("select * from users where users_id='$id'");
274 foreach($res as $u_res) {
275 $returns["id"] = $u_res["users_id"];
276 $returns["fullname"] = $u_res["user_full_name"];
277 $returns["password"] = $u_res["user_password"];
278 $returns["username"] = $u_res["user_username"];
279 $returns["email"] = $u_res["user_email"];
280 $returns["desc"] = $u_res["user_desc"];
281 $returns["status"] = $u_res["user_status"];
288 function gwvp_getUsers()
290 $conn = gwvp_ConnectDB();
292 $res = $conn->query("select * from users");
296 foreach($res as $u_res) {
297 $returns[$rn]["id"] = $u_res["users_id"];
298 $returns[$rn]["fullname"] = $u_res["user_full_name"];
299 $returns[$rn]["password"] = $u_res["user_password"];
300 $returns[$rn]["username"] = $u_res["user_username"];
301 $returns[$rn]["email"] = $u_res["user_email"];
302 $returns[$rn]["desc"] = $u_res["user_desc"];
303 $returns[$rn]["status"] = $u_res["user_status"];
310 function gwvp_deleteUser($email)
312 $conn = gwvp_ConnectDB();
314 $sql = "delete from users where user_email='$email'";
319 function gwvp_createGroup($group_name, $group_desc, $is_admin, $owner_id)
321 $conn = gwvp_ConnectDB();
324 * CREATE TABLE groups (
327 "groups_is_admin" INTEGER,
328 "groups_owner_id" INTEGER
337 $sql = "insert into groups values( null, '$group_name', '$group_desc', '$is_admin_t', '$owner_id')";
344 function gwvp_deleteGroup($groupname)
346 $conn = gwvp_ConnectDB();
348 $sql = "delete from groups where groups_name='$groupname'";
353 function gwvp_getGroupsForUser($email)
355 $conn = gwvp_ConnectDB();
358 select g.groups_name from
359 group_membership gm, groups g, users u
361 gm.groupmember_userid=u.users_id and
362 u.user_email='$email' and
363 gm.groupmember_groupid=g.groups_id and
364 g.groups_name='$groupname'
368 select g.groups_name from
369 group_membership gm, groups g, users u
371 gm.groupmember_userid=u.users_id and
372 u.user_email='$email' and
373 gm.groupmember_groupid=g.groups_id
376 $res = $conn->query($sql);
380 foreach($res as $u_res) {
381 $return[$rn] = $u_res[0];
388 function gwvp_getGroupsOwnedByUser($email)
390 $conn = gwvp_ConnectDB();
393 select g.groups_name from
394 group_membership gm, groups g, users u
396 gm.groupmember_userid=u.users_id and
397 u.user_email='$email' and
398 gm.groupmember_groupid=g.groups_id and
399 g.groups_name='$groupname'
403 select g.groups_name from
406 u.user_email='$email' and
407 u.users_id=g.groups_owner_id
410 $res = $conn->query($sql);
414 foreach($res as $u_res) {
415 $return[$rn] = $u_res[0];
423 function gwvp_groupOwner($groupname)
425 $conn = gwvp_ConnectDB();
427 $sql = "select u.user_email from users u, groups g where g.groups_name='$groupname' and g.groups_owner_id=u.users_id";
429 $res = $conn->query($sql);
431 foreach($res as $u_res) {
438 function gwvp_getGroups()
440 $conn = gwvp_ConnectDB();
442 $res = $conn->query("select * from groups");
446 foreach($res as $u_res) {
447 $returns[$rn]["id"] = $u_res["groups_id"];
448 $returns[$rn]["name"] = $u_res["groups_name"];
449 if($u_res["groups_is_admin"]=="1") $return[$rn]["admin"] = true;
450 else $return[$rn]["admin"] = false;
451 $returns[$rn]["admin"] = $u_res["groups_is_admin"];
452 $returns[$rn]["ownerid"] = $u_res["groups_owner_id"];
459 function gwvp_getGroupId($groupname)
461 $conn = gwvp_ConnectDB();
463 $sql = "select groups_id from groups where groups_name='$groupname'";
465 $res = $conn->query($sql);
467 foreach($res as $u_res) {
468 $return = $u_res["groups_id"];
474 function gwvp_getGroup($gid = null, $gname = null)
478 CREATE TABLE groups (
479 "groups_id" INTEGER PRIMARY KEY AUTOINCREMENT,
481 "groups_is_admin" INTEGER,
482 "groups_owner_id" INTEGER
486 $conn = gwvp_ConnectDB();
489 $sql = "select * from groups where groups_id='$gid'";
490 } else if ($gname != null) {
491 $sql = "select * from groups where groups_name='$gname'";
494 $res = $conn->query($sql);
496 foreach($res as $u_res) {
497 $return["id"] = $u_res["groups_id"];
498 $return["name"] = $u_res["groups_name"];
499 if($u_res["groups_is_admin"] == 1) {
500 $return["isadmin"] = true;
502 $return["isadmin"] = false;
504 $return["ownerid"] = $u_res["groups_owner_id"];
505 $return["description"] = $u_res["groups_description"];
511 function gwvp_getUserId($useremail=null, $username = null)
513 $conn = gwvp_ConnectDB();
515 if($useremail != null) {
516 $sql = "select users_id from users where user_email='$useremail'";
517 } else if($username != null) {
518 $sql = "select users_id from users where user_username='$username'";
521 $res = $conn->query($sql);
523 foreach($res as $u_res) {
524 $return = $u_res["users_id"];
530 function gwvp_getUserName($id = null, $email=null)
532 $conn = gwvp_ConnectDB();
535 $sql = "select user_username from users where user_email='$email'";
536 } else if($id != null) {
537 $sql = "select user_username from users where users_id='$id'";
540 $res = $conn->query($sql);
542 foreach($res as $u_res) {
543 $return = $u_res["user_username"];
550 function gwvp_getUserEmail($id)
552 $conn = gwvp_ConnectDB();
554 $sql = "select user_email from users where users_id='$id'";
556 $res = $conn->query($sql);
558 foreach($res as $u_res) {
559 $return = $u_res["user_email"];
565 function gwvp_deleteGroupMemberByID($uid, $gid)
567 $conn = gwvp_ConnectDB();
570 * CREATE TABLE "group_membership" (
571 "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
572 "groupmember_groupid" INTEGER,
573 "groupmember_userid" INTEGER
576 $sql = "delete from group_membership where groupmember_groupid='$gid' and groupmember_userid='$uid'";
584 function gwvp_addGroupMemberByID($uid, $gid)
586 $conn = gwvp_ConnectDB();
589 * CREATE TABLE "group_membership" (
590 "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
591 "groupmember_groupid" INTEGER,
592 "groupmember_userid" INTEGER
595 $sql = "insert into group_membership values (null, '$gid', '$uid')";
603 function gwvp_addGroupMember($email, $groupname)
605 $conn = gwvp_ConnectDB();
607 $uid = gwvp_getUserId($email);
608 $gid = gwvp_getGroupId($groupname);
611 * CREATE TABLE "group_membership" (
612 "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
613 "groupmember_groupid" INTEGER,
614 "groupmember_userid" INTEGER
617 if($uid!=false&&$gid!=false) gwvp_addGroupMemberByID($uid, $gid);
625 function gwvp_IsGroupMember($email, $groupname)
627 $conn = gwvp_ConnectDB();
629 // i think this is right
632 group_membership gm, groups g, users u
634 gm.groupmember_userid=u.users_id and
635 u.user_email='$email' and
636 gm.groupmember_groupid=g.groups_id and
637 g.groups_name='$groupname'
640 $res = $conn->query($sql);
642 foreach($res as $u_res) {
646 if($result == 0) return false;
647 if($result == 1) return true;
650 function gwvp_IsUserAdmin($email=null, $username = null)
652 $conn = gwvp_ConnectDB();
655 // TODO: clean this up, this should be a single query - idiot
657 $id = gwvp_getUserId($email);
658 $sql = "select groupmember_groupid from group_membership where groupmember_userid='$id'";
659 } else if($username != null) {
660 $id = gwvp_getUserId(null, $username);
661 $sql = "select groupmember_groupid from group_membership where groupmember_userid='$id'";
664 $res = $conn->query($sql);
667 foreach($res as $u_res) {
668 $gid[$rn] = $u_res["groupmember_groupid"];
672 if($gid !== false) foreach($gid as $gid_t) {
674 * CREATE TABLE groups (
677 "groups_is_admin" INTEGER,
678 "groups_owner_id" INTEGER
683 $sql = "select groups_is_admin from groups where groups_id='$gid_t'";
684 $res = $conn->query($sql);
685 foreach($res as $u_res) {
686 if($u_res["groups_is_admin"] == "1") return true;
693 function gwvp_ModifyUser($userid, $email=null, $fullname=null, $password=null, $username=null, $desc=null, $status=null)
696 * "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
697 "user_full_name" TEXT,
698 "user_password" TEXT,
699 "user_username" TEXT,
702 "user_status" INTEGER
706 $conn = gwvp_ConnectDB();
709 $sql = "update users set user_email='$email' where users_id='$userid'";
713 if($fullname != null) {
714 $sql = "update users set user_full_name='$fullname' where users_id='$userid'";
718 if($password != null) {
719 $shapass = sha1($password);
720 $sql = "update users set user_password='$shapass' where users_id='$userid'";
724 if($username != null) {
725 $sql = "update users set user_username='$username' where users_id='$userid'";
730 $sql = "update users set user_desc='$desc' where users_id='$userid'";
734 if($status != null) {
735 $sql = "update users set user_status='$status' where users_id='$userid'";
743 function gwvp_ModifyGroup($groupid, $groupname = null, $group_is_admin = null, $groups_owner_id = null)
746 * CREATE TABLE groups (
749 "groups_is_admin" INTEGER,
750 "groups_owner_id" INTEGER
754 $conn = gwvp_ConnectDB();
756 if($groupname != null) {
757 $sql = "update groups set groups_name='$groupname' where groups_id='$groupid'";
761 if($group_is_admin != null) {
762 $sql = "update groups set groups_is_admin='$group_is_admin' where groups_id='$groupid'";
766 if($groups_owner_id != null) {
767 $sql = "update groups set groups_owner_id='$groups_owner_id' where groups_id='$groupid'";
774 function gwvp_AddRepo($reponame, $repodesc, $repoowner)
776 $conn = gwvp_ConnectDB();
778 $sql = "insert into repos values (null, '$reponame', '$repodesc', '$repoowner')";
783 function gwvp_GetRepoList()
785 $conn = gwvp_ConnectDB();
789 CREATE TABLE "repos" (
790 "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
792 "repos_description" TEXT,
793 "repos_owner" INTEGER
798 $sql = "select * from repos";
800 $res = $conn->query($sql);
804 foreach($res as $u_res) {
805 $return[$rn]["id"] = $u_res["repos_id"];
806 $return[$rn]["name"] = $u_res["repos_name"];
807 $return[$rn]["description"] = $u_res["repos_description"];
808 $return[$rn]["owner"] = $u_res["repos_owner"];
814 /* functions we'll need to access data:
817 * getUserData(username)
819 * getGroupData(groupname)
821 * addGroupMember(...)
822 * deleteGroupMember(...)