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