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_gitBackendInterface()
31 // and this is where i re-code the git backend interface from scratch
34 $repo_base = gwvpmini_getConfigVal("repodir");
36 // TODO: we need to stop passing the repo name around as "repo.git", it needs to be just "repo"
39 /* bizare git problem that ignores 403's or continues on with a push despite them
40 error_log("FLAP for ".$_SERVER["REQUEST_URI"]);
41 if(isset($_REQUEST)) {
42 $dump = print_r($_REQUEST, true);
43 error_log("FLAP, $dump");
45 if(isset($_SERVER["PHP_AUTH_USER"])) {
46 error_log("FLAP: donut hole");
54 if(isset($_REQUEST["q"])) {
55 $query = $_REQUEST["q"];
56 $qspl = explode("/", $query);
58 $repo = preg_replace("/\.git$/", "", $qspl[1]);
59 $repoid = gwvpmini_GetRepoId($repo);
60 for($i=2; $i < count($qspl); $i++) {
61 $newloc .= "/".$qspl[$i];
65 if($repoid == false) {
66 gwvpmini_fourZeroFour();
70 // we do an update server cause its weird and i cant figure out when it actually needs to happen
71 chdir("$repo_base/$repo.git");
72 exec("/usr/bin/git update-server-info");
75 // so now we have the repo
76 // next we determine if this is a read or a write
78 if(isset($_REQUEST["service"])) {
79 if($_REQUEST["service"] == "git-receive-pack") {
80 error_log("got write as receivepack in post");
84 if($_SERVER["REQUEST_METHOD"] == "POST") {
89 // 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
90 // no, this isnt a solution cause auth'd read attempts will come up as writes...
91 //if(isset($_SERVER["PHP_AUTH_USER"])) {
99 // if its a write, we push for authentication
101 error_log("is write attempt, ask for login");
102 $person = gwvpmini_checkBasicAuthLogin();
103 if($person == false) {
104 error_log("person is false, push auth");
105 gwvpmini_AskForBasicAuth();
109 error_log("checking perms for $person against $repoid for repo $repo");
110 // here we pass to the git backend
111 error_log("perms are $perms and im allowed");
112 gwvpmini_callGitBackend($person["username"], $repo);
118 // if they're less then read, we need to then check the user auth permissions
121 $person = gwvpmini_checkBasicAuthLogin();
122 if($person == false) {
123 gwvpmini_AskForBasicAuth();
129 // if we made it this far, we a read and we have permissions to do so, just search the file from the repo
130 if(file_exists("$repo_base/$repo.git/$newloc")) {
131 error_log("would ask $repo for $repo.git/$newloc from $repo_base/$repo.git/$newloc");
132 $fh = fopen("$repo_base/$repo.git/$newloc", "rb");
134 error_log("pushing file");
136 echo fread($fh, 8192);
139 //echo "would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc, NE";
140 gwvpmini_fourZeroFour();
146 function gwvpmini_canManageRepo($userid, $repoid)
148 // only the owner or an admin can do these tasks
149 error_log("Checking repoid, $repoid against userid $userid");
151 if(gwvpmini_IsUserAdmin(null, null, $userid)) return true;
152 if(gwvpmini_IsRepoOwner($userid, $repoid)) return true;
156 function gwvpmini_callGitBackend($username, $repo)
158 // this is where things become a nightmare
159 $fh = fopen('php://input', "r");
161 $repo_base = gwvpmini_getConfigVal("repodir");
\r
164 $ruri = $_SERVER["REQUEST_URI"];
165 $strrem = "git/$repo.git";
166 $euri = str_replace($strrem, "", $_REQUEST["q"]);
167 //$euri = preg_replace("/^git\/$repo\.git/", "", $_REQUEST["q"]);
171 $rmeth = $_SERVER["REQUEST_METHOD"];
174 foreach($_REQUEST as $key => $var) {
176 //error_log("adding, $var from $key");
177 if($qs == "") $qs.="$key=$var";
178 else $qs.="&$key=$var";
186 // this is where the fun, it ends.
193 if(isset($procenv)) unset($procenv);
194 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
195 $procenv["PATH_TRANSLATED"] = "/$repo_base/$repo.git/$euri";
196 $procenv["REQUEST_METHOD"] = "$rmeth";
197 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
198 $procenv["QUERY_STRING"] = "$qs";
199 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
200 $procenv["REMOTE_USER"] = "$username";
201 $procenv["REMOTE_ADDR"] = $_SERVER["REMOTE_ADDR"];
202 $procenv["AUTH_TYPE"] = "Basic";
204 if(isset($_SERVER["CONTENT_TYPE"])) {
205 $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
207 //$procenv["CONTENT_TYPE"] = "";
209 if(isset($_SERVER["CONTENT_LENGTH"])) {
210 $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
213 error_log("path trans'd is /$repo_base/$repo.git/$euri from $ruri with ".$_REQUEST["q"]." $strrem");
218 $pwd = "/$repo_base/";
220 $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);
223 while(!$untilblank&&!feof($pipes[1])) {
224 $lines_t = fgets($pipes[1]);
225 $lines = trim($lines_t);
226 error_log("got line: $lines");
227 if($lines_t == "\r\n") {
229 error_log("now blank");
230 } else header($lines);
231 if($lines === false) {
232 error_log("got an unexpexted exit...");
242 if(!stream_set_blocking($fh,0)) {
243 error_log("cant set input non-blocking");
246 if(!stream_set_blocking($pipes[1],0)) {
247 error_log("cant set pipe1 non-blocking");
250 // i was going to use stream_select, but i feel this works better like this
254 $from_client_data = fread($fh,8192);
255 if($from_client_data !== false) fwrite($pipes[0], $from_client_data);
257 //fwrite($fl, $from_client_data);
258 $client_len = strlen($from_client_data);
260 error_log("client end");
265 // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
266 // but i do know that the fgets for the headers above should have comsued that
267 if(!feof($pipes[1])) {
268 $from_cgi_data_t = fread($pipes[1],8192);
269 $from_cgi_data = $from_cgi_data_t;
271 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
272 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
273 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
275 if(strlen($from_cgi_data_t)>0) {
276 // i dont get why this happens, and its very frustrating.. im not sure if its a bug in php
277 // or something the git-http-backend thing is doing..
278 // TODO: find out why this happens
279 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
280 if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) {
281 error_log("MOOOKS - we did trunc");
283 error_log("MOOOKS - we did not trunc");
289 if($from_cgi_data !== false) {
293 $cgi_len = strlen($from_cgi_data);
295 error_log("cgi end");
299 if(feof($pipes[1])) $continue = false;
301 if($client_len == 0 && $cgi_len == 0) {
303 error_log("sleep tick");
305 error_log("sizes: $client_len, $cgi_len");
307 error_log("from cgi: \"$from_cgi_data\"");
323 function gwvpmini_repoExists($name)
325 $repo_base = gwvpmini_getConfigVal("repodir");
327 if(file_exists("$repo_base/$name.git")) return true;
332 // 0 - anyone can clone/read, only owner can write
333 // 1 - noone can clone/read, repo is visible (i.e. name), only owner can read/write repo
334 // 2 - only owner can see anything
335 function gwvpmini_createGitRepo($name, $ownerid, $desc)
337 $repo_base = gwvpmini_getConfigVal("repodir");
339 // phew, this works, but i tell you this - bundles arent quite as nice as they should be
340 error_log("would create $repo_base/$name.git");
341 exec("/usr/bin/git init $repo_base/$name.git --bare > /tmp/gitlog 2>&1");
342 chdir("$repo_base/$name.git");
343 exec("/usr/bin/git update-server-info");
345 // gwvpmini_AddRepo($reponame, $repodesc, $repoowner, $defaultperms = 0)
346 gwvpmini_AddRepo($name, $desc, $ownerid);