started implementing the configuration dialog stuff
[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 }
398
399 function gwvp_deleteGroup($groupname)
400 {
401         $conn = gwvp_ConnectDB();
402
403         $sql = "delete from groups where groups_name='$groupname'";
404
405         $conn->query($sql);
406 }
407
408 function gwvp_getGroupsForUser($email = null, $userid = null)
409 {
410         $conn = gwvp_ConnectDB();
411
412         /*
413          select g.groups_name from
414          group_membership gm, groups g, users u
415          where
416          gm.groupmember_userid=u.users_id and
417          u.user_email='$email' and
418          gm.groupmember_groupid=g.groups_id and
419          g.groups_name='$groupname'
420          */
421         if($email != null) {
422                 $sql = "
423                                 select g.groups_name from 
424                                         group_membership gm, groups g, users u 
425                                 where 
426                                         gm.groupmember_userid=u.users_id and
427                                         u.user_email='$email' and
428                                         gm.groupmember_groupid=g.groups_id
429                 ";
430         } else if($userid != null) {
431                 $sql = "
432                                 select g.groups_name from 
433                                         group_membership gm, groups g, users u 
434                                 where 
435                                         gm.groupmember_userid=u.users_id and
436                                         u.users_id='$userid' and
437                                         gm.groupmember_groupid=g.groups_id
438                 ";
439         } else return false;
440
441         $res = $conn->query($sql);
442
443         $return = false;
444         $rn = 0;
445         foreach($res as $u_res) {
446                 $return[$rn] = $u_res[0];
447                 $rn++;
448         }
449
450         return $return;
451 }
452
453 function gwvp_getGroupsOwnedByUser($email)
454 {
455         $conn = gwvp_ConnectDB();
456
457         /*
458          select g.groups_name from
459          group_membership gm, groups g, users u
460          where
461          gm.groupmember_userid=u.users_id and
462          u.user_email='$email' and
463          gm.groupmember_groupid=g.groups_id and
464          g.groups_name='$groupname'
465          */
466
467         $sql = "
468                         select g.groups_name from 
469                                 groups g, users u 
470                         where 
471                                 u.user_email='$email' and
472                                 u.users_id=g.groups_owner_id
473         ";
474
475         $res = $conn->query($sql);
476
477         $return = false;
478         $rn = 0;
479         foreach($res as $u_res) {
480                 $return[$rn] = $u_res[0];
481                 $rn++;
482         }
483
484         return $return;
485
486 }
487
488 function gwvp_groupOwner($groupname)
489 {
490         $conn = gwvp_ConnectDB();
491
492         $sql = "select u.user_email from users u, groups g where g.groups_name='$groupname' and g.groups_owner_id=u.users_id";
493
494         $res = $conn->query($sql);
495         $return = false;
496         foreach($res as $u_res) {
497                 $return = $u_res[0];
498         }
499
500         return $return;
501 }
502
503 function gwvp_getGroups()
504 {
505         $conn = gwvp_ConnectDB();
506
507         $res = $conn->query("select * from groups");
508
509         $returns = false;
510         $rn = 0;
511         foreach($res as $u_res) {
512                 $returns[$rn]["id"] = $u_res["groups_id"];
513                 $returns[$rn]["name"] = $u_res["groups_name"];
514                 if($u_res["groups_is_admin"]=="1") $return[$rn]["admin"] = true;
515                 else $return[$rn]["admin"] = false;
516                 $returns[$rn]["admin"] = $u_res["groups_is_admin"];
517                 $returns[$rn]["ownerid"] = $u_res["groups_owner_id"];
518                 $rn++;
519         }
520
521         return $returns;
522 }
523
524 function gwvp_getGroupId($groupname)
525 {
526         $conn = gwvp_ConnectDB();
527
528         $sql = "select groups_id from groups where groups_name='$groupname'";
529
530         $res = $conn->query($sql);
531         $return = false;
532         foreach($res as $u_res) {
533                 $return = $u_res["groups_id"];
534         }
535
536         return $return;
537 }
538
539 function gwvp_getGroup($gid = null, $gname = null)
540 {
541         /* 
542          *      $groupsql = '
543                 CREATE TABLE groups (
544             "groups_id" INTEGER PRIMARY KEY AUTOINCREMENT,
545             "groups_name" TEXT,
546             "groups_is_admin" INTEGER,
547                 "groups_owner_id" INTEGER
548                 )';
549
550          */
551         $conn = gwvp_ConnectDB();
552         
553         if($gid != null) {
554                 $sql = "select * from groups where groups_id='$gid'";
555         } else if ($gname != null) {
556                 $sql = "select * from groups where groups_name='$gname'";
557         } else return false;
558         
559         $res = $conn->query($sql);
560         $return = false;
561         foreach($res as $u_res) {
562                 $return["id"] = $u_res["groups_id"];
563                 $return["name"] = $u_res["groups_name"];
564                 if($u_res["groups_is_admin"] == 1) {
565                         $return["isadmin"] = true;
566                 } else {
567                         $return["isadmin"] = false;
568                 }
569                 $return["ownerid"] = $u_res["groups_owner_id"];
570                 $return["description"] = $u_res["groups_description"];
571         }
572         
573         return $return;
574 }
575
576 function gwvp_getUserId($useremail=null, $username = null)
577 {
578         $conn = gwvp_ConnectDB();
579
580         if($useremail != null) {
581                 $sql = "select users_id from users where user_email='$useremail'";
582         } else if($username != null) {
583                 $sql = "select users_id from users where user_username='$username'";
584         } else return false;
585
586         $res = $conn->query($sql);
587         $return = false;
588         foreach($res as $u_res) {
589                 $return = $u_res["users_id"];
590         }
591
592         return $return;
593 }
594
595 function gwvp_getUserName($id = null, $email=null)
596 {
597         $conn = gwvp_ConnectDB();
598
599         if($email != null) { 
600                 $sql = "select user_username from users where user_email='$email'";
601         } else if($id != null) {
602                 $sql = "select user_username from users where users_id='$id'";
603         } else return false;
604
605         $res = $conn->query($sql);
606         $return = false;
607         foreach($res as $u_res) {
608                 $return = $u_res["user_username"];
609         }
610
611         return $return;
612 }
613
614
615 function gwvp_getUserEmail($id)
616 {
617         $conn = gwvp_ConnectDB();
618
619         $sql = "select user_email from users where users_id='$id'";
620
621         $res = $conn->query($sql);
622         $return = false;
623         foreach($res as $u_res) {
624                 $return = $u_res["user_email"];
625         }
626
627         return $return;
628 }
629
630 function gwvp_deleteGroupMemberByID($uid, $gid)
631 {
632         $conn = gwvp_ConnectDB();
633
634         /*
635          *              CREATE TABLE "group_membership" (
636          "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
637          "groupmember_groupid" INTEGER,
638          "groupmember_userid" INTEGER
639
640          */
641         $sql = "delete from group_membership where groupmember_groupid='$gid' and  groupmember_userid='$uid'";
642
643         $conn->query($sql);
644
645         return true;
646 }
647
648
649 function gwvp_addGroupMemberByID($uid, $gid)
650 {
651         $conn = gwvp_ConnectDB();
652
653         /*
654          *              CREATE TABLE "group_membership" (
655          "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
656          "groupmember_groupid" INTEGER,
657          "groupmember_userid" INTEGER
658
659          */
660         $sql = "insert into group_membership values (null, '$gid', '$uid')";
661
662         $conn->query($sql);
663
664         return true;
665 }
666
667
668 function gwvp_addGroupMember($email, $groupname)
669 {
670         $conn = gwvp_ConnectDB();
671
672         $uid = gwvp_getUserId($email);
673         $gid = gwvp_getGroupId($groupname);
674
675         /*
676          *              CREATE TABLE "group_membership" (
677          "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
678          "groupmember_groupid" INTEGER,
679          "groupmember_userid" INTEGER
680
681          */
682         if($uid!=false&&$gid!=false) gwvp_addGroupMemberByID($uid, $gid);
683         else return false;
684
685         return true;
686 }
687
688 function gwvp_IsGroupMember($email, $groupname)
689 {
690         $conn = gwvp_ConnectDB();
691
692         // i think this is right
693         $sql = "
694                         select count(*) from 
695                                 group_membership gm, groups g, users u 
696                         where 
697                                 gm.groupmember_userid=u.users_id and
698                                 u.user_email='$email' and
699                                 gm.groupmember_groupid=g.groups_id and
700                                 g.groups_name='$groupname'
701                         ";
702
703         $res = $conn->query($sql);
704         $result = 0;
705         foreach($res as $u_res) {
706                 $result = $u_res[0];
707         }
708
709         if($result == 0) return false;
710         if($result == 1) return true;
711 }
712
713 function gwvp_IsUserAdmin($email=null, $username = null, $userid = null)
714 {
715         $conn = gwvp_ConnectDB();
716
717
718         // TODO: clean this up, this should be a single query - idiot
719         if($email != null) {
720                 $id = gwvp_getUserId($email);
721                 $sql = "select groupmember_groupid from group_membership where groupmember_userid='$id'";
722         } else if($userid != null) {
723                 $sql = "select groupmember_groupid from group_membership where groupmember_userid='$userid'";
724         } else if($username != null) {
725                 $id = gwvp_getUserId(null, $username);
726                 $sql = "select groupmember_groupid from group_membership where groupmember_userid='$id'";
727         } else return false;
728
729         $res = $conn->query($sql);
730         $rn = 0;
731         $gid = false;
732         foreach($res as $u_res) {
733                 $gid[$rn] = $u_res["groupmember_groupid"];
734                 $rn++;
735         }
736
737         if($gid !== false) foreach($gid as $gid_t) {
738                 /*
739                  *              CREATE TABLE groups (
740                  "groups_id" INTEGER,
741                  "groups_name" TEXT,
742                  "groups_is_admin" INTEGER,
743                  "groups_owner_id" INTEGER
744                  )';
745
746                  */
747
748                 $sql = "select groups_is_admin from groups where groups_id='$gid_t'";
749                 $res = $conn->query($sql);
750                 foreach($res as $u_res) {
751                         if($u_res["groups_is_admin"] == "1") return true;
752                 }
753         }
754
755         return false;
756 }
757
758 function gwvp_ModifyUser($userid, $email=null, $fullname=null, $password=null, $username=null, $desc=null, $status=null)
759 {
760         /*
761          *          "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
762          "user_full_name" TEXT,
763          "user_password" TEXT,
764          "user_username" TEXT,
765          "user_email" TEXT,
766          "user_desc" TEXT,
767          "user_status" INTEGER
768
769          */
770
771         $conn = gwvp_ConnectDB();
772
773         if($email != null) {
774                 $sql = "update users set user_email='$email' where users_id='$userid'";
775                 $conn->query($sql);
776         }
777
778         if($fullname != null) {
779                 $sql = "update users set user_full_name='$fullname' where users_id='$userid'";
780                 $conn->query($sql);
781         }
782
783         if($password != null) {
784                 $shapass = sha1($password);
785                 $sql = "update users set user_password='$shapass' where users_id='$userid'";
786                 $conn->query($sql);
787         }
788
789         if($username != null) {
790                 $sql = "update users set user_username='$username' where users_id='$userid'";
791                 $conn->query($sql);
792         }
793
794         if($desc != null) {
795                 $sql = "update users set user_desc='$desc' where users_id='$userid'";
796                 $conn->query($sql);
797         }
798
799         if($status != null) {
800                 $sql = "update users set user_status='$status' where users_id='$userid'";
801                 $conn->query($sql);
802         }
803
804         return true;
805 }
806
807
808 function gwvp_ModifyGroup($groupid, $groupname = null, $group_is_admin = null, $groups_owner_id = null)
809 {
810         /*
811          *              CREATE TABLE groups (
812          "groups_id" INTEGER,
813          "groups_name" TEXT,
814          "groups_is_admin" INTEGER,
815                 "groups_owner_id" INTEGER
816                 )';
817
818          */
819         $conn = gwvp_ConnectDB();
820
821         if($groupname != null) {
822                 $sql = "update groups set groups_name='$groupname' where groups_id='$groupid'";
823                 $conn->query($sql);
824         }
825
826         if($group_is_admin != null) {
827                 $sql = "update groups set groups_is_admin='$group_is_admin' where groups_id='$groupid'";
828                 $conn->query($sql);
829         }
830
831         if($groups_owner_id != null) {
832                 $sql = "update groups set groups_owner_id='$groups_owner_id' where groups_id='$groupid'";
833                 $conn->query($sql);
834         }
835
836         return true;
837 }
838
839 function gwvp_GetRepoList()
840 {
841         $conn = gwvp_ConnectDB();
842
843         /*
844          *      $reposql = '
845                 CREATE TABLE "repos" (
846                 "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
847                 "repos_name" TEXT,
848                 "repos_description" TEXT,
849                 "repos_owner" INTEGER
850                 )';
851
852          */
853
854         $sql = "select * from repos";
855         
856         $res = $conn->query($sql);
857         
858         $return = false;
859         $rn = 0;
860         foreach($res as $u_res) {
861                 $return[$rn]["id"] = $u_res["repos_id"];
862                 $return[$rn]["name"] = $u_res["repos_name"];
863                 $return[$rn]["description"] = $u_res["repos_description"];
864                 $return[$rn]["owner"] = $u_res["repos_owner"];
865                 $rn++;
866         }
867         
868         return $return;
869 }
870
871 function gwvp_AddRepo($reponame, $repodesc, $repoowner, $defaultperms = 0)
872 {
873         $conn = gwvp_ConnectDB();
874         
875         $sql = "insert into repos values (null, '$reponame', '$repodesc', '$repoowner')";
876         
877         $conn->query($sql);
878         
879         $sql = "select repos_id from repos where repos_name='$reponame'";
880         $res = $conn->query($sql);
881         $rid = -1;
882         foreach($res as $repos) {
883                 $rid = $repos["repos_id"];
884         }
885         /*
886          *              CREATE TABLE "repoperms" (
887                 "repoperms_id" INTEGER PRIMARY KEY AUTOINCREMENT,
888                 "repo_id" INTEGER,
889                 "repoperms_type" TEXT,
890                 "repoperms_ref" TEXT
891
892          */
893         
894         /*
895          * // default perms:
896 // 0 - anyone can clone/read, only owner can write
897 // 1 - noone can clone/read, repo is visible (i.e. name), only owner can read/write repo
898 // 2 - only owner can see anything
899
900          */
901
902         switch($defaultperms) {
903                 case "1":
904                         gwvp_addRepoPermission($rid, "visible", "anon");
905                         break;
906                 case "2":
907                         // by 2, we do nothing, owner already has full perms
908                         break;
909                 default: // 0
910                         gwvp_addRepoPermission($rid, "read", "anon");
911                         
912         }
913 }
914
915 function gwvp_getRepoPermissions($repoid)
916 {
917         /*
918          *      // this looks like null, <repoid>, <read|visible|write>, user:<uid>|group:<gid>|authed|anon
919         // where authed = any authenticated user, anon = everyone (logged in, not logged in, etc)
920         // read|visible|write = can clone from repo|can see repo exists and see description but not clone from it|can push to repo
921         // TODO: is this sufficient? i have to think about it
922         $repoperms = '
923                 CREATE TABLE "repoperms" (
924                 "repoperms_id" INTEGER PRIMARY KEY AUTOINCREMENT,
925                 "repo_id" INTEGER,
926                 "repoperms_type" TEXT,
927                 "repoperms_ref" TEXT
928         )';
929
930          */
931         $conn = gwvp_ConnectDB();
932
933         $sql = "select * from repoperms where repo_id='$repoid'";
934         
935         $res = $conn->query($sql);
936         
937         $returns = false;
938         $rn = 0;
939         foreach($res as $perm) {
940                 $returns[$rn]["permid"] = $perm["repoperms_id"];
941                 $returns[$rn]["type"] = $perm["repoperms_type"];
942                 $returns[$rn]["ref"] = $perm["repoperms_ref"];
943                 $rn++;
944         }
945         
946         return $returns;
947 }
948
949 function gwvp_addRepoPermission($repoid, $permtype, $permref)
950 {
951         $conn = gwvp_ConnectDB();
952         
953         $sql = "insert into repoperms values(null, '$repoid', '$permtype', '$permref')";
954         
955         return $conn->query($sql);
956 }
957 /* functions we'll need to access data:
958  *
959  * getUsers(pattern)
960  * getUserData(username)
961  * getGroups(pattern)
962  * getGroupData(groupname)
963  * modifyGroup(...)
964  * addGroupMember(...)
965  * deleteGroupMember(...)
966  *
967  * createUser(...)
968  * deleteUser(...)
969  * modifyUser(...)
970  * createRepo(...)
971  * deleteRepo(...)
972  * getRepos()
973  */
974
975
976
977 ?>