fixing up the "is setup" check in the installer
[gwvp.git] / archive / gwvpgitbackend_poccode.php
1 <?php
2
3
4 function goWebGitBackEnd()
5 {
6         if(!isset($_SERVER["PHP_AUTH_USER"])) {
7                 header('WWW-Authenticate: Basic realm="My Realm"');
8                 header('HTTP/1.0 401 Unauthorized');
9                 exit(0);
10         } else {
11                 
12                 // well, i cant believe it, but this actually freaking works... for pulls/clones anyways... commits are not working...
13                 // but wow thats better then what i had hoped for
14                 // first thing to do
15                 $fh   = fopen('php://input', "r");
16                 
17                 $k1 = print_r($_REQUEST, true);
18                 $k2 = print_r($_SERVER, true);
19                 $k3 = print_r($_POST, true);
20                 $k4 = print_r($_GET, true);
21                 error_log("k1: $k1");
22                 error_log("k2: $k2");
23                 error_log("k3: $k3");
24                 error_log("k4: $k4");
25                 error_log($_SERVER["REQUEST_URI"]);
26                 
27                 $ruri = $_SERVER["REQUEST_URI"];
28                 $euri = str_replace("test.git", "", $_REQUEST["q"]);
29                 
30                 $rmeth = $_SERVER["REQUEST_METHOD"];
31                 
32                 $qs = "";
33                 foreach($_REQUEST as $key => $var) {
34                         if($key != "q") {
35                                 //error_log("adding, $var from $key");
36                                 if($qs == "") $qs.="$key=$var";
37                                 else $qs.="&$key=$var";
38                         }
39                 }
40                 
41                 //sleep(2);
42                 
43                 
44                 
45                 // this is where the fun, it ends.
46                 $myoutput = "";
47                 unset($myoutput);
48                 
49                 // this be nasty!
50                 
51                 // setup env
52                 if(isset($procenv))     unset($procenv);
53                 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
54                 $procenv["PATH_TRANSLATED"] = "/var/cache/git/test.git$euri";
55                 $procenv["REQUEST_METHOD"] = "$rmeth";
56                 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
57                 $procenv["QUERY_STRING"] = "$qs";
58                 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
59                 $procenv["REMOTE_USER"] = "user";
60                 $procenv["REMOTE_ADDR"] = "1.2.3.4";
61                 $procenv["AUTH_TYPE"] = "Basic";
62                 
63                 if(isset($_SERVER["CONTENT_TYPE"])) { 
64                         $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
65                 } else {
66                         //$procenv["CONTENT_TYPE"] = "";
67                 }
68                 if(isset($_SERVER["CONTENT_LENGTH"])) { 
69                         $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
70                 }
71                 
72                 
73
74                 $pwd = "/var/cache/git";
75                 
76                 //error_log("openproc");
77                 $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);
78                 //$proc = proc_open("/home/paulr/src/eclipse-workspace/gwvp/datacatch/../datacatcher.sh", array(array("pipe","r"),array("pipe","w"),array("file","/tmp/err", "a")), $pipes, $pwd, $procenv);
79                 //error_log("openproc2, $proc");
80                 
81                 //error_log("openproc3");
82                 
83                 //if($rmeth == "POST") {
84                 //}
85                 
86                 
87                 //error_log("openproc4");
88                 
89                 $untilblank = false;
90                 while(!$untilblank&&!feof($pipes[1])) {
91                         $lines_t = fgets($pipes[1]);
92                         $lines = trim($lines_t);
93                         error_log("got line: $lines");
94                         if($lines_t == "\r\n") {
95                                 $untilblank = true;
96                                 error_log("now blank");
97                         } else header($lines);
98                         if($lines === false) {
99                                 error_log("got an unexpexted exit...");
100                                 exit(0);
101                         }
102                         
103                 }
104                 
105                 //error_log("openproc5");
106                 // if would seam that post and output must be synchronised together somehow... i think
107                 /*
108                 $fh   = fopen('php://input', 'rb');
109                         if ($fh) {
110                                 while (!feof($fh)) {
111                                 $s = fread($fh, 1024);
112                                 if($s === false) {
113                                         error_log("unexpected eror on input read");
114                                 }
115                                 fwrite($pipes[0], $s);
116                                 }
117                                 fclose($fh);
118                         }
119                 
120                 // now the body
121                 $fl = fopen("/tmp/pushpipe", "ab");
122                 while(!feof($pipes[1])) {
123                         $d =  fread($pipes[1], 1024);
124                         error_log("got read, $d");
125                         if($d === false) {
126                                 errog_log("got unexpected false on reads");
127                         } else {
128                                 echo $d;
129                                 fwrite($fl, $d);
130                         }
131                 }*/
132                 
133                 // oh god, something goes wrong with all this data and i dont know what it is
134                 // but i think its cause proc_open doesnt deal with binary data properly
135                 $firstline = true;
136                 $continue = true;
137                 //$fl = fopen("/tmp/pushpipe", "w");
138                 
139                 if(!stream_set_blocking($fh,0)) {
140                         error_log("cant set input non-blocking");
141                 }
142                 // problem no 1 - dont do this
143                 //if(!stream_set_blocking($pipes[0],0)) {
144                         //error_log("cant set pipe 0 non-blocking");
145                 //}
146                 if(!stream_set_blocking($pipes[1],0)) {
147                         error_log("cant set pipe1 non-blocking");
148                 }
149                 
150                 // i was going to use stream_select, but i feel this works better like this
151                 while($continue) {
152                         // do client
153                         if(!feof($fh)) {
154                                 $from_client_data = fread($fh,8192);
155                                 if($from_client_data !== false) fwrite($pipes[0], $from_client_data);
156                                 fflush($pipes[0]);
157                                 //fwrite($fl, $from_client_data);
158                                 $client_len = strlen($from_client_data);
159                         } else {
160                                 error_log("client end");
161                                 $client_len = 0;
162                         }
163                         
164                         // do cgi
165                         // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
166                         // but i do know that the fgets for the headers above should have comsued that
167                         if(!feof($pipes[1])) {
168                                 $from_cgi_data_t = fread($pipes[1],8192);
169                                 $from_cgi_data = $from_cgi_data_t;
170                                 
171                                 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
172                                 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
173                                 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
174                                 if($firstline) {
175                                         if(strlen($from_cgi_data_t)>0) {
176                                                 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
177                                                 $firstline = false;
178                                         }
179                                 }
180                                 
181                                 if($from_cgi_data !== false) {
182                                         echo $from_cgi_data;
183                                         flush();
184                                 }
185                                 $cgi_len = strlen($from_cgi_data);
186                         } else {
187                                 error_log("cgi end");
188                                 $cgi_len = 0;
189                         }
190                         
191                         if(feof($pipes[1])) $continue = false;
192                         else {
193                                 if($client_len == 0 && $cgi_len == 0) {
194                                         usleep(200000);
195                                         error_log("sleep tick");
196                                 } else {
197                                         error_log("sizes: $client_len, $cgi_len");
198                                         if($cgi_len > 0) {
199                                                 error_log("from cgi: \"$from_cgi_data\"");
200                                         }
201                                 }
202                         }
203                         
204                 }
205                 
206                 
207                 //fclose($fl);
208                 fclose($fh);
209                 fclose($pipes[1]);
210                 fclose($pipes[0]);
211                 
212                 //error_log("openproc6");
213                 
214         }
215 }
216
217 ?>