some changes ...
[phpgitweb.git] / htdocs / lib / ContentProvider.class.php
1 <?php
2 /* ContentProvider.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 class ContentProvider {
20     private static $template_cache = array();
21         private static $overall_content = array();
22         
23         private $template, $subtemplate;
24         private $content = array();
25         
26     public function __construct($template, $subtemplate, $content = null) {
27                 $this->template = $template;
28                 $this->subtemplate = $subtemplate;
29                 
30                 if(is_array($content)) {
31                         $this->content = $content;
32                 }
33         }
34     
35         public function set($name, $value) {
36                 $this->content[strtolower($name)] = $value;
37         }
38         
39         public function append($name, $value) {
40                 if(!$value)
41                         return;
42                 if(!array_key_exists(strtolower($name), $this->content))
43                         $this->content[strtolower($name)] = array();
44                 if(!is_array($this->content[strtolower($name)]))
45                         $this->content[strtolower($name)] = array($this->content[strtolower($name)]);
46                 $this->content[strtolower($name)][] = $value;
47         }
48         
49         public static function overall_set($name, $value) {
50                 self::$overall_content[strtolower($name)] = $value;
51         }
52         
53         private function load_template($template, $subtemplate) {
54                 $tpl_name = 'templates/'.(GitConfig::TEMPLATE_NAME ? GitConfig::TEMPLATE_NAME : 'default').'/'.$template.'.tpl';
55                 if(!file_exists($tpl_name))     
56                         return null;
57                 $tpl = file($tpl_name);
58                 self::$template_cache[$template] = array();
59                 
60                 $cname = null;
61                 foreach ($tpl as $line) {
62                         if(preg_match('/^# \[(.*)\]/i', $line, $result)) {
63                                 $cname = $result[1];
64                                 self::$template_cache[$template][$cname] = '';
65                         } else if($cname)
66                                 self::$template_cache[$template][$cname] .= $line;
67                 }
68                 
69                 return (array_key_exists($subtemplate, self::$template_cache[$template]) ? self::$template_cache[$template][$subtemplate] : null);
70         }
71         
72         private function replace_placeholder($result) {
73                 $var = strtolower($result[1]);
74                 switch($var) {
75                 case "version":
76                         $rep = PHPGITWEB_VERSION;
77                         break;
78                 case "year":
79                         $rep = date("Y");
80                         break;
81                 case "rendertime":
82                         $rep = "%rendertime%"; //gets replaced later
83                         break;
84                 case "template_path":
85                         $rep = 'templates/'.(GitConfig::TEMPLATE_NAME ? GitConfig::TEMPLATE_NAME : 'default');
86                         break;
87                 default:
88                         if(array_key_exists($var, $this->content)) {
89                                 $rep = $this->resolve_content($this->content[$var]);
90                         } else if(array_key_exists($var, self::$overall_content)) {
91                                 $rep = $this->resolve_content(self::$overall_content[$var]);
92                         } else
93                                 $rep = $var;
94                 }
95                 return $rep;
96         }
97         
98         private function resolve_content($content) {
99                 $output = "";
100                 if(is_array($content)) {
101                         foreach($content as $part) {
102                                 $output .= $this->resolve_content($part);
103                         }
104                 } elseif(is_a($content, "ContentProvider"))
105                         $output = $content->output();
106                 else
107                         $output = $content;
108                 return $output;
109         }
110         
111         public function output() {
112                 $subtemplate = strtolower($this->subtemplate);
113                 if(array_key_exists($this->template, self::$template_cache))
114                         $template_html = (array_key_exists($subtemplate, self::$template_cache[$this->template]) ? self::$template_cache[$this->template][$subtemplate] : null);
115                 else {
116                         $template_html = $this->load_template($this->template, $subtemplate);
117                 }
118                 $template_html = preg_replace('/([\r\n]*)$/mD', '', $template_html);
119                 $template_html = preg_replace_callback('/%([^%\n]*)%/', array($this, "replace_placeholder"), $template_html);
120                 $template_html = preg_replace_callback('/%([^%\n]*)%/', array($this, "replace_placeholder"), $template_html);
121                 
122                 return $template_html;
123         }
124         
125 }
126
127 ?>