Allow parenthesis before hasPerm in smarty templates
[platal.git] / classes / plpage.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
e92ecb8c 3 * Copyright (C) 2003-2011 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,
5faeba22 48 $globals->coreroot . '/plugins/',
a33955e4
FB
49 $globals->spoolroot . '/plugins/');
50 $this->config_dir = $globals->spoolroot . '/configs/';
0337d704 51
b5e56a71 52 $this->compile_check = !empty($globals->debug) || $globals->smarty_autocompile;
0337d704 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');
36b41c6c 63 $this->register_prefilter('get_class_constants');
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;
5faeba22 82 return $globals->coreroot . '/templates/' . $tpl;
7cb40d85
FB
83 }
84
85 // }}}
86 // {{{ function coreTpl()
87
88 /** Use a template from the core.
89 */
90 public function coreTpl($tpl, $type = SKINNED)
91 {
7cb40d85
FB
92 $this->changeTpl(self::getCoreTpl($tpl), $type);
93 }
94
95 // }}}
e654517d 96 // {{{ function raw()
97
98 public function raw()
99 {
100 global $globals;
101 $this->assign('globals', $globals);
102 return $this->fetch($this->_tpl);
103 }
104
105 // }}}
0337d704 106 // {{{ function _run()
107
2b1ee50b 108 protected function _run($skin)
0337d704 109 {
80d0423e 110 global $globals, $platal, $TIME_BEGIN;
36f114ef 111
732e5855 112 Platal::session()->close();
36f114ef 113
e654517d 114 $this->register_prefilter('trimwhitespace');
115 $this->register_prefilter('form_force_encodings');
7a3f546b 116 $this->register_prefilter('wiki_include');
7cb40d85 117 $this->register_prefilter('core_include');
b6ec30c5 118 $this->register_prefilter('if_rewrites');
91ebb7ff
FB
119 $this->assign('pl_triggers', $this->_errors);
120 $this->assign('pl_errors', $this->nb_errs());
121 $this->assign('pl_failure', $this->_failure);
80d0423e 122 $this->assign_by_ref('platal', $platal);
08d7cc45 123 $this->assign_by_ref('globals', $globals);
a3a049fc 124
4a8a1e0a 125 if (Env::has('json') && count($this->_jsonVars)) {
126 return $this->jsonDisplay();
127 }
eaf30d86 128
7299bfce
FB
129 $display = Env::s('display');
130 if ($display == 'light' && $this->_page_type == SKINNED) {
8a105df2 131 $this->_page_type = SIMPLE;
7299bfce 132 } elseif ($display == 'raw') {
8a105df2 133 $this->_page_type = NO_SKIN;
7299bfce 134 } elseif ($display == 'full') {
e90c2e8d 135 $this->_page_type = SKINNED;
8a105df2 136 }
137
d58e3be0
SJ
138 if ($this->_page_type == SIMPLE) {
139 $this->assign('simple', true);
140 } else {
141 $this->assign('simple', false);
142 }
143
62a66dfc 144 switch ($this->_page_type) {
145 case NO_SKIN:
81e9c63f 146 if (!($globals->debug & DEBUG_SMARTY)) {
147 error_reporting(0);
148 }
0337d704 149 $this->display($this->_tpl);
150 exit;
62a66dfc 151
152 case SIMPLE:
62a66dfc 153 case SKINNED:
b4ee37a4
FB
154 $this->register_modifier('escape_html', 'escape_html');
155 $this->default_modifiers = Array('@escape_html');
0337d704 156 }
5aa58639
FB
157 if (S::i('auth') <= AUTH_PUBLIC) {
158 $this->register_outputfilter('hide_emails');
159 }
493b6abe 160 header("Accept-Charset: utf-8");
4a8a1e0a 161 if (Env::v('forceXml')) {
a286fc7a 162 pl_content_headers("text/xml");
4a8a1e0a 163 }
b4315e15 164
165 if (!$globals->debug) {
6995a9b9 166 error_reporting(0);
b4315e15 167 $this->display($skin);
c67e49ea 168 pl_print_errors(true);
b4315e15 169 exit;
170 }
a3a049fc 171
7da8ef90 172 $this->assign('validate', true);
81e9c63f 173 if (!($globals->debug & DEBUG_SMARTY)) {
174 error_reporting(0);
175 }
040a594c 176 $START_SMARTY = microtime(true);
b4315e15 177 $result = $this->fetch($skin);
040a594c
FB
178 $ttime = sprintf('Temps total: %.02fs (Smarty %.02fs) - Mémoire totale : %dKo<br />',
179 microtime(true) - $TIME_BEGIN, microtime(true) - $START_SMARTY,
180 memory_get_peak_usage(true) / 1024);
b4ee37a4
FB
181 if ($globals->debug & DEBUG_BT) {
182 PlBacktrace::clean();
183 $this->assign_by_ref('backtraces', PlBacktrace::$bt);
7cb40d85
FB
184 $result = str_replace('@@BACKTRACE@@',
185 $this->fetch(self::getCoreTpl('backtrace.tpl')),
186 $result);
b4ee37a4
FB
187 } else {
188 $result = str_replace('@@BACKTRACE@@', '', $result);
189 }
0337d704 190
b4ee37a4 191 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
81e9c63f 192 if ($globals->debug & DEBUG_VALID) {
d1bdef74 193 global $platal;
b4315e15 194 $fd = fopen($this->compile_dir."/valid.html","w");
195 fwrite($fd, $result);
196 fclose($fd);
a3a049fc 197
d1bdef74
FB
198 $replc = '<span id="html_valid"><span style="color: #860">VALIDATION HTML EN COURS</span></span>'
199 . '<script type="text/javascript">$("#html_valid").updateHtml("validator");</script>'
200 . '<br />';
0337d704 201 }
202
b4315e15 203 echo str_replace("@HOOK@", $ttime.$replc, $result);
0337d704 204 exit;
205 }
206
1490093c 207 abstract public function run();
208
0337d704 209 // }}}
0337d704 210 // {{{ function nb_errs()
211
2b1ee50b 212 public function nb_errs()
0337d704 213 {
71a753d3 214 return count($this->_errors['errors']);
0337d704 215 }
216
217 // }}}
218 // {{{ function trig()
219
a7d35093 220 private function trig($msg, $type = 'errors')
0337d704 221 {
71a753d3
FB
222 if (!isset($this->_errors[$type])) {
223 $this->_errors[$type] = array();
224 }
225 $this->_errors[$type][] = $msg;
226 }
227
228 public function trigError($msg)
229 {
230 $this->trig($msg, 'errors');
231 }
232
e00bc67e
FB
233 public function trigWarning($msg)
234 {
235 $this->trig($msg, 'warnings');
236 }
237
71a753d3
FB
238 public function trigSuccess($msg)
239 {
240 $this->trig($msg, 'success');
0337d704 241 }
242
243 // }}}
8adfe956
RB
244 // {{{ function trigRedirect
245
246 // Acts as trig(), but replaces the template with a simple one displaying
247 // the error messages and a "continue" link.
248 private function trigRedirect($msg, $continue, $type = 'errors')
249 {
250 $this->trig($msg, $type);
251 $this->coreTpl('msgredirect.tpl');
252 $this->assign('continue', $continue);
253 $this->run();
254 }
255
256 public function trigErrorRedirect($msg, $continue)
257 {
258 $this->trigRedirect($msg, $continue, 'errors');
259 }
260
261 public function trigWarningRedirect($msg, $continue)
262 {
263 $this->trigRedirect($msg, $continue, 'warnings');
264 }
265
266 public function trigSuccessRedirect($msg, $continue)
267 {
268 $this->trigRedirect($msg, $continue, 'success');
269 }
270
271 // }}}
0337d704 272 // {{{ function kill()
273
ca476935 274 public function kill($msg, $type = 'errors')
0337d704 275 {
36f472dc
FB
276 // PHP is used on command line... do not run the whole page stuff.
277 if (php_sapi_name() == 'cli') {
278 echo $msg . "\n";
fc2cfa65 279 exit(-1);
36f472dc
FB
280 }
281
90eba1aa 282 global $platal;
283
ca476935 284 $this->trig($msg, $type);
0fcbe8d0 285 $this->_failure = true;
0337d704 286 $this->run();
287 }
288
ca476935
SJ
289 public function killError($msg)
290 {
291 $this->kill($msg, 'errors');
292 }
293
294 public function killWarning($msg)
295 {
296 $this->kill($msg, 'warnings');
297 }
298
299 public function killSuccess($msg)
300 {
301 $this->kill($msg, 'success');
302 }
303
0337d704 304 // }}}
9902e47b
FB
305 // {{{ function setTitle
306
307 public function setTitle($title)
308 {
380e2a5a
FB
309 global $globals;
310 if (isset($globals->core->sitename)) {
311 $title = $globals->core->sitename . ' :: ' . $title;
312 }
9902e47b
FB
313 $this->assign('pl_title', $title);
314 }
315
316 // }}}
0337d704 317 // {{{ function addJsLink
318
ac6f41ac 319 public function addJsLink($filename, $internal = true, $static_content = true)
0337d704 320 {
ac6f41ac
SJ
321 if ($internal) {
322 if ($static_content) {
323 $this->append('pl_js', pl_static_content_path("javascript/", $filename));
324 } else {
325 $this->append('pl_js', "javascript/$filename");
326 }
47a44ed0 327 } else {
ac6f41ac 328 $this->append('pl_js', $filename);
47a44ed0 329 }
0337d704 330 }
331
332 // }}}
333 // {{{ function addCssLink
334
2b1ee50b 335 public function addCssLink($path)
0337d704 336 {
91ebb7ff 337 $this->append('pl_css', $path);
0337d704 338 }
339
340 // }}}
1e9a1d99
AA
341 // {{{ function addLink
342
343 public function addLink($rel, $path)
344 {
345 $this->append('pl_link', array('rel' => $rel, 'href' => $path));
346 }
347
348
349 // }}}
ea626742 350 // {{{ function addCssInline
351
2b1ee50b 352 public function addCssInline($css)
ea626742 353 {
354 if (!empty($css)) {
91ebb7ff 355 $this->append('pl_inline_css', $css);
eaf30d86 356 }
ea626742 357 }
358
359 // }}}
162370e7 360 // {{{ function setRssLink
361
2b1ee50b 362 public function setRssLink($title, $path)
162370e7 363 {
91ebb7ff 364 $this->assign('pl_rss', array('title' => $title, 'href' => $path));
162370e7 365 }
366
367 // }}}
4a8a1e0a 368 // {{{ function jsonDisplay
369 protected function jsonDisplay()
370 {
a286fc7a 371 pl_content_headers("text/javascript");
c67e49ea
FB
372 if (!empty(PlBacktrace::$bt)) {
373 $this->jsonAssign('pl_backtraces', PlBacktrace::$bt);
641802fe 374 }
b00a702a 375 array_walk_recursive($this->_jsonVars, "escape_XDB");
4a8a1e0a 376 $jsonbegin = Env::v('jsonBegin');
377 $jsonend = Env::v('jsonEnd');
378 if (Env::has('jsonVar')) {
379 $jsonbegin = Env::v('jsonVar').' = ';
380 $jsonend = ';';
381 } elseif (Env::has('jsonFunc')) {
382 $jsonbegin = Env::v('jsonFunc').'(';
383 $jsonend = ');';
384 }
385 echo $jsonbegin, json_encode($this->_jsonVars), $jsonend;
386 exit;
387 }
388 // }}}
c67e49ea
FB
389
390 public function runJSon()
391 {
392 pl_content_headers("text/javascript");
393 if (!empty(PlBacktrace::$bt)) {
394 $this->jsonAssign('pl_backtraces', PlBacktrace::$bt);
395 }
396 echo json_encode($this->_jsonVars);
397 exit;
398 }
399
4a8a1e0a 400 // {{{ function jsonAssign
401 public function jsonAssign($var, $value)
402 {
403 $this->_jsonVars[$var] = $value;
404 }
fba760d2 405
406 // }}}
4a8a1e0a 407}
408
b00a702a 409function escape_XDB(&$item, $key)
4a8a1e0a 410{
b00a702a 411 if ($item instanceof XDBIterator) {
4a8a1e0a 412 $expanded = array();
413 while ($a = $item->next()) {
414 $expanded[] = $a;
415 }
416 $item = $expanded;
417 }
0337d704 418}
419
b76f0797 420// {{{ function escape_html ()
421
422/**
423 * default smarty plugin, used to auto-escape dangerous html.
eaf30d86 424 *
b76f0797 425 * < --> &lt;
426 * > --> &gt;
427 * " --> &quot;
428 * & not followed by some entity --> &amp;
429 */
430function escape_html($string)
431{
432 if (is_string($string)) {
c1895524 433 return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
b76f0797 434 } else {
c1895524 435 return $string;
b76f0797 436 }
437}
438
439// }}}
440// {{{ function at_to_globals()
441
442/**
443 * helper
444 */
445
446function _to_globals($s) {
447 global $globals;
448 $t = explode('.',$s);
449 if (count($t) == 1) {
450 return var_export($globals->$t[0],true);
451 } else {
452 return var_export($globals->$t[0]->$t[1],true);
453 }
454}
455
456/**
457 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
458 */
459
ed4f7de0 460function at_to_globals($tpl_source, $smarty)
b76f0797 461{
462 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
7cb40d85 463}
b76f0797 464
7cb40d85 465// }}}
36b41c6c
SJ
466// {{{ function get_class_constants()
467
468/**
469 * helper
470 */
471
472function _get_class_const($class, $const)
473{
474 return var_export(constant($class . '::' . $const), true);
475}
476
477/**
478 * Compilation plugin used to import class constants through calls to #Class::CONSTANT#
479 */
480
481function get_class_constants($tpl_source, $smarty)
482{
483 return preg_replace('/#([a-zA-Z0-9_]+)::([A-Z0-9_]+)#/e', '_get_class_const(\'\\1\',\'\\2\')', $tpl_source);
484}
485
486// }}}
7cb40d85 487// {{{ function trimwhitespace
b76f0797 488
ed4f7de0 489function trimwhitespace($source, $smarty)
7cb40d85
FB
490{
491 $tags = '(script|pre|textarea)';
492 preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
493 $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
b76f0797 494
7cb40d85
FB
495 // remove all leading spaces, tabs and carriage returns NOT
496 // preceeded by a php close tag.
497 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
498 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
b76f0797 499
7cb40d85
FB
500 return $source;
501}
b76f0797 502
7cb40d85
FB
503// }}}
504// {{{ function wiki_include
7a3f546b 505
ed4f7de0 506function wiki_include($source, $smarty)
7a3f546b 507{
7cb40d85 508 global $globals;
7a3f546b 509 return preg_replace('/\{include( [^}]*)? wiki=([^} ]+)(.*?)\}/ui',
7cb40d85
FB
510 '{include\1 file="' . $globals->spoolroot . '/spool/wiki.d/cache_\2.tpl"\3 included=1}',
511 $source);
512}
513
ed4f7de0 514function core_include($source, $smarty)
7cb40d85
FB
515{
516 global $globals;
517 return preg_replace('/\{include( [^}]*)? core=([^} ]+)(.*?)\}/ui',
5faeba22 518 '{include\1 file="' . $globals->coreroot . '/templates/\2"\3}',
7a3f546b 519 $source);
520}
521
522// }}}
908db125
FB
523//{{{ function hasPerm
524
ed4f7de0 525function if_rewrites($source, $smarty)
908db125 526{
a2efe30f 527 $perms = 'isset($smarty.session.user|smarty:nodefaults) && $smarty.session.user';
ad274e6d
NI
528 return preg_replace(array('/\{(else)?if([^}]*)([ \(]\!?)hasPerms?\(([^)]+)\)([^}]*)\}/',
529 '/\{(else)?if([^}]*)([ \(]\!?)t\(([^)]+)\)([^}]*)\}/'),
530 array('{\1if\2\3(' . $perms . '->checkPerms(\4))\5}',
531 '{\1if\2\3(isset(\4|smarty:nodefaults) && (\4|smarty:nodefaults))\5}'),
7cb40d85 532 $source);
908db125
FB
533}
534
535// }}}
edc636b8 536// {{{
537
ed4f7de0 538function form_force_encodings($source, $smarty)
edc636b8 539{
540 return preg_replace('/<form[^\w]/',
a7de4ef7 541 '\0 accept-charset="utf-8" ',
edc636b8 542 $source);
543}
544
545// }}}
bf2e1ab0 546// {{{ function hide_emails
547
548function _hide_email($source)
549{
1bd2bc7e 550 $source = str_replace("\n", '', $source);
6b8d257b 551 return '<script type="text/javascript">//<![CDATA[' . "\n" .
91ebb7ff
FB
552 'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
553 '//]]></script>';
bf2e1ab0 554}
555
ed4f7de0 556function hide_emails($source, $smarty)
bf2e1ab0 557{
a18afbdc 558 if (!strpos($source, '@')) {
559 return $source;
560 }
561
bf2e1ab0 562 //prevent email replacement in <script> and <textarea>
120bd636 563 $tags = '(script|textarea|select)';
abe7e055 564 preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
565 $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
bf2e1ab0 566
567 //catch all emails in <a href="mailto:...">
a14159bf 568 preg_match_all("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", $source, $ahref);
569 $source = preg_replace("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", '&&&ahref&&&', $source);
bf2e1ab0 570
571 //prevant replacement in tag attributes
abe7e055 572 preg_match_all("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!ius", $source, $misc);
573 $source = preg_replace("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!ius", '&&&misc&&&', $source);
bf2e1ab0 574
575 //catch !
eaf30d86 576 $source = preg_replace('!([-a-z0-9_+.]+@[-a-z0-9_.]+)!iue', '_hide_email("\1")', $source);
bf2e1ab0 577 $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source);
bf2e1ab0 578
579 // restore data
2456ea61 580 $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source);
120bd636 581 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
bf2e1ab0 582
583 return $source;
584}
585
586// }}}
b76f0797 587
fa7ffd66 588// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
0337d704 589?>