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