acb1a7a28ee474c63d4cd327a84045a623df25dd
[gwvp.git] / gwvplib / gwvpdatabase.php
1 <?php
2
3 global $DB_CONNECTION;
4 $DB_CONNECTION = false;
5
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()
8 {
9
10 }
11
12 function gwvp_dbCreateSQLiteStructure($dbloc)
13 {
14         $usersql = '
15                 CREATE TABLE "users" (
16             "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
17             "user_full_name" TEXT,
18             "user_password" TEXT,
19             "user_username" TEXT,
20             "user_email" TEXT,
21             "user_desc" TEXT,
22             "user_status" TEXT
23                 )';
24
25         $groupsql = '
26                 CREATE TABLE groups (
27             "groups_id" INTEGER PRIMARY KEY AUTOINCREMENT,
28             "groups_name" TEXT,
29             "groups_description" TEXT,
30             "groups_is_admin" INTEGER,
31                 "groups_owner_id" INTEGER
32                 )';
33
34         $reposql = '
35                 CREATE TABLE "repos" (
36             "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
37             "repos_name" TEXT,
38             "repos_description" TEXT,
39             "repos_owner" INTEGER
40                 )';
41
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
46         $repoperms = '
47                 CREATE TABLE "repoperms" (
48                 "repoperms_id" INTEGER PRIMARY KEY AUTOINCREMENT,
49                 "repo_id" INTEGER,
50                 "repoperms_type" TEXT,
51                 "repoperms_ref" TEXT
52         )';
53
54         $configsql = '
55                 CREATE TABLE "config" (
56             "config_name" TEXT,
57             "config_value" TEXT
58                 )';
59
60         $groupmemsql = '
61                 CREATE TABLE "group_membership" (
62             "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
63             "groupmember_groupid" INTEGER,
64             "groupmember_userid" INTEGER
65                 )';
66
67         try {
68                 $DB_CONNECTION = new PDO("sqlite:$dbloc");
69         } catch(PDOException¬†$exep) {
70                 error_log("execpt on db open");
71                 return false;
72         }
73
74
75         $DB_CONNECTION->query($usersql);
76         $DB_CONNECTION->query($groupsql);
77         $DB_CONNECTION->query($reposql);
78         $DB_CONNECTION->query($repoperms);
79         $DB_CONNECTION->query($configsql);
80         $DB_CONNECTION->query($groupmemsql);
81 }
82
83 function gwvp_GetUserStatus($userid)
84 {
85         $conn = gwvp_ConnectDB();
86         
87         $sql = "select user_status from users where users_id='$userid'";
88         
89         $res = $conn->query($sql);
90         
91         $return = null;
92         foreach($res as $val) {
93                 $spl = explode(";", $val);
94                 
95                 $return["statusid"] = $spl[0];
96                 $return["extstatus"] = $spl[1];
97         }
98         
99 }
100
101 function gwvp_SetUserStatus($userid, $status, $extendedstatus=null)
102 {
103         /*
104          * user statues
105          * 0 - all good
106          * 1 - locked
107          * 2 - password locked
108          * 3 - awaiting registration completion
109          * 4 - awaiting password reset
110          * where use status = 3,4 the key for unlock is set as the extended status
111          * i.e. if a user goes thru registration, when the validation email gets to
112          * them they'll have a key in their email (128 or 256 bit), thats what
113          * the extended status field is used for
114          */
115         
116         $conn = gwvp_ConnectDB();
117         
118         if($extendedstatus != null) {
119                 $sql = "update users set user_status='$status;$extendedstatus' where users_id='$userid'";
120         } else {
121                 $sql = "update users set user_status='$status;0' where users_id='$userid'";
122         }
123         
124         return $conn->query($sql);
125         
126 }
127
128 function gwvp_forceDisconnect()
129 {
130         
131         global $DB_CONNECTION;
132         
133         $DB_CONNECTION = false;
134 }
135
136
137 function gwvp_getConfigVal($confname)
138 {
139         /*
140          *      $configsql = '
141                 CREATE TABLE "config" (
142             "config_name" TEXT,
143             "config_value" TEXT
144                 )';
145
146          */
147         
148         $conn = gwvp_ConnectDB();
149         
150         $sql = "select config_value from config where config_name='$confname'";
151         
152         $res = $conn->query($sql);
153         
154         $return = null;
155         foreach($res as $val) {
156                 $return = $val["config_value"];
157         }
158         
159         return $return;
160 }
161
162 function gwvp_eraseConfigVal($confname)
163 {
164         /*
165          *      $configsql = '
166                 CREATE TABLE "config" (
167             "config_name" TEXT,
168             "config_value" TEXT
169                 )';
170
171          */
172         
173         $conn = gwvp_ConnectDB();
174         
175         $sql = "delete from config where config_name='$confname'";
176         
177         return $conn->query($sql);
178 }
179
180 function gwvp_setConfigVal($confname, $confval)
181 {
182         /*
183          *      $configsql = '
184                 CREATE TABLE "config" (
185             "config_name" TEXT,
186             "config_value" TEXT
187                 )';
188
189          */
190         gwvp_eraseConfigVal($confname);
191
192         $conn = gwvp_ConnectDB();
193         
194         $sql = "insert into config values('$confname', '$confval')";
195         
196         return $conn->query($sql);
197 }
198
199
200 function gwvp_isDBSetup()
201 {
202         // for sqlite, we just check if the db exists, for everyone else, we check for a conneciton and go yay or nay
203         global $WEB_ROOT_FS, $BASE_URL, $data_directory, $db_type, $db_url;
204
205         if($db_type == "sqlite") {
206                 if(file_exists($db_url)) return true;
207                 else return false;
208         }
209
210         // TODO now for the connectables
211         // gwvp_ConnectDB();
212 }
213
214 function gwvp_ConnectDB()
215 {
216         global $WEB_ROOT_FS, $BASE_URL, $data_directory, $db_type, $db_name, $DB_CONNECTION;
217
218         // first check if $DB_CONNECTION IS live
219         if($DB_CONNECTION != false) return $DB_CONNECTION;
220
221         if($db_type == "sqlite") {
222                 $db_url = $db_name;
223                 if(!file_exists($db_name)) {
224                         error_log("$db_name does not exist - problem");
225                 }
226         }
227
228         // and here we go with pdo.
229         error_log("attmpting to open db, $db_type:$db_url");
230         try {
231                 $DB_CONNECTION = new PDO("$db_type:$db_url");
232         } catch(PDOException¬†$exep) {
233                 error_log("execpt on db open");
234                 return false;
235         }
236
237         return $DB_CONNECTION;
238 }
239
240 // TODO: we have to define what "Status" is
241 function gwvp_createUser($email, $fullname, $password, $username, $desc, $status)
242 {
243         $conn = gwvp_ConnectDB();
244
245         // TODO: change from sha1
246         $shapass = sha1($password);
247         //error_log("Create user called with $email");
248         $sql = "insert into users values (null, '$fullname', '$shapass', '$username', '$email', '$desc', '$status')";
249         error_log("Creating user, $sql");
250         return $conn->query($sql);
251         /*
252          *          "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
253          "user_full_name" TEXT,
254          "user_password" TEXT,
255          "user_username" TEXT,
256          "user_email" TEXT,
257          "user_desc" TEXT,
258          "user_status" INTEGER
259
260          */
261 }
262
263 function gwvp_getUser($username=null, $email=null, $id=null)
264 {
265         $conn = gwvp_ConnectDB();
266
267         if($username != null) {
268                 $res = $conn->query("select * from users where user_username='$username'");
269         } else if($email != null) {
270                 $res = $conn->query("select * from users where user_email='$email'");
271         } else if($id != null) {
272                 $res = $conn->query("select * from users where users_id='$id'");
273         } else return false;
274
275         $returns = false;
276         foreach($res as $u_res) {
277                 $returns["id"] = $u_res["users_id"];
278                 $returns["fullname"] = $u_res["user_full_name"];
279                 $returns["password"] = $u_res["user_password"];
280                 $returns["username"] = $u_res["user_username"];
281                 $returns["email"] = $u_res["user_email"];
282                 $returns["desc"] = $u_res["user_desc"];
283                 $returns["status"] = $u_res["user_status"];
284         }
285
286         return $returns;
287
288 }
289
290 function gwvp_getRepoOwner($repoid)
291 {
292         $conn = gwvp_ConnectDB();
293
294         $sql = "select repos_owner from repos where repos_id='$repoid'";
295
296         $res = $conn->query($sql);
297         
298         $return = false;
299         foreach($res as $rown) {
300                 $return = $rown["repos_owner"];
301         }
302         return $return;
303 }
304
305 function gwvp_getOwnedRepos($userid = null, $username = null)
306 {
307         $conn = gwvp_ConnectDB();
308         
309         if($username != null) {
310                 $details = gwvp_getUser($username);
311                 $uid = $details["id"];
312                 $sql = "select * from repos where repos_owner='$uid'";
313                 $res = $conn->query($sql);
314                 error_log("sql: $sql");
315         } else if($userid != null) {
316                 $sql = "select * from repos where repos_owner='$userid'";
317                 $res = $conn->query($sql);
318                 error_log("sql: $sql");
319         } else return false;
320         
321         /*
322          *              CREATE TABLE "repos" (
323             "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
324             "repos_name" TEXT,
325             "repos_description" TEXT,
326             "repos_owner" INTEGER
327                 )';
328
329          */
330
331         $returns = false;
332         $rn = 0;
333         foreach($res as $u_res) {
334                 $returns[$rn]["id"] = $u_res["repos_id"];
335                 $returns[$rn]["name"] = $u_res["repos_name"];
336                 $returns[$rn]["description"] = $u_res["repos_description"];
337                 $rn++;
338         }
339
340         return $returns;
341 }
342
343 function gwvp_getUsers()
344 {
345         $conn = gwvp_ConnectDB();
346
347         $res = $conn->query("select * from users");
348
349         $returns = false;
350         $rn = 0;
351         foreach($res as $u_res) {
352                 $returns[$rn]["id"] = $u_res["users_id"];
353                 $returns[$rn]["fullname"] = $u_res["user_full_name"];
354                 $returns[$rn]["password"] = $u_res["user_password"];
355                 $returns[$rn]["username"] = $u_res["user_username"];
356                 $returns[$rn]["email"] = $u_res["user_email"];
357                 $returns[$rn]["desc"] = $u_res["user_desc"];
358                 $returns[$rn]["status"] = $u_res["user_status"];
359                 $rn++;
360         }
361
362         return $returns;
363 }
364
365 function gwvp_deleteUser($email)
366 {
367         $conn = gwvp_ConnectDB();
368
369         $sql = "delete from users where user_email='$email'";
370
371         $conn->query($sql);
372 }
373
374 function gwvp_createGroup($group_name, $group_desc, $is_admin, $owner_id)
375 {
376         $conn = gwvp_ConnectDB();
377
378         /*
379          *              CREATE TABLE groups (
380          "groups_id" INTEGER,
381          "groups_name" TEXT,
382          "groups_is_admin" INTEGER,
383                 "groups_owner_id" INTEGER
384                 )';
385
386          */
387         if($is_admin) {
388                 $is_admin_t = 1;
389         } else {
390                 $is_admin_t = 0;
391         }
392         $sql = "insert into groups values( null, '$group_name', '$group_desc', '$is_admin_t', '$owner_id')";
393         
394
395         $conn->query($sql);
396         
397         // add the owner to the group
398         $gid = gwvp_getGroupId($group_name);
399         
400         
401         error_log("calling addgroupmember with $owner_id, $gid");
402         gwvp_addGroupMemberById($owner_id, $gid);
403         
404         return true;
405 }
406
407 function gwvp_deleteGroup($groupname)
408 {
409         $conn = gwvp_ConnectDB();
410
411         $sql = "delete from groups where groups_name='$groupname'";
412
413         $conn->query($sql);
414 }
415
416 function gwvp_getGroupsForUser($email = null, $userid = null)
417 {
418         $conn = gwvp_ConnectDB();
419
420         /*
421          select g.groups_name from
422          group_membership gm, groups g, users u
423          where
424          gm.groupmember_userid=u.users_id and
425          u.user_email='$email' and
426          gm.groupmember_groupid=g.groups_id and
427          g.groups_name='$groupname'
428          */
429         if($email != null) {
430                 $sql = "
431                                 select g.groups_name from 
432                                         group_membership gm, groups g, users u 
433                                 where 
434                                         gm.groupmember_userid=u.users_id and
435                                         u.user_email='$email' and
436                                         gm.groupmember_groupid=g.groups_id
437                 ";
438         } else if($userid != null) {
439                 $sql = "
440                                 select g.groups_name from 
441                                         group_membership gm, groups g, users u 
442                                 where 
443                                         gm.groupmember_userid=u.users_id and
444                                         u.users_id='$userid' and
445                                         gm.groupmember_groupid=g.groups_id
446                 ";
447         } else return false;
448
449         $res = $conn->query($sql);
450
451         $return = false;
452         $rn = 0;
453         foreach($res as $u_res) {
454                 $return[$rn] = $u_res[0];
455                 $rn++;
456         }
457
458         return $return;
459 }
460
461 function gwvp_getGroupsOwnedByUser($email)
462 {
463         $conn = gwvp_ConnectDB();
464
465         /*
466          select g.groups_name from
467          group_membership gm, groups g, users u
468          where
469          gm.groupmember_userid=u.users_id and
470          u.user_email='$email' and
471          gm.groupmember_groupid=g.groups_id and
472          g.groups_name='$groupname'
473          */
474
475         $sql = "
476                         select g.groups_name from 
477                                 groups g, users u 
478                         where 
479                                 u.user_email='$email' and
480                                 u.users_id=g.groups_owner_id
481         ";
482
483         $res = $conn->query($sql);
484
485         $return = false;
486         $rn = 0;
487         foreach($res as $u_res) {
488                 $return[$rn] = $u_res[0];
489                 $rn++;
490         }
491
492         return $return;
493
494 }
495
496 function gwvp_groupOwner($groupname)
497 {
498         $conn = gwvp_ConnectDB();
499
500         $sql = "select u.user_email from users u, groups g where g.groups_name='$groupname' and g.groups_owner_id=u.users_id";
501
502         $res = $conn->query($sql);
503         $return = false;
504         foreach($res as $u_res) {
505                 $return = $u_res[0];
506         }
507
508         return $return;
509 }
510
511 function gwvp_getGroups()
512 {
513         $conn = gwvp_ConnectDB();
514
515         $res = $conn->query("select * from groups");
516
517         $returns = false;
518         $rn = 0;
519         foreach($res as $u_res) {
520                 $returns[$rn]["id"] = $u_res["groups_id"];
521                 $returns[$rn]["name"] = $u_res["groups_name"];
522                 if($u_res["groups_is_admin"]=="1") $return[$rn]["admin"] = true;
523                 else $return[$rn]["admin"] = false;
524                 $returns[$rn]["admin"] = $u_res["groups_is_admin"];
525                 $returns[$rn]["ownerid"] = $u_res["groups_owner_id"];
526                 $rn++;
527         }
528
529         return $returns;
530 }
531
532 function gwvp_getGroupId($groupname)
533 {
534         $conn = gwvp_ConnectDB();
535
536         $sql = "select groups_id from groups where groups_name='$groupname'";
537
538         $res = $conn->query($sql);
539         $return = false;
540         foreach($res as $u_res) {
541                 $return = $u_res["groups_id"];
542         }
543
544         return $return;
545 }
546
547 function gwvp_getGroup($gid = null, $gname = null)
548 {
549         /* 
550          *      $groupsql = '
551                 CREATE TABLE groups (
552             "groups_id" INTEGER PRIMARY KEY AUTOINCREMENT,
553             "groups_name" TEXT,
554             "groups_is_admin" INTEGER,
555                 "groups_owner_id" INTEGER
556                 )';
557
558          */
559         $conn = gwvp_ConnectDB();
560         
561         if($gid != null) {
562                 $sql = "select * from groups where groups_id='$gid'";
563         } else if ($gname != null) {
564                 $sql = "select * from groups where groups_name='$gname'";
565         } else return false;
566         
567         $res = $conn->query($sql);
568         $return = false;
569         foreach($res as $u_res) {
570                 $return["id"] = $u_res["groups_id"];
571                 $return["name"] = $u_res["groups_name"];
572                 if($u_res["groups_is_admin"] == 1) {
573                         $return["isadmin"] = true;
574                 } else {
575                         $return["isadmin"] = false;
576                 }
577                 $return["ownerid"] = $u_res["groups_owner_id"];
578                 $return["description"] = $u_res["groups_description"];
579         }
580         
581         return $return;
582 }
583
584 function gwvp_getUserId($useremail=null, $username = null)
585 {
586         $conn = gwvp_ConnectDB();
587
588         if($useremail != null) {
589                 $sql = "select users_id from users where user_email='$useremail'";
590         } else if($username != null) {
591                 $sql = "select users_id from users where user_username='$username'";
592         } else return false;
593
594         $res = $conn->query($sql);
595         $return = false;
596         foreach($res as $u_res) {
597                 $return = $u_res["users_id"];
598         }
599
600         return $return;
601 }
602
603 function gwvp_getUserName($id = null, $email=null)
604 {
605         $conn = gwvp_ConnectDB();
606
607         if($email != null) { 
608                 $sql = "select user_username from users where user_email='$email'";
609         } else if($id != null) {
610                 $sql = "select user_username from users where users_id='$id'";
611         } else return false;
612
613         $res = $conn->query($sql);
614         $return = false;
615         foreach($res as $u_res) {
616                 $return = $u_res["user_username"];
617         }
618
619         return $return;
620 }
621
622
623 function gwvp_getUserEmail($id)
624 {
625         $conn = gwvp_ConnectDB();
626
627         $sql = "select user_email from users where users_id='$id'";
628
629         $res = $conn->query($sql);
630         $return = false;
631         foreach($res as $u_res) {
632                 $return = $u_res["user_email"];
633         }
634
635         return $return;
636 }
637
638 function gwvp_deleteGroupMemberByID($uid, $gid)
639 {
640         $conn = gwvp_ConnectDB();
641
642         /*
643          *              CREATE TABLE "group_membership" (
644          "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
645          "groupmember_groupid" INTEGER,
646          "groupmember_userid" INTEGER
647
648          */
649         $sql = "delete from group_membership where groupmember_groupid='$gid' and  groupmember_userid='$uid'";
650
651         $conn->query($sql);
652
653         return true;
654 }
655
656
657 function gwvp_addGroupMemberByID($uid, $gid)
658 {
659         $conn = gwvp_ConnectDB();
660
661         /*
662          *              CREATE TABLE "group_membership" (
663          "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
664          "groupmember_groupid" INTEGER,
665          "groupmember_userid" INTEGER
666
667          */
668         $sql = "insert into group_membership values (null, '$gid', '$uid')";
669
670         $conn->query($sql);
671
672         return true;
673 }
674
675
676 function gwvp_addGroupMember($email, $groupname)
677 {
678         $conn = gwvp_ConnectDB();
679
680         $uid = gwvp_getUserId($email);
681         $gid = gwvp_getGroupId($groupname);
682
683         /*
684          *              CREATE TABLE "group_membership" (
685          "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
686          "groupmember_groupid" INTEGER,
687          "groupmember_userid" INTEGER
688
689          */
690         if($uid!=false&&$gid!=false) gwvp_addGroupMemberByID($uid, $gid);
691         else return false;
692
693         return true;
694 }
695
696 function gwvp_IsGroupMemberById($uid, $gid)
697 {
698         $conn = gwvp_ConnectDB();
699         
700         $sql = "select count(*) from group_membership where groupmember_userid='$uid' and groupmember_groupid='$gid'";
701
702         $res = $conn->query($sql);
703         $result = 0;
704         foreach($res as $u_res) {
705                 $result = $u_res[0];
706         }
707
708         if($result == 0) return false;
709         if($result == 1) return true;
710 }
711
712 function gwvp_IsGroupMember($email, $groupname)
713 {
714         $conn = gwvp_ConnectDB();
715
716         // i think this is right
717         $sql = "
718                         select count(*) from 
719                                 group_membership gm, groups g, users u 
720                         where 
721                                 gm.groupmember_userid=u.users_id and
722                                 u.user_email='$email' and
723                                 gm.groupmember_groupid=g.groups_id and
724                                 g.groups_name='$groupname'
725                         ";
726
727         $res = $conn->query($sql);
728         $result = 0;
729         foreach($res as $u_res) {
730                 $result = $u_res[0];
731         }
732
733         if($result == 0) return false;
734         if($result == 1) return true;
735 }
736
737 function gwvp_IsGroupAdmin($groupname = null, $gid = null)
738 {
739         $conn = gwvp_ConnectDB();
740         
741         if($groupname != null) {
742                 $sql = "select groups_is_admin from groups where groups_name='$groupname'";
743         } else if($gid != null) {
744                 $sql = "select groups_is_admin from groups where groups_id='$gid'";
745         } else return false;
746         
747         $res = $conn->query($sql);
748         
749         $return = false;
750         foreach($res as $u_res) {
751                 if($u_res["groups_is_admin"] == "1") $return = true;
752         }
753         
754         return $return;
755 }
756
757 function gwvp_IsRepoOwner($userid, $repoid)
758 {
759         $conn = gwvp_ConnectDB();
760         
761         $sql = "select repos_owner from repos where repos_id='$repoid'";
762         
763         $res = $conn->query($sql);
764         
765         $return = false;
766         foreach($res as $u_res) {
767                 $return["owner"] = $u_res["repos_owner"];
768         }
769
770         if($return == false) return false;
771         if($return["owner"] == $userid) return true;
772         else return false;
773         
774         
775 }
776
777 function gwvp_IsUserAdmin($email=null, $username = null, $userid = null)
778 {
779         $conn = gwvp_ConnectDB();
780
781
782         // TODO: clean this up, this should be a single query - idiot
783         if($email != null) {
784                 $id = gwvp_getUserId($email);
785                 $sql = "select groupmember_groupid from group_membership where groupmember_userid='$id'";
786         } else if($userid != null) {
787                 $sql = "select groupmember_groupid from group_membership where groupmember_userid='$userid'";
788         } else if($username != null) {
789                 $id = gwvp_getUserId(null, $username);
790                 $sql = "select groupmember_groupid from group_membership where groupmember_userid='$id'";
791         } else return false;
792
793         $res = $conn->query($sql);
794         $rn = 0;
795         $gid = false;
796         foreach($res as $u_res) {
797                 $gid[$rn] = $u_res["groupmember_groupid"];
798                 $rn++;
799         }
800
801         if($gid !== false) foreach($gid as $gid_t) {
802                 /*
803                  *              CREATE TABLE groups (
804                  "groups_id" INTEGER,
805                  "groups_name" TEXT,
806                  "groups_is_admin" INTEGER,
807                  "groups_owner_id" INTEGER
808                  )';
809
810                  */
811
812                 $sql = "select groups_is_admin from groups where groups_id='$gid_t'";
813                 $res = $conn->query($sql);
814                 foreach($res as $u_res) {
815                         if($u_res["groups_is_admin"] == "1") return true;
816                 }
817         }
818
819         return false;
820 }
821
822 function gwvp_ModifyUser($userid, $email=null, $fullname=null, $password=null, $username=null, $desc=null, $status=null)
823 {
824         /*
825          *          "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
826          "user_full_name" TEXT,
827          "user_password" TEXT,
828          "user_username" TEXT,
829          "user_email" TEXT,
830          "user_desc" TEXT,
831          "user_status" INTEGER
832
833          */
834
835         $conn = gwvp_ConnectDB();
836
837         if($email != null) {
838                 $sql = "update users set user_email='$email' where users_id='$userid'";
839                 $conn->query($sql);
840         }
841
842         if($fullname != null) {
843                 $sql = "update users set user_full_name='$fullname' where users_id='$userid'";
844                 $conn->query($sql);
845         }
846
847         if($password != null) {
848                 $shapass = sha1($password);
849                 $sql = "update users set user_password='$shapass' where users_id='$userid'";
850                 $conn->query($sql);
851         }
852
853         if($username != null) {
854                 $sql = "update users set user_username='$username' where users_id='$userid'";
855                 $conn->query($sql);
856         }
857
858         if($desc != null) {
859                 $sql = "update users set user_desc='$desc' where users_id='$userid'";
860                 $conn->query($sql);
861         }
862
863         if($status != null) {
864                 $sql = "update users set user_status='$status' where users_id='$userid'";
865                 $conn->query($sql);
866         }
867
868         return true;
869 }
870
871
872 function gwvp_ModifyGroup($groupid, $groupname = null, $group_is_admin = null, $groups_owner_id = null)
873 {
874         /*
875          *              CREATE TABLE groups (
876          "groups_id" INTEGER,
877          "groups_name" TEXT,
878          "groups_is_admin" INTEGER,
879                 "groups_owner_id" INTEGER
880                 )';
881
882          */
883         $conn = gwvp_ConnectDB();
884
885         if($groupname != null) {
886                 $sql = "update groups set groups_name='$groupname' where groups_id='$groupid'";
887                 $conn->query($sql);
888         }
889
890         if($group_is_admin != null) {
891                 $sql = "update groups set groups_is_admin='$group_is_admin' where groups_id='$groupid'";
892                 $conn->query($sql);
893         }
894
895         if($groups_owner_id != null) {
896                 $sql = "update groups set groups_owner_id='$groups_owner_id' where groups_id='$groupid'";
897                 $conn->query($sql);
898         }
899
900         return true;
901 }
902
903 function gwvp_GetRepoId($reponame)
904 {
905         $conn = gwvp_ConnectDB();
906         
907         $sql = "select repos_id from repos where repos_name='$reponame'";
908         
909         $res = $conn->query($sql);
910         
911         $return = false;
912         foreach($res as $u_res) {
913                 $return = $u_res["repos_id"];
914         }
915         
916         return $return;
917         
918 }
919
920 function gwvp_GetRepo($rid)
921 {
922         $conn = gwvp_ConnectDB();
923         
924         $sql = "select * from repos where repos_id='$rid'";
925         
926         $res = $conn->query($sql);
927         
928         $return = false;
929         foreach($res as $u_res) {
930                 $return["id"] = $u_res["repos_id"];
931                 $return["name"] = $u_res["repos_name"];
932                 $return["description"] = $u_res["repos_description"];
933                 $return["owner"] = $u_res["repos_owner"];
934         }
935         
936         return $return;
937 }
938
939 function gwvp_GetRepoList()
940 {
941         $conn = gwvp_ConnectDB();
942
943         /*
944          *      $reposql = '
945                 CREATE TABLE "repos" (
946                 "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
947                 "repos_name" TEXT,
948                 "repos_description" TEXT,
949                 "repos_owner" INTEGER
950                 )';
951
952          */
953
954         $sql = "select * from repos";
955         
956         $res = $conn->query($sql);
957         
958         $return = false;
959         $rn = 0;
960         foreach($res as $u_res) {
961                 $return[$rn]["id"] = $u_res["repos_id"];
962                 $return[$rn]["name"] = $u_res["repos_name"];
963                 $return[$rn]["description"] = $u_res["repos_description"];
964                 $return[$rn]["owner"] = $u_res["repos_owner"];
965                 $rn++;
966         }
967         
968         return $return;
969 }
970
971 function gwvp_AddRepo($reponame, $repodesc, $repoowner, $defaultperms = 0)
972 {
973         $conn = gwvp_ConnectDB();
974         
975         $sql = "insert into repos values (null, '$reponame', '$repodesc', '$repoowner')";
976         
977         $conn->query($sql);
978         
979         $sql = "select repos_id from repos where repos_name='$reponame'";
980         $res = $conn->query($sql);
981         $rid = -1;
982         foreach($res as $repos) {
983                 $rid = $repos["repos_id"];
984         }
985         /*
986          *              CREATE TABLE "repoperms" (
987                 "repoperms_id" INTEGER PRIMARY KEY AUTOINCREMENT,
988                 "repo_id" INTEGER,
989                 "repoperms_type" TEXT,
990                 "repoperms_ref" TEXT
991
992          */
993         
994         /*
995          * // default perms:
996 // 0 - anyone can clone/read, only owner can write
997 // 1 - noone can clone/read, repo is visible (i.e. name), only owner can read/write repo
998 // 2 - only owner can see anything
999
1000          */
1001
1002         switch($defaultperms) {
1003                 case "1":
1004                         gwvp_addRepoPermission($rid, "visible", "anon");
1005                         break;
1006                 case "2":
1007                         // by 2, we do nothing, owner already has full perms
1008                         break;
1009                 default: // 0
1010                         gwvp_addRepoPermission($rid, "read", "anon");
1011                         
1012         }
1013 }
1014
1015 function gwvp_getRepoPermissions($repoid)
1016 {
1017         /*
1018          *      // this looks like null, <repoid>, <read|visible|write>, user:<uid>|group:<gid>|authed|anon
1019         // where authed = any authenticated user, anon = everyone (logged in, not logged in, etc)
1020         // read|visible|write = can clone from repo|can see repo exists and see description but not clone from it|can push to repo
1021         // TODO: is this sufficient? i have to think about it
1022         $repoperms = '
1023                 CREATE TABLE "repoperms" (
1024                 "repoperms_id" INTEGER PRIMARY KEY AUTOINCREMENT,
1025                 "repo_id" INTEGER,
1026                 "repoperms_type" TEXT,
1027                 "repoperms_ref" TEXT
1028         )';
1029
1030          */
1031         $conn = gwvp_ConnectDB();
1032
1033         $sql = "select * from repoperms where repo_id='$repoid'";
1034         
1035         $res = $conn->query($sql);
1036         
1037         $returns = false;
1038         $rn = 0;
1039         foreach($res as $perm) {
1040                 $returns[$rn]["id"] = $perm["repoperms_id"];
1041                 $returns[$rn]["type"] = $perm["repoperms_type"];
1042                 $returns[$rn]["ref"] = $perm["repoperms_ref"];
1043                 $rn++;
1044         }
1045         
1046         return $returns;
1047 }
1048
1049 function gwvp_removeRepoPermission($permid)
1050 {
1051         $conn = gwvp_ConnectDB();
1052         
1053         $sql = "delete from repoperms where repoperms_id='$permid'";
1054         error_log("attempting: \"$sql\"");
1055         
1056         return $conn->query($sql);
1057 }
1058
1059 function gwvp_addRepoPermission($repoid, $permtype, $permref)
1060 {
1061         $conn = gwvp_ConnectDB();
1062         
1063         error_log("PERMS: $repoid, $permtype, $permref");
1064         $sql = "insert into repoperms values(null, '$repoid', '$permtype', '$permref')";
1065         
1066         return $conn->query($sql);
1067 }
1068 /* functions we'll need to access data:
1069  *
1070  * getUsers(pattern)
1071  * getUserData(username)
1072  * getGroups(pattern)
1073  * getGroupData(groupname)
1074  * modifyGroup(...)
1075  * addGroupMember(...)
1076  * deleteGroupMember(...)
1077  *
1078  * createUser(...)
1079  * deleteUser(...)
1080  * modifyUser(...)
1081  * createRepo(...)
1082  * deleteRepo(...)
1083  * getRepos()
1084  */
1085
1086
1087
1088 ?>