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_CreateUpdateHookInRepo($repopath)
31 $fp = fopen("$repopath/hooks/$repopath", "w");
33 if(!$fp) error_log("could not create update hook");
35 // TODO: think about this one
39 function gwvpmini_gitBackendInterface()
41 // and this is where i re-code the git backend interface from scratch
44 header_remove("Pragma");
\r
45 header_remove("Cache-Control");
\r
46 header_remove("Set-Cookie");
\r
47 header_remove("Expires");
\r
48 header_remove("X-Powered-By");
\r
49 header_remove("Vary");
\r
52 $repo_base = gwvpmini_getConfigVal("repodir");
54 // TODO: we need to stop passing the repo name around as "repo.git", it needs to be just "repo"
57 /* bizare git problem that ignores 403's or continues on with a push despite them
58 error_log("FLAP for ".$_SERVER["REQUEST_URI"]);
59 if(isset($_REQUEST)) {
60 $dump = print_r($_REQUEST, true);
61 error_log("FLAP, $dump");
63 if(isset($_SERVER["PHP_AUTH_USER"])) {
64 error_log("FLAP: donut hole");
72 if(isset($_REQUEST["q"])) {
73 $query = $_REQUEST["q"];
74 $qspl = explode("/", $query);
76 $repo = preg_replace("/\.git$/", "", $qspl[1]);
77 $repoid = gwvpmini_GetRepoId($repo);
78 for($i=2; $i < count($qspl); $i++) {
79 $newloc .= "/".$qspl[$i];
83 if($repoid == false) {
84 gwvpmini_fourZeroFour();
88 // we do an update server cause its weird and i cant figure out when it actually needs to happen
89 chdir("$repo_base/$repo.git");
90 exec("/usr/bin/git update-server-info");
93 // so now we have the repo
94 // next we determine if this is a read or a write
96 // TODO: WE NEED TO FIX THIS, IT DOESNT ALWAYS DETECT a "WRITE"
98 if(isset($_REQUEST["service"])) {
99 if($_REQUEST["service"] == "git-receive-pack") {
100 error_log("got write as receivepack in post");
106 // 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
107 // no, this isnt a solution cause auth'd read attempts will come up as writes...
108 //if(isset($_SERVER["PHP_AUTH_USER"])) {
113 $person = gwvpmini_checkBasicAuthLogin();
115 // next, figure out permissions for repo
116 $rid = gwvpmini_GetRepoId($repo);
118 error_log("AT THIS POINT WE HAVE $uid, $rid, $repo $person");
122 error_log("ASK FOR BASIC AUTH");
123 gwvpmini_AskForBasicAuth();
126 $perm = gwvpmini_GetRepoPerm($rid, "a");
128 error_log("ASK FOR BASIC AUTH 2");
129 gwvpmini_AskForBasicAuth();
134 $uid = gwvpmini_GetUserId($person);
135 $perm = gwvpmini_GetRepoPerm($rid, $uid);
138 error_log("SEND FOFF");
139 gwvpmini_fourZeroThree();
144 gwvpmini_fourZeroThree();
150 // if its a write, we push for authentication
154 $person = "anonymous";
157 // if its a write, we check (before and after) the branch/tag info to see if they were updated
161 gwvpmini_callGitBackend($person, $repo);
168 // if we made it this far, we a read and we have permissions to do so, just search the file from the repo
169 /*if(file_exists("$repo_base/$repo.git/$newloc")) {
170 error_log("would ask $repo for $repo.git/$newloc from $repo_base/$repo.git/$newloc");
171 $fh = fopen("$repo_base/$repo.git/$newloc", "rb");
173 error_log("pushing file");
175 echo fread($fh, 8192);
178 error_log("would ask $repo for $repo/$newloc from $repo_base/$repo/$newloc, NE");
179 gwvpmini_fourZeroFour();
185 function gwvpmini_canManageRepo($userid, $repoid)
187 // only the owner or an admin can do these tasks
188 error_log("Checking repoid, $repoid against userid $userid");
190 if(gwvpmini_IsUserAdmin(null, null, $userid)) return true;
191 if(gwvpmini_IsRepoOwner($userid, $repoid)) return true;
195 function gwvpmini_callGitBackend($username, $repo)
197 // this is where things become a nightmare
198 $fh = fopen('php://input', "r");
200 $repo_base = gwvpmini_getConfigVal("repodir");
\r
203 $ruri = $_SERVER["REQUEST_URI"];
204 $strrem = "git/$repo.git";
205 $euri = str_replace($strrem, "", $_REQUEST["q"]);
206 //$euri = preg_replace("/^git\/$repo\.git/", "", $_REQUEST["q"]);
210 $rmeth = $_SERVER["REQUEST_METHOD"];
213 foreach($_REQUEST as $key => $var) {
215 //error_log("adding, $var from $key");
216 if($qs == "") $qs.="$key=$var";
217 else $qs.="&$key=$var";
224 // this is where the fun, it ends.
231 if(isset($procenv)) unset($procenv);
232 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
233 $procenv["PATH_TRANSLATED"] = "/$repo_base/$repo.git/$euri";
234 $procenv["REQUEST_METHOD"] = "$rmeth";
235 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
236 $procenv["QUERY_STRING"] = "$qs";
237 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
238 $procenv["REMOTE_USER"] = "$username";
239 $procenv["REMOTE_ADDR"] = $_SERVER["REMOTE_ADDR"];
240 $procenv["AUTH_TYPE"] = "Basic";
242 if(isset($_SERVER["CONTENT_TYPE"])) {
243 $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
245 //$procenv["CONTENT_TYPE"] = "";
247 if(isset($_SERVER["CONTENT_LENGTH"])) {
248 $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
251 error_log("path trans'd is /$repo_base/$repo.git/$euri from $ruri with ".$_REQUEST["q"]." $strrem");
256 $pwd = "/$repo_base/";
258 $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);
261 while(!$untilblank&&!feof($pipes[1])) {
262 $lines_t = fgets($pipes[1]);
263 $lines = trim($lines_t);
264 error_log("got line: $lines");
265 if($lines_t == "\r\n") {
267 error_log("now blank");
268 } else header($lines);
269 if($lines === false) {
270 error_log("got an unexpexted exit...");
280 if(!stream_set_blocking($fh,0)) {
281 error_log("cant set input non-blocking");
284 if(!stream_set_blocking($pipes[1],0)) {
285 error_log("cant set pipe1 non-blocking");
289 $fp = fopen("/tmp/gitup.".rand(0,4000000), "w");
290 // i was going to use stream_select, but i feel this works better like this
294 $from_client_data = fread($fh,8192);
295 if($from_client_data !== false) {
296 fwrite($pipes[0], $from_client_data);
297 fwrite($fp, $from_client_data);
300 //fwrite($fl, $from_client_data);
301 $client_len = strlen($from_client_data);
303 error_log("client end");
308 // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
309 // but i do know that the fgets for the headers above should have comsued that
310 if(!feof($pipes[1])) {
311 $from_cgi_data_t = fread($pipes[1],8192);
312 $from_cgi_data = $from_cgi_data_t;
314 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
315 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
316 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
318 if(strlen($from_cgi_data_t)>0) {
319 // i dont get why this happens, and its very frustrating.. im not sure if its a bug in php
320 // or something the git-http-backend thing is doing..
321 // TODO: find out why this happens
322 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
323 if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) {
324 error_log("MOOOKS - we did trunc");
326 error_log("MOOOKS - we did not trunc");
332 if($from_cgi_data !== false) {
336 $cgi_len = strlen($from_cgi_data);
338 error_log("cgi end");
342 if(feof($pipes[1])) $continue = false;
344 if($client_len == 0 && $cgi_len == 0) {
346 error_log("sleep tick");
348 error_log("sizes: $client_len, $cgi_len");
350 error_log("from cgi: \"$from_cgi_data\"");
366 function gwvpmini_repoExists($name)
368 $repo_base = gwvpmini_getConfigVal("repodir");
370 if(file_exists("$repo_base/$name.git")) return true;
375 // 0 - anyone can clone/read, only owner can write
376 // 1 - noone can clone/read, repo is visible (i.e. name), only owner can read/write repo
377 // 2 - only owner can see anything
378 function gwvpmini_createGitRepo($name, $ownerid, $desc)
380 $repo_base = gwvpmini_getConfigVal("repodir");
382 // phew, this works, but i tell you this - bundles arent quite as nice as they should be
383 error_log("would create $repo_base/$name.git");
384 exec("/usr/bin/git init $repo_base/$name.git --bare > /tmp/gitlog 2>&1");
385 chdir("$repo_base/$name.git");
386 exec("/usr/bin/git update-server-info");
388 // gwvpmini_AddRepo($reponame, $repodesc, $repoowner, $defaultperms = 0)
389 gwvpmini_AddRepo($name, $desc, $ownerid);