started some input validation code and unit tests for it
[gwvp.git] / gwvplib / gwvpuseradmin.php
1 <?php
2
3 // setup the call me function for useradmin - matches on url of admin/users
4 $CALL_ME_FUNCTIONS["useradmin"] = "gwvp_UserAdminCallMe";
5
6 $MENU_ITEMS["10users"]["text"] = "Users/Groups";
7 $MENU_ITEMS["10users"]["link"] = "$BASE_URL/admin/users";
8
9
10 function gwvp_UserAdminCallMe()
11 {
12         if(isset($_REQUEST["q"])) {
13                 $query = $_REQUEST["q"];
14                 $qspl = explode("/", $query);
15                 if(isset($qspl[0]) && isset($qspl[1])) {
16                         if($qspl[0] == "admin" && $qspl[1] == "users") {
17                                 if(isset($qspl[2])) {
18                                         switch($qspl[2]) {
19                                                 case "adduser":
20                                                         return "gwvp_AddUserPage";
21                                                         break;
22                                                 case "groupmember":
23                                                         return "gwvp_groupMemberChange";
24                                                         break;
25                                                 case "addgroup":
26                                                         return "gwvp_AddGroupPage";
27                                                         break;
28                                                 case "modify":
29                                                         return "gwvp_ModifyUserPage";
30                                                         break;
31                                                 case "modifygroup":
32                                                         return "gwvp_ModifyGroupPage";
33                                                         break;
34                                                 default:
35                                                         return "gwvp_UserAdminPage";
36                                         }                                       
37                                 } else {
38                                         return "gwvp_UserAdminPage";
39                                 }
40                         }
41                 } 
42                 else return false;
43         }
44         
45         return false;
46 }
47
48 function gwvp_AddUserPage()
49 {
50         global $BASE_URL;
51         
52         $email = $_REQUEST["email"];
53         $fullname = $_REQUEST["fullname"];
54         $pass1 = $_REQUEST["pass1"];
55         $pass2 = $_REQUEST["pass2"];
56         $username = $_REQUEST["username"];
57         $desc = $_REQUEST["desc"];
58         
59         // TODO: we need to do alot of checking here - that can come later
60         if(gwvp_createUser($email, $fullname, $pass1, $username, $desc, 0)) {
61                 gwvp_SendMessage("info", "user $username, $fullname ($email) created");
62                 header("Location: $BASE_URL/admin/users");
63                 return;
64         } else {
65                 gwvp_SendMessage("error", "error creating user for some unknown reason");
66                 header("Location: $BASE_URL/admin/users");
67                 return;
68         }
69
70         
71         /*
72                 echo "<tr><td>EMail</td><td><input type=\"text\" name=\"email\"></td>";
73                 echo "<td>Full Name</td><td><input type=\"text\" name=\"fullname\"></td></tr>";
74                 echo "<tr><td>Password</td><td><input type=\"text\" name=\"pass1\"></td>";
75                 echo "<td>Password Confirm</td><td><input type=\"text\" name=\"pass2\"></td></tr>";
76                 echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td>";
77                 echo "<td>Description</td><td><input type=\"text\" name=\"desc\"></td></tr>";
78                 */
79         
80 }
81
82 function gwvp_AddGroupPage()
83 {
84         /*
85          *              echo "<tr><td>Group Name</td><td><input type=\"text\" name=\"groupname\"><td></tr>";
86                 echo "<tr><td>Admin Group?</td><td><input type=\"checkbox\" name=\"admingroup\" class=\"mycheckbox\"></td></tr>";
87                 echo "<tr><td>Owner</td><td><div><select class=\"myselect\" name=\"groupowner\">";
88
89          */
90         global $BASE_URL;
91         
92         $gname = $_REQUEST["groupname"];
93         $isadmin = isset($_REQUEST["admingroup"]);
94         $gdesc = $_REQUEST["groupdesc"];
95         $owner = $_REQUEST["groupowner"];
96         
97         // gwvp_createGroup($group_name, $is_admin, $owner_id)
98         gwvp_createGroup("$gname", $gdesc, $isadmin, $owner);
99         // we also need to add the owner to the group
100         gwvp_addGroupMember(gwvp_getUserName($owner), $gname);
101         
102         
103         header("Location: $BASE_URL/admin/users");
104         return;
105         
106 }
107
108 function gwvp_UserAdminPage()
109 {
110         gwvp_goMainPage("gwvp_UserAdminPageBody");
111 }
112
113 function gwvp_ModifyUserPage()
114 {
115         gwvp_goMainPage("gwvp_ModifyUserPageBody");
116 }
117
118 function gwvp_ModifyGroupPage()
119 {
120         gwvp_goMainPage("gwvp_ModifyGroupPageBody");
121 }
122
123 function gwvp_ModifyUserPageBody()
124 {
125         //error_log("modify user body - coming in");
126         if(!gwvp_CheckAuthLevel("admin")) {
127                 gwvp_AuthNoPermsBody();
128                 return;
129         }
130         
131         
132         $uid = -1;
133         if(isset($_REQUEST["q"])) {
134                 $query = $_REQUEST["q"];
135                 $qspl = explode("/", $query);
136                 $uid = $qspl[3];
137         }
138         
139         echo "modify user $uid";
140 }
141
142 function gwvp_groupMemberChange()
143 {
144         global $LOGIN_TYPE, $BASE_URL;
145         
146         $gid = -1;
147         
148         if(isset($_REQUEST["q"])) {
149                 $query = $_REQUEST["q"];
150                 $qspl = explode("/", $query);
151                 $gid = $qspl[3];
152         }
153         error_log("into groupmemberchange with $gid");
154         if($gid!= -1) {
155                 
156         }
157         
158         if(isset($_REQUEST["add"])) {
159                 error_log("$gid add set to ".$_REQUEST["add"]);
160                 // this is an add op
161                 if(isset($_REQUEST["membersout"])) {
162                         foreach($_REQUEST["membersout"] as $uid) {
163                                 error_log("would add $uid from $gid");
164                                 gwvp_addGroupMemberByID($uid, $gid);
165                                 
166                         }
167                 }
168         }
169         if(isset($_REQUEST["remove"])) {
170                 error_log("$gid remove set to ".$_REQUEST["remove"]);
171                 // this is a remove op
172                 if(isset($_REQUEST["membersin"])) {
173                         foreach($_REQUEST["membersin"] as $uid) {
174                                 error_log("would remote $uid from $gid");
175                                 gwvp_deleteGroupMemberByID($uid, $gid);
176                         }
177                 }
178         }
179         
180         error_log("redirect to $BASE_URL/admin/users/modifygroup/$gid");
181         header("Location: $BASE_URL/admin/users/modifygroup/$gid");
182         //gwvp_goMainPage("gwvp_ModifyGroupPageBody");
183         
184         return;
185 }
186
187 function gwvp_ModifyGroupPageBody()
188 {
189         global $LOGIN_TYPE, $BASE_URL;
190         
191         $gid = -1;
192         if(isset($_REQUEST["q"])) {
193                 $query = $_REQUEST["q"];
194                 $qspl = explode("/", $query);
195                 $gid = $qspl[3];
196         }
197         
198         $users = gwvp_getUsers();
199         
200         $ginfo = gwvp_getGroup($gid);
201         $groupname = $ginfo["name"];
202         $groupdesc = $ginfo["description"];
203         if($ginfo["isadmin"]) $admin = "checked";
204         else $admin = "";
205         
206         echo "<h2>Modify Group - $groupname</h2>";
207         echo "<form method=\"post\" action=\"$BASE_URL/admin/users/groupmodify/$gid\">";
208         echo "<table>";
209         echo "<tr><td>Description</td><td><input type=\"text\" name=\"groupdesc\" value=\"$groupdesc\"></td></tr>";
210         echo "<tr><td>Admin</td><td><input type=\"checkbox\" name=\"is_admin\" $admin></td></tr>";
211         echo "<tr><td><input type=\"submit\" name=\"change\" value=\"Change\"></td></tr>";
212         
213         echo "</table>";
214         echo "</form>";
215         
216         echo "<h3>Group Membership</h3>";
217         echo "<form method=\"post\" action=\"$BASE_URL/admin/users/groupmember/$gid\">";
218         echo "<table border=\"1\"><tr><th>Members</th><th></th><th>All Users</th></tr>";
219         
220         echo "<tr><td>";
221         
222         
223         // members
224         echo "<select name=\"membersin[]\" size=\"20\" multiple=\"true\">";
225         foreach($users as $u_users) {
226                 $uid = $u_users["id"];
227                 $email = $u_users["email"];
228                 $username = $u_users["username"];
229                 $fullname = $u_users["fullname"];
230                 if(gwvp_IsGroupMember($email, $groupname)) {
231                         echo "<option value=\"$uid\">$username, $fullname ($email)</option>";
232                 }
233                 
234
235         }
236         
237         echo "</select>";
238         
239         
240         echo "</td><td>";
241         
242         // move buttons
243         echo "<input type=\"submit\" name=\"add\" value=\"<<\"><br>";
244         echo "<input type=\"submit\" name=\"remove\" value=\">>\"><br>";
245
246         
247         
248         echo "</td><td>";
249         
250         
251         // all users
252         echo "<select name=\"membersout[]\" size=\"20\" multiple=\"true\">";
253         foreach($users as $u_users) {
254                 $uid = $u_users["id"];
255                 $email = $u_users["email"];
256                 $username = $u_users["username"];
257                 $fullname = $u_users["fullname"];
258                 if(!gwvp_IsGroupMember($email, $groupname)) {
259                         echo "<option value=\"$uid\">$username, $fullname ($email)</option>";
260                 }
261         }
262         echo "</select>";
263         
264         
265         
266         echo "</td></tr></table>";
267         echo "</form>";
268         
269         
270         
271         
272         
273         return;
274 }
275
276 function gwvp_UserAdminPageBody()
277 {
278         global $LOGIN_TYPE, $BASE_URL;
279         
280         $groups = gwvp_getGroups();
281         $users = gwvp_getUsers();
282         
283         echo "<h2>Users and Groups</h2>";
284         echo "On this page you can manage users, groups, group membership and update your profile<br>";
285         echo "<table>";
286         
287         echo "<tr><td colspan=\"2\"><hr></td></tr>";
288         
289         
290         // Header part of table
291         // user self-management bit
292         if($LOGIN_TYPE != "anon") {
293                 echo "<tr><td valign=\"top\"><h3>My Profile</h3></td><td><h3>My Groups</h3></td></tr>";
294                 
295                 echo "<tr>";
296                 // user profile bit
297                 echo "<td valign=\"top\">";
298                 echo "User profile bits go here";
299                 echo "</td>";
300                 
301                 // now the group bit for the user
302                 echo "<td valign=\"top\">";
303                 echo "User owned groups, and groups their a member of go here";
304                 echo "</td>";
305                 echo "</tr>";
306                 
307                 echo "<tr><td colspan=\"2\"><hr></td></tr>";
308         }
309         
310         // admin only bit
311         if($LOGIN_TYPE == "admin") {
312                 echo "<tr><td valign=\"top\"><h3>Create User</h3></td><td><h3>Create Group</h3></td></tr>";
313                 
314                 // create user bit
315                 echo "<tr><td valign=\"top\">";
316                 
317                 echo "<form method=\"post\" action=\"$BASE_URL/admin/users/adduser\">";
318                 echo "<table>";
319                 echo "<tr><td>EMail</td><td><input type=\"text\" name=\"email\"></td>";
320                 echo "<td>Full Name</td><td><input type=\"text\" name=\"fullname\"></td></tr>";
321                 echo "<tr><td>Password</td><td><input type=\"text\" name=\"pass1\"></td>";
322                 echo "<td>Password Confirm</td><td><input type=\"text\" name=\"pass2\"></td></tr>";
323                 echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td>";
324                 echo "<td>Description</td><td><input type=\"text\" name=\"desc\"></td></tr>";
325                 echo "<tr><td><input type=\"submit\" name=\"Create\" value=\"Create\" class=\"buttons\"></td></tr>";
326                 echo "</table>";
327                 echo "</form>";
328                 
329                 echo "</td><td valign=\"top\">";
330                 
331                 // Create group
332                 echo "<form method=\"post\" action=\"$BASE_URL/admin/users/addgroup\">";
333                 echo "<table>";
334                 echo "<tr><td>Group Name</td><td><input type=\"text\" name=\"groupname\"><td></tr>";
335                 echo "<tr><td>Group Description</td><td><input type=\"text\" name=\"groupdesc\"><td></tr>";
336                 echo "<tr><td>Admin Group?</td><td><input type=\"checkbox\" name=\"admingroup\" class=\"mycheckbox\"></td></tr>";
337                 echo "<tr><td>Owner</td><td><div><select class=\"myselect\" name=\"groupowner\">";
338                 foreach($users as $u_users) {
339                         $uid = $u_users["id"];
340                         $email = $u_users["email"];
341                         $username = $u_users["username"];
342                         $fullname = $u_users["fullname"];
343                         echo "<option value=\"$uid\">$username, $fullname ($email)</option>";
344                 }
345                 echo "</select></div></td></tr>";
346                 
347                 
348                 
349                 echo "<tr><td><input type=\"submit\" name=\"Create\" value=\"Create\" class=\"buttons\"></td></tr>";
350                 echo "</table>";
351                 echo "</form>";
352                 
353                 
354                 echo "</td></tr>";
355                 echo "<tr><td colspan=\"2\"><hr></td></tr>";
356         }
357         
358         
359         
360         // TODO: whats seen here will depend GREATLY on setting in config - need to fix this later
361         // user list
362         echo "<tr><td valign=\"top\"><h3>Users</h3></td><td><h3>Groups</h3></td></tr>";
363         
364         echo "<tr><td>";
365         echo "<table border=\"1\">";
366         switch($LOGIN_TYPE) {
367                 case "anon":
368                         echo "<tr><th>Username</th><th>Groups</th><th>Description</th></tr>";
369                         break;
370                 case "admin":
371                         echo "<tr><th>EMail</th><th>Username</th><th>Full Name</th><th>Groups</th><th>Description</th><th>Admin?</th><th>Status</th><th>Modify</th></tr>";
372                         break;
373                 case "user":
374                         echo "<tr><th>Username</th><th>Groups</th><th>Description</th></tr>";
375                         break;
376         }
377                 
378         /*
379          *              $returns[$rn]["id"] = $u_res["users_id"];
380                 $returns[$rn]["fullname"] = $u_res["user_full_name"];
381                 $returns[$rn]["password"] = $u_res["user_password"];
382                 $returns[$rn]["username"] = $u_res["user_username"];
383                 $returns[$rn]["email"] = $u_res["user_email"];
384                 $returns[$rn]["desc"] = $u_res["user_desc"];
385                 $returns[$rn]["status"] = $u_res["user_status"];
386
387          */
388         foreach($users as $u_users) {
389                 $userid = $u_users["id"];
390                 $email = $u_users["email"];
391                 $fullname = $u_users["fullname"];
392                 $username = $u_users["username"];
393                 $desc = $u_users["desc"];
394                 $status = $u_users["status"];
395                 if(gwvp_IsUserAdmin($email) == 1) {
396                         $globaladmin = "Yes";
397                 } else {
398                         $globaladmin = "No";
399                 }
400                 
401                 
402                 // TODO: sort out group prints here
403                 $ingroups = gwvp_getGroupsForUser($email);
404                 
405                 $ownedgroups = gwvp_getGroupsOwnedByUser($email);
406                 //echo "vardump: ";
407                 //var_dump($ownedgroups);
408                 $ugroups = "";
409                 if($ownedgroups == false) $ogroups = "-";
410                 else {
411                         foreach($ownedgroups as $gr_u) {
412                                 $ugroups .= "<font color=\"#3333ff\">$gr_u</font><br>";
413                         }
414                 }
415                 trim($ugroups);
416                 
417                 
418                 if($ingroups !== false) foreach($ingroups as $grname) {
419                         $isownedgroup = false;
420                         if($ownedgroups !== false) foreach($ownedgroups as $gr_u) {
421                                 if($grname == $gr_u) $isownedgroup = true;
422                         }
423                         if(!$isownedgroup) $ugroups .= "$grname<br>";
424                 }
425                 trim($ugroups);
426                 
427                 switch($LOGIN_TYPE) {
428                         case "anon":
429                                 echo "<tr><td>$username</td><td>$ugroups</td><td>$desc</td></tr>";
430                                 break;
431                         case "admin":
432                                 echo "<tr><td>$email</td><td>$username</td><td>$fullname</td><td>$ugroups</td><td>$desc</td>";
433                                 echo "<td>$globaladmin</td><td>$status</td><td><a href=\"$BASE_URL/admin/users/modify/$userid\">Modify</a></td></tr>";
434                                 break;
435                         case "user":
436                                 echo "<tr><td>$username</td><td>$ugroups</td><td>$desc</td></tr>";
437                                 break;
438                 }
439                 
440                 
441         }
442         echo "</table>";
443         
444         echo "</td><td valign=\"top\">";
445         
446         // group part of table
447         
448         echo "<table border=\"1\">";
449         
450         switch($LOGIN_TYPE) {
451                 case "anon":
452                         echo "<tr><th>Group Name</th><th>Owner</th></tr>";
453                         break;
454                 case "admin":
455                         echo "<tr><th>Group Name</th><th>Owner</th><th>Admin Group?</th><th>Modify</th></tr>";
456                         break;
457                 case "user":
458                         echo "<tr><th>Group Name</th><th>Owner</th><th>Admin Group?</th></tr>";
459                         break;
460         }
461         
462         foreach($groups as $u_groups) {
463                 /*
464                  *              $returns[$rn]["id"] = $u_res["groups_id"];
465                 $returns[$rn]["name"] = $u_res["groups_name"];
466                 if($u_res["groups_is_admin"]=="1") $return[$rn]["admin"] = true;
467                 else $return[$rn]["admin"] = false;
468                 $returns[$rn]["admin"] = $u_res["groups_is_admin"];
469                 $returns[$rn]["ownerid"] = $u_res["groups_owner_id"];
470
471                  */
472                 $gname = $u_groups["name"];
473                 $gid = $u_groups["id"];
474                 $owner = gwvp_getUserEmail($u_groups["ownerid"]);
475                 if($u_groups["admin"]) $gadmin = "Yes";
476                 else $gadmin  = "No";
477
478                 switch($LOGIN_TYPE) {
479                         case "anon":
480                                 echo "<tr><td>$gname</td><td>$owner</td></tr>";
481                                 break;
482                         case "admin":
483                                 echo "<tr><td>$gname</td><td>$owner</td><td>$gadmin</td><td><a href=\"$BASE_URL/admin/users/modifygroup/$gid\">Modify</a></td></tr>";
484                                 break;
485                         case "user":
486                                 echo "<tr><td>$gname</td><td>$owner</td><td>$gadmin</td></tr>";
487                                 break;
488                 }
489                 
490                 
491         }
492         echo "</table>";
493         echo "</td></tr></table>";
494 }
495
496
497 ?>