76b2eb0f1e1a508053a9df9d46d1c8048fbd703a
[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                 default:
85                         if(array_key_exists($var, $this->content)) {
86                                 $rep = $this->resolve_content($this->content[$var]);
87                         } else if(array_key_exists($var, self::$overall_content)) {
88                                 $rep = $this->resolve_content(self::$overall_content[$var]);
89                         } else
90                                 $rep = $var;
91                 }
92                 return $rep;
93         }
94         
95         private function resolve_content($content) {
96                 $output = "";
97                 if(is_array($content)) {
98                         foreach($content as $part) {
99                                 $output .= $this->resolve_content($part);
100                         }
101                 } elseif(is_a($content, "ContentProvider"))
102                         $output = $content->output();
103                 else
104                         $output = $content;
105                 return $output;
106         }
107         
108         public function output() {
109                 $subtemplate = strtolower($this->subtemplate);
110                 if(array_key_exists($this->template, self::$template_cache))
111                         $template_html = (array_key_exists($subtemplate, self::$template_cache[$this->template]) ? self::$template_cache[$this->template][$subtemplate] : null);
112                 else {
113                         $template_html = $this->load_template($this->template, $subtemplate);
114                 }
115                 $template_html = preg_replace_callback('/%([^%]*)%/', array($this, "replace_placeholder"), $template_html);
116                 $template_html = preg_replace_callback('/%([^%]*)%/', array($this, "replace_placeholder"), $template_html);
117                 
118                 return $template_html;
119         }
120         
121 }
122
123 ?>