cd88b91e8b025b8e7347f849bc41f7fa22a8ce25
[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                 $this->content[strtolower($name)][] = $value;
45         }
46         
47         public static function overall_set($name, $value) {
48                 self::$overall_content[strtolower($name)] = $value;
49         }
50         
51         private function load_template($template, $subtemplate) {
52                 $tpl_name = 'templates/'.(GitConfig::TEMPLATE_NAME ? GitConfig::TEMPLATE_NAME : 'default').'/'.$template.'.tpl';
53                 if(!file_exists($tpl_name))     
54                         return null;
55                 $tpl = file($tpl_name);
56                 self::$template_cache[$template] = array();
57                 
58                 $cname = null;
59                 foreach ($tpl as $line) {
60                         if(preg_match('/^# \[(.*)\]/i', $line, $result)) {
61                                 $cname = $result[1];
62                                 self::$template_cache[$template][$cname] = '';
63                         } else if($cname)
64                                 self::$template_cache[$template][$cname] .= $line;
65                 }
66                 
67                 return (array_key_exists($subtemplate, self::$template_cache[$template]) ? self::$template_cache[$template][$subtemplate] : null);
68         }
69         
70         private function replace_placeholder($result) {
71                 $var = strtolower($result[1]);
72                 switch($var) {
73                 case "version":
74                         $rep = PHPGITWEB_VERSION;
75                         break;
76                 case "year":
77                         $rep = date("Y");
78                         break;
79                 case "title":
80                         $rep = GitConfig::GITWEB_TITLE;
81                         break;
82                 case "rendertime":
83                         $rep = "%rendertime%"; //gets replaced later
84                         break;
85                 default:
86                         if(array_key_exists($var, $this->content)) {
87                                 $rep = $this->resolve_content($this->content[$var]);
88                         } else if(array_key_exists($var, self::$overall_content)) {
89                                 $rep = $this->resolve_content(self::$overall_content[$var]);
90                         } else
91                                 $rep = $var;
92                 }
93                 return $rep;
94         }
95         
96         private function resolve_content($content) {
97                 $output = "";
98                 if(is_array($content)) {
99                         foreach($content as $part) {
100                                 $output .= $this->resolve_content($part);
101                         }
102                 } elseif(is_a($content, "ContentProvider"))
103                         $output = $content->output();
104                 else
105                         $output = $content;
106                 return $output;
107         }
108         
109         public function output() {
110                 $subtemplate = strtolower($this->subtemplate);
111                 if(array_key_exists($this->template, self::$template_cache))
112                         $template_html = (array_key_exists($subtemplate, self::$template_cache[$this->template]) ? self::$template_cache[$this->template][$subtemplate] : null);
113                 else {
114                         $template_html = $this->load_template($this->template, $subtemplate);
115                 }
116                 $template_html = preg_replace_callback('/%([^%]*)%/', array($this, "replace_placeholder"), $template_html);
117                 
118                 return $template_html;
119         }
120         
121 }
122
123 ?>