continued :)
[phpgitweb.git] / htdocs / lib / ProjectLoader.class.php
diff --git a/htdocs/lib/ProjectLoader.class.php b/htdocs/lib/ProjectLoader.class.php
new file mode 100644 (file)
index 0000000..eb1c789
--- /dev/null
@@ -0,0 +1,138 @@
+<?php
+/* ProjectLoader.class.php - phpgitweb
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+class ProjectLoader {
+    private $projects = NULL;
+    
+    private function loadProjects($load_details = true) {
+        $this->projects = array();
+        if(GitConfig::PROJECT_LIST) {
+            foreach(file(GitConfig::PROJECT_LIST) as $entry) {
+                $entry = explode(" ", $entry);
+                
+                if($load_details) {
+                    //check if project really exists
+                    $project = $this->loadProject($entry[0], false);
+                                       if($project === NULL)
+                                               continue;
+                } else
+                                       $project['name'] = $entry[0];
+                
+                $project['owner'] = $entry[1];
+                $this->projects[] = $project;
+            }
+        } else {
+            //walk through PROJECT_ROOT
+            
+        }
+    }
+    
+    private function loadProject($name, $check_strict_export = true) {
+        $project = array();
+               $project['name'] = $name;
+        
+        $dir_seperator = (substr(GitConfig::PROJECT_ROOT, -1) == '/' ? '' : '/');
+        if(is_dir(GitConfig::PROJECT_ROOT.$dir_seperator.$name))
+            $project['path'] = GitConfig::PROJECT_ROOT.$dir_seperator.$name;
+        else if(is_dir(GitConfig::PROJECT_ROOT.$dir_seperator.$name.".git"))
+            $project['path'] = GitConfig::PROJECT_ROOT.$dir_seperator.$name.".git";
+        else
+            return NULL;
+               if(is_dir($project['path'].'/.git'))
+                       $project['path'] .= '/.git';
+               
+        if(GitConfig::EXPORT_FILE && !file_exists($project['path'].'/'.GitConfig::EXPORT_FILE))
+            return NULL;
+        
+        if(GitConfig::STRICT_EXPORT && $check_strict_export) {
+            if($this->projects === NULL)
+                $this->loadProjects(false);
+            $found = false;
+            foreach($this->projects as $p) {
+                if(strtolower($p['name']) == strtolower($name)) {
+                    $found = true;
+                                       $project['name'] = $p['name'];
+                    break;
+                }
+            }
+            if(!$found)
+                return NULL;
+        }
+        
+               if(file_exists($project['path'].'/description'))
+                       $project['description'] = file_get_contents($project['path'].'/description');
+               else
+                       $project['description'] = "";
+               
+               if(GitConfig::PROJECT_OWNER)
+                       $project['owner'] = GitConfig::PROJECT_OWNER;
+               else {
+                       $project['owner'] = fileowner($project['path']);
+                       $owner = posix_getpwuid($project['owner']);
+                       if($owner && $owner['name'])
+                               $project['owner'] = $owner['name'];
+               }       
+               
+        return $project;
+    }
+    
+    public function getProjectList() {
+               $this->loadProjects();
+        return $this->projects;
+    }
+    
+    public function getProject($name) {
+        return $this->loadProject($name);
+    }
+       
+       public function getLastChange($project) {
+               if(!array_key_exists('last_activity', $project)) {
+                       $project['last_activity'] = GitCommand::last_activity($project['path']);
+                       if(!$project['last_activity'])
+                               $project['last_activity'] = 0;
+               }
+               return $project['last_activity'];
+       }
+       
+       private function getProjectRefsRecursive(&$project, $cref) {
+               if ($dh = opendir($project['path'].'/'.$cref)) {
+                       while (($file = readdir($dh)) !== false) {
+                               if($file == '.' || $file == '..')
+                                       continue;
+                               if(is_dir($project['path'].'/'.$cref.'/'.$file))
+                                       $this->getProjectRefsRecursive($project, $cref.'/'.$file);
+                               else {
+                                       $refval = file($project['path'].'/'.$cref.'/'.$file);
+                                       $project['refs'][$cref.($cref == '' ? '' : '/').$file] = str_replace(array("\r", "\n"), array("", ""), $refval[0]);
+                               }
+                       }
+               }
+       }
+       
+       public function getProjectRefs($project) {
+               if(!array_key_exists('refs', $project)) {
+                       if(is_dir($project['path'].'/refs')) {
+                               $project['refs'] = array();
+                               $this->getProjectRefsRecursive($project, 'refs');
+                       }
+               }
+               return $project['refs'];
+    }
+}
+
+?>
\ No newline at end of file