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