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