pulls/clones now work thru git-http-backend THRU my php script
[gwvp.git] / gwvplib / gwvpweb.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                 
15                 $k1 = print_r($_REQUEST, true);
16                 $k2 = print_r($_SERVER, true);
17                 $k3 = print_r($_POST, true);
18                 error_log("k1: $k1");
19                 error_log("k2: $k2");
20                 error_log("k3: $k3");
21                 
22                 $ruri = $_SERVER["REQUEST_URI"];
23                 $euri = str_replace("test.git", "", $_REQUEST["q"]);
24                 
25                 $rmeth = $_SERVER["REQUEST_METHOD"];
26                 
27                 $qs = "";
28                 foreach($_REQUEST as $key => $var) {
29                         if($key != "q") {
30                                 error_log("adding, $var from $key");
31                                 if($qs == "") $qs.="$key=$var";
32                                 else $qs.="&$key=$var";
33                         }
34                 }
35                 
36                 //sleep(2);
37                 
38                 $body = '';
39                 if($rmeth == "POST") {
40                         $body = file_get_contents("php://input");
41                 }
42                 
43                 
44                 // this is where the fun, it ends.
45                 $myoutput = "";
46                 unset($myoutput);
47                 
48                 // this be nasty!
49                 
50                 // setup env
51                 if(isset($procenv))     unset($procenv);
52                 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
53                 $procenv["PATH_TRANSLATED"] = "/var/cache/git/test.git$euri";
54                 $procenv["REQUEST_METHOD"] = "$rmeth";
55                 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
56                 $procenv["QUERY_STRING"] = "$qs";
57                 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
58                 $procenv["REMOTE_USER"] = "paulr";
59                 $procenv["REMOTE_ADDR"] = "1.2.3.4";
60                 $procenv["CONTENT_TYPE"] = "application/x-git-upload-pack-request";
61
62                 $pwd = "/var/cache/git";
63                 
64                 error_log("openproc");
65                 $proc = proc_open("/usr/lib/git-core/git-http-backend", array(array("pipe","r"),array("pipe","w"),array("file","/dev/null", "w")), $pipes, $pwd, $procenv);
66                 error_log("openproc2, $proc");
67                 
68                 $stdin = $pipes[0];
69                 $stdout = $pipes[1];
70                 
71                 error_log("openproc3");
72                 
73                 if($body != "") {
74                         fwrite($pipes[0], $body);
75                 }
76                 
77                 error_log("openproc4");
78                 
79                 $untilblank = false;
80                 while(!$untilblank&&!feof($pipes[1])) {
81                         $lines = rtrim(fgets($pipes[1]));
82                         error_log("got line: $lines");
83                         if($lines == "") {
84                                 $untilblank = true;
85                                 error_log("now blank");
86                         } else header($lines);
87                         if($lines === false) exit(0);
88                         
89                 }
90                 
91                 error_log("openproc5");
92                 
93                 // now the body
94                 while(!feof($pipes[1])) {
95                         echo fread($pipes[1], 1024);
96                 }
97                 
98                 error_log("openproc6");
99                 
100                 fclose($stdout);
101                 fclose($stdin);
102                 
103                 
104                 
105         }
106 }
107
108 ?>