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