pushed and pulled the first branch.. yes, i pushed, i pushed hard. but
[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         if($_SERVER["REQUEST_METHOD"] == "POST") {
106                         gwvp_AskForBasicAuth();
107                         gwvp_callGitBackend($repo);
108                         return;
109         }
110         
111         if(isset($_REQUEST["service"])) {
112                 if($_REQUEST["service"] == "git-receive-pack") {
113                         // we are a write call - we need auth and we're going to the backend proper
114                         gwvp_AskForBasicAuth();
115                         gwvp_callGitBackend($repo);
116                         return;
117                 }
118         }
119         
120         
121         if(file_exists("$repo_base/$repo/$newloc")) {
122                 error_log("would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc");
123                 $fh = fopen("$repo_base/$repo/$newloc", "rb");
124                 
125                 error_log("pushing file");
126                 while(!feof($fh)) {
127                         echo fread($fh, 8192);
128                 }
129         } else {
130                 echo "would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc, NE";
131                 header('HTTP/1.0 404 No Such Thing');
132                 return;
133         }
134 }
135
136
137 function gwvp_callGitBackend($repo)
138 {
139         // this is where things become a nightmare
140                 $fh   = fopen('php://input', "r");
141                 
142                 $ruri = $_SERVER["REQUEST_URI"];
143                 $strrem = "git/$repo";
144                 $euri = str_replace($strrem, "", $_REQUEST["q"]);
145                 //$euri = preg_replace("/^git\/$repo\.git/", "", $_REQUEST["q"]);
146                 
147                 
148                 
149                 $rmeth = $_SERVER["REQUEST_METHOD"];
150                 
151                 $qs = "";
152                 foreach($_REQUEST as $key => $var) {
153                         if($key != "q") {
154                                 //error_log("adding, $var from $key");
155                                 if($qs == "") $qs.="$key=$var";
156                                 else $qs.="&$key=$var";
157                         }
158                 }
159                 
160                 //sleep(2);
161                 
162                 
163                 
164                 // this is where the fun, it ends.
165                 $myoutput = "";
166                 unset($myoutput);
167                 
168                 // this be nasty!
169                 
170                 // setup env
171                 if(isset($procenv))     unset($procenv);
172                 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
173                 $procenv["PATH_TRANSLATED"] = "/tmp/$repo/$euri";
174                 $procenv["REQUEST_METHOD"] = "$rmeth";
175                 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
176                 $procenv["QUERY_STRING"] = "$qs";
177                 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
178                 $procenv["REMOTE_USER"] = "user";
179                 $procenv["REMOTE_ADDR"] = "1.2.3.4";
180                 $procenv["AUTH_TYPE"] = "Basic";
181                 
182                 if(isset($_SERVER["CONTENT_TYPE"])) { 
183                         $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
184                 } else {
185                         //$procenv["CONTENT_TYPE"] = "";
186                 }
187                 if(isset($_SERVER["CONTENT_LENGTH"])) { 
188                         $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
189                 }
190                 
191                 error_log("path trans'd is /tmp/$repo/$euri from $ruri with ".$_REQUEST["q"]." $strrem");
192                 
193
194                 $pwd = "/tmp/";
195                 
196                 $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);
197                 
198                 $untilblank = false;
199                 while(!$untilblank&&!feof($pipes[1])) {
200                         $lines_t = fgets($pipes[1]);
201                         $lines = trim($lines_t);
202                         error_log("got line: $lines");
203                         if($lines_t == "\r\n") {
204                                 $untilblank = true;
205                                 error_log("now blank");
206                         } else header($lines);
207                         if($lines === false) {
208                                 error_log("got an unexpexted exit...");
209                                 exit(0);
210                         }
211                         
212                 }
213                 
214
215                 $firstline = true;
216                 $continue = true;
217                 
218                 if(!stream_set_blocking($fh,0)) {
219                         error_log("cant set input non-blocking");
220                 }
221
222                 if(!stream_set_blocking($pipes[1],0)) {
223                         error_log("cant set pipe1 non-blocking");
224                 }
225                 
226                 // i was going to use stream_select, but i feel this works better like this
227                 while($continue) {
228                         // do client
229                         if(!feof($fh)) {
230                                 $from_client_data = fread($fh,8192);
231                                 if($from_client_data !== false) fwrite($pipes[0], $from_client_data);
232                                 fflush($pipes[0]);
233                                 //fwrite($fl, $from_client_data);
234                                 $client_len = strlen($from_client_data);
235                         } else {
236                                 error_log("client end");
237                                 $client_len = 0;
238                         }
239                         
240                         // do cgi
241                         // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
242                         // but i do know that the fgets for the headers above should have comsued that
243                         if(!feof($pipes[1])) {
244                                 $from_cgi_data_t = fread($pipes[1],8192);
245                                 $from_cgi_data = $from_cgi_data_t;
246                                 
247                                 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
248                                 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
249                                 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
250                                 if($firstline) {
251                                         if(strlen($from_cgi_data_t)>0) {
252                                                 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
253                                                 if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) {
254                                                         error_log("MOOOKS - we did trunc");
255                                                 }
256                                                 $firstline = false;
257                                         }
258                                 }
259                                 
260                                 if($from_cgi_data !== false) {
261                                         echo $from_cgi_data;
262                                         flush();
263                                 }
264                                 $cgi_len = strlen($from_cgi_data);
265                         } else {
266                                 error_log("cgi end");
267                                 $cgi_len = 0;
268                         }
269                         
270                         if(feof($pipes[1])) $continue = false;
271                         else {
272                                 if($client_len == 0 && $cgi_len == 0) {
273                                         usleep(200000);
274                                         error_log("sleep tick");
275                                 } else {
276                                         error_log("sizes: $client_len, $cgi_len");
277                                         if($cgi_len > 0) {
278                                                 error_log("from cgi: \"$from_cgi_data\"");
279                                         }
280                                 }
281                         }
282                         
283                 }
284                 
285                 
286                 //fclose($fl);
287                 fclose($fh);
288                 fclose($pipes[1]);
289                 fclose($pipes[0]);      
290 }
291
292
293
294 function gwvp_repoExists($name)
295 {
296         global $repo_base;
297         
298         if(file_exists("$repo_base/$name.git")) return true;
299         else return false;
300 }
301
302 function gwvp_createGitRepo($name, $bundle=null)
303 {
304         global $repo_base;
305         
306         // phew, this works, but i tell you this - bundles arent quite as nice as they should be
307         if($bundle == null) {
308                 error_log("would create $repo_base/$name.git");
309                 exec("/usr/bin/git init $repo_base/$name.git --bare > /tmp/gitlog 2>&1");
310                 chdir("$repo_base/$name.git");
311                 exec("/usr/bin/git update-server-info");
312         } else {
313                 error_log("create via mirror on $repo_base/$name.git");
314                 exec("/usr/bin/git clone --mirror $bundle $repo_base/$name.git > /tmp/gitlog 2>&1");
315                 chdir("$repo_base/$name.git");
316                 exec("/usr/bin/git update-server-info");
317         }
318         
319         return true;
320 }
321 ?>