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 header_remove("Pragma");
\r
35 header_remove("Cache-Control");
\r
36 header_remove("Set-Cookie");
\r
37 header_remove("Expires");
\r
38 header_remove("X-Powered-By");
\r
39 header_remove("Vary");
\r
42 $repo_base = gwvpmini_getConfigVal("repodir");
44 // TODO: we need to stop passing the repo name around as "repo.git", it needs to be just "repo"
47 /* bizare git problem that ignores 403's or continues on with a push despite them
48 error_log("FLAP for ".$_SERVER["REQUEST_URI"]);
49 if(isset($_REQUEST)) {
50 $dump = print_r($_REQUEST, true);
51 error_log("FLAP, $dump");
53 if(isset($_SERVER["PHP_AUTH_USER"])) {
54 error_log("FLAP: donut hole");
62 if(isset($_REQUEST["q"])) {
63 $query = $_REQUEST["q"];
64 $qspl = explode("/", $query);
66 $repo = preg_replace("/\.git$/", "", $qspl[1]);
67 $repoid = gwvpmini_GetRepoId($repo);
68 for($i=2; $i < count($qspl); $i++) {
69 $newloc .= "/".$qspl[$i];
73 if($repoid == false) {
74 gwvpmini_fourZeroFour();
78 // we do an update server cause its weird and i cant figure out when it actually needs to happen
79 chdir("$repo_base/$repo.git");
80 exec("/usr/bin/git update-server-info");
83 // so now we have the repo
84 // next we determine if this is a read or a write
86 if(isset($_REQUEST["service"])) {
87 if($_REQUEST["service"] == "git-receive-pack") {
88 error_log("got write as receivepack in post");
94 // 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
95 // no, this isnt a solution cause auth'd read attempts will come up as writes...
96 //if(isset($_SERVER["PHP_AUTH_USER"])) {
101 $person = gwvpmini_checkBasicAuthLogin();
103 // next, figure out permissions for repo
104 $rid = gwvpmini_GetRepoId($repo);
106 error_log("AT THIS POINT WE HAVE $uid, $rid, $repo $person");
110 error_log("ASK FOR BASIC AUTH");
111 gwvpmini_AskForBasicAuth();
114 $perm = gwvpmini_GetRepoPerm($rid, "a");
116 error_log("ASK FOR BASIC AUTH 2");
117 gwvpmini_AskForBasicAuth();
122 $uid = gwvpmini_GetUserId($person);
123 $perm = gwvpmini_GetRepoPerm($rid, $uid);
126 error_log("SEND FOFF");
127 gwvpmini_fourZeroThree();
132 gwvpmini_fourZeroThree();
138 // if its a write, we push for authentication
142 $person = "anonymous";
144 gwvpmini_callGitBackend($person, $repo);
148 // if we made it this far, we a read and we have permissions to do so, just search the file from the repo
149 /*if(file_exists("$repo_base/$repo.git/$newloc")) {
150 error_log("would ask $repo for $repo.git/$newloc from $repo_base/$repo.git/$newloc");
151 $fh = fopen("$repo_base/$repo.git/$newloc", "rb");
153 error_log("pushing file");
155 echo fread($fh, 8192);
158 error_log("would ask $repo for $repo/$newloc from $repo_base/$repo/$newloc, NE");
159 gwvpmini_fourZeroFour();
165 function gwvpmini_canManageRepo($userid, $repoid)
167 // only the owner or an admin can do these tasks
168 error_log("Checking repoid, $repoid against userid $userid");
170 if(gwvpmini_IsUserAdmin(null, null, $userid)) return true;
171 if(gwvpmini_IsRepoOwner($userid, $repoid)) return true;
175 function gwvpmini_callGitBackend($username, $repo)
177 // this is where things become a nightmare
178 $fh = fopen('php://input', "r");
180 $repo_base = gwvpmini_getConfigVal("repodir");
\r
183 $ruri = $_SERVER["REQUEST_URI"];
184 $strrem = "git/$repo.git";
185 $euri = str_replace($strrem, "", $_REQUEST["q"]);
186 //$euri = preg_replace("/^git\/$repo\.git/", "", $_REQUEST["q"]);
190 $rmeth = $_SERVER["REQUEST_METHOD"];
193 foreach($_REQUEST as $key => $var) {
195 //error_log("adding, $var from $key");
196 if($qs == "") $qs.="$key=$var";
197 else $qs.="&$key=$var";
205 // this is where the fun, it ends.
212 if(isset($procenv)) unset($procenv);
213 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
214 $procenv["PATH_TRANSLATED"] = "/$repo_base/$repo.git/$euri";
215 $procenv["REQUEST_METHOD"] = "$rmeth";
216 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
217 $procenv["QUERY_STRING"] = "$qs";
218 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
219 $procenv["REMOTE_USER"] = "$username";
220 $procenv["REMOTE_ADDR"] = $_SERVER["REMOTE_ADDR"];
221 $procenv["AUTH_TYPE"] = "Basic";
223 if(isset($_SERVER["CONTENT_TYPE"])) {
224 $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
226 //$procenv["CONTENT_TYPE"] = "";
228 if(isset($_SERVER["CONTENT_LENGTH"])) {
229 $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
232 error_log("path trans'd is /$repo_base/$repo.git/$euri from $ruri with ".$_REQUEST["q"]." $strrem");
237 $pwd = "/$repo_base/";
239 $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);
242 while(!$untilblank&&!feof($pipes[1])) {
243 $lines_t = fgets($pipes[1]);
244 $lines = trim($lines_t);
245 error_log("got line: $lines");
246 if($lines_t == "\r\n") {
248 error_log("now blank");
249 } else header($lines);
250 if($lines === false) {
251 error_log("got an unexpexted exit...");
261 if(!stream_set_blocking($fh,0)) {
262 error_log("cant set input non-blocking");
265 if(!stream_set_blocking($pipes[1],0)) {
266 error_log("cant set pipe1 non-blocking");
270 $fp = fopen("/tmp/gitup.".rand(0,4000000), "w");
271 // i was going to use stream_select, but i feel this works better like this
275 $from_client_data = fread($fh,8192);
276 if($from_client_data !== false) {
277 fwrite($pipes[0], $from_client_data);
278 fwrite($fp, $from_client_data);
281 //fwrite($fl, $from_client_data);
282 $client_len = strlen($from_client_data);
284 error_log("client end");
289 // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
290 // but i do know that the fgets for the headers above should have comsued that
291 if(!feof($pipes[1])) {
292 $from_cgi_data_t = fread($pipes[1],8192);
293 $from_cgi_data = $from_cgi_data_t;
295 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
296 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
297 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
299 if(strlen($from_cgi_data_t)>0) {
300 // i dont get why this happens, and its very frustrating.. im not sure if its a bug in php
301 // or something the git-http-backend thing is doing..
302 // TODO: find out why this happens
303 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
304 if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) {
305 error_log("MOOOKS - we did trunc");
307 error_log("MOOOKS - we did not trunc");
313 if($from_cgi_data !== false) {
317 $cgi_len = strlen($from_cgi_data);
319 error_log("cgi end");
323 if(feof($pipes[1])) $continue = false;
325 if($client_len == 0 && $cgi_len == 0) {
327 error_log("sleep tick");
329 error_log("sizes: $client_len, $cgi_len");
331 error_log("from cgi: \"$from_cgi_data\"");
347 function gwvpmini_repoExists($name)
349 $repo_base = gwvpmini_getConfigVal("repodir");
351 if(file_exists("$repo_base/$name.git")) return true;
356 // 0 - anyone can clone/read, only owner can write
357 // 1 - noone can clone/read, repo is visible (i.e. name), only owner can read/write repo
358 // 2 - only owner can see anything
359 function gwvpmini_createGitRepo($name, $ownerid, $desc)
361 $repo_base = gwvpmini_getConfigVal("repodir");
363 // phew, this works, but i tell you this - bundles arent quite as nice as they should be
364 error_log("would create $repo_base/$name.git");
365 exec("/usr/bin/git init $repo_base/$name.git --bare > /tmp/gitlog 2>&1");
366 chdir("$repo_base/$name.git");
367 exec("/usr/bin/git update-server-info");
369 // gwvpmini_AddRepo($reponame, $repodesc, $repoowner, $defaultperms = 0)
370 gwvpmini_AddRepo($name, $desc, $ownerid);