. */ 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(); if(!is_array($this->content[strtolower($name)])) $this->content[strtolower($name)] = array($this->content[strtolower($name)]); $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 "rendertime": $rep = "%rendertime%"; //gets replaced later break; case "template_path": $rep = 'templates/'.(GitConfig::TEMPLATE_NAME ? GitConfig::TEMPLATE_NAME : 'default'); 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('/([\r\n]*)$/mD', '', $template_html); $template_html = preg_replace_callback('/%([^%\n]*)%/', array($this, "replace_placeholder"), $template_html); $template_html = preg_replace_callback('/%([^%\n]*)%/', array($this, "replace_placeholder"), $template_html); return $template_html; } } ?>