continued :)
[phpgitweb.git] / htdocs / lib / PHPGitWeb.class.php
diff --git a/htdocs/lib/PHPGitWeb.class.php b/htdocs/lib/PHPGitWeb.class.php
new file mode 100644 (file)
index 0000000..06ae9b0
--- /dev/null
@@ -0,0 +1,138 @@
+<?php
+/* GITCore.class.php - phpgitweb
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+define("PHPGITWEB_VERSION", "0.0.1");
+require_once("lib/ContentProvider.class.php");
+require_once("lib/ProjectLoader.class.php");
+require_once("lib/GitCommand.class.php");
+require_once("lib/Tools.class.php");
+require_once("lib/graph.class.php");
+
+class PHPGitWeb {
+    private $page, $rendertime;
+       private $project, $project_loader, $project_header;
+       
+    public function __construct() {
+               $this->rendertime = microtime(true);
+               $this->page = new ContentProvider('main', 'main');
+               set_error_handler(array($this, "error_handler"));
+               $this->append_header_nav('projects', '?a=projects', false);
+               $this->page->set('git_version', GitCommand::core_version());
+               
+               $this->project_loader = new ProjectLoader();
+       }
+       
+       public function get_project_loader() {
+               return $this->project_loader;
+       }
+       
+       public function load_project($project) {
+               $this->project = $this->project_loader->getProject($project);
+               
+               if(!$this->project)
+                       $this->page->append('content', new ContentProvider('main', 'project_error'));
+               else {
+                       ContentProvider::overall_set('project', $this->project['name']);
+                       ContentProvider::overall_set('project_head', "HEAD");
+                       $this->project_header = new ContentProvider('main', 'project_header');
+                       $this->project_header->set('sub_nav', "");
+                       $this->page->append('content', $this->project_header);
+               }
+       }
+       
+       public function get_project_header() {
+               return $this->project_header;
+       }
+       
+       public function load_content($page) {
+               $class_name = 'page_'.basename($page);
+               $class_file = 'pages/'.basename($page).'.class.php';
+               $static_file = 'pages/'.basename($page).'.html';
+               if(file_exists($class_file)) {
+                       require_once($class_file);
+                       $pageobj = new $class_name;
+                       $this->page->append('content', $pageobj->main($this, $this->project));
+               } else if(file_exists($static_file)) {
+                       $this->page->append('content', file_get_contents($static_file));
+               } else
+                       $this->page->append('content', file_get_contents('pages/404.html'));
+               if($this->project_header) {
+                       $this->project_header->set('nav_summary',    new ContentProvider('main', ($page == 'summary'    ? 'project_header_nav_active' : 'project_header_nav_link'), array('name' => "summary",    'link' => "summary")));
+                       $this->project_header->set('nav_shortlog',   new ContentProvider('main', ($page == 'shortlog'   ? 'project_header_nav_active' : 'project_header_nav_link'), array('name' => "shortlog",   'link' => "shortlog")));
+                       $this->project_header->set('nav_log',        new ContentProvider('main', ($page == 'log'        ? 'project_header_nav_active' : 'project_header_nav_link'), array('name' => "log",        'link' => "log")));
+                       $this->project_header->set('nav_commit',     new ContentProvider('main', ($page == 'commit'     ? 'project_header_nav_active' : 'project_header_nav_link'), array('name' => "commit",     'link' => "commit")));
+                       $this->project_header->set('nav_commitdiff', new ContentProvider('main', ($page == 'commitdiff' ? 'project_header_nav_active' : 'project_header_nav_link'), array('name' => "commitdiff", 'link' => "commitdiff")));
+                       $this->project_header->set('nav_tree',       new ContentProvider('main', ($page == 'tree'       ? 'project_header_nav_active' : 'project_header_nav_link'), array('name' => "tree",       'link' => "tree")));
+               }
+       }
+       
+       public function append_header_nav($name, $link, $prepend_slash = true) {
+               if($prepend_slash)
+                       $this->page->append('header_nav', ' / ');
+               if($link) 
+                       $this->page->append('header_nav', '<a href="'.$link.'">'.$name.'</a>');
+               else
+                       $this->page->append('header_nav', $name);
+       }
+       
+       public function load_extension($extension) {
+               switch($extension) {
+               case "graph":
+                       $graph = new graph_image_generator();
+                       $graph->generate($_GET['gd']);
+                       break;
+               }
+       }
+       
+       public function get_content() {
+               $html = $this->page->output();
+               $rendertime = round(microtime(true) - $this->rendertime,3);
+               $html = str_replace(array("%rendertime%"), array($rendertime), $html);
+               return $html;
+       }
+    
+       public function error_handler($errno, $errstr, $errfile, $errline) {
+               $error = new ContentProvider('main', 'error');
+               $etype = "";
+               switch($errno) {
+               case E_ERROR: $etype = "E_ERROR"; break;
+               case E_WARNING: $etype = "E_WARNING"; break;
+               case E_PARSE: $etype = "E_PARSE"; break;
+               case E_NOTICE: $etype = "E_NOTICE"; break;
+               case E_CORE_ERROR: $etype = "E_CORE_ERROR"; break;
+               case E_CORE_WARNING: $etype = "E_CORE_WARNING"; break;
+               case E_COMPILE_ERROR: $etype = "E_COMPILE_ERROR"; break;
+               case E_COMPILE_WARNING: $etype = "E_COMPILE_WARNING"; break;
+               case E_USER_ERROR: $etype = "E_USER_ERROR"; break;
+               case E_USER_WARNING: $etype = "E_USER_WARNING"; break;
+               case E_USER_NOTICE: $etype = "E_USER_NOTICE"; break;
+               case E_STRICT: $etype = "E_STRICT"; break;
+               case E_RECOVERABLE_ERROR: $etype = "E_RECOVERABLE_ERROR"; break;
+               case E_DEPRECATED: $etype = "E_DEPRECATED"; break;
+               case E_USER_DEPRECATED: $etype = "E_USER_DEPRECATED"; break;
+               }
+               $error->set('errno', $errno);
+               $error->set('errtype', $etype);
+               $error->set('errstr', $errstr);
+               $error->set('errfile', $errfile);
+               $error->set('errline', $errline);
+               $this->page->append('content', $error);
+       }
+}
+
+?>
\ No newline at end of file