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