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