3 $CALL_ME_FUNCTIONS["gitcontrol"] = "gwvp_gitControlCallMe";
5 //$MENU_ITEMS["20repos"]["text"] = "Repo Admin";
6 //$MENU_ITEMS["20repos"]["link"] = "$BASE_URL/admin/repos";
8 function gwvp_gitControlCallMe()
10 if(isset($_REQUEST["q"])) {
11 $query = $_REQUEST["q"];
12 $qspl = explode("/", $query);
14 if($qspl[0] == "git") {
15 return "gwvp_gitBackendInterface";
25 function gwvp_repoPermissionCheck($repo, $user)
30 function gwvp_gitBackendInterface()
32 global $repo_base, $BASE_URL;
37 if(isset($_REQUEST["q"])) {
38 $query = $_REQUEST["q"];
39 $qspl = explode("/", $query);
41 for($i=2; $i < count($qspl); $i++) {
42 $newloc .= "/".$qspl[$i];
46 $actual_repo_name = preg_replace("/\.git$/", "", $repo);
48 $user = gwvp_checkBasicAuthLogin();
51 error_log("User is set to false, so its anonymouse");
53 error_log("user is $user");
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");
63 error_log("push auth");
64 gwvp_AskForBasicAuth();
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"
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
83 $agent = "git-unknown";
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) {
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";
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);
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);
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");
125 error_log("pushing file");
127 echo fread($fh, 8192);
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');
137 function gwvp_callGitBackend($repo)
139 // this is where things become a nightmare
140 $fh = fopen('php://input', "r");
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"]);
149 $rmeth = $_SERVER["REQUEST_METHOD"];
152 foreach($_REQUEST as $key => $var) {
154 //error_log("adding, $var from $key");
155 if($qs == "") $qs.="$key=$var";
156 else $qs.="&$key=$var";
164 // this is where the fun, it ends.
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";
182 if(isset($_SERVER["CONTENT_TYPE"])) {
183 $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
185 //$procenv["CONTENT_TYPE"] = "";
187 if(isset($_SERVER["CONTENT_LENGTH"])) {
188 $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
191 error_log("path trans'd is /tmp/$repo/$euri from $ruri with ".$_REQUEST["q"]." $strrem");
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);
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") {
205 error_log("now blank");
206 } else header($lines);
207 if($lines === false) {
208 error_log("got an unexpexted exit...");
218 if(!stream_set_blocking($fh,0)) {
219 error_log("cant set input non-blocking");
222 if(!stream_set_blocking($pipes[1],0)) {
223 error_log("cant set pipe1 non-blocking");
226 // i was going to use stream_select, but i feel this works better like this
230 $from_client_data = fread($fh,8192);
231 if($from_client_data !== false) fwrite($pipes[0], $from_client_data);
233 //fwrite($fl, $from_client_data);
234 $client_len = strlen($from_client_data);
236 error_log("client end");
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;
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.
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");
260 if($from_cgi_data !== false) {
264 $cgi_len = strlen($from_cgi_data);
266 error_log("cgi end");
270 if(feof($pipes[1])) $continue = false;
272 if($client_len == 0 && $cgi_len == 0) {
274 error_log("sleep tick");
276 error_log("sizes: $client_len, $cgi_len");
278 error_log("from cgi: \"$from_cgi_data\"");
294 function gwvp_repoExists($name)
298 if(file_exists("$repo_base/$name.git")) return true;
302 function gwvp_createGitRepo($name, $bundle=null)
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");
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");