92ac9818e2794638cadadc3bcc9cdcb891207429
[gwvp-mini.git] / gwvpmini / gwvpmini_view.php
1 <?php
2 global $HOME_PAGE_PROVIDERS;
3
4
5 $CALL_ME_FUNCTIONS["repoview"] = "gwvpmini_RepoViewCallMe";
6
7
8
9 function gwvpmini_RepoViewCallMe()
10 {
11         global $repo_view_call;
12         
13         // error_log("in admin callme");\r
14         if(isset($_REQUEST["q"])) {\r
15                 $query = $_REQUEST["q"];\r
16                 $qspl = explode("/", $query);\r
17                 if(isset($qspl[0])) {\r
18                         if($qspl[0] == "view") {\r
19                                 if(isset($qspl[1])) {
20                                         $repo_view_call = $qspl[1];
21                                         return "gwvpmini_RepoViewPage";\r
22                                 } else return false;\r
23                         }
24                         if($qspl[0] == "updaterepobaseperms") {
25                                 return "gwvpmini_UpdateRepoBasePerms";
26                         } 
27                         if($qspl[0] == "repoaddreader") {
28                                 return "gwvpmini_AddRepoReader";
29                         }
30                         if($qspl[0] == "repoaddcontrib") {
31                                 return "gwvpmini_AddRepoContributor";
32                         }
33                         if($qspl[0] == "reporemovereaders") {
34                                 return "gwvpmini_RemoveRepoReader";
35                         }
36                         if($qspl[0] == "reporemovecontribs") {
37                                 return "gwvpmini_RemoveRepoContributor";
38                         }
39                         if($qspl[0] == "repoupdatedesc") {
40                                 return "gwvpmini_RepoUpdateDescription";
41                         }
42                         return false;\r
43                 }\r
44                 else return false;\r
45         }\r
46 \r
47         return false;\r
48         
49         
50 }
51
52 function gwvpmini_RepoViewPage()\r
53 {
54         global $repo_view_call, $MENU_ITEMS, $BASE_URL;\r
55         \r
56         $MENU_ITEMS["40thisrepo"]["text"] = "$repo_view_call";\r
57         $MENU_ITEMS["40thisrepo"]["link"] = "$BASE_URL/view/$repo_view_call";\r
58         \r
59         gwvpmini_goMainPage("gwvpmini_RepoViewPageBody");\r
60 }
61
62 function gwvpmini_RepoViewPageBody()
63 {
64         global $repo_view_call, $MENU_ITEMS, $BASE_URL;
65         
66         $repo_base = gwvpmini_getConfigVal("repodir");\r
67
68         $bperms_f = gwvpmini_GetRepoPerms(gwvpmini_GetRepoId($repo_view_call));
69         $bperms = $bperms_f["b"];
70         
71         $owner_view = false;\r
72         
73         if($_SERVER["SERVER_PORT"] == 443) $proto="https://";
74         else $proto = "http://";
75         $sname = $_SERVER["SERVER_NAME"];
76         
77         $owner = gwvpmini_GetRepoOwnerDetailsFromName($repo_view_call);
78         $desc = gwvpmini_GetRepoDescFromName($repo_view_call);
79         $editdesc = preg_replace("/\<br\>/", "\n", $desc);
80         
81         $rid = gwvpmini_GetRepoId($repo_view_call);
82         
83         $owner_name = $owner["username"];
84         
85         // TODO: fix this so that if user has no read access to repo, they cant see it
86         if(isset($_SESSION["id"])) {
87                 if($owner["id"] == $_SESSION["id"]) {
88                         $owner_view = true;
89                 } else if ($bperms != "r") {
90                         // check user level perms
91                         $perm = gwvpmini_GetRepoPerm($rid, $_SESSION["id"]);
92                         if($perm < 1) {
93                                 header("Location: $BASE_URL");\r
94                                 return;
95                         }
96                 }
97         } else {
98                 if($bperms != "a") {
99                         header("Location: $BASE_URL");
100                         return;
101                 }
102         }
103         
104         
105         // error_log("STUFF:".print_r($owner,true));
106         if($bperms != "a") $login = $_SESSION["username"].":password@";
107         else $login = "";
108         $cloneurl = "git clone $proto$login$sname$BASE_URL/git/$repo_view_call.git";
109         echo "<textarea rows=1 cols=".strlen($cloneurl).">$cloneurl</textarea><br>";
110         
111         if($owner_view) $owner_extra = " (YOU)";
112         else $owner_extra = "";\r
113         
114         echo "<h2>".gwvpmini_HtmlGravatar($owner["email"], 30)."$repo_view_call - $owner_name$owner_extra</h2>";
115         if(!$owner_view) echo "$desc<br>";
116         
117         if($owner_view) {
118                 echo "<form method=\"post\" action=\"$BASE_URL/repoupdatedesc/$repo_view_call\">";\r
119                 echo "<h3>Description<h3><textarea name=\"desc\" cols=\"120\" rows=\"5\">$editdesc</textarea><br><input type=\"submit\" name=\"Update\" value=\"Update\">";\r
120                 echo "</form><br>";\r
121                 
122                 
123
124                 
125                 $anyo = "";
126                 $regd = "";
127                 $expl = "";
128                 if($bperms == "a") $anyo = " selected";\r
129                 if($bperms == "r") $regd = " selected";\r
130                 if($bperms == "x") $expl = " selected";\r
131                 
132                 // error_log("BPERMS: $bperms");
133                 
134                 if($bperms == "x") $cspan = 3;
135                 else $cspan = 2;
136                 
137                 echo "<table border=\"1\"><tr valign=\"top\"><tr><th colspan=\"$cspan\">Permissions</th></tr><td>";
138                 echo "<form method=\"post\" action=\"$BASE_URL/updaterepobaseperms/$repo_view_call\">";
139                 echo "<select name=\"base_perms\">";
140                 echo "<option value=\"a\"$anyo>Anyone can read</option>";
141                 echo "<option value=\"r\"$regd>Only registered users can read</option>";
142                 echo "<option value=\"x\"$expl>Explicit read permissions</option>";
143                 echo "</select>";
144                 echo "<input type=\"submit\" name=\"Set\" value=\"Set\">";
145                 echo "</form>";
146                 if($bperms == "x") {
147                         echo "</td><td><b>Readers</b><br>";
148                         echo "<form method=\"post\" action=\"$BASE_URL/reporemovereaders/$repo_view_call\">";
149                         $nl = 0;
150                         foreach($bperms_f as $key=>$val) {
151                                 if($val == 1) {
152                                         $dets = gwvpmini_getUser(null, null, $key);
153                                         echo gwvpmini_HtmlGravatar($dets["email"], 18, " ")."<input type=\"checkbox\" name=\"$key\"> ".$dets["username"]."<br>";
154                                         $nl = 1;
155                                 }
156                         }
157                         if($nl==1) echo "<input type=\"submit\" name=\"remove\" value=\"Remove Selected\">";
158                         echo "</form>";
159                         echo "<form method=\"post\" action=\"$BASE_URL/repoaddreader/$repo_view_call\">";
160                         echo "<input type=\"text\" name=\"readerusername\"> <input type=\"submit\" name=\"Add\" value=\"Add\">";
161                         echo "</form><br>";
162                 }
163                 
164                 echo "</td><td><b>Contributors</b><br>";
165                 echo "<form method=\"post\" action=\"$BASE_URL/reporemovecontribs/$repo_view_call\">";
166                 $nl = 0;
167                 foreach($bperms_f as $key=>$val) {\r
168                         if($val == 2) {\r
169                                 $dets = gwvpmini_getUser(null, null, $key);\r
170                                 echo gwvpmini_HtmlGravatar($dets["email"], 18, " ")."<input type=\"checkbox\" name=\"$key\"> ".$dets["username"]."<br>";
171                                 $nl = 1;\r
172                         }\r
173                 }\r
174                 if($nl==1) echo "<input type=\"submit\" name=\"remove\" value=\"Remove Selected\">";\r
175                 echo "</form>";
176                 
177                 echo "<form method=\"post\" action=\"$BASE_URL/repoaddcontrib/$repo_view_call\">";
178                 echo "<input type=\"text\" name=\"contribusername\"> <input type=\"submit\" name=\"Add\" value=\"Add\">";
179                 echo "</form><br>";
180                 echo "</td></tr></table>";
181                 
182         }
183         //echo "command: git log --git-dir=$repo_base/$repo_view_call.git --pretty=format:\"%H\" -10";
184         $rs = popen("git --git-dir=$repo_base/$repo_view_call.git log --pretty=format:\"%H\" -10", "r");
185         $commitids = array();
186         $i = 0;
187         if($rs) {
188                 while(!feof($rs)) {
189                         $flin = fgets($rs);
190                         if($flin !== false) {
191                                 $commitids[$i] = trim($flin);
192                                 $i++;
193                         }
194                 }
195                 fclose($rs);
196         } else {
197                 $commitids = false;
198         }
199         
200         if($commitids != false) {
201                 echo "<hr>Commits<br>";
202                 echo "<table border=\"1\">";
203                 echo "<tr><th>Committed By</th><th>Date</th><th>Commit Log Entry</th></tr>";
204                 foreach($commitids as $ids) {
205                         $rs = popen("git --git-dir=$repo_base/$repo_view_call.git log --pretty=format:\"%at%n%ce%n%an%n%s\" $ids -1 2> /dev/null", "r");
206                         if($rs) {
207                                 $flin1 = trim(fgets($rs));
208                                 $flin2 = trim(fgets($rs));
209                                 $flin3 = trim(fgets($rs));
210                                 while(!feof($rs)) {
211                                         $flin4 = fread($rs, 8192);
212                                 }
213                                 $flon =  gwvpmini_emailToUserLink($flin2);
214                                 if(!$flon) {
215                                         $flon = "$flin3 (external)";
216                                 }
217                         }
218                         $timedifftxt = gwvpmini_TimeDiffText($flin1);
219                         echo "<tr><td>".gwvpmini_HtmlGravatar($flin2, 18)."$flon</td><td>$timedifftxt</td><td>$flin4</td></tr>";
220                 }
221                 echo "</table>";
222         } else {
223                 echo "No commit information available yet<br>";
224         }
225 }
226
227 function gwvpmini_UpdateRepoBasePerms()
228 {
229         global $BASE_URL, $repo_view_call;
230         
231         if(isset($_REQUEST["q"])) {\r
232                 $query = $_REQUEST["q"];\r
233                 $qspl = explode("/", $query);
234                 // error_log("PLOOP:qview".print_r($qspl, true));\r
235         }
236         
237         if(isset($qspl[1])) $repo_view_call = $qspl[1];
238         else {
239                 // error_log("PLOOP: no repo name");
240                 header("Location: $BASE_URL/view/$repo_view_call");
241                 return;
242         }
243         
244         $newperms = $_REQUEST["base_perms"];
245         
246         $owner = gwvpmini_GetRepoOwnerDetailsFromName($repo_view_call);\r
247         $desc = gwvpmini_GetRepoDescFromName($repo_view_call);\r
248         \r
249         $owner_name = $owner["username"];
250         
251         $owner_view = false;
252         if(isset($_SESSION["id"])) {\r
253                 if($owner["id"] == $_SESSION["id"]) {\r
254                         $owner_view = true;\r
255                 }\r
256         }
257         
258         $rid = gwvpmini_GetRepoId($repo_view_call);
259         
260         if(!$owner_view) {
261                 gwvpmini_SendMessage("error", "failure updating permission for repo");
262                 // error_log("PLOOP: attempt to update from non-owner");
263         } else {
264                 // error_log("PLOOP: updateds: ".print_r($_REQUEST, true));
265                 gwvpmini_ChangeRepoPerm($rid, "b", $_REQUEST["base_perms"]);
266                 gwvpmini_SendMessage("info", "Base permissions for repo updated");
267         }\r
268         
269         header("Location: $BASE_URL/view/$repo_view_call");
270 }
271
272 function gwvpmini_AddRepoReader()
273 {
274         global $BASE_URL, $repo_view_call;\r
275         \r
276         if(isset($_REQUEST["q"])) {\r
277                 $query = $_REQUEST["q"];\r
278                 $qspl = explode("/", $query);\r
279                 // error_log("PLOOP:qview".print_r($qspl, true));\r
280         }\r
281         \r
282         if(isset($qspl[1])) $repo_view_call = $qspl[1];\r
283         else {\r
284                 // error_log("PLOOP: no repo name");
285                 // TODO: btw, this makes no sense\r
286                 header("Location: $BASE_URL/view/$repo_view_call");\r
287                 return;\r
288         }\r
289         \r
290         $newperms = $_REQUEST["base_perms"];\r
291         \r
292         $owner = gwvpmini_GetRepoOwnerDetailsFromName($repo_view_call);\r
293         $desc = gwvpmini_GetRepoDescFromName($repo_view_call);\r
294         \r
295         $owner_name = $owner["username"];\r
296         \r
297         $owner_view = false;\r
298         if(isset($_SESSION["id"])) {\r
299                 if($owner["id"] == $_SESSION["id"]) {\r
300                         $owner_view = true;\r
301                 }\r
302         }
303         if(!$owner_view) {
304                 gwvpmini_SendMessage("error", "failure updating permission for repo");\r
305                 // error_log("PLOOP: attempt to update from non-owner");
306                 header("Location: $BASE_URL/view/$repo_view_call");
307                 return;
308         }\r
309         
310         $auid = gwvpmini_GetUserId($_REQUEST["readerusername"]);
311         
312         if($auid == $_SESSION["id"]) {
313                 gwvpmini_SendMessage("error", "You cannot add yourself as a reader as you already own the repo");
314                 header("Location: $BASE_URL/view/$repo_view_call");\r
315                 return;
316         }
317         
318         if($auid > 0)  {\r
319                 $rid = gwvpmini_GetRepoId($repo_view_call);
320                 
321                 gwvpmini_ChangeRepoPerm($rid, $auid, 1);
322                 gwvpmini_SendMessage("info", "Added user ".$_REQUEST["readerusername"]." as a reader");
323                 header("Location: $BASE_URL/view/$repo_view_call");\r
324                 return;
325         } else {
326                 gwvpmini_SendMessage("error", "Couldnt find user with username of ".$_REQUEST["readerusername"]);
327                 header("Location: $BASE_URL/view/$repo_view_call");
328                 return;
329         }
330         
331         
332 }
333
334 function gwvpmini_AddRepoContributor()
335 {
336         global $BASE_URL, $repo_view_call;\r
337         \r
338         if(isset($_REQUEST["q"])) {\r
339                 $query = $_REQUEST["q"];\r
340                 $qspl = explode("/", $query);\r
341                 // error_log("PLOOP:qview".print_r($qspl, true));\r
342         }\r
343         \r
344         if(isset($qspl[1])) $repo_view_call = $qspl[1];\r
345         else {\r
346                 // error_log("PLOOP: no repo name");\r
347                 // TODO: btw, this makes no sense\r
348                 header("Location: $BASE_URL/view/$repo_view_call");\r
349                 return;\r
350         }\r
351         \r
352         $newperms = $_REQUEST["base_perms"];\r
353         \r
354         $owner = gwvpmini_GetRepoOwnerDetailsFromName($repo_view_call);\r
355         $desc = gwvpmini_GetRepoDescFromName($repo_view_call);\r
356         \r
357         $owner_name = $owner["username"];\r
358         \r
359         $owner_view = false;\r
360         if(isset($_SESSION["id"])) {\r
361                 if($owner["id"] == $_SESSION["id"]) {\r
362                         $owner_view = true;\r
363                 }\r
364         }\r
365         if(!$owner_view) {\r
366                 gwvpmini_SendMessage("error", "failure updating permission for repo");\r
367                 // error_log("PLOOP: attempt to update from non-owner");\r
368                 header("Location: $BASE_URL/view/$repo_view_call");\r
369                 return;\r
370         }\r
371         \r
372         $auid = gwvpmini_GetUserId($_REQUEST["contribusername"]);\r
373         \r
374         if($auid == $_SESSION["id"]) {\r
375                 gwvpmini_SendMessage("error", "You cannot add yourself as a contributor as you already own the repo");\r
376                 header("Location: $BASE_URL/view/$repo_view_call");\r
377                 return;\r
378         }\r
379         \r
380         if($auid > 0)  {\r
381                 $rid = gwvpmini_GetRepoId($repo_view_call);\r
382         \r
383                 gwvpmini_ChangeRepoPerm($rid, $auid, 2);\r
384                 gwvpmini_SendMessage("info", "Added user ".$_REQUEST["contribusername"]." as a contributor");\r
385                 header("Location: $BASE_URL/view/$repo_view_call");\r
386                 return;\r
387         } else {\r
388                 gwvpmini_SendMessage("error", "Couldnt find user with username of ".$_REQUEST["contribusername"]);\r
389                 header("Location: $BASE_URL/view/$repo_view_call");\r
390                 return;\r
391         }
392 }
393
394 function gwvpmini_RemoveRepoContributor()
395 {
396         
397         global $BASE_URL, $repo_view_call;\r
398         \r
399         if(isset($_REQUEST["q"])) {\r
400                 $query = $_REQUEST["q"];\r
401                 $qspl = explode("/", $query);\r
402                 // error_log("PLOOP:qview".print_r($qspl, true));\r
403         }\r
404         \r
405         if(isset($qspl[1])) $repo_view_call = $qspl[1];\r
406         else {\r
407                 // error_log("PLOOP: no repo name");\r
408                 // TODO: btw, this makes no sense\r
409                 header("Location: $BASE_URL/view/$repo_view_call");\r
410                 return;\r
411         }\r
412         \r
413         \r
414         $owner = gwvpmini_GetRepoOwnerDetailsFromName($repo_view_call);\r
415         $desc = gwvpmini_GetRepoDescFromName($repo_view_call);\r
416         \r
417         $owner_name = $owner["username"];\r
418         \r
419         $owner_view = false;\r
420         if(isset($_SESSION["id"])) {\r
421                 if($owner["id"] == $_SESSION["id"]) {\r
422                         $owner_view = true;\r
423                 }\r
424         }\r
425         if(!$owner_view) {\r
426                 gwvpmini_SendMessage("error", "failure updating permission for repo");\r
427                 // error_log("PLOOP: attempt to update from non-owner");\r
428                 header("Location: $BASE_URL/view/$repo_view_call");\r
429                 return;\r
430         }
431         
432         $rid = gwvpmini_GetRepoId($repo_view_call);
433         
434         $bperms_f = gwvpmini_GetRepoPerms($rid);
435         
436         foreach($bperms_f as $key=>$val) {
437                 if($val == 2) {
438                         if(isset($_REQUEST["$key"])) {
439                                 gwvpmini_ChangeRepoPerm($rid, $key, 0);
440                         }
441                 }
442         }
443         
444         gwvpmini_SendMessage("info", "Repo permissions updated");
445         header("Location: $BASE_URL/view/$repo_view_call");\r
446         return;\r
447         
448 }\r
449
450 \r
451 function gwvpmini_RemoveRepoReader()\r
452 {\r
453 \r
454         global $BASE_URL, $repo_view_call;\r
455 \r
456         if(isset($_REQUEST["q"])) {\r
457                 $query = $_REQUEST["q"];\r
458                 $qspl = explode("/", $query);\r
459                 // error_log("PLOOP:qview".print_r($qspl, true));\r
460         }\r
461 \r
462         if(isset($qspl[1])) $repo_view_call = $qspl[1];\r
463         else {\r
464                 // error_log("PLOOP: no repo name");\r
465                 // TODO: btw, this makes no sense\r
466                 header("Location: $BASE_URL/view/$repo_view_call");\r
467                 return;\r
468         }\r
469 \r
470 \r
471         $owner = gwvpmini_GetRepoOwnerDetailsFromName($repo_view_call);\r
472         $desc = gwvpmini_GetRepoDescFromName($repo_view_call);\r
473 \r
474         $owner_name = $owner["username"];\r
475 \r
476         $owner_view = false;\r
477         if(isset($_SESSION["id"])) {\r
478                 if($owner["id"] == $_SESSION["id"]) {\r
479                         $owner_view = true;\r
480                 }\r
481         }\r
482         if(!$owner_view) {\r
483                 gwvpmini_SendMessage("error", "failure updating permission for repo");\r
484                 // error_log("PLOOP: attempt to update from non-owner");\r
485                 header("Location: $BASE_URL/view/$repo_view_call");\r
486                 return;\r
487         }\r
488 \r
489         $rid = gwvpmini_GetRepoId($repo_view_call);\r
490 \r
491         $bperms_f = gwvpmini_GetRepoPerms($rid);\r
492 \r
493         foreach($bperms_f as $key=>$val) {\r
494                 if($val == 1) {\r
495                         if(isset($_REQUEST["$key"])) {\r
496                                 gwvpmini_ChangeRepoPerm($rid, $key, 0);\r
497                         }\r
498                 }\r
499         }\r
500 \r
501         gwvpmini_SendMessage("info", "Repo permissions updated");\r
502         header("Location: $BASE_URL/view/$repo_view_call");\r
503         return;\r
504 \r
505 }
506
507 function gwvpmini_RepoUpdateDescription()
508 {
509         global $BASE_URL, $repo_view_call;\r
510         \r
511         if(isset($_REQUEST["q"])) {\r
512                 $query = $_REQUEST["q"];\r
513                 $qspl = explode("/", $query);\r
514                 // error_log("PLOOP:qview".print_r($qspl, true));\r
515         }\r
516         \r
517         if(isset($qspl[1])) $repo_view_call = $qspl[1];\r
518         else {\r
519                 // error_log("PLOOP: no repo name");\r
520                 // TODO: btw, this makes no sense\r
521                 header("Location: $BASE_URL/view/$repo_view_call");\r
522                 return;\r
523         }\r
524         \r
525         \r
526         $owner = gwvpmini_GetRepoOwnerDetailsFromName($repo_view_call);\r
527         $desc = gwvpmini_GetRepoDescFromName($repo_view_call);\r
528         \r
529         $owner_name = $owner["username"];\r
530         \r
531         $owner_view = false;\r
532         if(isset($_SESSION["id"])) {\r
533                 if($owner["id"] == $_SESSION["id"]) {\r
534                         $owner_view = true;\r
535                 }\r
536         }\r
537         if(!$owner_view) {\r
538                 gwvpmini_SendMessage("error", "failure updating description for repo");\r
539                 // error_log("PLOOP: attempt to update from non-owner");\r
540                 header("Location: $BASE_URL/view/$repo_view_call");\r
541                 return;\r
542         }\r
543         \r
544         $rid = gwvpmini_GetRepoId($repo_view_call);\r
545         \r
546         gwvpmini_UpdateRepoDescription($rid, $_REQUEST["desc"]);
547                 \r
548         gwvpmini_SendMessage("info", "Repo description updated");\r
549         header("Location: $BASE_URL/view/$repo_view_call");\r
550         return;
551 }\r
552
553 ?>