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