proposal quick fix for rebuilding search_table even if the
[platal.git] / classes / platalpage.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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->register_prefilter('form_force_encodings');
59 $this->addJsLink('xorg.js');
60 }
61
62 // }}}
63 // {{{ function changeTpl()
64
65 function changeTpl($tpl, $type = SKINNED)
66 {
67 $this->_tpl = $tpl;
68 $this->_page_type = $type;
69 $this->assign('xorg_tpl', $tpl);
70 }
71
72 // }}}
73 // {{{ function _run()
74
75 function _run($skin)
76 {
77 global $globals, $TIME_BEGIN;
78
79 session_write_close();
80
81 $this->assign('xorg_errors', $this->_errors);
82 $this->assign('xorg_failure', $this->_failure);
83 $this->assign('globals', $globals);
84
85 if (Env::v('display') == 'light') {
86 $this->_page_type = SIMPLE;
87 } elseif (Env::v('display') == 'raw') {
88 $this->_page_type = NO_SKIN;
89 } elseif (Env::v('display') == 'full') {
90 $this->_page_typ = SKINNED;
91 }
92
93 switch ($this->_page_type) {
94 case NO_SKIN:
95 error_reporting(0);
96 $this->display($this->_tpl);
97 exit;
98
99 case SIMPLE:
100 $this->assign('simple', true);
101
102 case SKINNED:
103 $this->register_modifier('escape_html', 'escape_html');
104 $this->default_modifiers = Array('@escape_html');
105 }
106 $this->register_outputfilter('hide_emails');
107 $this->addJsLink('wiki.js');
108 header("Accept-Charset: utf-8");
109
110 if (!$globals->debug) {
111 error_reporting(0);
112 $this->display($skin);
113 exit;
114 }
115
116 if ($globals->debug & 1) {
117 $this->assign('db_trace', XDB::trace_format($this, 'skin/common.database-debug.tpl'));
118 }
119
120 $this->assign('validate', true);
121 error_reporting(0);
122 $result = $this->fetch($skin);
123 $ttime = sprintf('Temps total: %.02fs<br />', microtime_float() - $TIME_BEGIN);
124 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
125
126 if ($globals->debug & 2) {
127
128 $fd = fopen($this->compile_dir."/valid.html","w");
129 fwrite($fd, $result);
130 fclose($fd);
131
132 exec($globals->spoolroot."/bin/devel/xhtml.validate.pl ".$this->compile_dir."/valid.html", $val);
133 foreach ($val as $h) {
134 if (preg_match("/^X-W3C-Validator-Errors: (\d+)$/", $h, $m)) {
135 $replc = '<span style="color: #080;">HTML OK</span><br />';
136 if ($m[1]) {
137 $replc = "<span class='erreur'><a href='http://validator.w3.org/check?uri={$globals->baseurl}"
138 ."/valid.html&amp;ss=1#result'>{$m[1]} ERREUR(S) !!!</a></span><br />";
139 }
140 break;
141 }
142 }
143 }
144
145 echo str_replace("@HOOK@", $ttime.$replc, $result);
146 exit;
147 }
148
149 // }}}
150 // {{{ function nb_errs()
151
152 function nb_errs()
153 {
154 return count($this->_errors);
155 }
156
157 // }}}
158 // {{{ function trig()
159
160 function trig($msg)
161 {
162 $this->_errors[] = $msg;
163 }
164
165 // }}}
166 // {{{ function kill()
167
168 function kill($msg)
169 {
170 global $platal;
171
172 $this->assign('platal', $platal);
173 $this->trig($msg);
174 $this->_failure = true;
175 $this->run();
176 }
177
178 // }}}
179 // {{{ function addJsLink
180
181 function addJsLink($path)
182 {
183 $this->append('xorg_js', $path);
184 }
185
186 // }}}
187 // {{{ function addCssLink
188
189 function addCssLink($path)
190 {
191 $this->append('xorg_css', $path);
192 }
193
194 // }}}
195 // {{{ function addCssInline
196
197 function addCssInline($css)
198 {
199 if (!empty($css)) {
200 $this->append('xorg_inline_css', $css);
201 }
202 }
203
204 // }}}
205 // {{{ function setRssLink
206
207 function setRssLink($title, $path)
208 {
209 $this->assign('xorg_rss', array('title' => $title, 'href' => $path));
210 }
211
212 // }}}
213 }
214
215 // {{{ function escape_html ()
216
217 /**
218 * default smarty plugin, used to auto-escape dangerous html.
219 *
220 * < --> &lt;
221 * > --> &gt;
222 * " --> &quot;
223 * & not followed by some entity --> &amp;
224 */
225 function escape_html($string)
226 {
227 if (is_string($string)) {
228 $transtbl = Array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;', '\'' => '&#39;');
229 return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/", "&amp;" , strtr($string, $transtbl));
230 } else {
231 return $string;
232 }
233 }
234
235 // }}}
236 // {{{ function at_to_globals()
237
238 /**
239 * helper
240 */
241
242 function _to_globals($s) {
243 global $globals;
244 $t = explode('.',$s);
245 if (count($t) == 1) {
246 return var_export($globals->$t[0],true);
247 } else {
248 return var_export($globals->$t[0]->$t[1],true);
249 }
250 }
251
252 /**
253 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
254 */
255
256 function at_to_globals($tpl_source, &$smarty)
257 {
258 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
259 }
260
261 // }}}
262 // {{{ function trimwhitespace
263
264 function trimwhitespace($source, &$smarty)
265 {
266 $tags = '(script|pre|textarea)';
267 preg_match_all("!<$tags.*?>.*?</$tags>!ius", $source, $tagsmatches);
268 $source = preg_replace("!<$tags.*?>.*?</$tags>!ius", "&&&tags&&&", $source);
269
270 // remove all leading spaces, tabs and carriage returns NOT
271 // preceeded by a php close tag.
272 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
273 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
274
275 return $source;
276 }
277
278 // }}}
279 // {{{
280
281 function form_force_encodings($source, &$smarty)
282 {
283 return preg_replace('/<form[^\w]/',
284 '\0 accept-charset="utf-8" ',
285 $source);
286 }
287
288 // }}}
289 // {{{ function hide_emails
290
291 function _hide_email($source)
292 {
293 $source = str_replace("\n", '', $source);
294 return '<script type="text/javascript">//<![CDATA[' . "\n" .
295 'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
296 '//]]></script>';
297 }
298
299 function hide_emails($source, &$smarty)
300 {
301 //prevent email replacement in <script> and <textarea>
302 $tags = '(script|textarea|select)';
303 preg_match_all("!<$tags.*?>.*?</$tags>!ius", $source, $tagsmatches);
304 $source = preg_replace("!<$tags.*?>.*?</$tags>!ius", "&&&tags&&&", $source);
305
306 //catch all emails in <a href="mailto:...">
307 preg_match_all("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", $source, $ahref);
308 $source = preg_replace("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", '&&&ahref&&&', $source);
309
310 //prevant replacement in tag attributes
311 preg_match_all("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!is", $source, $misc);
312 $source = preg_replace("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!is", '&&&misc&&&', $source);
313
314 //catch !
315 $source = preg_replace('!([-a-z0-9_+.]+@[-a-z0-9_.]+)!ie', '_hide_email("\1")', $source);
316 $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source);
317
318 // restore data
319 $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source);
320 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
321
322 return $source;
323 }
324
325 // }}}
326
327 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
328 ?>