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