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