moved the old git poc code off and away from the main directory
[gwvp.git] / gwvplib / gwvprepoadmin.php
1 <?php
2 $CALL_ME_FUNCTIONS["repoadmin"] = "gwvp_RepoAdminCallMe";
3
4 $MENU_ITEMS["20repos"]["text"] = "Repo Admin";
5 $MENU_ITEMS["20repos"]["link"] = "$BASE_URL/admin/repos";
6
7 function gwvp_RepoAdminCallMe()
8 {
9         
10         error_log("in repoadmin callme");
11         if(isset($_REQUEST["q"])) {
12                 $query = $_REQUEST["q"];
13                 $qspl = explode("/", $query);
14                 if(isset($qspl[0]) && isset($qspl[1])) {
15                         if($qspl[0] == "admin" && $qspl[1] == "repos") {
16                                 error_log("i got here, where next?");
17                                 if(isset($qspl[2])) {
18                                         switch($qspl[2]) {
19                                                 case "create":
20                                                         return "gwvp_CreateRepoPage";
21                                                         break;
22                                                 case "docreate":
23                                                         return "gwvp_DoCreateRepoPage";
24                                                         break;
25                                                 case "manage":
26                                                         return "gwvp_ManageRepoPage";
27                                                         break;
28                                                 case "updateperms":
29                                                         return "gwvp_UpdateRepoPerms";
30                                                         break;
31                                                 case "details":
32                                                         return "gwvp_RepoDetailsPage";
33                                                         break;
34                                                 default:
35                                                         return "gwvp_RepoAdminPage";
36                                         }                                       
37                                 } else {
38                                         return "gwvp_RepoAdminPage";
39                                 }
40                         }
41                 } 
42                 else return false;
43         }
44         
45         return false;
46 }
47
48 function gwvp_ManageRepoPage()
49 {
50         gwvp_goMainPage("gwvp_ManageRepoPageBody");
51 }
52
53 function gwvp_RepoAdminPage()
54 {
55         gwvp_goMainPage("gwvp_RepoAdminPageBody");
56 }
57
58 function gwvp_CreateRepoPage()
59 {
60         gwvp_goMainPage("gwvp_CreateRepoPageBody");
61 }
62
63 function gwvp_RepoDetailsPage()
64 {
65         gwvp_goMainPage("gwvp_RepoDetailsPageBody");
66 }
67
68 function gwvp_RepoDetailsPageBody()
69 {
70         echo "I am a repo details page";
71 }
72
73 function gwvp_UpdateRepoPerms()
74 {
75         global $LOGIN_TYPE, $BASE_URL;
76         
77         $rid = -1;
78         if(isset($_REQUEST["q"])) {
79                 $query = $_REQUEST["q"];
80                 $qspl = explode("/", $query);
81                 $rid = $qspl[3];
82         }
83
84         // a visible addition
85         if(isset($_REQUEST["visadd"])) {
86                 foreach($_REQUEST["vismembersout"] as $mems) {
87                         //gwvp_addRepoPermission($repoid, $permtype, $permref);
88                         gwvp_addRepoPermission($rid, "visible", $mems);
89                 }
90         }
91         
92         // a visible removal
93         if(isset($_REQUEST["visremove"])) {
94                 foreach($_REQUEST["vismembersin"] as $rems) {
95                         error_log("Remove permid, $rems");
96                         gwvp_removeRepoPermission($rems);
97                 }
98         }
99
100         // a read addition
101         if(isset($_REQUEST["readadd"])) {
102                 foreach($_REQUEST["readmembersout"] as $mems) {
103                         //gwvp_addRepoPermission($repoid, $permtype, $permref);
104                         gwvp_addRepoPermission($rid, "read", $mems);
105                 }
106         }
107         
108         // a read removal
109         if(isset($_REQUEST["readremove"])) {
110                 foreach($_REQUEST["readmembersin"] as $rems) {
111                         error_log("Remove permid, $rems");
112                         gwvp_removeRepoPermission($rems);
113                 }
114         }
115
116         // a write addition
117         if(isset($_REQUEST["writeadd"])) {
118                 foreach($_REQUEST["writemembersout"] as $mems) {
119                         //gwvp_addRepoPermission($repoid, $permtype, $permref);
120                         gwvp_addRepoPermission($rid, "write", $mems);
121                 }
122         }
123         
124         // a write removal
125         if(isset($_REQUEST["writeremove"])) {
126                 foreach($_REQUEST["writemembersin"] as $rems) {
127                         error_log("Remove permid, $rems");
128                         gwvp_removeRepoPermission($rems);
129                 }
130         }
131         
132         gwvp_SendMessage("info", "Permissions Updated");
133         header("Location: $BASE_URL/admin/repos/manage/$rid");
134         return;
135 }
136
137 //function gwvp_createGitRepo($name, $ownerid, $desc, $defaultperms=0, $bundle=null)
138
139
140 function gwvp_DoCreateRepoPage()
141 {
142         global $BASE_URL;
143         
144         $reponame = $_REQUEST["reponame"];
145         $repodesc = $_REQUEST["repodesc"];
146         
147         $defperms = 0;
148         
149         if(isset($_REQUEST["defperms"])) {
150                 switch($_REQUEST["defperms"]) {
151                         case "permsall":
152                                 $defperms = 0;
153                                 break;
154                         case "permsmeonly":
155                                 $defperms = 1;
156                                 break;
157                         case "permsinvisible":
158                                 $defperms = 2;
159                                 break;
160                         default:
161                                 $defperms = 2;
162                 }
163         }
164         
165         // TODO: this code is bollocks, need to redo
166         if(gwvp_repoExists($reponame)) {
167                 gwvp_SendMessage("error", "a repository with the name <b>\"$reponame\"</b> already exists");
168                 //header("Location: $BASE_URL/admin/repos/create?reponameobv=$reponame&repodescobv=$repodesc");
169         } else if($_FILES["bundlefile"]["size"] > 0) { //               if(isset($_FILES["bundlefile"]["size"]))  <--- this needs to happen here TODO
170                 error_log("bundle file tmpname is ".$_FILES["bundlefile"]["tmp_name"]);
171                 // function gwvp_createGitRepo($name, $ownerid, $desc, $defaultperms=0, $bundle=null)
172                 // TODO: deal with default perms
173                 gwvp_createGitRepo($reponame, $_SESSION["id"], $repodesc, $_FILES["bundlefile"]["tmp_name"], $defperms);
174                 gwvp_SendMessage("info", "Repo, $reponame, created");
175         } else if(gwvp_createGitRepo($reponame, $_SESSION["id"], $repodesc, null, $defperms)) {
176                 gwvp_SendMessage("info", "Repo, $reponame, created");
177         }
178         header("Location: $BASE_URL/admin/repos");
179         
180         
181 }
182
183 function gwvp_CreateRepoPageBody()
184 {
185         global $BASE_URL;
186         
187         $repo_base = gwvp_getConfigVal("repodir");
188         
189         $reponameobv = "";
190         $repodescobv = "";
191         
192         if(isset($_REQUEST["reponameobv"])) $reponameobv = $_REQUEST["reponameobv"];
193         if(isset($_REQUEST["repodescobv"])) $repodescobv = $_REQUEST["repodescobv"];
194         
195         echo "<h2>Create a Repo</h2>";
196         echo "<form method=\"post\" enctype=\"multipart/form-data\" action=\"$BASE_URL/admin/repos/docreate\">";
197         echo "<table>";
198         echo "<tr><td>Repository Name</td><td bgcolor=\"#eeeeee\"><input type=\"text\" name=\"reponame\" value=\"$reponameobv\"></td></tr>";
199         echo "<tr><td>Repository Description</td><td bgcolor=\"#eeeeee\"><input type=\"text\" name=\"repodesc\" value=\"$repodescobv\"></td></tr>";
200         echo "<tr><td>Repository Bundle</td><td bgcolor=\"#eeeeee\"><input type=\"file\" name=\"bundlefile\"></td><td><i>Create a bundle for pro-creating the git repository (export your git bundle with \"git bundle create /tmp/filename --branches\")<br>";
201         echo "Typically you wouldn't use this as its easier to \"push\" to the repo after its created by adding it as a remote and pushing your local master branch</i></td></tr>";
202         
203         echo "<tr><td>Default Permisison Set</td><td bgcolor=\"#eeeeee\">";
204         
205         echo "<table>";
206         echo "<tr><td><input type=\"radio\" name=\"defperms\" value=\"permsall\" checked></td><td>Anyone Can Read, Only you can write</td></tr>";
207         echo "<tr><td><input type=\"radio\" name=\"defperms\" value=\"permsmeonly\"></td><td>Anyone can see the repository exists, but only you can read or write to it</td></tr>";
208         echo "<tr><td><input type=\"radio\" name=\"defperms\" value=\"permsinvisible\"></td><td>Repository only visible to you</td></tr>";
209         echo "</table>";
210         
211         
212         echo "</td><td><i>Permissions can be changed in repository management later</i></td></tr>";
213         
214         echo "</table>";
215         
216         
217         
218         
219         
220         echo "<input type=\"submit\" name=\"create\" value=\"Create\"><br>";
221         echo "</form>";
222 }
223
224 function gwvp_RepoAdminPageBody()
225 {
226         // first we need a menu
227         global $BASE_URL;
228         
229         echo "<h2>Repo Management</h2>";
230         echo "<a href=\"$BASE_URL/admin/repos/create\">Create a Repo</a><br>";
231         
232         if(isset($_SESSION["isloggedin"])) {
233                 echo "<h3>Your Repo's</h3>";
234                 $ownreps = gwvp_getOwnedRepos($_SESSION["id"]);
235                 if($ownreps != false) {
236                         echo "<table border=\"1\">";
237                         echo "<tr><th>Repo Name</th><th>Repo description</th></tr>";
238                         foreach($ownreps as $repos) {
239                                 $mjay = print_r($repos, true);
240                                 error_log("snafu: $mjay");
241                                 $reponame = $repos["name"];
242                                 $repodesc = $repos["description"];
243                                 $rid = $repos["id"];
244                                 echo "<tr><td>$reponame</td><td>$repodesc</td><td><a href=\"$BASE_URL/admin/repos/manage/$rid\">Manage</a></td></tr>";
245                         }
246                         echo "</table>";
247                 } else {
248                         echo "You own no repositories";
249                 }
250                 echo "<hr>";
251         }
252         
253         
254         // next we need a repo list - with perms checking - ug
255         // i must also remember that the home page will also contain a list of repos and that this page is solely for maintance
256         // and creation of repos - so i dont need to get over-worked about the info stored on this page outside of those activities
257         echo "<h3>All Repositories</h3>";
258         $rlist = gwvp_GetRepoList();
259         echo "<table border=\"1\"><tr><th>Repo Name</th><th>Repo Description</th><th>Repo Owner</th></tr>";
260         foreach($rlist as $u_res) {
261                 $rid = $u_res["id"];
262                 $rname = $u_res["name"];
263                 $rdesc = $u_res["description"];
264                 $rown = gwvp_getUserName($u_res["owner"]);
265                 $manordetslink = "details";
266                 $manordets = "Details";
267                 if(isset($_SESSION["id"])) {
268                         if(gwvp_resolvRepoPerms($_SESSION["id"], $rid)>3) {
269                                 $manordetslink = "manage";
270                                 $manordets = "Manage";
271                         }
272                 }
273                 echo "<tr><td>$rname</td><td>$rdesc</td><td>$rown</td><td><a href=\"$BASE_URL/admin/repos/$manordetslink/$rid\">$manordets</a></td></tr>";
274         }
275         echo "</table>";
276         
277         return;
278 }
279
280 function gwvp_DisemableRefId($who)
281 {
282         if($who == "anon") return "Everyone";
283         if($who == "authed") return "All Authenticated User";
284         
285         $epl = explode(":", $who);
286         
287         if($epl[0] == "user") {
288                 $username = gwvp_getUserName($epl[1]);
289                 return "User: $username";
290         } else if ($epl[0] == "group") {
291                 $grdent = gwvp_getGroup($epl[1]);
292                 $groupname = $grdent["name"];
293                 return "Group: $groupname";
294         } else return "unknown";
295 }
296
297 function gwvp_ManageRepoPageBody()
298 {
299         global $LOGIN_TYPE, $BASE_URL;
300         
301         $rid = -1;
302         if(isset($_REQUEST["q"])) {
303                 $query = $_REQUEST["q"];
304                 $qspl = explode("/", $query);
305                 $rid = $qspl[3];
306         }
307         
308         $repodets = gwvp_GetRepo($rid);
309         $reponame = $repodets["name"];
310         $repodesc = $repodets["description"];
311         $repoownid = $repodets["owner"];
312         $owndby = gwvp_getUserName($repoownid);
313         $users = gwvp_getUsers();
314         $groups = gwvp_getGroups();
315         $repoperms = gwvp_getRepoPermissions($rid);
316         
317         
318         echo "<h2>Repository Management</h2>";
319         echo "<b>$owndby's</b> Repository <i>$reponame</i><br>";
320         
321         echo "<form method=\"post\" action=\"$BASE_URL/admin/repos/update/$rid\">";
322         
323         echo "<table>";
324         echo "<tr><td>Description</td><td><input type=\"text\" name=\"desc\" value=\"$repodesc\"></td></tr>";
325         echo "</table>";
326         echo "<input type=\"submit\" name=\"update\" value=\"Update\"><br>";
327         echo "</form>";
328         
329         
330         echo "<h3>Repository Permissions</h3>";
331         echo "<form method=\"post\" action=\"$BASE_URL/admin/repos/updateperms/$rid\">";
332         echo "<table>";
333         echo "<tr bgcolor=\"#eeeee0\"><th>Visibility</th><th>Read/Clone</th><th>Write/Push</th></tr>";
334         echo "<tr><td bgcolor=\"#eeeeff\">";
335         
336         
337         
338         
339         // visibility section
340         $visin[0] = 0;
341         echo "<table>";
342         echo "<tr><th>Allowed</th><td></td><th>All</th></tr>";
343         echo "<tr><td>";
344         // list allowed users
345         echo "<select name=\"vismembersin[]\" size=\"20\" multiple=\"true\">";
346         $ninvis = 0;
347         foreach($repoperms as $v_perms) {
348                 if($v_perms["type"] == "visible") {
349                         $who = $v_perms["ref"]; // now we need to disemble ref
350                         $pid = $v_perms["id"];
351                         $refid = gwvp_DisemableRefId($who);
352                         
353                         $visin[$who] = true;
354                         
355                         echo "<option value=\"$pid\">$refid</option>";
356                         $ninvis++;
357                 }
358         }
359         if($ninvis == 0) echo "<option>--------------</option>";
360
361         echo "</select>";
362         
363         echo "</td><td>";
364         // buttons
365         echo "<input type=\"submit\" name=\"visadd\" value=\"<<\"><br>";
366         echo "<input type=\"submit\" name=\"visremove\" value=\">>\"><br>";
367         
368         echo "</td><td>";
369         // list all users/groups/specials
370         $noutvis = 0;
371         echo "<select name=\"vismembersout[]\" size=\"20\" multiple=\"true\">";
372         if(!isset($visin["anon"])) echo "<option value=\"anon\">Everyone</option>";
373         if(!isset($visin["authed"])) echo "<option value=\"authed\">All Authenticated User</option>";
374         foreach($groups as $u_groups) {
375                 $gname = $u_groups["name"];
376                 $gid = $u_groups["id"];
377                 if(!gwvp_IsGroupAdmin(null, $gid)) if(!isset($visin["group:$gid"])) {
378                         echo "<option value=\"group:$gid\">Group: $gname</option>";
379                         $noutvis++;
380                 }
381         }
382         foreach($users as $u_users) {
383                 $uid = $u_users["id"];
384                 $email = $u_users["email"];
385                 $username = $u_users["username"];
386                 $fullname = $u_users["fullname"];
387                 if(!gwvp_IsUserAdmin(null, null, $uid)) if(!isset($visin["user:$uid"])) {
388                         echo "<option value=\"user:$uid\">User: $username</option>";
389                         $noutvis++;
390                 }
391         }
392         if($noutvis == 0) echo "<option>-------------------</option>";
393         echo "</select>";
394         
395         echo "</td></tr>";
396         echo "</table>";
397         // end visibility section
398         
399         
400         echo "</td><td bgcolor=\"#eeffee\">";
401         // Read/clone/pull section
402         $readin[0] = 0;
403         echo "<table>";
404         echo "<tr><th>Allowed</th><td></td><th>All</th></tr>";
405         echo "<tr><td>";
406         // list allowed users
407         echo "<select name=\"readmembersin[]\" size=\"20\" multiple=\"true\">";
408         $ninread = 0;
409         foreach($repoperms as $v_perms) {
410                 if($v_perms["type"] == "read") {
411                         $who = $v_perms["ref"]; // now we need to disemble ref
412                         $pid = $v_perms["id"];
413                         $refid = gwvp_DisemableRefId($who);
414                         
415                         $readin[$who] = true;
416                         
417                         echo "<option value=\"$pid\">$refid</option>";
418                         $ninread++;
419                 }
420         }
421         if($ninread == 0) echo "<option>-------------------</option>";
422
423         echo "</select>";
424         
425         echo "</td><td>";
426         // buttons
427         echo "<input type=\"submit\" name=\"readadd\" value=\"<<\"><br>";
428         echo "<input type=\"submit\" name=\"readremove\" value=\">>\"><br>";
429         
430         echo "</td><td>";
431         // list all users/groups/specials
432         echo "<select name=\"readmembersout[]\" size=\"20\" multiple=\"true\">";
433         $noutread = 0;
434         if(!isset($readin["anon"])) echo "<option value=\"anon\">Everyone</option>";
435         if(!isset($readin["authed"])) echo "<option value=\"authed\">All Authenticated User</option>";
436         foreach($groups as $u_groups) {
437                 $gname = $u_groups["name"];
438                 $gid = $u_groups["id"];
439                 if(!gwvp_IsGroupAdmin(null, $gid)) if(!isset($readin["group:$gid"])) {
440                         echo "<option value=\"group:$gid\">Group: $gname</option>";
441                         $noutread++;
442                 }
443         }
444         foreach($users as $u_users) {
445                 $uid = $u_users["id"];
446                 $email = $u_users["email"];
447                 $username = $u_users["username"];
448                 $fullname = $u_users["fullname"];
449                 if(!gwvp_IsUserAdmin(null, null, $uid)) if(!isset($readin["user:$uid"])) {
450                         echo "<option value=\"user:$uid\">User: $username</option>";
451                         $noutread++;
452                 }
453         }
454         if($noutread == 0) echo "<option>-------------------</option>";
455         echo "</select>";
456         
457         echo "</td></tr>";
458         echo "</table>";
459         // end read/clone/pull section
460         
461         
462         echo "</td><td bgcolor=\"#ffeeee\">";
463         // write/push section
464         $writein[0] = 0;
465         echo "<table>";
466         echo "<tr><th>Allowed</th><td></td><th>All</th></tr>";
467         echo "<tr><td>";
468         // list allowed users
469         echo "<select name=\"writemembersin[]\" size=\"20\" multiple=\"true\">";
470         $ninwrite = 0;
471         foreach($repoperms as $v_perms) {
472                 if($v_perms["type"] == "write") {
473                         $who = $v_perms["ref"]; // now we need to disemble ref
474                         $pid = $v_perms["id"];
475                         $refid = gwvp_DisemableRefId($who);
476                         
477                         $writein[$who] = true;
478                         
479                         echo "<option value=\"$pid\">$refid</option>";
480                         $ninwrite++;
481                 }
482         }
483         if($ninwrite == 0) echo "<option>-------------------</option>";
484         echo "</select>";
485         
486         echo "</td><td>";
487         // buttons
488         echo "<input type=\"submit\" name=\"writeadd\" value=\"<<\"><br>";
489         echo "<input type=\"submit\" name=\"writeremove\" value=\">>\"><br>";
490         
491         echo "</td><td>";
492         // list all users/groups/specials
493         echo "<select name=\"writemembersout[]\" size=\"20\" multiple=\"true\">";
494         $noutwrite = 0;
495         // cant have anon for writes if(!isset($writein["anon"])) echo "<option value=\"anon\">Everyone</option>";
496         if(!isset($writein["authed"])) echo "<option value=\"authed\">All Authenticated User</option>";
497         foreach($groups as $u_groups) {
498                 $gname = $u_groups["name"];
499                 $gid = $u_groups["id"];
500                 if(!gwvp_IsGroupAdmin(null, $gid)) if(!isset($writein["group:$gid"])) {
501                         $noutwrite++;
502                         echo "<option value=\"group:$gid\">Group: $gname</option>";
503                 }
504         }
505         foreach($users as $u_users) {
506                 $uid = $u_users["id"];
507                 $email = $u_users["email"];
508                 $username = $u_users["username"];
509                 $fullname = $u_users["fullname"];
510                 if(!gwvp_IsUserAdmin(null, null, $uid)) if(!isset($writein["user:$uid"])) {
511                         $noutwrite++;
512                         echo "<option value=\"user:$uid\">User: $username</option>";
513                 }
514         }
515         if($noutwrite == 0) echo "<option>-------------------</option>";
516         echo "</select>";
517         
518         echo "</td></tr>";
519         echo "</table>";
520         // end write/push section
521         
522         echo "</td></tr>";
523         
524         echo "</table>";
525         echo "</form>";
526 }
527
528
529 ?>