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->coreroot
. '/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->coreroot
. '/templates/' . $tpl;
86 // {{{ function coreTpl()
88 /** Use a template from the core.
90 public function coreTpl($tpl, $type = SKINNED
)
92 $this->changeTpl(self
::getCoreTpl($tpl), $type);
101 $this->assign('globals', $globals);
102 return $this->fetch($this->_tpl
);
106 // {{{ function _run()
108 protected function _run($skin)
110 global $globals, $platal, $TIME_BEGIN;
112 Platal
::session()->close();
114 $this->register_prefilter('trimwhitespace');
115 $this->register_prefilter('form_force_encodings');
116 $this->register_prefilter('wiki_include');
117 $this->register_prefilter('core_include');
118 $this->register_prefilter('if_rewrites');
119 $this->assign('pl_triggers', $this->_errors
);
120 $this->assign('pl_errors', $this->nb_errs());
121 $this->assign('pl_failure', $this->_failure
);
122 $this->assign_by_ref('platal', $platal);
123 $this->assign_by_ref('globals', $globals);
125 if (Env
::has('json') && count($this->_jsonVars
)) {
126 return $this->jsonDisplay();
129 $display = Env
::s('display');
130 if ($display == 'light' && $this->_page_type
== SKINNED
) {
131 $this->_page_type
= SIMPLE
;
132 } elseif ($display == 'raw') {
133 $this->_page_type
= NO_SKIN
;
134 } elseif ($display == 'full') {
135 $this->_page_type
= SKINNED
;
138 if ($this->_page_type
== SIMPLE
) {
139 $this->assign('simple', true
);
141 $this->assign('simple', false
);
144 switch ($this->_page_type
) {
146 if (!($globals->debug
& DEBUG_SMARTY
)) {
149 $this->display($this->_tpl
);
154 $this->register_modifier('escape_html', 'escape_html');
155 $this->default_modifiers
= Array('@escape_html');
157 if (S
::i('auth') <= AUTH_PUBLIC
) {
158 $this->register_outputfilter('hide_emails');
160 header("Accept-Charset: utf-8");
161 if (Env
::v('forceXml')) {
162 pl_content_headers("text/xml");
165 if (!$globals->debug
) {
167 $this->display($skin);
168 pl_print_errors(true
);
172 $this->assign('validate', true
);
173 if (!($globals->debug
& DEBUG_SMARTY
)) {
176 $START_SMARTY = microtime(true
);
177 $result = $this->fetch($skin);
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);
181 if ($globals->debug
& DEBUG_BT
) {
182 PlBacktrace
::clean();
183 $this->assign_by_ref('backtraces', PlBacktrace
::$bt);
184 $result = str_replace('@@BACKTRACE@@',
185 $this->fetch(self
::getCoreTpl('backtrace.tpl')),
188 $result = str_replace('@@BACKTRACE@@', '', $result);
191 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
192 if ($globals->debug
& DEBUG_VALID
) {
194 $fd = fopen($this->compile_dir
."/valid.html","w");
195 fwrite($fd, $result);
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>'
203 echo str_replace("@HOOK@", $ttime.$replc, $result);
207 abstract public function run();
210 // {{{ function nb_errs()
212 public function nb_errs()
214 return count($this->_errors
['errors']);
218 // {{{ function trig()
220 private function trig($msg, $type = 'errors')
222 if (!isset($this->_errors
[$type])) {
223 $this->_errors
[$type] = array();
225 $this->_errors
[$type][] = $msg;
228 public function trigError($msg)
230 $this->trig($msg, 'errors');
233 public function trigWarning($msg)
235 $this->trig($msg, 'warnings');
238 public function trigSuccess($msg)
240 $this->trig($msg, 'success');
244 // {{{ function trigRedirect
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')
250 $this->trig($msg, $type);
251 $this->coreTpl('msgredirect.tpl');
252 $this->assign('continue', $continue);
256 public function trigErrorRedirect($msg, $continue)
258 $this->trigRedirect($msg, $continue, 'errors');
261 public function trigWarningRedirect($msg, $continue)
263 $this->trigRedirect($msg, $continue, 'warnings');
266 public function trigSuccessRedirect($msg, $continue)
268 $this->trigRedirect($msg, $continue, 'success');
272 // {{{ function kill()
274 public function kill($msg, $type = 'errors')
276 // PHP is used on command line... do not run the whole page stuff.
277 if (php_sapi_name() == 'cli') {
284 $this->trig($msg, $type);
285 $this->_failure
= true
;
289 public function killError($msg)
291 $this->kill($msg, 'errors');
294 public function killWarning($msg)
296 $this->kill($msg, 'warnings');
299 public function killSuccess($msg)
301 $this->kill($msg, 'success');
305 // {{{ function setTitle
307 public function setTitle($title)
310 if (isset($globals->core
->sitename
)) {
311 $title = $globals->core
->sitename
. ' :: ' . $title;
313 $this->assign('pl_title', $title);
317 // {{{ function addJsLink
319 public function addJsLink($filename, $internal = true
, $static_content = true
)
322 if ($static_content) {
323 $this->append('pl_js', pl_static_content_path("javascript/", $filename));
325 $this->append('pl_js', "javascript/$filename");
328 $this->append('pl_js', $filename);
333 // {{{ function addCssLink
335 public function addCssLink($path)
337 $this->append('pl_css', $path);
341 // {{{ function addLink
343 public function addLink($rel, $path)
345 $this->append('pl_link', array('rel' => $rel, 'href' => $path));
350 // {{{ function addCssInline
352 public function addCssInline($css)
355 $this->append('pl_inline_css', $css);
360 // {{{ function setRssLink
362 public function setRssLink($title, $path)
364 $this->assign('pl_rss', array('title' => $title, 'href' => $path));
368 // {{{ function jsonDisplay
369 protected function jsonDisplay()
371 pl_content_headers("text/javascript");
372 if (!empty(PlBacktrace
::$bt)) {
373 $this->jsonAssign('pl_backtraces', PlBacktrace
::$bt);
375 array_walk_recursive($this->_jsonVars
, "escape_XDB");
376 $jsonbegin = Env
::v('jsonBegin');
377 $jsonend = Env
::v('jsonEnd');
378 if (Env
::has('jsonVar')) {
379 $jsonbegin = Env
::v('jsonVar').' = ';
381 } elseif (Env
::has('jsonFunc')) {
382 $jsonbegin = Env
::v('jsonFunc').'(';
385 echo $jsonbegin, json_encode($this->_jsonVars
), $jsonend;
390 public function runJSon()
392 pl_content_headers("text/javascript");
393 if (!empty(PlBacktrace
::$bt)) {
394 $this->jsonAssign('pl_backtraces', PlBacktrace
::$bt);
396 echo json_encode($this->_jsonVars
);
400 // {{{ function jsonAssign
401 public function jsonAssign($var, $value)
403 $this->_jsonVars
[$var] = $value;
409 function escape_XDB(&$item, $key)
411 if ($item instanceof XDBIterator
) {
413 while ($a = $item->next()) {
420 // {{{ function escape_html ()
423 * default smarty plugin, used to auto-escape dangerous html.
428 * & not followed by some entity --> &
430 function escape_html($string)
432 if (is_string($string)) {
433 return htmlspecialchars($string, ENT_QUOTES
, 'UTF-8');
440 // {{{ function at_to_globals()
446 function _to_globals($s) {
448 $t = explode('.',$s);
449 if (count($t) == 1) {
450 return var_export($globals->$t[0],true
);
452 return var_export($globals->$t[0]->$t[1],true
);
457 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
460 function at_to_globals($tpl_source, $smarty)
462 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
466 // {{{ function get_class_constants()
472 function _get_class_const($class, $const)
474 return var_export(constant($class . '::' . $const), true
);
478 * Compilation plugin used to import class constants through calls to #Class::CONSTANT#
481 function get_class_constants($tpl_source, $smarty)
483 return preg_replace('/#([a-zA-Z0-9_]+)::([A-Z0-9_]+)#/e', '_get_class_const(\'\\1\',\'\\2\')', $tpl_source);
487 // {{{ function trimwhitespace
489 function trimwhitespace($source, $smarty)
491 $tags = '(script|pre|textarea)';
492 preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
493 $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
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);
504 // {{{ function wiki_include
506 function wiki_include($source, $smarty)
509 return preg_replace('/\{include( [^}]*)? wiki=([^} ]+)(.*?)\}/ui',
510 '{include\1 file="' . $globals->spoolroot
. '/spool/wiki.d/cache_\2.tpl"\3 included=1}',
514 function core_include($source, $smarty)
517 return preg_replace('/\{include( [^}]*)? core=([^} ]+)(.*?)\}/ui',
518 '{include\1 file="' . $globals->coreroot
. '/templates/\2"\3}',
523 //{{{ function hasPerm
525 function if_rewrites($source, $smarty)
527 $perms = 'isset($smarty.session.user|smarty:nodefaults) && $smarty.session.user';
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}'),
538 function form_force_encodings($source, $smarty)
540 return preg_replace('/<form[^\w]/',
541 '\0 accept-charset="utf-8" ',
546 // {{{ function hide_emails
548 function _hide_email($source)
550 $source = str_replace("\n", '', $source);
551 return '<script type="text/javascript">//<![CDATA[' . "\n" .
552 'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
556 function hide_emails($source, $smarty)
558 if (!strpos($source, '@')) {
562 //prevent email replacement in <script> and <textarea>
563 $tags = '(script|textarea|select)';
564 preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
565 $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
567 //catch all emails in <a href="mailto:...">
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);
571 //prevant replacement in tag attributes
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);
576 $source = preg_replace('!([-a-z0-9_+.]+@[-a-z0-9_.]+)!iue', '_hide_email("\1")', $source);
577 $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source);
580 $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source);
581 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
588 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: