2 /***************************************************************************
3 * Copyright (C) 2003-2009 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
);
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->addJsLink('jquery.js');
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_has_perms');
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 if (Env
::v('display') == 'light') {
131 $this->_page_type
= SIMPLE
;
132 } elseif (Env
::v('display') == 'raw') {
133 $this->_page_type
= NO_SKIN
;
134 } elseif (Env
::v('display') == 'full') {
135 $this->_page_typ
= SKINNED
;
138 switch ($this->_page_type
) {
140 if (!($globals->debug
& DEBUG_SMARTY
)) {
143 $this->display($this->_tpl
);
147 $this->assign('simple', true
);
150 $this->register_modifier('escape_html', 'escape_html');
151 $this->default_modifiers
= Array('@escape_html');
153 if (S
::i('auth') <= AUTH_PUBLIC
) {
154 $this->register_outputfilter('hide_emails');
156 $this->addJsLink('wiki.js');
157 header("Accept-Charset: utf-8");
158 if (Env
::v('forceXml')) {
159 header("Content-Type: text/xml; charset=utf-8");
162 if (!$globals->debug
) {
164 $this->display($skin);
168 $this->assign('validate', true
);
169 if (!($globals->debug
& DEBUG_SMARTY
)) {
172 $START_SMARTY = microtime(true
);
173 $result = $this->fetch($skin);
174 $ttime = sprintf('Temps total: %.02fs (Smarty %.02fs) - Mémoire totale : %dKo<br />',
175 microtime(true
) - $TIME_BEGIN, microtime(true
) - $START_SMARTY,
176 memory_get_peak_usage(true
) / 1024);
177 if ($globals->debug
& DEBUG_BT
) {
178 PlBacktrace
::clean();
179 $this->assign_by_ref('backtraces', PlBacktrace
::$bt);
180 $result = str_replace('@@BACKTRACE@@',
181 $this->fetch(self
::getCoreTpl('backtrace.tpl')),
184 $result = str_replace('@@BACKTRACE@@', '', $result);
187 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
188 if ($globals->debug
& DEBUG_VALID
) {
189 $fd = fopen($this->compile_dir
."/valid.html","w");
190 fwrite($fd, $result);
193 exec($globals->spoolroot
."/bin/devel/xhtml.validate.pl ".$this->compile_dir
."/valid.html", $val);
194 foreach ($val as $h) {
195 if (preg_match("/^X-W3C-Validator-Errors: (\d+)$/", $h, $m)) {
196 $replc = '<span style="color: #080;">HTML OK</span><br />';
198 $replc = "<span class='erreur'><a href='http://validator.w3.org/check?uri={$globals->baseurl}"
199 ."/valid.html&ss=1#result'>{$m[1]} ERREUR(S) !!!</a></span><br />";
206 echo str_replace("@HOOK@", $ttime.$replc, $result);
210 abstract public function run();
213 // {{{ function nb_errs()
215 public function nb_errs()
217 return count($this->_errors
['errors']);
221 // {{{ function trig()
223 private function trig($msg, $type = 'errors')
225 if (!isset($this->_errors
[$type])) {
226 $this->_errors
[$type] = array();
228 $this->_errors
[$type][] = $msg;
231 public function trigError($msg)
233 $this->trig($msg, 'errors');
236 public function trigWarning($msg)
238 $this->trig($msg, 'warnings');
241 public function trigSuccess($msg)
243 $this->trig($msg, 'success');
247 // {{{ function kill()
249 public function kill($msg, $type = 'errors')
251 // PHP is used on command line... do not run the whole page stuff.
252 if (php_sapi_name() == 'cli') {
259 $this->trig($msg, $type);
260 $this->_failure
= true
;
264 public function killError($msg)
266 $this->kill($msg, 'errors');
269 public function killWarning($msg)
271 $this->kill($msg, 'warnings');
274 public function killSuccess($msg)
276 $this->kill($msg, 'success');
280 // {{{ function setTitle
282 public function setTitle($title)
285 if (isset($globals->core
->sitename
)) {
286 $title = $globals->core
->sitename
. ' :: ' . $title;
288 $this->assign('pl_title', $title);
292 // {{{ function addJsLink
294 public function addJsLink($path)
296 $this->append('pl_js', $path);
300 // {{{ function addCssLink
302 public function addCssLink($path)
304 $this->append('pl_css', $path);
308 // {{{ function addLink
310 public function addLink($rel, $path)
312 $this->append('pl_link', array('rel' => $rel, 'href' => $path));
317 // {{{ function addCssInline
319 public function addCssInline($css)
322 $this->append('pl_inline_css', $css);
327 // {{{ function setRssLink
329 public function setRssLink($title, $path)
331 $this->assign('pl_rss', array('title' => $title, 'href' => $path));
335 // {{{ function jsonDisplay
336 protected function jsonDisplay()
338 header("Content-type: text/javascript; charset=utf-8");
339 array_walk_recursive($this->_jsonVars
, "escape_xorgDB");
340 $jsonbegin = Env
::v('jsonBegin');
341 $jsonend = Env
::v('jsonEnd');
342 if (Env
::has('jsonVar')) {
343 $jsonbegin = Env
::v('jsonVar').' = ';
345 } elseif (Env
::has('jsonFunc')) {
346 $jsonbegin = Env
::v('jsonFunc').'(';
349 echo $jsonbegin, json_encode($this->_jsonVars
), $jsonend;
353 // {{{ function jsonAssign
354 public function jsonAssign($var, $value)
356 $this->_jsonVars
[$var] = $value;
362 function escape_xorgDB(&$item, $key)
364 if ($item instanceof XOrgDBIterator
) {
366 while ($a = $item->next()) {
373 // {{{ function escape_html ()
376 * default smarty plugin, used to auto-escape dangerous html.
381 * & not followed by some entity --> &
383 function escape_html($string)
385 if (is_string($string)) {
386 return htmlspecialchars($string, ENT_QUOTES
, 'UTF-8');
393 // {{{ function at_to_globals()
399 function _to_globals($s) {
401 $t = explode('.',$s);
402 if (count($t) == 1) {
403 return var_export($globals->$t[0],true
);
405 return var_export($globals->$t[0]->$t[1],true
);
410 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
413 function at_to_globals($tpl_source, &$smarty)
415 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
419 // {{{ function trimwhitespace
421 function trimwhitespace($source, &$smarty)
423 $tags = '(script|pre|textarea)';
424 preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
425 $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
427 // remove all leading spaces, tabs and carriage returns NOT
428 // preceeded by a php close tag.
429 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
430 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
436 // {{{ function wiki_include
438 function wiki_include($source, &$smarty)
441 return preg_replace('/\{include( [^}]*)? wiki=([^} ]+)(.*?)\}/ui',
442 '{include\1 file="' . $globals->spoolroot
. '/spool/wiki.d/cache_\2.tpl"\3 included=1}',
446 function core_include($source, &$smarty)
449 return preg_replace('/\{include( [^}]*)? core=([^} ]+)(.*?)\}/ui',
450 '{include\1 file="' . $globals->spoolroot
. '/core/templates/\2"\3}',
455 //{{{ function hasPerm
457 function if_has_perms($source, &$smarty)
459 $source = preg_replace('/\{if([^}]*) (\!?)hasPerms\(([^)]+)\)([^}]*)\}/',
460 '{if\1 \2$smarty.session.perms->hasFlagCombination(\3)\4}',
462 return preg_replace('/\{if([^}]*) (\!?)hasPerm\(([^)]+)\)([^}]*)\}/',
463 '{if\1 \2($smarty.session.perms && $smarty.session.perms->hasFlag(\3))\4}',
470 function form_force_encodings($source, &$smarty)
472 return preg_replace('/<form[^\w]/',
473 '\0 accept-charset="utf-8" ',
478 // {{{ function hide_emails
480 function _hide_email($source)
482 $source = str_replace("\n", '', $source);
483 return '<script type="text/javascript">//<![CDATA[' . "\n" .
484 'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
488 function hide_emails($source, &$smarty)
490 if (!strpos($source, '@')) {
494 //prevent email replacement in <script> and <textarea>
495 $tags = '(script|textarea|select)';
496 preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
497 $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
499 //catch all emails in <a href="mailto:...">
500 preg_match_all("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", $source, $ahref);
501 $source = preg_replace("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", '&&&ahref&&&', $source);
503 //prevant replacement in tag attributes
504 preg_match_all("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!ius", $source, $misc);
505 $source = preg_replace("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!ius", '&&&misc&&&', $source);
508 $source = preg_replace('!([-a-z0-9_+.]+@[-a-z0-9_.]+)!iue', '_hide_email("\1")', $source);
509 $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source);
512 $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source);
513 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
520 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: