oops, forgotten !
[platal.git] / classes / Page.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 require_once 'smarty/libs/Smarty.class.php';
23
24 class PlatalPage extends Smarty
25 {
26 var $_page_type;
27 var $_tpl;
28 var $_errors;
29 var $_failure;
30
31 // defaults
32 var $caching = false;
33 var $config_overwrite = false;
34 var $use_sub_dirs = false;
35
36 // {{{ function PlatalPage()
37
38 function PlatalPage($tpl, $type = SKINNED)
39 {
40 global $globals;
41
42 $this->Smarty();
43
44 $this->template_dir = $globals->spoolroot."/templates/";
45 $this->compile_dir = $globals->spoolroot."/spool/templates_c/";
46 array_unshift($this->plugins_dir, $globals->spoolroot."/plugins/");
47 $this->config_dir = $globals->spoolroot."/configs/";
48
49 $this->compile_check = !empty($globals->debug);
50
51 $this->_page_type = $type;
52 $this->_tpl = $tpl;
53 $this->_errors = array();
54 $this->_failure = false;
55
56 $this->register_prefilter('at_to_globals');
57 $this->register_prefilter('trimwhitespace');
58 $this->addJsLink('xorg.js');
59 }
60
61 // }}}
62 // {{{ function changeTpl()
63
64 function changeTpl($tpl, $type = SKINNED)
65 {
66 $this->_tpl = $tpl;
67 $this->_page_type = $type;
68 $this->assign('xorg_tpl', $tpl);
69 }
70
71 // }}}
72 // {{{ function _run()
73
74 function _run($skin)
75 {
76 global $globals, $TIME_BEGIN;
77
78 session_write_close();
79
80 $this->assign("xorg_errors", $this->_errors);
81 $this->assign("xorg_failure", $this->_failure);
82
83 switch ($this->_page_type) {
84 case NO_SKIN:
85 $this->display($this->_tpl);
86 exit;
87
88 case SIMPLE:
89 $this->assign('simple', true);
90 case SKINNED:
91 $this->register_modifier('escape_html', 'escape_html');
92 $this->default_modifiers = Array('@escape_html');
93 }
94
95 if (!$globals->debug) {
96 $this->display($skin);
97 exit;
98 }
99
100 if ($globals->debug & 1) {
101 $this->assign('db_trace', XDB::trace_format($this, 'database-debug.tpl'));
102 }
103
104 $this->assign('validate', true);
105 $result = $this->fetch($skin);
106 $ttime .= sprintf('Temps total: %.02fs<br />', microtime_float() - $TIME_BEGIN);
107 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
108
109 if ($globals->debug & 2) {
110
111 $fd = fopen($this->compile_dir."/valid.html","w");
112 fwrite($fd, $result);
113 fclose($fd);
114
115 exec($globals->spoolroot."/bin/devel/xhtml.validate.pl ".$this->compile_dir."/valid.html", $val);
116 foreach ($val as $h) {
117 if (preg_match("/^X-W3C-Validator-Errors: (\d+)$/", $h, $m)) {
118 $replc = '<span style="color: #080;">HTML OK</span><br />';
119 if ($m[1]) {
120 $replc = "<span class='erreur'><a href='http://validator.w3.org/check?uri={$globals->baseurl}"
121 ."/valid.html&amp;ss=1#result'>{$m[1]} ERREUR(S) !!!</a></span><br />";
122 }
123 break;
124 }
125 }
126 }
127
128 echo str_replace("@HOOK@", $ttime.$replc, $result);
129 exit;
130 }
131
132 // }}}
133 // {{{ function nb_errs()
134
135 function nb_errs()
136 {
137 return count($this->_errors);
138 }
139
140 // }}}
141 // {{{ function trig()
142
143 function trig($msg)
144 {
145 $this->_errors[] = $msg;
146 }
147
148 // }}}
149 // {{{ function kill()
150
151 function kill($msg)
152 {
153 $this->trig($msg);
154 $this->_failure = true;
155 $this->run();
156 }
157
158 // }}}
159 // {{{ function addJsLink
160
161 function addJsLink($path)
162 {
163 $this->append('xorg_js', $path);
164 }
165
166 // }}}
167 // {{{ function addCssLink
168
169 function addCssLink($path)
170 {
171 $this->append('xorg_css', $path);
172 }
173
174 // }}}
175 }
176
177 // {{{ function escape_html ()
178
179 /**
180 * default smarty plugin, used to auto-escape dangerous html.
181 *
182 * < --> &lt;
183 * > --> &gt;
184 * " --> &quot;
185 * & not followed by some entity --> &amp;
186 */
187 function escape_html($string)
188 {
189 if (is_string($string)) {
190 $transtbl = Array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;', '\'' => '&#39;');
191 return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/", "&amp;" , strtr($string, $transtbl));
192 } else {
193 return $string;
194 }
195 }
196
197 // }}}
198 // {{{ function at_to_globals()
199
200 /**
201 * helper
202 */
203
204 function _to_globals($s) {
205 global $globals;
206 $t = explode('.',$s);
207 if (count($t) == 1) {
208 return var_export($globals->$t[0],true);
209 } else {
210 return var_export($globals->$t[0]->$t[1],true);
211 }
212 }
213
214 /**
215 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
216 */
217
218 function at_to_globals($tpl_source, &$smarty)
219 {
220 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
221 }
222
223 // }}}
224 // {{{ function trimwhitespace
225
226 function trimwhitespace($source, &$smarty)
227 {
228 $tags = array('script', 'pre', 'textarea');
229
230 foreach ($tags as $tag) {
231 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, ${$tag});
232 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
233 }
234
235 // remove all leading spaces, tabs and carriage returns NOT
236 // preceeded by a php close tag.
237 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
238
239 foreach ($tags as $tag) {
240 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
241 }
242
243 return $source;
244 }
245
246 // }}}
247
248 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
249 ?>