332a6e4809d40a9a54e328334bca5d05502d99fb
[phpgitweb.git] / htdocs / lib / ProjectLoader.class.php
1 <?php
2 /* ProjectLoader.class.php - phpgitweb
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
4  * 
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License 
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
17  */
18
19 class ProjectLoader {
20         private $projects = NULL;
21         
22         private function loadProjects($load_details = true) {
23                 $this->projects = array();
24                 if(GitConfig::PROJECT_LIST) {
25                         foreach(file(GitConfig::PROJECT_LIST) as $entry) {
26                                 $entry = explode(" ", $entry);
27                                 
28                                 if($load_details) {
29                                         //check if project really exists
30                                         $project = $this->loadProject($entry[0], false);
31                                         if($project === NULL)
32                                                 continue;
33                                 } else
34                                         $project['name'] = $entry[0];
35                                 
36                                 $project['owner'] = $entry[1];
37                                 $this->projects[] = $project;
38                         }
39                 } else {
40                         //walk through PROJECT_ROOT
41                         
42                 }
43         }
44         
45         private function loadProject($name, $check_strict_export = true) {
46                 $project = array();
47                 $project['name'] = $name;
48                 
49                 $dir_seperator = (substr(GitConfig::PROJECT_ROOT, -1) == '/' ? '' : '/');
50                 if(!Validation::validate_path($name))
51                         return NULL;
52                 if(is_dir(GitConfig::PROJECT_ROOT.$dir_seperator.$name))
53                         $project['path'] = GitConfig::PROJECT_ROOT.$dir_seperator.$name;
54                 else if(is_dir(GitConfig::PROJECT_ROOT.$dir_seperator.$name.".git"))
55                         $project['path'] = GitConfig::PROJECT_ROOT.$dir_seperator.$name.".git";
56                 else
57                         return NULL;
58                 if(is_dir($project['path'].'/.git'))
59                         $project['path'] .= '/.git';
60                 
61                 if(GitConfig::EXPORT_FILE && !file_exists($project['path'].'/'.GitConfig::EXPORT_FILE))
62                         return NULL;
63                 
64                 if(GitConfig::STRICT_EXPORT && $check_strict_export) {
65                         if($this->projects === NULL)
66                                 $this->loadProjects(false);
67                         $found = false;
68                         foreach($this->projects as $p) {
69                                 if(strtolower($p['name']) == strtolower($name)) {
70                                         $found = true;
71                                         $project['name'] = $p['name'];
72                                         $project['owner'] = $p['owner'];
73                                         break;
74                                 }
75                         }
76                         if(!$found)
77                                 return NULL;
78                 }
79                 
80                 if(file_exists($project['path'].'/description'))
81                         $project['description'] = file_get_contents($project['path'].'/description');
82                 else
83                         $project['description'] = "";
84                 
85                 if(!array_key_exists('owner', $project) || $project['owner'] == null) {
86                         if(GitConfig::PROJECT_OWNER)
87                                 $project['owner'] = GitConfig::PROJECT_OWNER;
88                         else {
89                                 $project['owner'] = fileowner($project['path']);
90                                 $owner = posix_getpwuid($project['owner']);
91                                 if($owner && $owner['name'])
92                                         $project['owner'] = $owner['name'];
93                         }
94                 }
95                 
96                 return $project;
97         }
98         
99         public function getProjectList() {
100                 $this->loadProjects();
101                 return $this->projects;
102         }
103         
104         public function getProject($name) {
105                 return $this->loadProject($name);
106         }
107         
108         public function getLastChange($project) {
109                 if(!array_key_exists('last_activity', $project)) {
110                         $project['last_activity'] = GitCommand::last_activity($project['path']);
111                         if(!$project['last_activity'])
112                                 $project['last_activity'] = 0;
113                 }
114                 return $project['last_activity'];
115         }
116         
117         private function getProjectRefsRecursive(&$project, $cref) {
118                 if ($dh = opendir($project['path'].'/'.$cref)) {
119                         while (($file = readdir($dh)) !== false) {
120                                 if($file[0] == '.')
121                                         continue;
122                                 if(is_dir($project['path'].'/'.$cref.'/'.$file))
123                                         $this->getProjectRefsRecursive($project, $cref.'/'.$file);
124                                 else {
125                                         $refval = file($project['path'].'/'.$cref.'/'.$file);
126                                         $project['refs'][$cref.($cref == '' ? '' : '/').$file] = str_replace(array("\r", "\n"), array("", ""), $refval[0]);
127                                 }
128                         }
129                 }
130         }
131         
132         public function getProjectRefs($project) {
133                 if(!array_key_exists('refs', $project)) {
134                         if(is_dir($project['path'].'/refs')) {
135                                 $project['refs'] = array();
136                                 $this->getProjectRefsRecursive($project, 'refs');
137                         }
138                 }
139                 return $project['refs'];
140         }
141 }
142
143 ?>