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