X-Git-Url: http://git.pjr.cc/?p=gwvp.git;a=blobdiff_plain;f=gwvplib%2Fgwvpgitcontrol.php;h=d1b0699e68ee819290025f8e7d442bf6e75c52b3;hp=a949fabdcab66683148c3e95df679eb47b6eec20;hb=98ea64d926a23982b9cfb5b468be87f882268566;hpb=c7bb18886251e5187afbdb8d2101e200eb895ab6 diff --git a/gwvplib/gwvpgitcontrol.php b/gwvplib/gwvpgitcontrol.php index a949fab..d1b0699 100644 --- a/gwvplib/gwvpgitcontrol.php +++ b/gwvplib/gwvpgitcontrol.php @@ -1,6 +1,6 @@ $var) { + if($key != "q") { + //error_log("adding, $var from $key"); + if($qs == "") $qs.="$key=$var"; + else $qs.="&$key=$var"; + } + } + + //sleep(2); + + + + // this is where the fun, it ends. + $myoutput = ""; + unset($myoutput); + + // this be nasty! + + // setup env + if(isset($procenv)) unset($procenv); + $procenv["GATEWAY_INTERFACE"] = "CGI/1.1"; + $procenv["PATH_TRANSLATED"] = "/tmp/$repo/$euri"; + $procenv["REQUEST_METHOD"] = "$rmeth"; + $procenv["GIT_HTTP_EXPORT_ALL"] = "1"; + $procenv["QUERY_STRING"] = "$qs"; + $procenv["HTTP_USER_AGENT"] = "git/1.7.1"; + $procenv["REMOTE_USER"] = "user"; + $procenv["REMOTE_ADDR"] = "1.2.3.4"; + $procenv["AUTH_TYPE"] = "Basic"; + + if(isset($_SERVER["CONTENT_TYPE"])) { + $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"]; + } else { + //$procenv["CONTENT_TYPE"] = ""; + } + if(isset($_SERVER["CONTENT_LENGTH"])) { + $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"]; + } + + error_log("path trans'd is /tmp/$repo/$euri from $ruri with ".$_REQUEST["q"]." $strrem"); + + + + + $pwd = "/tmp/"; + + $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); + + $untilblank = false; + while(!$untilblank&&!feof($pipes[1])) { + $lines_t = fgets($pipes[1]); + $lines = trim($lines_t); + error_log("got line: $lines"); + if($lines_t == "\r\n") { + $untilblank = true; + error_log("now blank"); + } else header($lines); + if($lines === false) { + error_log("got an unexpexted exit..."); + exit(0); + } + + } + + + $firstline = true; + $continue = true; + + if(!stream_set_blocking($fh,0)) { + error_log("cant set input non-blocking"); + } + + if(!stream_set_blocking($pipes[1],0)) { + error_log("cant set pipe1 non-blocking"); + } + + // i was going to use stream_select, but i feel this works better like this + while($continue) { + // do client + if(!feof($fh)) { + $from_client_data = fread($fh,8192); + if($from_client_data !== false) fwrite($pipes[0], $from_client_data); + fflush($pipes[0]); + //fwrite($fl, $from_client_data); + $client_len = strlen($from_client_data); + } else { + error_log("client end"); + $client_len = 0; + } + + // do cgi + // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly, + // but i do know that the fgets for the headers above should have comsued that + if(!feof($pipes[1])) { + $from_cgi_data_t = fread($pipes[1],8192); + $from_cgi_data = $from_cgi_data_t; + + // i dont know if this will solve it... it coudl cause some serious issues elsewhere + // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it + // i.e. why the pointer doesnt increment over it, cause the freads above then get them again. + if($firstline) { + if(strlen($from_cgi_data_t)>0) { + // i dont get why this happens, and its very frustrating.. im not sure if its a bug in php + // or something the git-http-backend thing is doing.. + // TODO: find out why this happens + $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t); + if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) { + error_log("MOOOKS - we did trunc"); + } else { + error_log("MOOOKS - we did not trunc"); + } + $firstline = false; + } + } + + if($from_cgi_data !== false) { + echo $from_cgi_data; + flush(); + } + $cgi_len = strlen($from_cgi_data); + } else { + error_log("cgi end"); + $cgi_len = 0; + } + + if(feof($pipes[1])) $continue = false; + else { + if($client_len == 0 && $cgi_len == 0) { + usleep(200000); + error_log("sleep tick"); + } else { + error_log("sizes: $client_len, $cgi_len"); + if($cgi_len > 0) { + error_log("from cgi: \"$from_cgi_data\""); + } + } + } + + } + + + //fclose($fl); + fclose($fh); + fclose($pipes[1]); + fclose($pipes[0]); +} + + function gwvp_repoExists($name) { - global $repo_base; + $repo_base = gwvp_getConfigVal("repodir"); if(file_exists("$repo_base/$name.git")) return true; else return false; } -function gwvp_createGitRepo($name) +// default perms: +// 0 - anyone can clone/read, only owner can write +// 1 - noone can clone/read, repo is visible (i.e. name), only owner can read/write repo +// 2 - only owner can see anything +function gwvp_createGitRepo($name, $ownerid, $desc, $bundle=null, $defaultperms=0) { - global $repo_base; + $repo_base = gwvp_getConfigVal("repodir"); - error_log("would create $repo_base/$name.git"); + // phew, this works, but i tell you this - bundles arent quite as nice as they should be + if($bundle == null) { + error_log("would create $repo_base/$name.git"); + exec("/usr/bin/git init $repo_base/$name.git --bare > /tmp/gitlog 2>&1"); + chdir("$repo_base/$name.git"); + exec("/usr/bin/git update-server-info"); + } else { + error_log("create via mirror on $repo_base/$name.git"); + exec("/usr/bin/git clone --mirror $bundle $repo_base/$name.git > /tmp/gitlog 2>&1"); + chdir("$repo_base/$name.git"); + exec("/usr/bin/git update-server-info"); + } + + // gwvp_AddRepo($reponame, $repodesc, $repoowner, $defaultperms = 0) + gwvp_AddRepo($name, $desc, $ownerid, $defaultperms); return true; } + +// this funciton returns one of three things, read, visible, write, none +// as +// 0 - none +// 1 - visible +// 2 - read +// 3 - write +function gwvp_resolvRepoPerms($userid, $repoid) +{ + $ownerid = gwvp_getRepoOwner($repoid); + $isadmin = gwvp_IsUserAdmin(null, null, $userid); + + if($isadmin) return 3; + + if($userid == $ownerid) return 3; + + // now we load the perms table and pray + $repoperms = gwvp_getRepoPermissions($repoid); + $usergroups = gwvp_getGroupsForUser(null, $userid); + + $maxperm = 0; + if($repoperms != false) foreach($repoperms as $perm) { + // need to go thru each perm, then check it agains the user we're trying to figure + // the perms on + switch($perm["type"]) { + case "read": + $permval = 2; + break; + case "visible": + $permval = 1; + break; + case "write": + $permval = 3; + break; + default: + $permval = 0; + } + + // we only var if permval is greater then current + if($permval > $maxperm) { + //error_log("going into check for $maxperm/$permval, ".$perm["ref"]); + if($perm["ref"] == "anon") { + $maxperm = $permval; + } else if($perm["ref"] == "authed") { + $maxperm = $permval; + } else { + // now we do splits + $spl = explode(":", $perm["ref"]); + $idtype = $spl[0]; + $idval = $spl[1]; + if($idtype == "group") { + // function gwvp_IsGroupMember($email, $groupname) + if(gwvp_IsGroupMemberById($userid, $idval)) $maxperm = $permval; + } else if ($idtype == "user") { + //error_log("checking $userid, $idval"); + if($userid == $idval) $maxperm = $permval; + } + } + } + } + + // thats TOTALLY going to work... -_0 we should really write a unit test for this, but thats a bit + // hard given the db req's so for now, we'll leave it as is + return $maxperm; +} + ?> \ No newline at end of file