bea736db92d4ea6433599fad24945a269e76d4c3
[gwvp-mini.git] / gwvpmini / gwvpmini_gitbackend.php
1 <?php
2
3 $CALL_ME_FUNCTIONS["gitcontrol"] = "gwvpmini_gitControlCallMe";
4
5 //$MENU_ITEMS["20repos"]["text"] = "Repo Admin";
6 //$MENU_ITEMS["20repos"]["link"] = "$BASE_URL/admin/repos";
7
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()
12 {
13         if(isset($_REQUEST["q"])) {
14                 $query = $_REQUEST["q"];
15                 $qspl = explode("/", $query);
16                 if(isset($qspl[0])) {
17                         if($qspl[0] == "git") {
18                                 return "gwvpmini_gitBackendInterface";
19                         }
20                 } 
21                 else return false;
22         }
23         
24         return false;
25         
26 }
27
28
29 function gwvpmini_gitBackendInterface()
30 {
31         // and this is where i re-code the git backend interface from scratch
32         global $BASE_URL;
33         
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
40         
41         
42         $repo_base = gwvpmini_getConfigVal("repodir");
43         
44         // TODO: we need to stop passing the repo name around as "repo.git", it needs to be just "repo"
45         
46         
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");
52         }
53         if(isset($_SERVER["PHP_AUTH_USER"])) {
54                 error_log("FLAP: donut hole");
55         }*/
56         
57
58         
59         $repo = "";
60         $repoid = false;
61         $newloc = "/";
62         if(isset($_REQUEST["q"])) {
63                 $query = $_REQUEST["q"];
64                 $qspl = explode("/", $query);
65                 // TODO do this with 
66                 $repo = preg_replace("/\.git$/", "", $qspl[1]);
67                 $repoid = gwvpmini_GetRepoId($repo);
68                 for($i=2; $i < count($qspl); $i++) {
69                         $newloc .= "/".$qspl[$i];
70                 }
71         }
72         
73         if($repoid == false) {
74                 gwvpmini_fourZeroFour();
75                 return;
76         }
77         
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");
81         
82         
83         // so now we have the repo
84         // next we determine if this is a read or a write
85         $write = false;
86         if(isset($_REQUEST["service"])) {
87                 if($_REQUEST["service"] == "git-receive-pack") {
88                         error_log("got write as receivepack in post");
89                         $write = true;
90                 }
91         }
92         
93         //$write = true;
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"])) {
97                 //$write = true;
98         //}
99         
100         
101         $person = gwvpmini_checkBasicAuthLogin();
102         //$write = true;
103         // next, figure out permissions for repo
104         $rid = gwvpmini_GetRepoId($repo);
105         $uid = -1;
106         error_log("AT THIS POINT WE HAVE $uid, $rid, $repo $person");
107         
108         if(!$person) {
109                 if($write) {
110                         error_log("ASK FOR BASIC AUTH");
111                         gwvpmini_AskForBasicAuth();
112                         return;
113                 } else {
114                         $perm = gwvpmini_GetRepoPerm($rid, "a");
115                         if($perm < 1) {
116                                 error_log("ASK FOR BASIC AUTH 2");
117                                 gwvpmini_AskForBasicAuth();
118                                 return;
119                         }
120                 }
121         } else {
122                 $uid = gwvpmini_GetUserId($person);
123                 $perm = gwvpmini_GetRepoPerm($rid, $uid);
124                 if($write) {
125                         if($perm < 2) {
126                                 error_log("SEND FOFF");
127                                 gwvpmini_fourZeroThree();
128                                 return;
129                         }
130                 } else {
131                         if($perm < 1) {
132                                 gwvpmini_fourZeroThree();
133                                 return;
134                         }
135                 }
136         }
137         
138         // if its a write, we push for authentication
139         
140         //if($write) {
141                 gwvpmini_callGitBackend($person, $repo);
142                 return;
143         //}
144
145         // if we made it this far, we a read and we have permissions to do so, just search the file from the repo
146         /*if(file_exists("$repo_base/$repo.git/$newloc")) {
147                 error_log("would ask $repo for $repo.git/$newloc from $repo_base/$repo.git/$newloc");
148                 $fh = fopen("$repo_base/$repo.git/$newloc", "rb");
149                 
150                 error_log("pushing file");
151                 while(!feof($fh)) {
152                         echo fread($fh, 8192);
153                 }
154         } else {
155                 error_log("would ask $repo for $repo/$newloc from $repo_base/$repo/$newloc, NE");
156                 gwvpmini_fourZeroFour();
157                 return;
158         }*/
159         
160 }
161
162 function gwvpmini_canManageRepo($userid, $repoid)
163 {
164         // only the owner or an admin can do these tasks
165         error_log("Checking repoid, $repoid against userid $userid");
166         
167         if(gwvpmini_IsUserAdmin(null, null, $userid)) return true;
168         if(gwvpmini_IsRepoOwner($userid, $repoid)) return true;
169         return false;
170 }
171
172 function gwvpmini_callGitBackend($username, $repo)
173 {
174         // this is where things become a nightmare
175                 $fh   = fopen('php://input', "r");
176                 
177                 $repo_base = gwvpmini_getConfigVal("repodir");\r
178                 
179                 
180                 $ruri = $_SERVER["REQUEST_URI"];
181                 $strrem = "git/$repo.git";
182                 $euri = str_replace($strrem, "", $_REQUEST["q"]);
183                 //$euri = preg_replace("/^git\/$repo\.git/", "", $_REQUEST["q"]);
184                 
185                 
186                 
187                 $rmeth = $_SERVER["REQUEST_METHOD"];
188                 
189                 $qs = "";
190                 foreach($_REQUEST as $key => $var) {
191                         if($key != "q") {
192                                 //error_log("adding, $var from $key");
193                                 if($qs == "") $qs.="$key=$var";
194                                 else $qs.="&$key=$var";
195                         }
196                 }
197                 
198                 //sleep(2);
199                 
200                 
201                 
202                 // this is where the fun, it ends.
203                 $myoutput = "";
204                 unset($myoutput);
205                 
206                 // this be nasty!
207                 
208                 // setup env
209                 if(isset($procenv))     unset($procenv);
210                 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
211                 $procenv["PATH_TRANSLATED"] = "/$repo_base/$repo.git/$euri";
212                 $procenv["REQUEST_METHOD"] = "$rmeth";
213                 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
214                 $procenv["QUERY_STRING"] = "$qs";
215                 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
216                 $procenv["REMOTE_USER"] = "$username";
217                 $procenv["REMOTE_ADDR"] = $_SERVER["REMOTE_ADDR"];
218                 $procenv["AUTH_TYPE"] = "Basic";
219                 
220                 if(isset($_SERVER["CONTENT_TYPE"])) { 
221                         $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
222                 } else {
223                         //$procenv["CONTENT_TYPE"] = "";
224                 }
225                 if(isset($_SERVER["CONTENT_LENGTH"])) { 
226                         $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
227                 }
228                 
229                 error_log("path trans'd is /$repo_base/$repo.git/$euri from $ruri with ".$_REQUEST["q"]." $strrem");
230                 
231                 
232                 
233
234                 $pwd = "/$repo_base/";
235                 
236                 $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);
237                 
238                 $untilblank = false;
239                 while(!$untilblank&&!feof($pipes[1])) {
240                         $lines_t = fgets($pipes[1]);
241                         $lines = trim($lines_t);
242                         error_log("got line: $lines");
243                         if($lines_t == "\r\n") {
244                                 $untilblank = true;
245                                 error_log("now blank");
246                         } else header($lines);
247                         if($lines === false) {
248                                 error_log("got an unexpexted exit...");
249                                 exit(0);
250                         }
251                         
252                 }
253                 
254
255                 $firstline = true;
256                 $continue = true;
257                 
258                 if(!stream_set_blocking($fh,0)) {
259                         error_log("cant set input non-blocking");
260                 }
261
262                 if(!stream_set_blocking($pipes[1],0)) {
263                         error_log("cant set pipe1 non-blocking");
264                 }
265                 
266                 // i was going to use stream_select, but i feel this works better like this
267                 while($continue) {
268                         // do client
269                         if(!feof($fh)) {
270                                 $from_client_data = fread($fh,8192);
271                                 if($from_client_data !== false) fwrite($pipes[0], $from_client_data);
272                                 fflush($pipes[0]);
273                                 //fwrite($fl, $from_client_data);
274                                 $client_len = strlen($from_client_data);
275                         } else {
276                                 error_log("client end");
277                                 $client_len = 0;
278                         }
279                         
280                         // do cgi
281                         // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
282                         // but i do know that the fgets for the headers above should have comsued that
283                         if(!feof($pipes[1])) {
284                                 $from_cgi_data_t = fread($pipes[1],8192);
285                                 $from_cgi_data = $from_cgi_data_t;
286                                 
287                                 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
288                                 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
289                                 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
290                                 if($firstline) {
291                                         if(strlen($from_cgi_data_t)>0) {
292                                                 // i dont get why this happens, and its very frustrating.. im not sure if its a bug in php
293                                                 // or something the git-http-backend thing is doing..
294                                                 // TODO: find out why this happens
295                                                 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
296                                                 if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) {
297                                                         error_log("MOOOKS - we did trunc");
298                                                 } else {
299                                                         error_log("MOOOKS - we did not trunc");
300                                                 }
301                                                 $firstline = false;
302                                         }
303                                 }
304                                 
305                                 if($from_cgi_data !== false) {
306                                         echo $from_cgi_data;
307                                         flush();
308                                 }
309                                 $cgi_len = strlen($from_cgi_data);
310                         } else {
311                                 error_log("cgi end");
312                                 $cgi_len = 0;
313                         }
314                         
315                         if(feof($pipes[1])) $continue = false;
316                         else {
317                                 if($client_len == 0 && $cgi_len == 0) {
318                                         usleep(200000);
319                                         error_log("sleep tick");
320                                 } else {
321                                         error_log("sizes: $client_len, $cgi_len");
322                                         if($cgi_len > 0) {
323                                                 error_log("from cgi: \"$from_cgi_data\"");
324                                         }
325                                 }
326                         }
327                         
328                 }
329                 
330                 
331                 //fclose($fl);
332                 fclose($fh);
333                 fclose($pipes[1]);
334                 fclose($pipes[0]);      
335 }
336
337
338
339 function gwvpmini_repoExists($name)
340 {
341         $repo_base = gwvpmini_getConfigVal("repodir");
342         
343         if(file_exists("$repo_base/$name.git")) return true;
344         else return false;
345 }
346
347 // default perms:
348 // 0 - anyone can clone/read, only owner can write
349 // 1 - noone can clone/read, repo is visible (i.e. name), only owner can read/write repo
350 // 2 - only owner can see anything
351 function gwvpmini_createGitRepo($name, $ownerid, $desc)
352 {
353         $repo_base = gwvpmini_getConfigVal("repodir");
354         
355         // phew, this works, but i tell you this - bundles arent quite as nice as they should be
356         error_log("would create $repo_base/$name.git");
357         exec("/usr/bin/git init $repo_base/$name.git --bare > /tmp/gitlog 2>&1");
358         chdir("$repo_base/$name.git");
359         exec("/usr/bin/git update-server-info");
360
361         // gwvpmini_AddRepo($reponame, $repodesc, $repoowner, $defaultperms = 0)
362         gwvpmini_AddRepo($name, $desc, $ownerid);
363         
364         return true;
365 }
366
367
368 ?>