reactivated project (dev: http://git-dev.pk910.de)
[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(" ", str_replace(array("\r", "\n"), array("", ""), $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                                 if(count($entry) > 1)
37                                         $project['owner'] = $entry[1];
38                                 $this->projects[] = $project;
39                         }
40                 } else {
41                         //walk through PROJECT_ROOT
42                         
43                 }
44         }
45         
46         private function loadProject($name, $check_strict_export = true) {
47                 $project = array();
48                 $project['name'] = $name;
49                 
50                 $dir_seperator = (substr(GitConfig::PROJECT_ROOT, -1) == '/' ? '' : '/');
51                 if(!Validation::validate_path($name))
52                         return NULL;
53                 if(is_dir(GitConfig::PROJECT_ROOT.$dir_seperator.$name))
54                         $project['path'] = GitConfig::PROJECT_ROOT.$dir_seperator.$name;
55                 else if(is_dir(GitConfig::PROJECT_ROOT.$dir_seperator.$name.".git"))
56                         $project['path'] = GitConfig::PROJECT_ROOT.$dir_seperator.$name.".git";
57                 else
58                         return NULL;
59                 if(is_dir($project['path'].'/.git'))
60                         $project['path'] .= '/.git';
61                 
62                 if(GitConfig::EXPORT_FILE && !file_exists($project['path'].'/'.GitConfig::EXPORT_FILE))
63                         return NULL;
64                 
65                 if(GitConfig::STRICT_EXPORT && $check_strict_export) {
66                         if($this->projects === NULL)
67                                 $this->loadProjects(false);
68                         $found = false;
69                         foreach($this->projects as $p) {
70                                 if(strtolower($p['name']) == strtolower($name)) {
71                                         $found = true;
72                                         $project['name'] = $p['name'];
73                                         if(isset($p['owner']))
74                                                 $project['owner'] = $p['owner'];
75                                         break;
76                                 }
77                         }
78                         if(!$found)
79                                 return NULL;
80                 }
81                 
82                 if(file_exists($project['path'].'/description'))
83                         $project['description'] = file_get_contents($project['path'].'/description');
84                 else
85                         $project['description'] = "";
86                 
87                 if(!array_key_exists('owner', $project) || $project['owner'] == null) {
88                         if(GitConfig::PROJECT_OWNER)
89                                 $project['owner'] = GitConfig::PROJECT_OWNER;
90                         else {
91                                 $project['owner'] = fileowner($project['path']);
92                                 $owner = posix_getpwuid($project['owner']);
93                                 if($owner && $owner['name'])
94                                         $project['owner'] = $owner['name'];
95                         }
96                 }
97                 
98                 return $project;
99         }
100         
101         public function getProjectList() {
102                 $this->loadProjects();
103                 return $this->projects;
104         }
105         
106         public function getProject($name) {
107                 return $this->loadProject($name);
108         }
109         
110         public function getLastChange($project) {
111                 if(!array_key_exists('last_activity', $project)) {
112                         $project['last_activity'] = GitCommand::last_activity($project['path']);
113                         if(!$project['last_activity'])
114                                 $project['last_activity'] = 0;
115                 }
116                 return $project['last_activity'];
117         }
118         
119         private function getProjectRefsRecursive(&$project, $cref) {
120                 if ($dh = opendir($project['path'].'/'.$cref)) {
121                         while (($file = readdir($dh)) !== false) {
122                                 if($file[0] == '.')
123                                         continue;
124                                 if(is_dir($project['path'].'/'.$cref.'/'.$file))
125                                         $this->getProjectRefsRecursive($project, $cref.'/'.$file);
126                                 else {
127                                         $refval = file($project['path'].'/'.$cref.'/'.$file);
128                                         $project['refs'][$cref.($cref == '' ? '' : '/').$file] = str_replace(array("\r", "\n"), array("", ""), $refval[0]);
129                                 }
130                         }
131                 }
132         }
133         
134         public function getProjectRefs($project) {
135                 if(!array_key_exists('refs', $project)) {
136                         if(is_dir($project['path'].'/refs')) {
137                                 $project['refs'] = array();
138                                 $this->getProjectRefsRecursive($project, 'refs');
139                         }
140                 }
141                 return $project['refs'];
142         }
143 }
144
145 ?>