3 $CALL_ME_FUNCTIONS["gitcontrol"] = "gwvpmini_gitControlCallMe";
5 //$MENU_ITEMS["20repos"]["text"] = "Repo Admin";
6 //$MENU_ITEMS["20repos"]["link"] = "$BASE_URL/admin/repos";
8 // TODO: we could actually change backend interface such that is
9 // will respond to any url's that contain "repo.git" rather then
10 // having to be $BASE_URL/git/repo.git
11 function gwvpmini_gitControlCallMe()
13 if(isset($_REQUEST["q"])) {
14 $query = $_REQUEST["q"];
15 $qspl = explode("/", $query);
17 if($qspl[0] == "git") {
18 return "gwvpmini_gitBackendInterface";
29 function gwvpmini_CreateRepoHooks($repopath, $cmdpath, $reponame)
31 $fp = fopen("$repopath/hooks/pre-receive", "w");
33 if(!$fp) error_log("could not create pre-receive hook");
35 // TODO: think about this one
36 //$script = '#!/bin/bash'."\n\n".'DCOMMIT=`cat`'."\n".'START=`echo $DCOMMIT|cut -d " " -f 1`'."\n".'END=`echo $DCOMMIT|cut -d " " -f 2`'."\n".'REF=`echo $DCOMMIT|cut -d " " -f 3`'."\n\n";
37 $script = "#!/bin/bash\n\nDCOMMIT=".'`cat`'."\n\nphp $cmdpath $reponame \$REMOTE_USER pre-receive \$DCOMMIT\n\n";
42 chmod("$repopath/hooks/pre-receive", 0755);
45 $fp = fopen("$repopath/hooks/update", "w");
47 if(!$fp) error_log("could not create update hook");
49 // TODO: think about this one
51 $script = "#!/bin/bash\n\nphp $cmdpath $reponame \$REMOTE_USER update \$1 \$2 \$3\n\n";
56 chmod("$repopath/hooks/update", 0755);
59 function gwvpmini_gitBackendInterface()
61 // and this is where i re-code the git backend interface from scratch
62 global $BASE_URL, $cmd_line_tool, $git_cli_cmd;
64 header_remove("Pragma");
\r
65 header_remove("Cache-Control");
\r
66 header_remove("Set-Cookie");
\r
67 header_remove("Expires");
\r
68 header_remove("X-Powered-By");
\r
69 header_remove("Vary");
70 //header("Content-Encoding: none");
\r
73 $repo_base = gwvpmini_getConfigVal("repodir");
75 // TODO: we need to stop passing the repo name around as "repo.git", it needs to be just "repo"
78 /* bizare git problem that ignores 403's or continues on with a push despite them
79 // error_log("FLAP for ".$_SERVER["REQUEST_URI"]);
80 if(isset($_REQUEST)) {
81 $dump = print_r($_REQUEST, true);
82 // error_log("FLAP, $dump");
84 if(isset($_SERVER["PHP_AUTH_USER"])) {
85 // error_log("FLAP: donut hole");
88 error_log("REQUESTINBACKEND: ".print_r($_REQUEST, true));
93 if(isset($_REQUEST["q"])) {
94 $query = $_REQUEST["q"];
95 $qspl = explode("/", $query);
97 $repo = preg_replace("/\.git$/", "", $qspl[1]);
98 $repoid = gwvpmini_GetRepoId($repo);
99 for($i=2; $i < count($qspl); $i++) {
100 $newloc .= "/".$qspl[$i];
104 if($repoid == false) {
105 gwvpmini_fourZeroFour();
109 // we do an update server cause its weird and i cant figure out when it actually needs to happen
110 chdir("$repo_base/$repo.git");
111 // dont believe i have to do this
112 //exec("/usr/bin/git update-server-info");
114 if(!file_exists("$repo_base/$repo.git/hooks/pre-receive") || !file_exists("$repo_base/$repo.git/hooks/update")) {
115 // error_log("WRITING HOOKS");
116 gwvpmini_CreateRepoHooks("$repo_base/$repo.git", $cmd_line_tool, $repo);
120 // so now we have the repo
121 // next we determine if this is a read or a write
123 // TODO: WE NEED TO FIX THIS, IT DOESNT ALWAYS DETECT a "WRITE"
125 if(isset($_REQUEST["service"])) {
126 if($_REQUEST["service"] == "git-receive-pack") {
127 // error_log("got write as receivepack in post");
131 if(preg_match("/.*git-receive-pack$/", $_REQUEST["q"])) $write = true;
133 // THIS MAY CAUSE ISSUES LATER ON but we do it cause the git client ignores our 403 when it uses git-receive-pack after an auth
134 // no, this isnt a solution cause auth'd read attempts will come up as writes...
135 //if(isset($_SERVER["PHP_AUTH_USER"])) {
140 $person = gwvpmini_checkBasicAuthLogin();
142 // next, figure out permissions for repo
143 $rid = gwvpmini_GetRepoId($repo);
145 // error_log("AT THIS POINT WE HAVE $uid, $rid, $repo $person");
149 // error_log("ASK FOR BASIC AUTH");
150 gwvpmini_AskForBasicAuth();
153 $perm = gwvpmini_GetRepoPerm($rid, "a");
155 // error_log("ASK FOR BASIC AUTH 2");
156 gwvpmini_AskForBasicAuth();
161 $uid = gwvpmini_GetUserId($person);
162 $perm = gwvpmini_GetRepoPerm($rid, $uid);
165 // error_log("SEND FOFF");
166 gwvpmini_fourZeroThree();
171 gwvpmini_fourZeroThree();
177 // if its a write, we push for authentication
181 $person = "anonymous";
184 // if its a write, we check (before and after) the branch/tag info to see if they were updated
186 error_log("REQUESTINBACKEND: processed as write");
188 error_log("REQUESTINBACKEND: processed as read");
191 gwvpmini_callGitBackend($person, $repo);
198 // if we made it this far, we a read and we have permissions to do so, just search the file from the repo
199 /*if(file_exists("$repo_base/$repo.git/$newloc")) {
200 // error_log("would ask $repo for $repo.git/$newloc from $repo_base/$repo.git/$newloc");
201 $fh = fopen("$repo_base/$repo.git/$newloc", "rb");
203 // error_log("pushing file");
205 echo fread($fh, 8192);
208 // error_log("would ask $repo for $repo/$newloc from $repo_base/$repo/$newloc, NE");
209 gwvpmini_fourZeroFour();
215 function gwvpmini_canManageRepo($userid, $repoid)
217 // only the owner or an admin can do these tasks
218 // error_log("Checking repoid, $repoid against userid $userid");
220 if(gwvpmini_IsUserAdmin(null, null, $userid)) return true;
221 if(gwvpmini_IsRepoOwner($userid, $repoid)) return true;
226 // TODO: this whole bit needs a re-write - seriously, like totally!
227 function gwvpmini_callGitBackend($username, $repo)
230 global $git_backend_cmd, $git_cli_cmd, $php_cli_cmd, $data_directory, $cmd_line_tool;
\r
232 // this is where things become a nightmare
233 $fh = fopen('php://input', "r");
235 $repo_base = gwvpmini_getConfigVal("repodir");
\r
238 $ruri = $_SERVER["REQUEST_URI"];
239 $strrem = "git/$repo.git";
240 $euri = str_replace($strrem, "", $_REQUEST["q"]);
241 //$euri = preg_replace("/^git\/$repo\.git/", "", $_REQUEST["q"]);
245 $rmeth = $_SERVER["REQUEST_METHOD"];
248 foreach($_REQUEST as $key => $var) {
250 //// error_log("adding, $var from $key");
251 if($qs == "") $qs.="$key=$var";
252 else $qs.="&$key=$var";
258 $userdets = gwvpmini_getUser($username);
260 // this is where the fun, it ends.
267 if(isset($procenv)) unset($procenv);
268 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
269 $procenv["PATH_TRANSLATED"] = "/$repo_base/$repo.git/$euri";
270 $procenv["REQUEST_METHOD"] = "$rmeth";
271 $procenv["GIT_COMMITTER_NAME"] = $userdets["fullname"];
272 $procenv["GIT_COMMITTER_EMAIL"] = $userdets["email"];
273 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
274 $procenv["QUERY_STRING"] = "$qs";
275 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
276 $procenv["REMOTE_USER"] = "$username";
277 $procenv["REMOTE_ADDR"] = $_SERVER["REMOTE_ADDR"];
278 $procenv["AUTH_TYPE"] = "Basic";
280 //// error_log("PROCENV: ".print_r($procenv,true));
282 if(isset($_SERVER["CONTENT_TYPE"])) {
283 $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
285 //$procenv["CONTENT_TYPE"] = "";
287 if(isset($_SERVER["CONTENT_LENGTH"])) {
288 $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
291 $headers = getallheaders();
293 //error_log("path trans'd is /$repo_base/$repo.git/$euri from $ruri with ".$_REQUEST["q"]." $strrem pre is ".print_r($_REQUEST,true)." and foff ".print_r($headers, true));
295 $pwd = "/$repo_base/";
297 $proc = proc_open("$git_backend_cmd", array(array("pipe","rb"),array("pipe","wb"),array("file","/tmp/err", "a")), $pipes, $pwd, $procenv);
300 while(!$untilblank&&!feof($pipes[1])) {
301 $lines_t = fgets($pipes[1]);
302 $lines = trim($lines_t);
303 // error_log("got line: $lines");
304 if($lines_t == "\r\n") {
306 // error_log("now blank");
307 } else header($lines);
308 if($lines === false) {
309 // error_log("got an unexpexted exit...");
319 if(!stream_set_blocking($fh,0)) {
320 // error_log("cant set input non-blocking");
323 if(!stream_set_blocking($pipes[1],0)) {
324 // error_log("cant set pipe1 non-blocking");
329 //$fp = fopen("/tmp/gitup.".rand(0,4000000), "w");
330 $from_client_data = "";
331 // i was going to use stream_select, but i feel this works better like this
335 // lets start again....
338 if(isset($headers["Content-Length"])) {
339 $cl = $headers["Content-Length"];
340 while(strlen($from_client_data) < $cl) {
341 error_log("re-read ".strlen($from_client_data)." of $cl");
342 $from_client_data .= fread($fh, $cl);
344 } else $from_client_data = fread($fh,8192);
346 // TODO: BIG TODO: must rewrite this bit. its ugly as sin, it works, but it probably shouldnt.
348 if(isset($headers["Content-Encoding"])) {
349 if($headers["Content-Encoding"] == "gzip") {
350 //error_log("DAM YOU GIT CLIENT and your retarded gzip encoding");
351 $from_client_data = gzinflate(substr($from_client_data, 10));
352 //$from_client_data = gzdecode($from_client_data);
355 if($from_client_data !== false) {
357 // push the data from the client straight to backend
358 fwrite($pipes[0], $from_client_data);
359 //fwrite($fp, $from_client_data);
362 //fwrite($fl, $from_client_data);
363 $client_len = strlen($from_client_data);
365 //error_log("client end");
374 // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
375 // but i do know that the fgets for the headers above should have comsued that
376 if(!feof($pipes[1])) {
377 $from_cgi_data_t = fread($pipes[1],8192);
378 $from_cgi_data = $from_cgi_data_t;
380 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
381 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
382 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
384 if(strlen($from_cgi_data_t)>0) {
385 // i dont get why this happens, and its very frustrating.. im not sure if its a bug in php
386 // or something the git-http-backend thing is doing..
387 // TODO: find out why this happens
388 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
390 if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) {
391 error_log("MOOOKS - we did trunc");
393 error_log("MOOOKS - we did not trunc");
399 if($from_cgi_data !== false && $from_cgi_data != "") {
400 //error_log("send to client of size ".strlen($from_cgi_data));
405 $cgi_len = strlen($from_cgi_data);
407 // error_log("cgi end");
413 if(feof($pipes[1])) $continue = false;
415 if($client_len == 0 && $cgi_len == 0) {
417 //error_log("sleep tick");
419 if($stlimit > 300) $continue = false; // if we get no output from git backend after 1 minute, we close..... something went wrong
422 //error_log("sizes: $client_len, $cgi_len");
424 //error_log("from cgi: \"$from_cgi_data\"");
432 error_log("and im done...");
444 function gwvpmini_repoExists($name)
446 $repo_base = gwvpmini_getConfigVal("repodir");
448 if(file_exists("$repo_base/$name.git")) return true;
453 // 0 - anyone can clone/read, only owner can write
454 // 1 - noone can clone/read, repo is visible (i.e. name), only owner can read/write repo
455 // 2 - only owner can see anything
456 function gwvpmini_createGitRepo($name, $ownerid, $desc, $defperms, $clonefrom, $isremoteclone)
458 global $cmd_line_tool,$git_cli_cmd,$php_cli_cmd;
460 $repo_base = gwvpmini_getConfigVal("repodir");
462 if($clonefrom !== false) {
463 error_log("how did i end up in clonefrom? $clonefrom");
464 if(!$isremoteclone) {
465 exec("$git_cli_cmd clone --bare $repo_base/$clonefrom.git $repo_base/$name.git >> /tmp/gitlog 2>&1");
466 gwvpmini_AddRepo($name, $desc, $ownerid, $defperms, $clonefrom);
468 // we do this from an outside call in the background
469 $cmd = "$php_cli_cmd $cmd_line_tool $clonefrom $name backgroundclone >> /tmp/gitlog 2>&1 &";
470 error_log("cmd called as $cmd");
472 gwvpmini_AddRepo($name, $desc, $ownerid, $defperms, $clonefrom);
473 $rn = gwvpmini_getRepo(null, $name, null);
475 gwvpmini_SetRepoCloning($rid);
476 gwvpmini_SendMessageByDb("info", "Background clone initiated for $name ($rid) from $clonefrom... your repo will be available once the background clone is finished", $ownerid);
481 // phew, this works, but i tell you this - bundles arent quite as nice as they should be
482 // error_log("would create $repo_base/$name.git");
483 exec("$git_cli_cmd init $repo_base/$name.git --bare >> /tmp/gitlog 2>&1");
484 chdir("$repo_base/$name.git");
485 exec("$git_cli_cmd update-server-info");
487 // gwvpmini_AddRepo($reponame, $repodesc, $repoowner, $defaultperms = 0)
488 gwvpmini_AddRepo($name, $desc, $ownerid, $defperms, $clonefrom);