continued :)
[phpgitweb.git] / htdocs / lib / PHPGitWeb.class.php
1 <?php
2 /* GITCore.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 define("PHPGITWEB_VERSION", "0.0.1");
20 require_once("lib/ContentProvider.class.php");
21 require_once("lib/ProjectLoader.class.php");
22 require_once("lib/CommitLoader.class.php");
23 require_once("lib/GitCommand.class.php");
24 require_once("lib/Tools.class.php");
25 require_once("lib/Validation.class.php");
26 require_once("lib/graph.class.php");
27
28 class PHPGitWeb {
29     private $page, $rendertime;
30         private $project, $project_loader, $project_header;
31         
32     public function __construct() {
33                 $this->rendertime = microtime(true);
34                 $this->page = new ContentProvider('main', 'main');
35                 set_error_handler(array($this, "error_handler"));
36                 $this->append_header_nav('projects', '?a=projects', false);
37                 $this->page->set('git_version', GitCommand::core_version());
38                 
39                 $this->project_loader = new ProjectLoader();
40                 $this->page->append('title', GitConfig::GITWEB_TITLE);
41         }
42         
43         public function get_project_loader() {
44                 return $this->project_loader;
45         }
46         
47         public function load_project($project) {
48                 $this->project = $this->project_loader->getProject($project);
49                 
50                 if(!$this->project)
51                         $this->page->append('content', new ContentProvider('main', 'err404_project'));
52                 else {
53                         ContentProvider::overall_set('project', $this->project['name']);
54                         ContentProvider::overall_set('project_head', "HEAD");
55                         $this->append_header_nav($this->project['name'], '?p='.$this->project['name'].'&a=summary', true);
56                         $this->append_title(" - ".$this->project['name'], false);
57                         $this->project_header = new ContentProvider('main', 'project_header');
58                         $this->project_header->set('sub_nav', "");
59                         $this->page->append('content', $this->project_header);
60                 }
61         }
62         
63         public function get_project_header() {
64                 return $this->project_header;
65         }
66         
67         public function load_content($page) {
68                 $class_name = 'page_'.basename($page);
69                 $class_file = 'pages/'.basename($page).'.class.php';
70                 $static_file = 'pages/'.basename($page).'.html';
71                 if(file_exists($class_file)) {
72                         require_once($class_file);
73                         $pageobj = new $class_name;
74                         $this->page->append('content', $pageobj->main($this, $this->project));
75                 } else if(file_exists($static_file)) {
76                         $this->page->append('content', file_get_contents($static_file));
77                 } else
78                         $this->page->append('content', file_get_contents('pages/404.html'));
79                 if($this->project_header) {
80                         $this->project_header->set('nav_summary',    new ContentProvider('main', ($page == 'summary'    ? 'project_header_nav_active' : 'project_header_nav_link'), array('name' => "summary",    'link' => "summary")));
81                         $this->project_header->set('nav_shortlog',   new ContentProvider('main', ($page == 'shortlog'   ? 'project_header_nav_active' : 'project_header_nav_link'), array('name' => "shortlog",   'link' => "shortlog")));
82                         $this->project_header->set('nav_log',        new ContentProvider('main', ($page == 'log'        ? 'project_header_nav_active' : 'project_header_nav_link'), array('name' => "log",        'link' => "log")));
83                         $this->project_header->set('nav_commit',     new ContentProvider('main', ($page == 'commit'     ? 'project_header_nav_active' : 'project_header_nav_link'), array('name' => "commit",     'link' => "commit")));
84                         $this->project_header->set('nav_commitdiff', new ContentProvider('main', ($page == 'commitdiff' ? 'project_header_nav_active' : 'project_header_nav_link'), array('name' => "commitdiff", 'link' => "commitdiff")));
85                         $this->project_header->set('nav_tree',       new ContentProvider('main', ($page == 'tree'       ? 'project_header_nav_active' : 'project_header_nav_link'), array('name' => "tree",       'link' => "tree")));
86                 }
87         }
88         
89         public function append_header_nav($name, $link, $prepend_slash = true) {
90                 if($prepend_slash)
91                         $this->page->append('header_nav', ' / ');
92                 if($link) 
93                         $this->page->append('header_nav', '<a href="'.$link.'">'.$name.'</a>');
94                 else
95                         $this->page->append('header_nav', $name);
96         }
97         
98         public function append_title($name, $prepend_slash = true) {
99                 if($prepend_slash)
100                         $this->page->append('title', '/');
101                 $this->page->append('title', $name);
102         }
103         
104         public function append_sub_nav($html) {
105                 if($this->project_header)
106                         $this->project_header->append('sub_nav', $html);
107         }
108         
109         public function load_extension($extension) {
110                 switch($extension) {
111                 case "graph":
112                         $graph = new graph_image_generator();
113                         $graph->generate($_GET['gd']);
114                         break;
115                 }
116         }
117         
118         public function get_content() {
119                 $html = $this->page->output();
120                 $rendertime = round(microtime(true) - $this->rendertime,3);
121                 $html = str_replace(array("%rendertime%"), array($rendertime), $html);
122                 return $html;
123         }
124     
125         public function error_handler($errno, $errstr, $errfile, $errline) {
126                 $error = new ContentProvider('main', 'error');
127                 $etype = "";
128                 switch($errno) {
129                 case E_ERROR: $etype = "E_ERROR"; break;
130                 case E_WARNING: $etype = "E_WARNING"; break;
131                 case E_PARSE: $etype = "E_PARSE"; break;
132                 case E_NOTICE: $etype = "E_NOTICE"; break;
133                 case E_CORE_ERROR: $etype = "E_CORE_ERROR"; break;
134                 case E_CORE_WARNING: $etype = "E_CORE_WARNING"; break;
135                 case E_COMPILE_ERROR: $etype = "E_COMPILE_ERROR"; break;
136                 case E_COMPILE_WARNING: $etype = "E_COMPILE_WARNING"; break;
137                 case E_USER_ERROR: $etype = "E_USER_ERROR"; break;
138                 case E_USER_WARNING: $etype = "E_USER_WARNING"; break;
139                 case E_USER_NOTICE: $etype = "E_USER_NOTICE"; break;
140                 case E_STRICT: $etype = "E_STRICT"; break;
141                 case E_RECOVERABLE_ERROR: $etype = "E_RECOVERABLE_ERROR"; break;
142                 case E_DEPRECATED: $etype = "E_DEPRECATED"; break;
143                 case E_USER_DEPRECATED: $etype = "E_USER_DEPRECATED"; break;
144                 }
145                 $error->set('errno', $errno);
146                 $error->set('errtype', $etype);
147                 $error->set('errstr', $errstr);
148                 $error->set('errfile', $errfile);
149                 $error->set('errline', $errline);
150                 $this->page->append('content', $error);
151         }
152 }
153
154 ?>