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;
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");
\r
72 $repo_base = gwvpmini_getConfigVal("repodir");
74 // TODO: we need to stop passing the repo name around as "repo.git", it needs to be just "repo"
77 /* bizare git problem that ignores 403's or continues on with a push despite them
78 // error_log("FLAP for ".$_SERVER["REQUEST_URI"]);
79 if(isset($_REQUEST)) {
80 $dump = print_r($_REQUEST, true);
81 // error_log("FLAP, $dump");
83 if(isset($_SERVER["PHP_AUTH_USER"])) {
84 // error_log("FLAP: donut hole");
87 error_log("REQUESTINBACKEND: ".print_r($_REQUEST, true));
92 if(isset($_REQUEST["q"])) {
93 $query = $_REQUEST["q"];
94 $qspl = explode("/", $query);
96 $repo = preg_replace("/\.git$/", "", $qspl[1]);
97 $repoid = gwvpmini_GetRepoId($repo);
98 for($i=2; $i < count($qspl); $i++) {
99 $newloc .= "/".$qspl[$i];
103 if($repoid == false) {
104 gwvpmini_fourZeroFour();
108 // we do an update server cause its weird and i cant figure out when it actually needs to happen
109 chdir("$repo_base/$repo.git");
110 // dont believe i have to do this
111 //exec("/usr/bin/git update-server-info");
113 if(!file_exists("$repo_base/$repo.git/hooks/pre-receive") || !file_exists("$repo_base/$repo.git/hooks/update")) {
114 // error_log("WRITING HOOKS");
115 gwvpmini_CreateRepoHooks("$repo_base/$repo.git", $cmd_line_tool, $repo);
119 // so now we have the repo
120 // next we determine if this is a read or a write
122 // TODO: WE NEED TO FIX THIS, IT DOESNT ALWAYS DETECT a "WRITE"
124 if(isset($_REQUEST["service"])) {
125 if($_REQUEST["service"] == "git-receive-pack") {
126 // error_log("got write as receivepack in post");
130 if(preg_match("/.*git-receive-pack$/", $_REQUEST["q"])) $write = true;
132 // 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
133 // no, this isnt a solution cause auth'd read attempts will come up as writes...
134 //if(isset($_SERVER["PHP_AUTH_USER"])) {
139 $person = gwvpmini_checkBasicAuthLogin();
141 // next, figure out permissions for repo
142 $rid = gwvpmini_GetRepoId($repo);
144 // error_log("AT THIS POINT WE HAVE $uid, $rid, $repo $person");
148 // error_log("ASK FOR BASIC AUTH");
149 gwvpmini_AskForBasicAuth();
152 $perm = gwvpmini_GetRepoPerm($rid, "a");
154 // error_log("ASK FOR BASIC AUTH 2");
155 gwvpmini_AskForBasicAuth();
160 $uid = gwvpmini_GetUserId($person);
161 $perm = gwvpmini_GetRepoPerm($rid, $uid);
164 // error_log("SEND FOFF");
165 gwvpmini_fourZeroThree();
170 gwvpmini_fourZeroThree();
176 // if its a write, we push for authentication
180 $person = "anonymous";
183 // if its a write, we check (before and after) the branch/tag info to see if they were updated
185 error_log("REQUESTINBACKEND: processed as write");
187 error_log("REQUESTINBACKEND: processed as read");
190 gwvpmini_callGitBackend($person, $repo);
197 // if we made it this far, we a read and we have permissions to do so, just search the file from the repo
198 /*if(file_exists("$repo_base/$repo.git/$newloc")) {
199 // error_log("would ask $repo for $repo.git/$newloc from $repo_base/$repo.git/$newloc");
200 $fh = fopen("$repo_base/$repo.git/$newloc", "rb");
202 // error_log("pushing file");
204 echo fread($fh, 8192);
207 // error_log("would ask $repo for $repo/$newloc from $repo_base/$repo/$newloc, NE");
208 gwvpmini_fourZeroFour();
214 function gwvpmini_canManageRepo($userid, $repoid)
216 // only the owner or an admin can do these tasks
217 // error_log("Checking repoid, $repoid against userid $userid");
219 if(gwvpmini_IsUserAdmin(null, null, $userid)) return true;
220 if(gwvpmini_IsRepoOwner($userid, $repoid)) return true;
224 function gwvpmini_callGitBackend($username, $repo)
226 // this is where things become a nightmare
227 $fh = fopen('php://input', "r");
229 $repo_base = gwvpmini_getConfigVal("repodir");
\r
232 $ruri = $_SERVER["REQUEST_URI"];
233 $strrem = "git/$repo.git";
234 $euri = str_replace($strrem, "", $_REQUEST["q"]);
235 //$euri = preg_replace("/^git\/$repo\.git/", "", $_REQUEST["q"]);
239 $rmeth = $_SERVER["REQUEST_METHOD"];
242 foreach($_REQUEST as $key => $var) {
244 //// error_log("adding, $var from $key");
245 if($qs == "") $qs.="$key=$var";
246 else $qs.="&$key=$var";
252 $userdets = gwvpmini_getUser($username);
254 // this is where the fun, it ends.
261 if(isset($procenv)) unset($procenv);
262 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
263 $procenv["PATH_TRANSLATED"] = "/$repo_base/$repo.git/$euri";
264 $procenv["REQUEST_METHOD"] = "$rmeth";
265 $procenv["GIT_COMMITTER_NAME"] = $userdets["fullname"];
266 $procenv["GIT_COMMITTER_EMAIL"] = $userdets["email"];
267 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
268 $procenv["QUERY_STRING"] = "$qs";
269 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
270 $procenv["REMOTE_USER"] = "$username";
271 $procenv["REMOTE_ADDR"] = $_SERVER["REMOTE_ADDR"];
272 $procenv["AUTH_TYPE"] = "Basic";
274 //// error_log("PROCENV: ".print_r($procenv,true));
276 if(isset($_SERVER["CONTENT_TYPE"])) {
277 $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
279 //$procenv["CONTENT_TYPE"] = "";
281 if(isset($_SERVER["CONTENT_LENGTH"])) {
282 $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
285 // error_log("path trans'd is /$repo_base/$repo.git/$euri from $ruri with ".$_REQUEST["q"]." $strrem");
290 $pwd = "/$repo_base/";
292 $proc = proc_open("/usr/lib/git-core/git-http-backend", array(array("pipe","rb"),array("pipe","wb"),array("file","/tmp/err", "a")), $pipes, $pwd, $procenv);
295 while(!$untilblank&&!feof($pipes[1])) {
296 $lines_t = fgets($pipes[1]);
297 $lines = trim($lines_t);
298 // error_log("got line: $lines");
299 if($lines_t == "\r\n") {
301 // error_log("now blank");
302 } else header($lines);
303 if($lines === false) {
304 // error_log("got an unexpexted exit...");
314 if(!stream_set_blocking($fh,0)) {
315 // error_log("cant set input non-blocking");
318 if(!stream_set_blocking($pipes[1],0)) {
319 // error_log("cant set pipe1 non-blocking");
324 $fp = fopen("/tmp/gitup.".rand(0,4000000), "w");
325 // i was going to use stream_select, but i feel this works better like this
329 $from_client_data = fread($fh,8192);
330 if($from_client_data !== false) {
331 fwrite($pipes[0], $from_client_data);
332 fwrite($fp, $from_client_data);
335 //fwrite($fl, $from_client_data);
336 $client_len = strlen($from_client_data);
338 // error_log("client end");
344 // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
345 // but i do know that the fgets for the headers above should have comsued that
346 if(!feof($pipes[1])) {
347 $from_cgi_data_t = fread($pipes[1],8192);
348 $from_cgi_data = $from_cgi_data_t;
350 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
351 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
352 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
354 if(strlen($from_cgi_data_t)>0) {
355 // i dont get why this happens, and its very frustrating.. im not sure if its a bug in php
356 // or something the git-http-backend thing is doing..
357 // TODO: find out why this happens
358 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
359 if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) {
360 // error_log("MOOOKS - we did trunc");
362 // error_log("MOOOKS - we did not trunc");
368 if($from_cgi_data !== false) {
372 $cgi_len = strlen($from_cgi_data);
374 // error_log("cgi end");
378 if(feof($pipes[1])) $continue = false;
380 if($client_len == 0 && $cgi_len == 0) {
382 // error_log("sleep tick");
384 if($stlimit > 50) $continue = false;
387 // error_log("sizes: $client_len, $cgi_len");
389 // error_log("from cgi: \"$from_cgi_data\"");
405 function gwvpmini_repoExists($name)
407 $repo_base = gwvpmini_getConfigVal("repodir");
409 if(file_exists("$repo_base/$name.git")) return true;
414 // 0 - anyone can clone/read, only owner can write
415 // 1 - noone can clone/read, repo is visible (i.e. name), only owner can read/write repo
416 // 2 - only owner can see anything
417 function gwvpmini_createGitRepo($name, $ownerid, $desc, $defperms, $clonefrom, $isremoteclone)
419 $repo_base = gwvpmini_getConfigVal("repodir");
421 if($clonefrom !== false) {
422 error_log("how did i end up in clonefrom? $clonefrom");
423 if(!$isremoteclone) {
424 exec("/usr/bin/git clone --bare $repo_base/$clonefrom.git $repo_base/$name.git >> /tmp/gitlog 2>&1");
425 gwvpmini_AddRepo($name, $desc, $ownerid, $defperms, $clonefrom);
427 // we do this from an outside call in the background
428 gwvpmini_SendMessage("error", "Cant clone from remote repos yet");
433 // phew, this works, but i tell you this - bundles arent quite as nice as they should be
434 // error_log("would create $repo_base/$name.git");
435 exec("/usr/bin/git init $repo_base/$name.git --bare >> /tmp/gitlog 2>&1");
436 chdir("$repo_base/$name.git");
437 exec("/usr/bin/git update-server-info");
439 // gwvpmini_AddRepo($reponame, $repodesc, $repoowner, $defaultperms = 0)
440 gwvpmini_AddRepo($name, $desc, $ownerid, $defperms, $clonefrom);