reaplced the git service code (main gitbackendinterface()) with a new
[gwvp.git] / gwvplib / gwvpgitcontrol.php
index 4622f27..a59a02b 100644 (file)
@@ -4,6 +4,7 @@ $CALL_ME_FUNCTIONS["gitcontrol"] = "gwvp_gitControlCallMe";
 
 //$MENU_ITEMS["20repos"]["text"] = "Repo Admin";
 //$MENU_ITEMS["20repos"]["link"] = "$BASE_URL/admin/repos";
+$HOME_PAGE_PROVIDERS["gitlog"] = "gwvp_GitLogProvider";
 
 function gwvp_gitControlCallMe()
 {
@@ -22,6 +23,11 @@ function gwvp_gitControlCallMe()
        
 }
 
+function gwvp_GitLogProvider()
+{
+       echo "<br>gitload provider loaded on homepage<br>";
+}
+
 function gwvp_repoPermissionCheck($repo, $user)
 {
        return true;
@@ -29,8 +35,104 @@ function gwvp_repoPermissionCheck($repo, $user)
 
 function gwvp_gitBackendInterface()
 {
-       global $repo_base, $BASE_URL;
+       // and this is where i re-code the git backend interface from scratch
+       global $BASE_URL;
+       
+       $repo_base = gwvp_getConfigVal("repodir");
+       
+       // TODO: we need to stop passing the repo name around as "repo.git", it needs to be just "repo"
+       
+       $repo = "";
+       $repoid = -1;
+       $newloc = "/";
+       if(isset($_REQUEST["q"])) {
+               $query = $_REQUEST["q"];
+               $qspl = explode("/", $query);
+               $repo = $qspl[1];
+               $repoid = gwvp_resolvRepoPerms($repo);
+               for($i=2; $i < count($qspl); $i++) {
+                       $newloc .= "/".$qspl[$i];
+               }
+       }
+       
+       if($repoid == -1) {
+               gwvp_fourZeroFour();
+               return;
+       }
+       
+       // so now we have the repo
+       // next we determine if this is a read or a write
+       $write = false;
+       if(isset($_REQUEST["service"])) {
+               if($_REQUEST["service"] == "git-receive-pack") {
+                       $write = true;
+               }
+       }
+       if($_SERVER["REQUEST_METHOD"] == "POST") {
+               $write = true;
+       }
+       
+       // if its a write, we push for authentication
+       if($write) {
+               $person = gwvp_checkBasicAuthLogin();
+               if($person == false) {
+                       gwvp_AskForBasicAuth();
+                       return;
+               } else {
+                       $perms = gwvp_resolvRepoPerms($person["id"], $repoid);
+                       if($perms < 3) {
+                               gwvp_fourZeroThree();
+                               return;
+                       } else {
+                               // here we pass to the git backend
+                               gwvp_callGitBackend($person["username"], $repo);
+                       }
+               }
+               return;
+       }
+       
+       // if not we figure out the anon permissions for a repo
+       $perms = gwvp_resolvRepoPerms(-1, $repoid);
+       
+       // if they're less then read, we need to then check the user auth permissions
+       if($perms < 2) {
+               // we ask for auth
+               $person = gwvp_checkBasicAuthLogin();
+               if($person == false) {
+                       gwvp_AskForBasicAuth();
+                       return;
+               } else {
+                       $perms = gwvp_resolvRepoPerms($person["id"], $repoid);
+                       if($perms < 3) {
+                               gwvp_fourZeroThree();
+                               return;
+                       }
+               }
+       }
+       
+       // if we made it this far, we a read and we have permissions to do so, just search the file from the repo
+       if(file_exists("$repo_base/$repo/$newloc")) {
+               error_log("would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc");
+               $fh = fopen("$repo_base/$repo/$newloc", "rb");
+               
+               error_log("pushing file");
+               while(!feof($fh)) {
+                       echo fread($fh, 8192);
+               }
+       } else {
+               //echo "would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc, NE";
+               gwvp_fourZeroFour();
+               return;
+       }
+       
+}
+
+
+function gwvp_gitBackendInterface_old()
+{
+       global $BASE_URL;
        
+       $repo_base = gwvp_getConfigVal("repodir");
        
        $repo = "";
        $newloc = "/";
@@ -102,6 +204,9 @@ function gwvp_gitBackendInterface()
        // now we need to rebuild the actual request or do we?
        //$basegit = "$BASE_URL/git/something.git";
        //$newloc = preg_replace("/^$basegit/", "", $_SERVER["REQUEST_URI"]);
+       chdir("$repo_base/$repo");
+       exec("/usr/bin/git update-server-info");
+       
        if($_SERVER["REQUEST_METHOD"] == "POST") {
                        gwvp_AskForBasicAuth();
                        gwvp_callGitBackend($repo);
@@ -133,8 +238,17 @@ function gwvp_gitBackendInterface()
        }
 }
 
+function gwvp_canManageRepo($userid, $repoid)
+{
+       // only the owner or an admin can do these tasks
+       error_log("Checking repoid, $repoid against userid $userid");
+       
+       if(gwvp_IsUserAdmin(null, null, $userid)) return true;
+       if(gwvp_IsRepoOwner($userid, $repoid)) return true;
+       return false;
+}
 
-function gwvp_callGitBackend($repo)
+function gwvp_callGitBackend($username, $reponame)
 {
        // this is where things become a nightmare
                $fh   = fopen('php://input', "r");
@@ -175,7 +289,7 @@ function gwvp_callGitBackend($repo)
                $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
                $procenv["QUERY_STRING"] = "$qs";
                $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
-               $procenv["REMOTE_USER"] = "user";
+               $procenv["REMOTE_USER"] = "$username";
                $procenv["REMOTE_ADDR"] = "1.2.3.4";
                $procenv["AUTH_TYPE"] = "Basic";
                
@@ -190,6 +304,8 @@ function gwvp_callGitBackend($repo)
                
                error_log("path trans'd is /tmp/$repo/$euri from $ruri with ".$_REQUEST["q"]." $strrem");
                
+               
+               
 
                $pwd = "/tmp/";
                
@@ -249,9 +365,14 @@ function gwvp_callGitBackend($repo)
                                // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
                                if($firstline) {
                                        if(strlen($from_cgi_data_t)>0) {
+                                               // i dont get why this happens, and its very frustrating.. im not sure if its a bug in php
+                                               // or something the git-http-backend thing is doing..
+                                               // TODO: find out why this happens
                                                $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
                                                if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) {
                                                        error_log("MOOOKS - we did trunc");
+                                               } else {
+                                                       error_log("MOOOKS - we did not trunc");
                                                }
                                                $firstline = false;
                                        }
@@ -293,15 +414,19 @@ function gwvp_callGitBackend($repo)
 
 function gwvp_repoExists($name)
 {
-       global $repo_base;
+       $repo_base = gwvp_getConfigVal("repodir");
        
        if(file_exists("$repo_base/$name.git")) return true;
        else return false;
 }
 
-function gwvp_createGitRepo($name, $bundle=null)
+// default perms:
+// 0 - anyone can clone/read, only owner can write
+// 1 - noone can clone/read, repo is visible (i.e. name), only owner can read/write repo
+// 2 - only owner can see anything
+function gwvp_createGitRepo($name, $ownerid, $desc, $bundle=null, $defaultperms=0)
 {
-       global $repo_base;
+       $repo_base = gwvp_getConfigVal("repodir");
        
        // phew, this works, but i tell you this - bundles arent quite as nice as they should be
        if($bundle == null) {
@@ -315,7 +440,76 @@ function gwvp_createGitRepo($name, $bundle=null)
                chdir("$repo_base/$name.git");
                exec("/usr/bin/git update-server-info");
        }
+
+       // gwvp_AddRepo($reponame, $repodesc, $repoowner, $defaultperms = 0)
+       gwvp_AddRepo($name, $desc, $ownerid, $defaultperms);
        
        return true;
 }
+
+// this funciton returns one of three things, read, visible, write, none
+// as
+// 0 - none
+// 1 - visible
+// 2 - read
+// 3 - write
+function gwvp_resolvRepoPerms($userid, $repoid)
+{
+       $ownerid = gwvp_getRepoOwner($repoid);
+       $isadmin = gwvp_IsUserAdmin(null, null, $userid);
+       
+       if($isadmin) return 3;
+       
+       if($userid == $ownerid) return 3;
+       
+       // now we load the perms table and pray
+       $repoperms = gwvp_getRepoPermissions($repoid);
+       $usergroups = gwvp_getGroupsForUser(null, $userid);
+
+       $maxperm = 0;
+       if($repoperms != false) foreach($repoperms as $perm) {
+               // need to go thru each perm, then check it agains the user we're trying to figure
+               // the perms on
+               switch($perm["type"]) {
+                       case "read":
+                               $permval = 2;
+                               break;
+                       case "visible":
+                               $permval = 1;
+                               break;
+                       case "write":
+                               $permval = 3;
+                               break;
+                       default:
+                               $permval = 0;
+               }
+               
+               // we only var if permval is greater then current
+               if($permval > $maxperm) {
+                       //error_log("going into check for $maxperm/$permval, ".$perm["ref"]);
+                       if($perm["ref"] == "anon") {
+                               $maxperm = $permval;
+                       } else if($perm["ref"] == "authed") {
+                               $maxperm = $permval;
+                       } else {
+                               // now we do splits
+                               $spl = explode(":", $perm["ref"]);
+                               $idtype = $spl[0];
+                               $idval = $spl[1];
+                               if($idtype == "group") {
+                                       // function gwvp_IsGroupMember($email, $groupname)
+                                       if(gwvp_IsGroupMemberById($userid, $idval)) $maxperm = $permval;
+                               } else if ($idtype == "user") {
+                                       //error_log("checking $userid, $idval");
+                                       if($userid == $idval) $maxperm = $permval;
+                               }
+                       }
+               }
+       }
+       
+       // thats TOTALLY going to work... -_0 we should really write a unit test for this, but thats a bit
+       // hard given the db req's so for now, we'll leave it as is
+       return $maxperm;
+}
+
 ?>
\ No newline at end of file