Fix another notice.
[platal.git] / classes / plpage.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
a7f778a5 3 * Copyright (C) 2003-2009 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
5640f093
FB
22if (!@include_once 'smarty/libs/Smarty.class.php') {
23 require_once 'smarty/Smarty.class.php';
24}
0337d704 25
04334c61 26abstract class PlPage extends Smarty
0337d704 27{
2b1ee50b 28 private $_page_type;
29 private $_tpl;
30 private $_errors;
31 private $_failure;
4a8a1e0a 32 private $_jsonVars;
0337d704 33
04334c61 34 // {{{ function PlPage()
0337d704 35
abde67b1 36 public function __construct()
0337d704 37 {
2b1ee50b 38 parent::Smarty();
0337d704 39
2b1ee50b 40 global $globals;
796aea34 41
abe7e055 42 $this->caching = false;
43 $this->config_overwrite = false;
44 $this->use_sub_dirs = false;
a33955e4
FB
45 $this->template_dir = $globals->spoolroot . '/templates/';
46 $this->compile_dir = $globals->spoolroot . '/spool/templates_c/';
47 array_unshift($this->plugins_dir,
48 $globals->spoolroot . '/core/plugins/',
49 $globals->spoolroot . '/plugins/');
50 $this->config_dir = $globals->spoolroot . '/configs/';
0337d704 51
52 $this->compile_check = !empty($globals->debug);
53
71a753d3 54 $this->_errors = array('errors' => array());
4a8a1e0a 55 $this->_jsonVars = array();
860fdaec 56 $this->_failure = false;
0337d704 57
fe556813 58 if ($globals->mode != 'rw') {
d27380cd
AA
59 $this->trigError("En raison d'une maintenance, une partie des fonctionnalités du site est"
60 . " actuellement désactivée, en particulier aucune donnée ne sera sauvegardée");
fe556813 61 }
0337d704 62 $this->register_prefilter('at_to_globals');
4b4b4b67 63 $this->addJsLink('jquery.js');
0337d704 64 }
65
66 // }}}
67 // {{{ function changeTpl()
68
2b1ee50b 69 public function changeTpl($tpl, $type = SKINNED)
0337d704 70 {
91ebb7ff
FB
71 $this->_tpl = $tpl;
72 $this->_page_type = $type;
73 $this->assign('pl_tpl', $tpl);
0337d704 74 }
75
76 // }}}
7cb40d85
FB
77 // {{{ function getCoreTpl()
78
79 public static function getCoreTpl($tpl)
80 {
81 global $globals;
82 return $globals->spoolroot . '/core/templates/' . $tpl;
83 }
84
85 // }}}
86 // {{{ function coreTpl()
87
88 /** Use a template from the core.
89 */
90 public function coreTpl($tpl, $type = SKINNED)
91 {
92 global $globals;
93 $this->changeTpl(self::getCoreTpl($tpl), $type);
94 }
95
96 // }}}
e654517d 97 // {{{ function raw()
98
99 public function raw()
100 {
101 global $globals;
102 $this->assign('globals', $globals);
103 return $this->fetch($this->_tpl);
104 }
105
106 // }}}
0337d704 107 // {{{ function _run()
108
2b1ee50b 109 protected function _run($skin)
0337d704 110 {
80d0423e 111 global $globals, $platal, $TIME_BEGIN;
36f114ef 112
732e5855 113 Platal::session()->close();
36f114ef 114
e654517d 115 $this->register_prefilter('trimwhitespace');
116 $this->register_prefilter('form_force_encodings');
7a3f546b 117 $this->register_prefilter('wiki_include');
7cb40d85 118 $this->register_prefilter('core_include');
b6ec30c5 119 $this->register_prefilter('if_rewrites');
91ebb7ff
FB
120 $this->assign('pl_triggers', $this->_errors);
121 $this->assign('pl_errors', $this->nb_errs());
122 $this->assign('pl_failure', $this->_failure);
80d0423e 123 $this->assign_by_ref('platal', $platal);
08d7cc45 124 $this->assign_by_ref('globals', $globals);
a3a049fc 125
4a8a1e0a 126 if (Env::has('json') && count($this->_jsonVars)) {
127 return $this->jsonDisplay();
128 }
eaf30d86 129
8a105df2 130 if (Env::v('display') == 'light') {
131 $this->_page_type = SIMPLE;
132 } elseif (Env::v('display') == 'raw') {
133 $this->_page_type = NO_SKIN;
134 } elseif (Env::v('display') == 'full') {
135 $this->_page_typ = SKINNED;
136 }
137
62a66dfc 138 switch ($this->_page_type) {
139 case NO_SKIN:
81e9c63f 140 if (!($globals->debug & DEBUG_SMARTY)) {
141 error_reporting(0);
142 }
0337d704 143 $this->display($this->_tpl);
144 exit;
62a66dfc 145
146 case SIMPLE:
c6db254a 147 $this->assign('simple', true);
bf2e1ab0 148
62a66dfc 149 case SKINNED:
b4ee37a4
FB
150 $this->register_modifier('escape_html', 'escape_html');
151 $this->default_modifiers = Array('@escape_html');
0337d704 152 }
5aa58639
FB
153 if (S::i('auth') <= AUTH_PUBLIC) {
154 $this->register_outputfilter('hide_emails');
155 }
bf2e1ab0 156 $this->addJsLink('wiki.js');
493b6abe 157 header("Accept-Charset: utf-8");
4a8a1e0a 158 if (Env::v('forceXml')) {
159 header("Content-Type: text/xml; charset=utf-8");
160 }
b4315e15 161
162 if (!$globals->debug) {
6995a9b9 163 error_reporting(0);
b4315e15 164 $this->display($skin);
165 exit;
166 }
a3a049fc 167
7da8ef90 168 $this->assign('validate', true);
81e9c63f 169 if (!($globals->debug & DEBUG_SMARTY)) {
170 error_reporting(0);
171 }
040a594c 172 $START_SMARTY = microtime(true);
b4315e15 173 $result = $this->fetch($skin);
040a594c
FB
174 $ttime = sprintf('Temps total: %.02fs (Smarty %.02fs) - Mémoire totale : %dKo<br />',
175 microtime(true) - $TIME_BEGIN, microtime(true) - $START_SMARTY,
176 memory_get_peak_usage(true) / 1024);
b4ee37a4
FB
177 if ($globals->debug & DEBUG_BT) {
178 PlBacktrace::clean();
179 $this->assign_by_ref('backtraces', PlBacktrace::$bt);
7cb40d85
FB
180 $result = str_replace('@@BACKTRACE@@',
181 $this->fetch(self::getCoreTpl('backtrace.tpl')),
182 $result);
b4ee37a4
FB
183 } else {
184 $result = str_replace('@@BACKTRACE@@', '', $result);
185 }
0337d704 186
b4ee37a4 187 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
81e9c63f 188 if ($globals->debug & DEBUG_VALID) {
b4315e15 189 $fd = fopen($this->compile_dir."/valid.html","w");
190 fwrite($fd, $result);
191 fclose($fd);
a3a049fc 192
b4315e15 193 exec($globals->spoolroot."/bin/devel/xhtml.validate.pl ".$this->compile_dir."/valid.html", $val);
194 foreach ($val as $h) {
195 if (preg_match("/^X-W3C-Validator-Errors: (\d+)$/", $h, $m)) {
196 $replc = '<span style="color: #080;">HTML OK</span><br />';
197 if ($m[1]) {
198 $replc = "<span class='erreur'><a href='http://validator.w3.org/check?uri={$globals->baseurl}"
199 ."/valid.html&amp;ss=1#result'>{$m[1]} ERREUR(S) !!!</a></span><br />";
0337d704 200 }
b4315e15 201 break;
0337d704 202 }
203 }
0337d704 204 }
205
b4315e15 206 echo str_replace("@HOOK@", $ttime.$replc, $result);
0337d704 207 exit;
208 }
209
1490093c 210 abstract public function run();
211
0337d704 212 // }}}
0337d704 213 // {{{ function nb_errs()
214
2b1ee50b 215 public function nb_errs()
0337d704 216 {
71a753d3 217 return count($this->_errors['errors']);
0337d704 218 }
219
220 // }}}
221 // {{{ function trig()
222
a7d35093 223 private function trig($msg, $type = 'errors')
0337d704 224 {
71a753d3
FB
225 if (!isset($this->_errors[$type])) {
226 $this->_errors[$type] = array();
227 }
228 $this->_errors[$type][] = $msg;
229 }
230
231 public function trigError($msg)
232 {
233 $this->trig($msg, 'errors');
234 }
235
e00bc67e
FB
236 public function trigWarning($msg)
237 {
238 $this->trig($msg, 'warnings');
239 }
240
71a753d3
FB
241 public function trigSuccess($msg)
242 {
243 $this->trig($msg, 'success');
0337d704 244 }
245
246 // }}}
0337d704 247 // {{{ function kill()
248
ca476935 249 public function kill($msg, $type = 'errors')
0337d704 250 {
36f472dc
FB
251 // PHP is used on command line... do not run the whole page stuff.
252 if (php_sapi_name() == 'cli') {
253 echo $msg . "\n";
254 exit;
255 }
256
90eba1aa 257 global $platal;
258
ca476935 259 $this->trig($msg, $type);
0fcbe8d0 260 $this->_failure = true;
0337d704 261 $this->run();
262 }
263
ca476935
SJ
264 public function killError($msg)
265 {
266 $this->kill($msg, 'errors');
267 }
268
269 public function killWarning($msg)
270 {
271 $this->kill($msg, 'warnings');
272 }
273
274 public function killSuccess($msg)
275 {
276 $this->kill($msg, 'success');
277 }
278
0337d704 279 // }}}
9902e47b
FB
280 // {{{ function setTitle
281
282 public function setTitle($title)
283 {
380e2a5a
FB
284 global $globals;
285 if (isset($globals->core->sitename)) {
286 $title = $globals->core->sitename . ' :: ' . $title;
287 }
9902e47b
FB
288 $this->assign('pl_title', $title);
289 }
290
291 // }}}
0337d704 292 // {{{ function addJsLink
293
2b1ee50b 294 public function addJsLink($path)
0337d704 295 {
91ebb7ff 296 $this->append('pl_js', $path);
0337d704 297 }
298
299 // }}}
300 // {{{ function addCssLink
301
2b1ee50b 302 public function addCssLink($path)
0337d704 303 {
91ebb7ff 304 $this->append('pl_css', $path);
0337d704 305 }
306
307 // }}}
1e9a1d99
AA
308 // {{{ function addLink
309
310 public function addLink($rel, $path)
311 {
312 $this->append('pl_link', array('rel' => $rel, 'href' => $path));
313 }
314
315
316 // }}}
ea626742 317 // {{{ function addCssInline
318
2b1ee50b 319 public function addCssInline($css)
ea626742 320 {
321 if (!empty($css)) {
91ebb7ff 322 $this->append('pl_inline_css', $css);
eaf30d86 323 }
ea626742 324 }
325
326 // }}}
162370e7 327 // {{{ function setRssLink
328
2b1ee50b 329 public function setRssLink($title, $path)
162370e7 330 {
91ebb7ff 331 $this->assign('pl_rss', array('title' => $title, 'href' => $path));
162370e7 332 }
333
334 // }}}
4a8a1e0a 335 // {{{ function jsonDisplay
336 protected function jsonDisplay()
337 {
338 header("Content-type: text/javascript; charset=utf-8");
339 array_walk_recursive($this->_jsonVars, "escape_xorgDB");
340 $jsonbegin = Env::v('jsonBegin');
341 $jsonend = Env::v('jsonEnd');
342 if (Env::has('jsonVar')) {
343 $jsonbegin = Env::v('jsonVar').' = ';
344 $jsonend = ';';
345 } elseif (Env::has('jsonFunc')) {
346 $jsonbegin = Env::v('jsonFunc').'(';
347 $jsonend = ');';
348 }
349 echo $jsonbegin, json_encode($this->_jsonVars), $jsonend;
350 exit;
351 }
352 // }}}
353 // {{{ function jsonAssign
354 public function jsonAssign($var, $value)
355 {
356 $this->_jsonVars[$var] = $value;
357 }
fba760d2 358
359 // }}}
4a8a1e0a 360}
361
362function escape_xorgDB(&$item, $key)
363{
7cb40d85 364 if ($item instanceof XOrgDBIterator) {
4a8a1e0a 365 $expanded = array();
366 while ($a = $item->next()) {
367 $expanded[] = $a;
368 }
369 $item = $expanded;
370 }
0337d704 371}
372
b76f0797 373// {{{ function escape_html ()
374
375/**
376 * default smarty plugin, used to auto-escape dangerous html.
eaf30d86 377 *
b76f0797 378 * < --> &lt;
379 * > --> &gt;
380 * " --> &quot;
381 * & not followed by some entity --> &amp;
382 */
383function escape_html($string)
384{
385 if (is_string($string)) {
c1895524 386 return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
b76f0797 387 } else {
c1895524 388 return $string;
b76f0797 389 }
390}
391
392// }}}
393// {{{ function at_to_globals()
394
395/**
396 * helper
397 */
398
399function _to_globals($s) {
400 global $globals;
401 $t = explode('.',$s);
402 if (count($t) == 1) {
403 return var_export($globals->$t[0],true);
404 } else {
405 return var_export($globals->$t[0]->$t[1],true);
406 }
407}
408
409/**
410 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
411 */
412
413function at_to_globals($tpl_source, &$smarty)
414{
415 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
7cb40d85 416}
b76f0797 417
7cb40d85
FB
418// }}}
419// {{{ function trimwhitespace
b76f0797 420
7cb40d85
FB
421function trimwhitespace($source, &$smarty)
422{
423 $tags = '(script|pre|textarea)';
424 preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
425 $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
b76f0797 426
7cb40d85
FB
427 // remove all leading spaces, tabs and carriage returns NOT
428 // preceeded by a php close tag.
429 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
430 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
b76f0797 431
7cb40d85
FB
432 return $source;
433}
b76f0797 434
7cb40d85
FB
435// }}}
436// {{{ function wiki_include
7a3f546b 437
438function wiki_include($source, &$smarty)
439{
7cb40d85 440 global $globals;
7a3f546b 441 return preg_replace('/\{include( [^}]*)? wiki=([^} ]+)(.*?)\}/ui',
7cb40d85
FB
442 '{include\1 file="' . $globals->spoolroot . '/spool/wiki.d/cache_\2.tpl"\3 included=1}',
443 $source);
444}
445
446function core_include($source, &$smarty)
447{
448 global $globals;
449 return preg_replace('/\{include( [^}]*)? core=([^} ]+)(.*?)\}/ui',
450 '{include\1 file="' . $globals->spoolroot . '/core/templates/\2"\3}',
7a3f546b 451 $source);
452}
453
454// }}}
908db125
FB
455//{{{ function hasPerm
456
b6ec30c5 457function if_rewrites($source, &$smarty)
908db125 458{
b6ec30c5
FB
459 $perms = 'isset($smarty.session.perms|smarty:nodefaults) && $smarty.session.perms|smarty:nodefaults && $smarty.session.perms';
460 return preg_replace(array('/\{if([^}]*) (\!?)hasPerms\(([^)]+)\)([^}]*)\}/',
461 '/\{if([^}]*) (\!?)hasPerm\(([^)]+)\)([^}]*)\}/',
462 '/\{if([^}]*) (\!?)t\(([^)]+)\)([^}]*)\}/'),
463 array('{if\1 \2(' . $perms . '->hasFlagCombination(\3))\4}',
464 '{if\1 \2(' . $perms . '->hasFlag(\3))\4}',
465 '{if\1 \2(isset(\3|smarty:nodefaults) && (\3|smarty:nodefaults))\4}'),
7cb40d85 466 $source);
908db125
FB
467}
468
469// }}}
edc636b8 470// {{{
471
472function form_force_encodings($source, &$smarty)
473{
474 return preg_replace('/<form[^\w]/',
a7de4ef7 475 '\0 accept-charset="utf-8" ',
edc636b8 476 $source);
477}
478
479// }}}
bf2e1ab0 480// {{{ function hide_emails
481
482function _hide_email($source)
483{
1bd2bc7e 484 $source = str_replace("\n", '', $source);
6b8d257b 485 return '<script type="text/javascript">//<![CDATA[' . "\n" .
91ebb7ff
FB
486 'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
487 '//]]></script>';
bf2e1ab0 488}
489
490function hide_emails($source, &$smarty)
491{
a18afbdc 492 if (!strpos($source, '@')) {
493 return $source;
494 }
495
bf2e1ab0 496 //prevent email replacement in <script> and <textarea>
120bd636 497 $tags = '(script|textarea|select)';
abe7e055 498 preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
499 $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
bf2e1ab0 500
501 //catch all emails in <a href="mailto:...">
a14159bf 502 preg_match_all("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", $source, $ahref);
503 $source = preg_replace("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", '&&&ahref&&&', $source);
bf2e1ab0 504
505 //prevant replacement in tag attributes
abe7e055 506 preg_match_all("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!ius", $source, $misc);
507 $source = preg_replace("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!ius", '&&&misc&&&', $source);
bf2e1ab0 508
509 //catch !
eaf30d86 510 $source = preg_replace('!([-a-z0-9_+.]+@[-a-z0-9_.]+)!iue', '_hide_email("\1")', $source);
bf2e1ab0 511 $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source);
bf2e1ab0 512
513 // restore data
2456ea61 514 $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source);
120bd636 515 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
bf2e1ab0 516
517 return $source;
518}
519
520// }}}
b76f0797 521
a7de4ef7 522// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 523?>