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