2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 if (!@include_once
'smarty/libs/Smarty.class.php') {
23 require_once 'smarty/Smarty.class.php';
26 abstract class PlPage
extends Smarty
34 // {{{ function PlPage()
36 public function __construct()
42 $this->caching
= false
;
43 $this->config_overwrite
= false
;
44 $this->use_sub_dirs
= false
;
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/';
52 $this->compile_check
= !empty($globals->debug
) ||
$globals->smarty_autocompile
;
54 $this->_errors
= array('errors' => array());
55 $this->_jsonVars
= array();
56 $this->_failure
= false
;
58 if ($globals->mode
!= 'rw') {
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");
62 $this->register_prefilter('at_to_globals');
63 $this->register_prefilter('get_class_constants');
67 // {{{ function changeTpl()
69 public function changeTpl($tpl, $type = SKINNED
)
72 $this->_page_type
= $type;
73 $this->assign('pl_tpl', $tpl);
77 // {{{ function getCoreTpl()
79 public static function getCoreTpl($tpl)
82 return $globals->spoolroot
. '/core/templates/' . $tpl;
86 // {{{ function coreTpl()
88 /** Use a template from the core.
90 public function coreTpl($tpl, $type = SKINNED
)
93 $this->changeTpl(self
::getCoreTpl($tpl), $type);
102 $this->assign('globals', $globals);
103 return $this->fetch($this->_tpl
);
107 // {{{ function _run()
109 protected function _run($skin)
111 global $globals, $platal, $TIME_BEGIN;
113 Platal
::session()->close();
115 $this->register_prefilter('trimwhitespace');
116 $this->register_prefilter('form_force_encodings');
117 $this->register_prefilter('wiki_include');
118 $this->register_prefilter('core_include');
119 $this->register_prefilter('if_rewrites');
120 $this->assign('pl_triggers', $this->_errors
);
121 $this->assign('pl_errors', $this->nb_errs());
122 $this->assign('pl_failure', $this->_failure
);
123 $this->assign_by_ref('platal', $platal);
124 $this->assign_by_ref('globals', $globals);
126 if (Env
::has('json') && count($this->_jsonVars
)) {
127 return $this->jsonDisplay();
130 $display = Env
::s('display');
131 if ($display == 'light' && $this->_page_type
== SKINNED
) {
132 $this->_page_type
= SIMPLE
;
133 } elseif ($display == 'raw') {
134 $this->_page_type
= NO_SKIN
;
135 } elseif ($display == 'full') {
136 $this->_page_type
= SKINNED
;
139 if ($this->_page_type
== SIMPLE
) {
140 $this->assign('simple', true
);
142 $this->assign('simple', false
);
145 switch ($this->_page_type
) {
147 if (!($globals->debug
& DEBUG_SMARTY
)) {
150 $this->display($this->_tpl
);
155 $this->register_modifier('escape_html', 'escape_html');
156 $this->default_modifiers
= Array('@escape_html');
158 if (S
::i('auth') <= AUTH_PUBLIC
) {
159 $this->register_outputfilter('hide_emails');
161 header("Accept-Charset: utf-8");
162 if (Env
::v('forceXml')) {
163 pl_content_headers("text/xml");
166 if (!$globals->debug
) {
168 $this->display($skin);
169 pl_print_errors(true
);
173 $this->assign('validate', true
);
174 if (!($globals->debug
& DEBUG_SMARTY
)) {
177 $START_SMARTY = microtime(true
);
178 $result = $this->fetch($skin);
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);
182 if ($globals->debug
& DEBUG_BT
) {
183 PlBacktrace
::clean();
184 $this->assign_by_ref('backtraces', PlBacktrace
::$bt);
185 $result = str_replace('@@BACKTRACE@@',
186 $this->fetch(self
::getCoreTpl('backtrace.tpl')),
189 $result = str_replace('@@BACKTRACE@@', '', $result);
192 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
193 if ($globals->debug
& DEBUG_VALID
) {
195 $fd = fopen($this->compile_dir
."/valid.html","w");
196 fwrite($fd, $result);
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>'
204 echo str_replace("@HOOK@", $ttime.$replc, $result);
208 abstract public function run();
211 // {{{ function nb_errs()
213 public function nb_errs()
215 return count($this->_errors
['errors']);
219 // {{{ function trig()
221 private function trig($msg, $type = 'errors')
223 if (!isset($this->_errors
[$type])) {
224 $this->_errors
[$type] = array();
226 $this->_errors
[$type][] = $msg;
229 public function trigError($msg)
231 $this->trig($msg, 'errors');
234 public function trigWarning($msg)
236 $this->trig($msg, 'warnings');
239 public function trigSuccess($msg)
241 $this->trig($msg, 'success');
245 // {{{ function trigRedirect
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')
251 $this->trig($msg, $type);
252 $this->coreTpl('msgredirect.tpl');
253 $this->assign('continue', $continue);
257 public function trigErrorRedirect($msg, $continue)
259 $this->trigRedirect($msg, $continue, 'errors');
262 public function trigWarningRedirect($msg, $continue)
264 $this->trigRedirect($msg, $continue, 'warnings');
267 public function trigSuccessRedirect($msg, $continue)
269 $this->trigRedirect($msg, $continue, 'success');
273 // {{{ function kill()
275 public function kill($msg, $type = 'errors')
277 // PHP is used on command line... do not run the whole page stuff.
278 if (php_sapi_name() == 'cli') {
285 $this->trig($msg, $type);
286 $this->_failure
= true
;
290 public function killError($msg)
292 $this->kill($msg, 'errors');
295 public function killWarning($msg)
297 $this->kill($msg, 'warnings');
300 public function killSuccess($msg)
302 $this->kill($msg, 'success');
306 // {{{ function setTitle
308 public function setTitle($title)
311 if (isset($globals->core
->sitename
)) {
312 $title = $globals->core
->sitename
. ' :: ' . $title;
314 $this->assign('pl_title', $title);
318 // {{{ function addJsLink
320 public function addJsLink($filename, $internal = true
, $static_content = true
)
323 if ($static_content) {
324 $this->append('pl_js', pl_static_content_path("javascript/", $filename));
326 $this->append('pl_js', "javascript/$filename");
329 $this->append('pl_js', $filename);
334 // {{{ function addCssLink
336 public function addCssLink($path)
338 $this->append('pl_css', $path);
342 // {{{ function addLink
344 public function addLink($rel, $path)
346 $this->append('pl_link', array('rel' => $rel, 'href' => $path));
351 // {{{ function addCssInline
353 public function addCssInline($css)
356 $this->append('pl_inline_css', $css);
361 // {{{ function setRssLink
363 public function setRssLink($title, $path)
365 $this->assign('pl_rss', array('title' => $title, 'href' => $path));
369 // {{{ function jsonDisplay
370 protected function jsonDisplay()
372 pl_content_headers("text/javascript");
373 if (!empty(PlBacktrace
::$bt)) {
374 $this->jsonAssign('pl_backtraces', PlBacktrace
::$bt);
376 array_walk_recursive($this->_jsonVars
, "escape_XDB");
377 $jsonbegin = Env
::v('jsonBegin');
378 $jsonend = Env
::v('jsonEnd');
379 if (Env
::has('jsonVar')) {
380 $jsonbegin = Env
::v('jsonVar').' = ';
382 } elseif (Env
::has('jsonFunc')) {
383 $jsonbegin = Env
::v('jsonFunc').'(';
386 echo $jsonbegin, json_encode($this->_jsonVars
), $jsonend;
391 public function runJSon()
393 pl_content_headers("text/javascript");
394 if (!empty(PlBacktrace
::$bt)) {
395 $this->jsonAssign('pl_backtraces', PlBacktrace
::$bt);
397 echo json_encode($this->_jsonVars
);
401 // {{{ function jsonAssign
402 public function jsonAssign($var, $value)
404 $this->_jsonVars
[$var] = $value;
410 function escape_XDB(&$item, $key)
412 if ($item instanceof XDBIterator
) {
414 while ($a = $item->next()) {
421 // {{{ function escape_html ()
424 * default smarty plugin, used to auto-escape dangerous html.
429 * & not followed by some entity --> &
431 function escape_html($string)
433 if (is_string($string)) {
434 return htmlspecialchars($string, ENT_QUOTES
, 'UTF-8');
441 // {{{ function at_to_globals()
447 function _to_globals($s) {
449 $t = explode('.',$s);
450 if (count($t) == 1) {
451 return var_export($globals->$t[0],true
);
453 return var_export($globals->$t[0]->$t[1],true
);
458 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
461 function at_to_globals($tpl_source, $smarty)
463 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
467 // {{{ function get_class_constants()
473 function _get_class_const($class, $const)
475 return var_export(constant($class . '::' . $const), true
);
479 * Compilation plugin used to import class constants through calls to #Class::CONSTANT#
482 function get_class_constants($tpl_source, $smarty)
484 return preg_replace('/#([a-zA-Z0-9_]+)::([A-Z0-9_]+)#/e', '_get_class_const(\'\\1\',\'\\2\')', $tpl_source);
488 // {{{ function trimwhitespace
490 function trimwhitespace($source, $smarty)
492 $tags = '(script|pre|textarea)';
493 preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
494 $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
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);
505 // {{{ function wiki_include
507 function wiki_include($source, $smarty)
510 return preg_replace('/\{include( [^}]*)? wiki=([^} ]+)(.*?)\}/ui',
511 '{include\1 file="' . $globals->spoolroot
. '/spool/wiki.d/cache_\2.tpl"\3 included=1}',
515 function core_include($source, $smarty)
518 return preg_replace('/\{include( [^}]*)? core=([^} ]+)(.*?)\}/ui',
519 '{include\1 file="' . $globals->spoolroot
. '/core/templates/\2"\3}',
524 //{{{ function hasPerm
526 function if_rewrites($source, $smarty)
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}'),
539 function form_force_encodings($source, $smarty)
541 return preg_replace('/<form[^\w]/',
542 '\0 accept-charset="utf-8" ',
547 // {{{ function hide_emails
549 function _hide_email($source)
551 $source = str_replace("\n", '', $source);
552 return '<script type="text/javascript">//<![CDATA[' . "\n" .
553 'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
557 function hide_emails($source, $smarty)
559 if (!strpos($source, '@')) {
563 //prevent email replacement in <script> and <textarea>
564 $tags = '(script|textarea|select)';
565 preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
566 $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
568 //catch all emails in <a href="mailto:...">
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);
572 //prevant replacement in tag attributes
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);
577 $source = preg_replace('!([-a-z0-9_+.]+@[-a-z0-9_.]+)!iue', '_hide_email("\1")', $source);
578 $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source);
581 $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source);
582 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
589 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: