dddf0b2429aa5c674af8d964f538c565798bc895
[gwvp.git] / gwvplib / gwvpgitcontrol.php
1 <?php
2
3 $CALL_ME_FUNCTIONS["gitcontrol"] = "gwvp_gitControlCallMe";
4
5 //$MENU_ITEMS["20repos"]["text"] = "Repo Admin";
6 //$MENU_ITEMS["20repos"]["link"] = "$BASE_URL/admin/repos";
7
8 function gwvp_gitControlCallMe()
9 {
10         if(isset($_REQUEST["q"])) {
11                 $query = $_REQUEST["q"];
12                 $qspl = explode("/", $query);
13                 if(isset($qspl[0])) {
14                         if($qspl[0] == "git") {
15                                 return "gwvp_gitBackendInterface";
16                         }
17                 } 
18                 else return false;
19         }
20         
21         return false;
22         
23 }
24
25 function gwvp_repoPermissionCheck($repo, $user)
26 {
27         return true;
28 }
29
30 function gwvp_gitBackendInterface()
31 {
32         global $repo_base, $BASE_URL;
33         
34         
35         $repo = "";
36         $newloc = "/";
37         if(isset($_REQUEST["q"])) {
38                 $query = $_REQUEST["q"];
39                 $qspl = explode("/", $query);
40                 $repo = $qspl[1];
41                 for($i=2; $i < count($qspl); $i++) {
42                         $newloc .= "/".$qspl[$i];
43                 }
44         }
45         
46         $actual_repo_name = preg_replace("/\.git$/", "", $repo); 
47         
48         $user = gwvp_checkBasicAuthLogin();
49
50         if(!$user) {
51                 error_log("User is set to false, so its anonymouse");
52         } else {
53                 error_log("user is $user");
54         }
55         
56         // must remember that $user of false is anonymous when we code gwvp_repoPerm'sCheck()
57         if(!gwvp_repoPermissionCheck($actual_repo_name, $user)) {
58                 error_log("perms check fails - start auth");
59                 if(isset($_SERVER["PHP_AUTH_USER"])) {
60                         error_log("have auth - push 403");
61                         gwvp_fourZeroThree();
62                 } else {
63                         error_log("push auth");
64                         gwvp_AskForBasicAuth();
65                         return;
66                 }
67         }
68         
69         // we need to quite a bit of parsing in here. The "repo" will always be /git/repo.git
70         // but if we get here from a browser, we need to forward back to a normal repo viewer
71         // the only way i can think of doing this is to check the useragent for the word "git"
72         
73         /*
74          * here we need to
75          * 1) figure out the repo its acessing
76          * 2) figure out the perms on the repo
77          * 3) determine if its a pull or a push
78          * - if its a pull, we just serve straight from the fs
79          * - if its a push, we go thru git-http-backend
80          * 4) if it requiers auth, we push to auth
81          * 
82          */
83         $agent = "git-unknown";
84         $isgitagent = false;
85         
86         // tested the user agent bit with jgit from eclipse and normal git... seems to work
87         if(isset($_SERVER["HTTP_USER_AGENT"])) {
88                 $agent = $_SERVER["HTTP_USER_AGENT"];
89                 error_log("in git backend with user agent $agent");
90                 if(stristr($agent, "git")!==false) {
91                         $isgitagent = true;
92                 }
93         }
94         
95         
96                 
97         /* dont need this code right now
98         if($isgitagent) echo "GIT: i am a git backened interface for a repo $repo, agent $agent";
99         else echo "NOT GIT: i am a git backened interface for a repo $repo, agent $agent";
100         */
101         
102         // now we need to rebuild the actual request or do we?
103         //$basegit = "$BASE_URL/git/something.git";
104         //$newloc = preg_replace("/^$basegit/", "", $_SERVER["REQUEST_URI"]);
105         chdir("$repo_base/$repo");
106         exec("/usr/bin/git update-server-info");
107         
108         if($_SERVER["REQUEST_METHOD"] == "POST") {
109                         gwvp_AskForBasicAuth();
110                         gwvp_callGitBackend($repo);
111                         return;
112         }
113         
114         if(isset($_REQUEST["service"])) {
115                 if($_REQUEST["service"] == "git-receive-pack") {
116                         // we are a write call - we need auth and we're going to the backend proper
117                         gwvp_AskForBasicAuth();
118                         gwvp_callGitBackend($repo);
119                         return;
120                 }
121         }
122         
123         
124         if(file_exists("$repo_base/$repo/$newloc")) {
125                 error_log("would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc");
126                 $fh = fopen("$repo_base/$repo/$newloc", "rb");
127                 
128                 error_log("pushing file");
129                 while(!feof($fh)) {
130                         echo fread($fh, 8192);
131                 }
132         } else {
133                 echo "would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc, NE";
134                 header('HTTP/1.0 404 No Such Thing');
135                 return;
136         }
137 }
138
139
140 function gwvp_callGitBackend($repo)
141 {
142         // this is where things become a nightmare
143                 $fh   = fopen('php://input', "r");
144                 
145                 $ruri = $_SERVER["REQUEST_URI"];
146                 $strrem = "git/$repo";
147                 $euri = str_replace($strrem, "", $_REQUEST["q"]);
148                 //$euri = preg_replace("/^git\/$repo\.git/", "", $_REQUEST["q"]);
149                 
150                 
151                 
152                 $rmeth = $_SERVER["REQUEST_METHOD"];
153                 
154                 $qs = "";
155                 foreach($_REQUEST as $key => $var) {
156                         if($key != "q") {
157                                 //error_log("adding, $var from $key");
158                                 if($qs == "") $qs.="$key=$var";
159                                 else $qs.="&$key=$var";
160                         }
161                 }
162                 
163                 //sleep(2);
164                 
165                 
166                 
167                 // this is where the fun, it ends.
168                 $myoutput = "";
169                 unset($myoutput);
170                 
171                 // this be nasty!
172                 
173                 // setup env
174                 if(isset($procenv))     unset($procenv);
175                 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
176                 $procenv["PATH_TRANSLATED"] = "/tmp/$repo/$euri";
177                 $procenv["REQUEST_METHOD"] = "$rmeth";
178                 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
179                 $procenv["QUERY_STRING"] = "$qs";
180                 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
181                 $procenv["REMOTE_USER"] = "user";
182                 $procenv["REMOTE_ADDR"] = "1.2.3.4";
183                 $procenv["AUTH_TYPE"] = "Basic";
184                 
185                 if(isset($_SERVER["CONTENT_TYPE"])) { 
186                         $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
187                 } else {
188                         //$procenv["CONTENT_TYPE"] = "";
189                 }
190                 if(isset($_SERVER["CONTENT_LENGTH"])) { 
191                         $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
192                 }
193                 
194                 error_log("path trans'd is /tmp/$repo/$euri from $ruri with ".$_REQUEST["q"]." $strrem");
195                 
196                 
197                 
198
199                 $pwd = "/tmp/";
200                 
201                 $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);
202                 
203                 $untilblank = false;
204                 while(!$untilblank&&!feof($pipes[1])) {
205                         $lines_t = fgets($pipes[1]);
206                         $lines = trim($lines_t);
207                         error_log("got line: $lines");
208                         if($lines_t == "\r\n") {
209                                 $untilblank = true;
210                                 error_log("now blank");
211                         } else header($lines);
212                         if($lines === false) {
213                                 error_log("got an unexpexted exit...");
214                                 exit(0);
215                         }
216                         
217                 }
218                 
219
220                 $firstline = true;
221                 $continue = true;
222                 
223                 if(!stream_set_blocking($fh,0)) {
224                         error_log("cant set input non-blocking");
225                 }
226
227                 if(!stream_set_blocking($pipes[1],0)) {
228                         error_log("cant set pipe1 non-blocking");
229                 }
230                 
231                 // i was going to use stream_select, but i feel this works better like this
232                 while($continue) {
233                         // do client
234                         if(!feof($fh)) {
235                                 $from_client_data = fread($fh,8192);
236                                 if($from_client_data !== false) fwrite($pipes[0], $from_client_data);
237                                 fflush($pipes[0]);
238                                 //fwrite($fl, $from_client_data);
239                                 $client_len = strlen($from_client_data);
240                         } else {
241                                 error_log("client end");
242                                 $client_len = 0;
243                         }
244                         
245                         // do cgi
246                         // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
247                         // but i do know that the fgets for the headers above should have comsued that
248                         if(!feof($pipes[1])) {
249                                 $from_cgi_data_t = fread($pipes[1],8192);
250                                 $from_cgi_data = $from_cgi_data_t;
251                                 
252                                 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
253                                 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
254                                 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
255                                 if($firstline) {
256                                         if(strlen($from_cgi_data_t)>0) {
257                                                 // i dont get why this happens, and its very frustrating.. im not sure if its a bug in php
258                                                 // or something the git-http-backend thing is doing..
259                                                 // TODO: find out why this happens
260                                                 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
261                                                 if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) {
262                                                         error_log("MOOOKS - we did trunc");
263                                                 } else {
264                                                         error_log("MOOOKS - we did not trunc");
265                                                 }
266                                                 $firstline = false;
267                                         }
268                                 }
269                                 
270                                 if($from_cgi_data !== false) {
271                                         echo $from_cgi_data;
272                                         flush();
273                                 }
274                                 $cgi_len = strlen($from_cgi_data);
275                         } else {
276                                 error_log("cgi end");
277                                 $cgi_len = 0;
278                         }
279                         
280                         if(feof($pipes[1])) $continue = false;
281                         else {
282                                 if($client_len == 0 && $cgi_len == 0) {
283                                         usleep(200000);
284                                         error_log("sleep tick");
285                                 } else {
286                                         error_log("sizes: $client_len, $cgi_len");
287                                         if($cgi_len > 0) {
288                                                 error_log("from cgi: \"$from_cgi_data\"");
289                                         }
290                                 }
291                         }
292                         
293                 }
294                 
295                 
296                 //fclose($fl);
297                 fclose($fh);
298                 fclose($pipes[1]);
299                 fclose($pipes[0]);      
300 }
301
302
303
304 function gwvp_repoExists($name)
305 {
306         global $repo_base;
307         
308         if(file_exists("$repo_base/$name.git")) return true;
309         else return false;
310 }
311
312 function gwvp_createGitRepo($name, $bundle=null)
313 {
314         global $repo_base;
315         
316         // phew, this works, but i tell you this - bundles arent quite as nice as they should be
317         if($bundle == null) {
318                 error_log("would create $repo_base/$name.git");
319                 exec("/usr/bin/git init $repo_base/$name.git --bare > /tmp/gitlog 2>&1");
320                 chdir("$repo_base/$name.git");
321                 exec("/usr/bin/git update-server-info");
322         } else {
323                 error_log("create via mirror on $repo_base/$name.git");
324                 exec("/usr/bin/git clone --mirror $bundle $repo_base/$name.git > /tmp/gitlog 2>&1");
325                 chdir("$repo_base/$name.git");
326                 exec("/usr/bin/git update-server-info");
327         }
328         
329         return true;
330 }
331 ?>