3 $CALL_ME_FUNCTIONS["gitcontrol"] = "gwvp_gitControlCallMe";
5 //$MENU_ITEMS["20repos"]["text"] = "Repo Admin";
6 //$MENU_ITEMS["20repos"]["link"] = "$BASE_URL/admin/repos";
7 $HOME_PAGE_PROVIDERS["gitlog"] = "gwvp_GitLogProvider";
9 function gwvp_gitControlCallMe()
11 if(isset($_REQUEST["q"])) {
12 $query = $_REQUEST["q"];
13 $qspl = explode("/", $query);
15 if($qspl[0] == "git") {
16 return "gwvp_gitBackendInterface";
26 function gwvp_GitLogProvider()
28 echo "<br>gitload provider loaded on homepage<br>";
31 function gwvp_repoPermissionCheck($repo, $user)
36 function gwvp_gitBackendInterface()
38 // and this is where i re-code the git backend interface from scratch
41 $repo_base = gwvp_getConfigVal("repodir");
43 // TODO: we need to stop passing the repo name around as "repo.git", it needs to be just "repo"
48 if(isset($_REQUEST["q"])) {
49 $query = $_REQUEST["q"];
50 $qspl = explode("/", $query);
52 $repo = preg_replace("/\.git$/", "", $qspl[1]);
53 $repoid = gwvp_GetRepoId($repo);
54 for($i=2; $i < count($qspl); $i++) {
55 $newloc .= "/".$qspl[$i];
59 if($repoid == false) {
64 // we do an update server cause its weird and i cant figure out when it actually needs to happen
65 chdir("$repo_base/$repo.git");
66 exec("/usr/bin/git update-server-info");
69 // so now we have the repo
70 // next we determine if this is a read or a write
72 if(isset($_REQUEST["service"])) {
73 if($_REQUEST["service"] == "git-receive-pack") {
77 if($_SERVER["REQUEST_METHOD"] == "POST") {
81 // if its a write, we push for authentication
83 $person = gwvp_checkBasicAuthLogin();
84 if($person == false) {
85 gwvp_AskForBasicAuth();
88 error_log("checking perms for $person against $repoid");
89 $perms = gwvp_resolvRepoPerms(gwvp_getUserId($person), $repoid);
94 // here we pass to the git backend
95 error_log("perms are $perms and im allowed");
96 gwvp_callGitBackend($person["username"], $repo);
102 // if not we figure out the anon permissions for a repo
103 $perms = gwvp_resolvRepoPerms(-1, $repoid);
105 // if they're less then read, we need to then check the user auth permissions
108 $person = gwvp_checkBasicAuthLogin();
109 if($person == false) {
110 gwvp_AskForBasicAuth();
113 $perms = gwvp_resolvRepoPerms(gwvp_getUserId($person), $repoid);
115 $dump = print_r($person, true);
116 error_log("in basic read, called 403 for $perms $dump");
117 gwvp_fourZeroThree();
123 // if we made it this far, we a read and we have permissions to do so, just search the file from the repo
124 if(file_exists("$repo_base/$repo.git/$newloc")) {
125 error_log("would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc");
126 $fh = fopen("$repo_base/$repo.git/$newloc", "rb");
128 error_log("pushing file");
130 echo fread($fh, 8192);
133 //echo "would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc, NE";
141 function gwvp_gitBackendInterface_old()
145 $repo_base = gwvp_getConfigVal("repodir");
149 if(isset($_REQUEST["q"])) {
150 $query = $_REQUEST["q"];
151 $qspl = explode("/", $query);
153 for($i=2; $i < count($qspl); $i++) {
154 $newloc .= "/".$qspl[$i];
158 $actual_repo_name = preg_replace("/\.git$/", "", $repo);
160 $user = gwvp_checkBasicAuthLogin();
163 error_log("User is set to false, so its anonymouse");
165 error_log("user is $user");
168 // must remember that $user of false is anonymous when we code gwvp_repoPerm'sCheck()
169 if(!gwvp_repoPermissionCheck($actual_repo_name, $user)) {
170 error_log("perms check fails - start auth");
171 if(isset($_SERVER["PHP_AUTH_USER"])) {
172 error_log("have auth - push 403");
173 gwvp_fourZeroThree();
175 error_log("push auth");
176 gwvp_AskForBasicAuth();
181 // we need to quite a bit of parsing in here. The "repo" will always be /git/repo.git
182 // but if we get here from a browser, we need to forward back to a normal repo viewer
183 // the only way i can think of doing this is to check the useragent for the word "git"
187 * 1) figure out the repo its acessing
188 * 2) figure out the perms on the repo
189 * 3) determine if its a pull or a push
190 * - if its a pull, we just serve straight from the fs
191 * - if its a push, we go thru git-http-backend
192 * 4) if it requiers auth, we push to auth
195 $agent = "git-unknown";
198 // tested the user agent bit with jgit from eclipse and normal git... seems to work
199 if(isset($_SERVER["HTTP_USER_AGENT"])) {
200 $agent = $_SERVER["HTTP_USER_AGENT"];
201 error_log("in git backend with user agent $agent");
202 if(stristr($agent, "git")!==false) {
209 /* dont need this code right now
210 if($isgitagent) echo "GIT: i am a git backened interface for a repo $repo, agent $agent";
211 else echo "NOT GIT: i am a git backened interface for a repo $repo, agent $agent";
214 // now we need to rebuild the actual request or do we?
215 //$basegit = "$BASE_URL/git/something.git";
216 //$newloc = preg_replace("/^$basegit/", "", $_SERVER["REQUEST_URI"]);
217 chdir("$repo_base/$repo");
218 exec("/usr/bin/git update-server-info");
220 if($_SERVER["REQUEST_METHOD"] == "POST") {
221 gwvp_AskForBasicAuth();
222 gwvp_callGitBackend($repo);
226 if(isset($_REQUEST["service"])) {
227 if($_REQUEST["service"] == "git-receive-pack") {
228 // we are a write call - we need auth and we're going to the backend proper
229 gwvp_AskForBasicAuth();
230 gwvp_callGitBackend($repo);
236 if(file_exists("$repo_base/$repo/$newloc")) {
237 error_log("would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc");
238 $fh = fopen("$repo_base/$repo/$newloc", "rb");
240 error_log("pushing file");
242 echo fread($fh, 8192);
245 echo "would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc, NE";
246 header('HTTP/1.0 404 No Such Thing');
251 function gwvp_canManageRepo($userid, $repoid)
253 // only the owner or an admin can do these tasks
254 error_log("Checking repoid, $repoid against userid $userid");
256 if(gwvp_IsUserAdmin(null, null, $userid)) return true;
257 if(gwvp_IsRepoOwner($userid, $repoid)) return true;
261 function gwvp_callGitBackend($username, $repo)
263 // this is where things become a nightmare
264 $fh = fopen('php://input', "r");
266 $ruri = $_SERVER["REQUEST_URI"];
267 $strrem = "git/$repo.git";
268 $euri = str_replace($strrem, "", $_REQUEST["q"]);
269 //$euri = preg_replace("/^git\/$repo\.git/", "", $_REQUEST["q"]);
273 $rmeth = $_SERVER["REQUEST_METHOD"];
276 foreach($_REQUEST as $key => $var) {
278 //error_log("adding, $var from $key");
279 if($qs == "") $qs.="$key=$var";
280 else $qs.="&$key=$var";
288 // this is where the fun, it ends.
295 if(isset($procenv)) unset($procenv);
296 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
297 $procenv["PATH_TRANSLATED"] = "/tmp/$repo.git/$euri";
298 $procenv["REQUEST_METHOD"] = "$rmeth";
299 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
300 $procenv["QUERY_STRING"] = "$qs";
301 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
302 $procenv["REMOTE_USER"] = "$username";
303 $procenv["REMOTE_ADDR"] = "1.2.3.4";
304 $procenv["AUTH_TYPE"] = "Basic";
306 if(isset($_SERVER["CONTENT_TYPE"])) {
307 $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
309 //$procenv["CONTENT_TYPE"] = "";
311 if(isset($_SERVER["CONTENT_LENGTH"])) {
312 $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
315 error_log("path trans'd is /tmp/$repo.git/$euri from $ruri with ".$_REQUEST["q"]." $strrem");
322 $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);
325 while(!$untilblank&&!feof($pipes[1])) {
326 $lines_t = fgets($pipes[1]);
327 $lines = trim($lines_t);
328 error_log("got line: $lines");
329 if($lines_t == "\r\n") {
331 error_log("now blank");
332 } else header($lines);
333 if($lines === false) {
334 error_log("got an unexpexted exit...");
344 if(!stream_set_blocking($fh,0)) {
345 error_log("cant set input non-blocking");
348 if(!stream_set_blocking($pipes[1],0)) {
349 error_log("cant set pipe1 non-blocking");
352 // i was going to use stream_select, but i feel this works better like this
356 $from_client_data = fread($fh,8192);
357 if($from_client_data !== false) fwrite($pipes[0], $from_client_data);
359 //fwrite($fl, $from_client_data);
360 $client_len = strlen($from_client_data);
362 error_log("client end");
367 // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
368 // but i do know that the fgets for the headers above should have comsued that
369 if(!feof($pipes[1])) {
370 $from_cgi_data_t = fread($pipes[1],8192);
371 $from_cgi_data = $from_cgi_data_t;
373 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
374 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
375 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
377 if(strlen($from_cgi_data_t)>0) {
378 // i dont get why this happens, and its very frustrating.. im not sure if its a bug in php
379 // or something the git-http-backend thing is doing..
380 // TODO: find out why this happens
381 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
382 if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) {
383 error_log("MOOOKS - we did trunc");
385 error_log("MOOOKS - we did not trunc");
391 if($from_cgi_data !== false) {
395 $cgi_len = strlen($from_cgi_data);
397 error_log("cgi end");
401 if(feof($pipes[1])) $continue = false;
403 if($client_len == 0 && $cgi_len == 0) {
405 error_log("sleep tick");
407 error_log("sizes: $client_len, $cgi_len");
409 error_log("from cgi: \"$from_cgi_data\"");
425 function gwvp_repoExists($name)
427 $repo_base = gwvp_getConfigVal("repodir");
429 if(file_exists("$repo_base/$name.git")) return true;
434 // 0 - anyone can clone/read, only owner can write
435 // 1 - noone can clone/read, repo is visible (i.e. name), only owner can read/write repo
436 // 2 - only owner can see anything
437 function gwvp_createGitRepo($name, $ownerid, $desc, $bundle=null, $defaultperms=0)
439 $repo_base = gwvp_getConfigVal("repodir");
441 // phew, this works, but i tell you this - bundles arent quite as nice as they should be
442 if($bundle == null) {
443 error_log("would create $repo_base/$name.git");
444 exec("/usr/bin/git init $repo_base/$name.git --bare > /tmp/gitlog 2>&1");
445 chdir("$repo_base/$name.git");
446 exec("/usr/bin/git update-server-info");
448 error_log("create via mirror on $repo_base/$name.git");
449 exec("/usr/bin/git clone --mirror $bundle $repo_base/$name.git > /tmp/gitlog 2>&1");
450 chdir("$repo_base/$name.git");
451 exec("/usr/bin/git update-server-info");
454 // gwvp_AddRepo($reponame, $repodesc, $repoowner, $defaultperms = 0)
455 gwvp_AddRepo($name, $desc, $ownerid, $defaultperms);
460 // this funciton returns one of three things, read, visible, write, none
466 function gwvp_resolvRepoPerms($userid, $repoid)
468 $ownerid = gwvp_getRepoOwner($repoid);
469 $isadmin = gwvp_IsUserAdmin(null, null, $userid);
471 if($isadmin) return 3;
473 if($userid == $ownerid) return 3;
475 // now we load the perms table and pray
476 $repoperms = gwvp_getRepoPermissions($repoid);
477 $usergroups = gwvp_getGroupsForUser(null, $userid);
480 if($repoperms != false) foreach($repoperms as $perm) {
481 // need to go thru each perm, then check it agains the user we're trying to figure
483 switch($perm["type"]) {
497 // we only var if permval is greater then current
498 if($permval > $maxperm) {
499 //error_log("going into check for $maxperm/$permval, ".$perm["ref"]);
500 if($perm["ref"] == "anon") {
502 } else if($perm["ref"] == "authed") {
506 $spl = explode(":", $perm["ref"]);
509 if($idtype == "group") {
510 // function gwvp_IsGroupMember($email, $groupname)
511 if(gwvp_IsGroupMemberById($userid, $idval)) $maxperm = $permval;
512 } else if ($idtype == "user") {
513 //error_log("checking $userid, $idval");
514 if($userid == $idval) $maxperm = $permval;
520 // thats TOTALLY going to work... -_0 we should really write a unit test for this, but thats a bit
521 // hard given the db req's so for now, we'll leave it as is