replacing the cli cmds with variables and configuration in config.php
[gwvp-mini.git] / gwvpmini / gwvpmini_gitrepo.php
1 <?php
2 global $HOME_PAGE_PROVIDERS;\r
3
4
5 $CALL_ME_FUNCTIONS["repoadmin"] = "gwvpmini_RepoCallMe";
6 $HOME_PAGE_PROVIDERS["00gitlog"] = "gwvpmini_GitLogProvider";\r
7 \r
8 \r
9 // the home_page_provders bit is an array\r
10 \r
11 $MENU_ITEMS["10repos"]["text"] = "Repos";\r
12 $MENU_ITEMS["10repos"]["link"] = "$BASE_URL/repos";
13
14
15 function gwvpmini_RepoCallMe()\r
16 {\r
17 \r
18         // error_log("in repoadmin callme - err?");
19         // error_log(print_r($_REQUEST, true));\r
20         if(isset($_REQUEST["q"])) {
21                 // error_log("in repoadmin callme, for Q");\r
22                 $query = $_REQUEST["q"];\r
23                 $qspl = explode("/", $query);\r
24                 if(isset($qspl[0])) {\r
25                         if($qspl[0] == "repos") {
26                                 // error_log("in repos call");
27                                 if(isset($qspl[1])) {
28                                         if($qspl[1] == "create") {
29                                                 return "gwvpmini_RepoCreate";
30                                         } else {
31                                                 return "gwvpmini_RepoMainPage";
32                                         }
33                                 } else {\r
34                                         // error_log("i got here, where next?");\r
35                                         return "gwvpmini_RepoMainPage";
36                                 }\r
37                         } else return false;\r
38                 }\r
39                 else return false;\r
40         }\r
41 \r
42         return false;\r
43 }\r
44
45
46 function gwvpmini_RepoMainPage()\r
47 {\r
48         gwvpmini_goMainPage("gwvpmini_RepoMainPageBody");\r
49 }\r
50 \r
51
52 function gwvpmini_RepoMainPageBody()
53 {
54         global $BASE_URL;
55         
56         if(gwvpmini_isLoggedIn()) {
57                 $repos = gwvpmini_GetOwnedRepos($_SESSION["username"]);
58                 error_log("repos, ".print_r($repos, true));
59                 if(!$repos) {
60                         echo "You currently own no repos<br>";  
61                 } else {
62                         echo "<h2>Your Repos</h2>";
63                         echo "<table border=\"1\"><tr><th>Repo Name</th><th>Repo Description</th><th>Last Log</th></tr>";
64                         foreach($repos as $repo) {
65                                 $name = $repo["name"];
66                                 $desc = $repo["desc"];
67                                 $stat = $repo["status"];
68                                 $llog = "";\r
69                                 if($stat != 0) {\r
70                                         switch($stat) {\r
71                                                 case 1:\r
72                                                         $llog = "Repo Administratively Disabled";\r
73                                                         break;\r
74                                                 case 2:\r
75                                                         $llog = "Repo Cloning from remote";\r
76                                                         break;\r
77                                         }\r
78                                 } else {
79                                 
80                                         if($desc == "") $desc = "none";
81                                         
82                                         $repo_base = gwvpmini_getConfigVal("repodir");
83                                         $cmd = "git --git-dir=\"$repo_base/$name.git\" log --all -1 2> /dev/null";
84                                         // error_log("CMD: $cmd");
85                                         //system("$cmd");
86                                         $fls = popen($cmd, "r");
87                                         $tks = "";
88                                         if($fls !== false) while(!feof($fls)) {
89                                                 $tks .= fread($fls,1024);
90                                         }
91                                         
92                                         if($tks == "") {
93                                                 $llog =  "No Log Info Yet";
94                                         } else $llog = $tks;
95                                         
96                                 }
97                                 echo "<tr><td><a href=\"$BASE_URL/view/$name\">$name</a></td><td>$desc</td><td>$llog</td></tr>";
98                         }
99                         echo "</table>";
100                 }
101                 gwvpmini_GitCreateRepoForm();\r
102                 
103                 
104                 $contreps = gwvpmini_GetContributedRepos($_SESSION["username"]);
105                 
106                 if($contreps !== false) {
107                         echo "<h2>Repos you contribute to</h2>";
108                         echo "<table border=\"1\"><tr><th>Repo Name</th><th>Owner</th><th>Repo Description</th><th>Last Log</th></tr>";
109                         foreach($contreps as $repo) {
110                                 $name = $repo["name"];\r
111                                 $desc = $repo["desc"];
112                                 $stat = $repo["status"];
113                                 if($stat != 0) {
114                                         switch($stat) {
115                                                 case 1:
116                                                         $lastlog = "Repo Administratively Disabled";
117                                                         break;
118                                                 case 2:
119                                                         $lastlog = "Repo Cloning from remote";
120                                                         break;
121                                         }
122                                 } else {
123                                         $repo_base = gwvpmini_getConfigVal("repodir");\r
124                                         $cmd = "git --git-dir=\"$repo_base/$name.git\" log --all -1 2> /dev/null";
125                                         // error_log("CMD: $cmd");
126                                         //system("$cmd");
127                                         $fls = popen($cmd, "r");
128                                         $tks = "";
129                                         if($fls !== false) while(!feof($fls)) {
130                                                 $tks .= fread($fls,1024);
131                                         }
132                                         
133                                         if($tks == "") {
134                                                 $lastlog = "No Log Info Yet";
135                                         } else $lastlog = $tks;
136                                 }
137                                 
138                                 $owner = gwvpmini_getUser(null, null, $repo["owner"]);
139                                 $repname = "<a href=\"$BASE_URL/view/$name\">$name</a>";
140                                 $repown = gwvpmini_HtmlGravatar($owner["email"], 30, "<br>")."<a href=\"$BASE_URL/user/".$owner["username"]."\">".$owner["username"]."</a>";
141                                 
142                                 
143                                 echo "<tr><td>$repname</td><td>$repown</td><td>$desc</td><td>$lastlog</td></tr>";
144                         }
145                         echo "</table>";
146                 }
147         }
148         return true;
149 }
150
151
152 function gwvpmini_GitLogProvider()\r
153 {
154         global $cmd_line_tool,$git_cli_cmd,$php_cli_cmd;\r
155         /*\r
156          * The home page provider will:\r
157         * 1) show the last 10 commits for every repository - though, excluding private repos\r
158         * 2) if loged in, show the last commit on any repo's the user owns\r
159         *\r
160         * So i need a table thats going to list "writes" by user - as in POST writes but only\r
161         * put that info into the stats (doesnt exist) db if the repo is publically readable\r
162         *\r
163         * Or... should we instead just list every repo?\r
164         */
165         
166         global $BASE_URL;
167         
168         echo "<h2>Repo Activity</h2>";
169         if(gwvpmini_isLoggedIn()) {\r
170                 $repos = gwvpmini_GetOwnedRepos($_SESSION["username"]);
171                 if(!$repos) {
172                         echo "You currently own no repos<br>";  
173                 } else {
174                         echo "<h2>Your Repos</h2>";
175                         echo "<table border=\"1\"><tr><th>Repo Name</th><th>Repo Description</th><th>Repo Log</th></tr>";
176                         foreach($repos as $repo) {
177                                 $name = $repo["name"];
178                                 $desc = $repo["desc"];
179                                 
180                                 if($desc == "") $desc = "-";
181                                 echo "<tr><td><a href=\"$BASE_URL/view/$name\">$name</a></td><td>$desc</td>";
182                                 echo "<td>";
183                                 $repo_base = gwvpmini_getConfigVal("repodir");
184                                 $cmd = "$git_cli_cmd --git-dir=\"$repo_base/$name.git\" log --all -1 2> /dev/null";
185                                 // error_log("CMD: $cmd");
186                                 //system("$cmd");
187                                 $fls = popen($cmd, "r");
188                                 $tks = "";
189                                 if($fls !== false) while(!feof($fls)) {
190                                         $tks .= fread($fls,1024);
191                                 }
192                                 
193                                 if($tks == "") {
194                                         echo "No Log Info Yet";
195                                 } else echo $tks;
196                                 echo "</td>";
197                                 echo "</tr>";
198                         }
199                         echo "</table>";
200                 }\r
201         }
202 }
203
204 function gwvpmini_GitCreateRepoForm()
205 {
206         global $BASE_URL;
207         
208         echo "<form method=\"post\" action=\"$BASE_URL/repos/create\">";
209         echo "<table border=\"1\">";
210         echo "<tr><th colspan=\"2\">Create Repo</th></tr>";
211         echo "<tr><th>Repo Name</th><td><input type=\"text\" name=\"reponame\"></td><td>Name of your repo - letters, numbers _ and - only</td></tr>";
212         echo "<tr><th>Repo Description</th><td><input type=\"text\" name=\"repodesc\"></td><td>Description of your repo</td></tr>";
213         echo "<tr><th>Read Permissions</th><td>";
214         echo "<select name=\"perms\">";
215         echo "<option value=\"perms-public\">Anyone Can Read</option>";
216         echo "<option value=\"perms-registered\">Must be Registered To Read</option>";
217         echo "<option value=\"perms-onlywrite\">Only Writers can Read</option>";
218         echo "</select></td><td>The basic permissions for the initial repo creation</td></tr>";
219         echo "<tr><th>Clone From</th><td><input type=\"text\" name=\"clonefrom\"></td><td>Either a repo name (existing on this site) or a git url to clone from (blank for none)</td></tr>";
220         echo "<tr><td colspan=\"2\"><input type=\"submit\" name=\"Create\" value=\"Create\"></td></tr>";
221         echo "</table>";
222         echo "</form>";
223 }
224
225 function gwvpmini_RepoCreate()
226 {
227         
228         global $BASE_URL;
229         
230         // TODO: check the stuff out
231         // first reponame
232         $inputcheck = true;
233         
234
235         // remove a .git at the end if it was input
236         $_REQUEST["reponame"] = preg_replace("/\.git$/", "", $_REQUEST["reponame"]);
237         
238         // check for valid chars
239         $replcheck = preg_replace("/[a-zA-Z0-9_\-\.]/", "", $_REQUEST["reponame"]);
240         if(strlen($replcheck)>0) {
241                 $inputcheck = false;
242                 $inputcheckerror = "Repo name contains invalid characters, repos can only contain a-z, A-Z, 0-9, _, - and .";
243         }
244         
245         $clonefrom = false;
246         $fromremote = false;
247         if(isset($_REQUEST["clonefrom"])) {
248                 if($_REQUEST["clonefrom"] != "") {
249                         $clonefrom = $_REQUEST["clonefrom"];
250                         if(preg_match("/git.*:\/\/.*/", $clonefrom)>0) {
251                                 $fromremote = true;
252                         }
253                         if(preg_match("/http.*\:\/\//", $clonefrom)>0) $fromremote = true;
254                 }
255         }
256         
257         if($clonefrom !== false && $fromremote == false) {
258                 // check the local repo exists
259                 $rn = gwvpmini_getRepo(null, $clonefrom, null);
260                 $uid = $_SESSION["id"];
261                 
262                 if($rn == false) {
263                         gwvpmini_SendMessage("error", "local repo $clonefrom given as upstream clone, however $clonefrom doesnt exist on this site (or you cant read it unbake)");
264                         header("Location: $BASE_URL/repos");
265                         return;
266                 }
267                 
268                 // resolve repo permissions on the read/clone
269                 if(gwvpmini_GetRepoPerm($rn["id"], $uid) < 1) {
270                         gwvpmini_SendMessage("error", "local repo $clonefrom given as upstream clone, however $clonefrom doesnt exist on this site (or you cant read it bake)");
271                         header("Location: $BASE_URL/repos");
272                         return;
273                 }
274         }
275         
276         $defperms = "a";
277         switch($_REQUEST["perms"]) {
278                 case "perms-registered":
279                         $defperms = "r";
280                         break;
281                 case "perms-onlywrite":
282                         $defperms = "x";
283                         break;
284         }
285         
286         if(!$inputcheck) {
287                 gwvpmini_SendMessage("error", "$inputcheckerror");\r
288                 header("Location: $BASE_URL/repos");
289         } else  if(gwvpmini_isLoggedIn()) {
290                 //gwvpmini_createGitRepo($name, $ownerid, $desc, $bundle=null, $defaultperms=0)
291                 if(gwvpmini_HaveRepo($_REQUEST["reponame"])) {
292                         gwvpmini_SendMessage("error", "Repo ".$_REQUEST["reponame"]." already exists");\r
293                         header("Location: $BASE_URL/repos");
294                 } else {
295                         if(gwvpmini_createGitRepo($_REQUEST["reponame"], $_SESSION["id"], $_REQUEST["repodesc"], $defperms, $clonefrom, $fromremote)) {
296                                 gwvpmini_SendMessage("info", "Repo ".$_REQUEST["reponame"]." has been created");
297                         }
298                         header("Location: $BASE_URL/repos");
299                 }
300         } else {
301                 gwvpmini_SendMessage("info", "Must be logged in to create repo");
302                 header("Location: $BASE_URL/repos");
303         }
304 }
305
306 function gwvpmini_HaveRepo($reponame)
307 {
308         $repo_base = gwvpmini_getConfigVal("repodir");
309         
310         if(file_exists("$repo_base/$reponame.git")) return true;
311 }
312
313
314 function gwvpmini_RemoveRepo($rid)
315 {
316         $repo_base = gwvpmini_getConfigVal("repodir");
317         
318         $repdet = gwvpmini_getRepo(null, null, $rid);
319         
320         $rname = $repdet["name"];
321         
322         // error_log("FROM PANTS:".print_r($repdet,true)." ----------- ".print_r($rname, true));
323         
324         if($repdet != false && $rname != "") {
325                 if(file_exists("$repo_base/$rname.git")) {
326                         // recursive remove - frightening
327                         if(gwvpmini_RecursiveDelete("$repo_base/$rname.git")) {
328                                 gwvpmini_RemoveRepoDB($rid);
329                         }
330                 }
331         } return false;
332 }
333
334 function gwvpmini_RecursiveDelete($fpath)
335 {
336         // error_log("RECURSEDETELE: ".$fpath);
337         if(is_file($fpath)){\r
338                 return @unlink($fpath);\r
339         }\r
340         elseif(is_dir($fpath)){\r
341                 $scan = glob(rtrim($fpath,'/').'/*');\r
342                 foreach($scan as $index=>$path){\r
343                         gwvpmini_RecursiveDelete($path);\r
344                 }\r
345                 return @rmdir($fpath);\r
346         }
347 }
348
349 function gwvpmini_CompressCommitId($cid)
350 {
351         $compressedcid = substr($cid, 0, 5)."...".substr($cid, strlen($cid)-5, strlen($cid));
352         
353         return $compressedcid;
354 }
355
356 function gwvpmini_GetCommitDetail($repo, $commitid)
357 {
358         global $cmd_line_tool,$git_cli_cmd,$php_cli_cmd;
359         
360         $repo_base = gwvpmini_getConfigVal("repodir");
361         
362         $cmd = "$git_cli_cmd --git-dir=$repo_base/$repo.git log $commitid -1 --format='%an'";   
363         exec($cmd, $commitername, $returnvar);
364
365         $cmd = "$git_cli_cmd --git-dir=$repo_base/$repo.git log $commitid -1 --format='%ae'";   
366         exec($cmd, $commiteremail, $returnvar);
367
368         $cmd = "$git_cli_cmd --git-dir=$repo_base/$repo.git log $commitid -1 --format='%ct'";   
369         exec($cmd, $commitertime, $returnvar);
370
371         $cmd = "$git_cli_cmd --git-dir=$repo_base/$repo.git log $commitid -1 --format='%s'";    
372         exec($cmd, $commiterlog, $returnvar);
373
374         $cmd = "$git_cli_cmd --git-dir=$repo_base/$repo.git log $commitid -1 --format='%b'";    
375         exec($cmd, $commiterbody, $returnvar);
376 }
377
378 function gwvpmini_GetCommitList($repo, $branch, $num=20)
379 {
380         global $git_cli_cmd,$git_cli_cmd,$php_cli_cmd;
381         
382         $repo_base = gwvpmini_getConfigVal("repodir");
383         
384         $cmd = "$git_cli_cmd --git-dir=$repo_base/$repo.git log $commitid -1 --format='%an'";\r
385         exec($cmd, $commitername, $returnvar);\r
386         
387 }
388
389 function gwvpmini_GetRefList($repo)
390 {
391         global $cmd_line_tool,$git_cli_cmd,$php_cli_cmd;
392         
393         $repo_base = gwvpmini_getConfigVal("repodir");
394
395         $cmd = "$git_cli_cmd --git-dir=$repo_base/$repo.git for-each-ref $commitid --format='%(objecttype):%(objectname):%(refname)'";
396         error_log("command was $cmd");
397         exec($cmd, $reflist, $returnvar);
398         
399         return $reflist;
400 }
401         
402 ?>