Added some form validation bits, with the ability to return posts back
[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($configsql);
79         $DB_CONNECTION->query($groupmemsql);
80 }
81
82 function gwvp_GetUserStatus($userid)
83 {
84         $conn = gwvp_ConnectDB();
85         
86         $sql = "select user_status from users where users_id='$userid'";
87         
88         $res = $conn->query($sql);
89         
90         $return = null;
91         foreach($res as $val) {
92                 $spl = explode(";", $val);
93                 
94                 $return["statusid"] = $spl[0];
95                 $return["extstatus"] = $spl[1];
96         }
97         
98 }
99
100 function gwvp_SetUserStatus($userid, $status, $extendedstatus=null)
101 {
102         /*
103          * user statues
104          * 0 - all good
105          * 1 - locked
106          * 2 - password locked
107          * 3 - awaiting registration completion
108          * 4 - awaiting password reset
109          * where use status = 3,4 the key for unlock is set as the extended status
110          * i.e. if a user goes thru registration, when the validation email gets to
111          * them they'll have a key in their email (128 or 256 bit), thats what
112          * the extended status field is used for
113          */
114         
115         $conn = gwvp_ConnectDB();
116         
117         if($extendedstatus != null) {
118                 $sql = "update users set user_status='$status;$extendedstatus' where users_id='$userid'";
119         } else {
120                 $sql = "update users set user_status='$status;0' where users_id='$userid'";
121         }
122         
123         return $conn->query($sql);
124         
125 }
126
127 function gwvp_forceDisconnect()
128 {
129         
130         global $DB_CONNECTION;
131         
132         $DB_CONNECTION = false;
133 }
134
135
136 function gwvp_getConfigVal($confname)
137 {
138         /*
139          *      $configsql = '
140                 CREATE TABLE "config" (
141             "config_name" TEXT,
142             "config_value" TEXT
143                 )';
144
145          */
146         
147         $conn = gwvp_ConnectDB();
148         
149         $sql = "select * from config where config_name='$confname'";
150         
151         $res = $conn->query($sql);
152         
153         $return = null;
154         foreach($res as $val) {
155                 $return = $val;
156         }
157         
158 }
159
160 function gwvp_eraseConfigVal($confname)
161 {
162         /*
163          *      $configsql = '
164                 CREATE TABLE "config" (
165             "config_name" TEXT,
166             "config_value" TEXT
167                 )';
168
169          */
170         
171         $conn = gwvp_ConnectDB();
172         
173         $sql = "delete from config where config_name='$confname'";
174         
175         return $conn->query($sql);
176 }
177
178 function gwvp_setConfigVal($confname, $confval)
179 {
180         /*
181          *      $configsql = '
182                 CREATE TABLE "config" (
183             "config_name" TEXT,
184             "config_value" TEXT
185                 )';
186
187          */
188         gwvp_eraseConfigVal($confname);
189
190         $conn = gwvp_ConnectDB();
191         
192         $sql = "insert into config values('$confname', '$confval')";
193         
194         return $conn->query($sql);
195 }
196
197
198 function gwvp_isDBSetup()
199 {
200         // for sqlite, we just check if the db exists, for everyone else, we check for a conneciton and go yay or nay
201         global $WEB_ROOT_FS, $BASE_URL, $repo_base, $data_directory, $db_type, $db_url;
202
203         if($db_type == "sqlite") {
204                 if(file_exists($db_url)) return true;
205                 else return false;
206         }
207
208         // TODO now for the connectables
209         // gwvp_ConnectDB();
210 }
211
212 function gwvp_ConnectDB()
213 {
214         global $WEB_ROOT_FS, $BASE_URL, $repo_base, $data_directory, $db_type, $db_name, $DB_CONNECTION;
215
216         // first check if $DB_CONNECTION IS live
217         if($DB_CONNECTION != false) return $DB_CONNECTION;
218
219         if($db_type == "sqlite") {
220                 $db_url = $db_name;
221                 if(!file_exists($db_name)) {
222                         error_log("$db_name does not exist - problem");
223                 }
224         }
225
226         // and here we go with pdo.
227         error_log("attmpting to open db, $db_type:$db_url");
228         try {
229                 $DB_CONNECTION = new PDO("$db_type:$db_url");
230         } catch(PDOException $exep) {
231                 error_log("execpt on db open");
232                 return false;
233         }
234
235         return $DB_CONNECTION;
236 }
237
238 // TODO: we have to define what "Status" is
239 function gwvp_createUser($email, $fullname, $password, $username, $desc, $status)
240 {
241         $conn = gwvp_ConnectDB();
242
243         // TODO: change from sha1
244         $shapass = sha1($password);
245         //error_log("Create user called with $email");
246         $sql = "insert into users values (null, '$fullname', '$shapass', '$username', '$email', '$desc', '$status')";
247         error_log("Creating user, $sql");
248         return $conn->query($sql);
249         /*
250          *          "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
251          "user_full_name" TEXT,
252          "user_password" TEXT,
253          "user_username" TEXT,
254          "user_email" TEXT,
255          "user_desc" TEXT,
256          "user_status" INTEGER
257
258          */
259 }
260
261 function gwvp_getUser($username=null, $email=null, $id=null)
262 {
263         $conn = gwvp_ConnectDB();
264
265         if($username != null) {
266                 $res = $conn->query("select * from users where user_username='$username'");
267         } else if($email != null) {
268                 $res = $conn->query("select * from users where user_email='$email'");
269         } else if($id != null) {
270                 $res = $conn->query("select * from users where users_id='$id'");
271         } else return false;
272
273         $returns = false;
274         foreach($res as $u_res) {
275                 $returns["id"] = $u_res["users_id"];
276                 $returns["fullname"] = $u_res["user_full_name"];
277                 $returns["password"] = $u_res["user_password"];
278                 $returns["username"] = $u_res["user_username"];
279                 $returns["email"] = $u_res["user_email"];
280                 $returns["desc"] = $u_res["user_desc"];
281                 $returns["status"] = $u_res["user_status"];
282         }
283
284         return $returns;
285
286 }
287
288 function gwvp_getUsers()
289 {
290         $conn = gwvp_ConnectDB();
291
292         $res = $conn->query("select * from users");
293
294         $returns = false;
295         $rn = 0;
296         foreach($res as $u_res) {
297                 $returns[$rn]["id"] = $u_res["users_id"];
298                 $returns[$rn]["fullname"] = $u_res["user_full_name"];
299                 $returns[$rn]["password"] = $u_res["user_password"];
300                 $returns[$rn]["username"] = $u_res["user_username"];
301                 $returns[$rn]["email"] = $u_res["user_email"];
302                 $returns[$rn]["desc"] = $u_res["user_desc"];
303                 $returns[$rn]["status"] = $u_res["user_status"];
304                 $rn++;
305         }
306
307         return $returns;
308 }
309
310 function gwvp_deleteUser($email)
311 {
312         $conn = gwvp_ConnectDB();
313
314         $sql = "delete from users where user_email='$email'";
315
316         $conn->query($sql);
317 }
318
319 function gwvp_createGroup($group_name, $group_desc, $is_admin, $owner_id)
320 {
321         $conn = gwvp_ConnectDB();
322
323         /*
324          *              CREATE TABLE groups (
325          "groups_id" INTEGER,
326          "groups_name" TEXT,
327          "groups_is_admin" INTEGER,
328                 "groups_owner_id" INTEGER
329                 )';
330
331          */
332         if($is_admin) {
333                 $is_admin_t = 1;
334         } else {
335                 $is_admin_t = 0;
336         }
337         $sql = "insert into groups values( null, '$group_name', '$group_desc', '$is_admin_t', '$owner_id')";
338         
339
340         $conn->query($sql);
341
342 }
343
344 function gwvp_deleteGroup($groupname)
345 {
346         $conn = gwvp_ConnectDB();
347
348         $sql = "delete from groups where groups_name='$groupname'";
349
350         $conn->query($sql);
351 }
352
353 function gwvp_getGroupsForUser($email)
354 {
355         $conn = gwvp_ConnectDB();
356
357         /*
358          select g.groups_name from
359          group_membership gm, groups g, users u
360          where
361          gm.groupmember_userid=u.users_id and
362          u.user_email='$email' and
363          gm.groupmember_groupid=g.groups_id and
364          g.groups_name='$groupname'
365          */
366
367         $sql = "
368                         select g.groups_name from 
369                                 group_membership gm, groups g, users u 
370                         where 
371                                 gm.groupmember_userid=u.users_id and
372                                 u.user_email='$email' and
373                                 gm.groupmember_groupid=g.groups_id
374         ";
375
376         $res = $conn->query($sql);
377
378         $return = false;
379         $rn = 0;
380         foreach($res as $u_res) {
381                 $return[$rn] = $u_res[0];
382                 $rn++;
383         }
384
385         return $return;
386 }
387
388 function gwvp_getGroupsOwnedByUser($email)
389 {
390         $conn = gwvp_ConnectDB();
391
392         /*
393          select g.groups_name from
394          group_membership gm, groups g, users u
395          where
396          gm.groupmember_userid=u.users_id and
397          u.user_email='$email' and
398          gm.groupmember_groupid=g.groups_id and
399          g.groups_name='$groupname'
400          */
401
402         $sql = "
403                         select g.groups_name from 
404                                 groups g, users u 
405                         where 
406                                 u.user_email='$email' and
407                                 u.users_id=g.groups_owner_id
408         ";
409
410         $res = $conn->query($sql);
411
412         $return = false;
413         $rn = 0;
414         foreach($res as $u_res) {
415                 $return[$rn] = $u_res[0];
416                 $rn++;
417         }
418
419         return $return;
420
421 }
422
423 function gwvp_groupOwner($groupname)
424 {
425         $conn = gwvp_ConnectDB();
426
427         $sql = "select u.user_email from users u, groups g where g.groups_name='$groupname' and g.groups_owner_id=u.users_id";
428
429         $res = $conn->query($sql);
430         $return = false;
431         foreach($res as $u_res) {
432                 $return = $u_res[0];
433         }
434
435         return $return;
436 }
437
438 function gwvp_getGroups()
439 {
440         $conn = gwvp_ConnectDB();
441
442         $res = $conn->query("select * from groups");
443
444         $returns = false;
445         $rn = 0;
446         foreach($res as $u_res) {
447                 $returns[$rn]["id"] = $u_res["groups_id"];
448                 $returns[$rn]["name"] = $u_res["groups_name"];
449                 if($u_res["groups_is_admin"]=="1") $return[$rn]["admin"] = true;
450                 else $return[$rn]["admin"] = false;
451                 $returns[$rn]["admin"] = $u_res["groups_is_admin"];
452                 $returns[$rn]["ownerid"] = $u_res["groups_owner_id"];
453                 $rn++;
454         }
455
456         return $returns;
457 }
458
459 function gwvp_getGroupId($groupname)
460 {
461         $conn = gwvp_ConnectDB();
462
463         $sql = "select groups_id from groups where groups_name='$groupname'";
464
465         $res = $conn->query($sql);
466         $return = false;
467         foreach($res as $u_res) {
468                 $return = $u_res["groups_id"];
469         }
470
471         return $return;
472 }
473
474 function gwvp_getGroup($gid = null, $gname = null)
475 {
476         /* 
477          *      $groupsql = '
478                 CREATE TABLE groups (
479             "groups_id" INTEGER PRIMARY KEY AUTOINCREMENT,
480             "groups_name" TEXT,
481             "groups_is_admin" INTEGER,
482                 "groups_owner_id" INTEGER
483                 )';
484
485          */
486         $conn = gwvp_ConnectDB();
487         
488         if($gid != null) {
489                 $sql = "select * from groups where groups_id='$gid'";
490         } else if ($gname != null) {
491                 $sql = "select * from groups where groups_name='$gname'";
492         } else return false;
493         
494         $res = $conn->query($sql);
495         $return = false;
496         foreach($res as $u_res) {
497                 $return["id"] = $u_res["groups_id"];
498                 $return["name"] = $u_res["groups_name"];
499                 if($u_res["groups_is_admin"] == 1) {
500                         $return["isadmin"] = true;
501                 } else {
502                         $return["isadmin"] = false;
503                 }
504                 $return["ownerid"] = $u_res["groups_owner_id"];
505                 $return["description"] = $u_res["groups_description"];
506         }
507         
508         return $return;
509 }
510
511 function gwvp_getUserId($useremail=null, $username = null)
512 {
513         $conn = gwvp_ConnectDB();
514
515         if($useremail != null) {
516                 $sql = "select users_id from users where user_email='$useremail'";
517         } else if($username != null) {
518                 $sql = "select users_id from users where user_username='$username'";
519         } else return false;
520
521         $res = $conn->query($sql);
522         $return = false;
523         foreach($res as $u_res) {
524                 $return = $u_res["users_id"];
525         }
526
527         return $return;
528 }
529
530 function gwvp_getUserName($id = null, $email=null)
531 {
532         $conn = gwvp_ConnectDB();
533
534         if($email != null) { 
535                 $sql = "select user_username from users where user_email='$email'";
536         } else if($id != null) {
537                 $sql = "select user_username from users where users_id='$id'";
538         } else return false;
539
540         $res = $conn->query($sql);
541         $return = false;
542         foreach($res as $u_res) {
543                 $return = $u_res["user_username"];
544         }
545
546         return $return;
547 }
548
549
550 function gwvp_getUserEmail($id)
551 {
552         $conn = gwvp_ConnectDB();
553
554         $sql = "select user_email from users where users_id='$id'";
555
556         $res = $conn->query($sql);
557         $return = false;
558         foreach($res as $u_res) {
559                 $return = $u_res["user_email"];
560         }
561
562         return $return;
563 }
564
565 function gwvp_deleteGroupMemberByID($uid, $gid)
566 {
567         $conn = gwvp_ConnectDB();
568
569         /*
570          *              CREATE TABLE "group_membership" (
571          "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
572          "groupmember_groupid" INTEGER,
573          "groupmember_userid" INTEGER
574
575          */
576         $sql = "delete from group_membership where groupmember_groupid='$gid' and  groupmember_userid='$uid'";
577
578         $conn->query($sql);
579
580         return true;
581 }
582
583
584 function gwvp_addGroupMemberByID($uid, $gid)
585 {
586         $conn = gwvp_ConnectDB();
587
588         /*
589          *              CREATE TABLE "group_membership" (
590          "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
591          "groupmember_groupid" INTEGER,
592          "groupmember_userid" INTEGER
593
594          */
595         $sql = "insert into group_membership values (null, '$gid', '$uid')";
596
597         $conn->query($sql);
598
599         return true;
600 }
601
602
603 function gwvp_addGroupMember($email, $groupname)
604 {
605         $conn = gwvp_ConnectDB();
606
607         $uid = gwvp_getUserId($email);
608         $gid = gwvp_getGroupId($groupname);
609
610         /*
611          *              CREATE TABLE "group_membership" (
612          "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
613          "groupmember_groupid" INTEGER,
614          "groupmember_userid" INTEGER
615
616          */
617         if($uid!=false&&$gid!=false) gwvp_addGroupMemberByID($uid, $gid);
618         else return false;
619
620         $conn->query($sql);
621
622         return true;
623 }
624
625 function gwvp_IsGroupMember($email, $groupname)
626 {
627         $conn = gwvp_ConnectDB();
628
629         // i think this is right
630         $sql = "
631                         select count(*) from 
632                                 group_membership gm, groups g, users u 
633                         where 
634                                 gm.groupmember_userid=u.users_id and
635                                 u.user_email='$email' and
636                                 gm.groupmember_groupid=g.groups_id and
637                                 g.groups_name='$groupname'
638                         ";
639
640         $res = $conn->query($sql);
641         $result = 0;
642         foreach($res as $u_res) {
643                 $result = $u_res[0];
644         }
645
646         if($result == 0) return false;
647         if($result == 1) return true;
648 }
649
650 function gwvp_IsUserAdmin($email=null, $username = null)
651 {
652         $conn = gwvp_ConnectDB();
653
654
655         // TODO: clean this up, this should be a single query - idiot
656         if($email != null) {
657                 $id = gwvp_getUserId($email);
658                 $sql = "select groupmember_groupid from group_membership where groupmember_userid='$id'";
659         } else if($username != null) {
660                 $id = gwvp_getUserId(null, $username);
661                 $sql = "select groupmember_groupid from group_membership where groupmember_userid='$id'";
662         } else return false;
663
664         $res = $conn->query($sql);
665         $rn = 0;
666         $gid = false;
667         foreach($res as $u_res) {
668                 $gid[$rn] = $u_res["groupmember_groupid"];
669                 $rn++;
670         }
671
672         if($gid !== false) foreach($gid as $gid_t) {
673                 /*
674                  *              CREATE TABLE groups (
675                  "groups_id" INTEGER,
676                  "groups_name" TEXT,
677                  "groups_is_admin" INTEGER,
678                  "groups_owner_id" INTEGER
679                  )';
680
681                  */
682
683                 $sql = "select groups_is_admin from groups where groups_id='$gid_t'";
684                 $res = $conn->query($sql);
685                 foreach($res as $u_res) {
686                         if($u_res["groups_is_admin"] == "1") return true;
687                 }
688         }
689
690         return false;
691 }
692
693 function gwvp_ModifyUser($userid, $email=null, $fullname=null, $password=null, $username=null, $desc=null, $status=null)
694 {
695         /*
696          *          "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
697          "user_full_name" TEXT,
698          "user_password" TEXT,
699          "user_username" TEXT,
700          "user_email" TEXT,
701          "user_desc" TEXT,
702          "user_status" INTEGER
703
704          */
705
706         $conn = gwvp_ConnectDB();
707
708         if($email != null) {
709                 $sql = "update users set user_email='$email' where users_id='$userid'";
710                 $conn->query($sql);
711         }
712
713         if($fullname != null) {
714                 $sql = "update users set user_full_name='$fullname' where users_id='$userid'";
715                 $conn->query($sql);
716         }
717
718         if($password != null) {
719                 $shapass = sha1($password);
720                 $sql = "update users set user_password='$shapass' where users_id='$userid'";
721                 $conn->query($sql);
722         }
723
724         if($username != null) {
725                 $sql = "update users set user_username='$username' where users_id='$userid'";
726                 $conn->query($sql);
727         }
728
729         if($desc != null) {
730                 $sql = "update users set user_desc='$desc' where users_id='$userid'";
731                 $conn->query($sql);
732         }
733
734         if($status != null) {
735                 $sql = "update users set user_status='$status' where users_id='$userid'";
736                 $conn->query($sql);
737         }
738
739         return true;
740 }
741
742
743 function gwvp_ModifyGroup($groupid, $groupname = null, $group_is_admin = null, $groups_owner_id = null)
744 {
745         /*
746          *              CREATE TABLE groups (
747          "groups_id" INTEGER,
748          "groups_name" TEXT,
749          "groups_is_admin" INTEGER,
750                 "groups_owner_id" INTEGER
751                 )';
752
753          */
754         $conn = gwvp_ConnectDB();
755
756         if($groupname != null) {
757                 $sql = "update groups set groups_name='$groupname' where groups_id='$groupid'";
758                 $conn->query($sql);
759         }
760
761         if($group_is_admin != null) {
762                 $sql = "update groups set groups_is_admin='$group_is_admin' where groups_id='$groupid'";
763                 $conn->query($sql);
764         }
765
766         if($groups_owner_id != null) {
767                 $sql = "update groups set groups_owner_id='$groups_owner_id' where groups_id='$groupid'";
768                 $conn->query($sql);
769         }
770
771         return true;
772 }
773
774 function gwvp_AddRepo($reponame, $repodesc, $repoowner)
775 {
776         $conn = gwvp_ConnectDB();
777         
778         $sql = "insert into repos values (null, '$reponame', '$repodesc', '$repoowner')";
779         
780         $conn->query($sql);
781 }
782
783 function gwvp_GetRepoList()
784 {
785         $conn = gwvp_ConnectDB();
786
787         /*
788          *      $reposql = '
789                 CREATE TABLE "repos" (
790                 "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
791                 "repos_name" TEXT,
792                 "repos_description" TEXT,
793                 "repos_owner" INTEGER
794                 )';
795
796          */
797
798         $sql = "select * from repos";
799         
800         $res = $conn->query($sql);
801         
802         $return = false;
803         $rn = 0;
804         foreach($res as $u_res) {
805                 $return[$rn]["id"] = $u_res["repos_id"];
806                 $return[$rn]["name"] = $u_res["repos_name"];
807                 $return[$rn]["description"] = $u_res["repos_description"];
808                 $return[$rn]["owner"] = $u_res["repos_owner"];
809                 $rn++;
810         }
811         
812         return $return;
813 }
814 /* functions we'll need to access data:
815  *
816  * getUsers(pattern)
817  * getUserData(username)
818  * getGroups(pattern)
819  * getGroupData(groupname)
820  * modifyGroup(...)
821  * addGroupMember(...)
822  * deleteGroupMember(...)
823  *
824  * createUser(...)
825  * deleteUser(...)
826  * modifyUser(...)
827  * createRepo(...)
828  * deleteRepo(...)
829  * getRepos()
830  */
831
832
833
834 ?>