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