continued :)
[phpgitweb.git] / htdocs / lib / ContentProvider.class.php
diff --git a/htdocs/lib/ContentProvider.class.php b/htdocs/lib/ContentProvider.class.php
new file mode 100644 (file)
index 0000000..cd88b91
--- /dev/null
@@ -0,0 +1,123 @@
+<?php
+/* ContentProvider.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/>. 
+ */
+
+class ContentProvider {
+    private static $template_cache = array();
+       private static $overall_content = array();
+       
+       private $template, $subtemplate;
+       private $content = array();
+       
+    public function __construct($template, $subtemplate, $content = null) {
+               $this->template = $template;
+               $this->subtemplate = $subtemplate;
+               
+               if(is_array($content)) {
+                       $this->content = $content;
+               }
+       }
+    
+       public function set($name, $value) {
+               $this->content[strtolower($name)] = $value;
+       }
+       
+       public function append($name, $value) {
+               if(!$value)
+                       return;
+               if(!array_key_exists(strtolower($name), $this->content))
+                       $this->content[strtolower($name)] = array();
+               $this->content[strtolower($name)][] = $value;
+       }
+       
+       public static function overall_set($name, $value) {
+               self::$overall_content[strtolower($name)] = $value;
+       }
+       
+       private function load_template($template, $subtemplate) {
+               $tpl_name = 'templates/'.(GitConfig::TEMPLATE_NAME ? GitConfig::TEMPLATE_NAME : 'default').'/'.$template.'.tpl';
+               if(!file_exists($tpl_name))     
+                       return null;
+               $tpl = file($tpl_name);
+               self::$template_cache[$template] = array();
+               
+               $cname = null;
+               foreach ($tpl as $line) {
+                       if(preg_match('/^# \[(.*)\]/i', $line, $result)) {
+                               $cname = $result[1];
+                               self::$template_cache[$template][$cname] = '';
+                       } else if($cname)
+                               self::$template_cache[$template][$cname] .= $line;
+               }
+               
+               return (array_key_exists($subtemplate, self::$template_cache[$template]) ? self::$template_cache[$template][$subtemplate] : null);
+       }
+       
+       private function replace_placeholder($result) {
+               $var = strtolower($result[1]);
+               switch($var) {
+               case "version":
+                       $rep = PHPGITWEB_VERSION;
+                       break;
+               case "year":
+                       $rep = date("Y");
+                       break;
+               case "title":
+                       $rep = GitConfig::GITWEB_TITLE;
+                       break;
+               case "rendertime":
+                       $rep = "%rendertime%"; //gets replaced later
+                       break;
+               default:
+                       if(array_key_exists($var, $this->content)) {
+                               $rep = $this->resolve_content($this->content[$var]);
+                       } else if(array_key_exists($var, self::$overall_content)) {
+                               $rep = $this->resolve_content(self::$overall_content[$var]);
+                       } else
+                               $rep = $var;
+               }
+               return $rep;
+       }
+       
+       private function resolve_content($content) {
+               $output = "";
+               if(is_array($content)) {
+                       foreach($content as $part) {
+                               $output .= $this->resolve_content($part);
+                       }
+               } elseif(is_a($content, "ContentProvider"))
+                       $output = $content->output();
+               else
+                       $output = $content;
+               return $output;
+       }
+       
+       public function output() {
+               $subtemplate = strtolower($this->subtemplate);
+               if(array_key_exists($this->template, self::$template_cache))
+                       $template_html = (array_key_exists($subtemplate, self::$template_cache[$this->template]) ? self::$template_cache[$this->template][$subtemplate] : null);
+               else {
+                       $template_html = $this->load_template($this->template, $subtemplate);
+               }
+               $template_html = preg_replace_callback('/%([^%]*)%/', array($this, "replace_placeholder"), $template_html);
+               
+               return $template_html;
+       }
+       
+}
+
+?>
\ No newline at end of file