875981e674af398446983a1985c80ef609b4dbff
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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');
66 // {{{ function changeTpl()
68 public function changeTpl($tpl, $type = SKINNED
)
71 $this->_page_type
= $type;
72 $this->assign('pl_tpl', $tpl);
76 // {{{ function getCoreTpl()
78 public static function getCoreTpl($tpl)
81 return $globals->spoolroot
. '/core/templates/' . $tpl;
85 // {{{ function coreTpl()
87 /** Use a template from the core.
89 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 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
;
137 switch ($this->_page_type
) {
139 if (!($globals->debug
& DEBUG_SMARTY
)) {
142 $this->display($this->_tpl
);
146 $this->assign('simple', true
);
149 $this->register_modifier('escape_html', 'escape_html');
150 $this->default_modifiers
= Array('@escape_html');
152 if (S
::i('auth') <= AUTH_PUBLIC
) {
153 $this->register_outputfilter('hide_emails');
155 header("Accept-Charset: utf-8");
156 if (Env
::v('forceXml')) {
157 pl_content_headers("text/xml");
160 if (!$globals->debug
) {
162 $this->display($skin);
166 $this->assign('validate', true
);
167 if (!($globals->debug
& DEBUG_SMARTY
)) {
170 $START_SMARTY = microtime(true
);
171 $result = $this->fetch($skin);
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);
175 if ($globals->debug
& DEBUG_BT
) {
176 PlBacktrace
::clean();
177 $this->assign_by_ref('backtraces', PlBacktrace
::$bt);
178 $result = str_replace('@@BACKTRACE@@',
179 $this->fetch(self
::getCoreTpl('backtrace.tpl')),
182 $result = str_replace('@@BACKTRACE@@', '', $result);
185 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
186 if ($globals->debug
& DEBUG_VALID
) {
187 $fd = fopen($this->compile_dir
."/valid.html","w");
188 fwrite($fd, $result);
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 />';
196 $replc = "<span class='erreur'><a href='http://validator.w3.org/check?uri={$globals->baseurl}"
197 ."/valid.html&ss=1#result'>{$m[1]} ERREUR(S) !!!</a></span><br />";
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 kill()
247 public function kill($msg, $type = 'errors')
249 // PHP is used on command line... do not run the whole page stuff.
250 if (php_sapi_name() == 'cli') {
257 $this->trig($msg, $type);
258 $this->_failure
= true
;
262 public function killError($msg)
264 $this->kill($msg, 'errors');
267 public function killWarning($msg)
269 $this->kill($msg, 'warnings');
272 public function killSuccess($msg)
274 $this->kill($msg, 'success');
278 // {{{ function setTitle
280 public function setTitle($title)
283 if (isset($globals->core
->sitename
)) {
284 $title = $globals->core
->sitename
. ' :: ' . $title;
286 $this->assign('pl_title', $title);
290 // {{{ function addJsLink
292 public function addJsLink($filename, $static_content = true
)
294 if ($static_content) {
295 $this->append('pl_js', pl_static_content_path("javascript/", $filename));
297 $this->append('pl_js', "javascript/$filename");
302 // {{{ function addCssLink
304 public function addCssLink($path)
306 $this->append('pl_css', $path);
310 // {{{ function addLink
312 public function addLink($rel, $path)
314 $this->append('pl_link', array('rel' => $rel, 'href' => $path));
319 // {{{ function addCssInline
321 public function addCssInline($css)
324 $this->append('pl_inline_css', $css);
329 // {{{ function setRssLink
331 public function setRssLink($title, $path)
333 $this->assign('pl_rss', array('title' => $title, 'href' => $path));
337 // {{{ function jsonDisplay
338 protected function jsonDisplay()
340 pl_content_headers("text/javascript");
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').' = ';
347 } elseif (Env
::has('jsonFunc')) {
348 $jsonbegin = Env
::v('jsonFunc').'(';
351 echo $jsonbegin, json_encode($this->_jsonVars
), $jsonend;
355 // {{{ function jsonAssign
356 public function jsonAssign($var, $value)
358 $this->_jsonVars
[$var] = $value;
364 function escape_xorgDB(&$item, $key)
366 if ($item instanceof XOrgDBIterator
) {
368 while ($a = $item->next()) {
375 // {{{ function escape_html ()
378 * default smarty plugin, used to auto-escape dangerous html.
383 * & not followed by some entity --> &
385 function escape_html($string)
387 if (is_string($string)) {
388 return htmlspecialchars($string, ENT_QUOTES
, 'UTF-8');
395 // {{{ function at_to_globals()
401 function _to_globals($s) {
403 $t = explode('.',$s);
404 if (count($t) == 1) {
405 return var_export($globals->$t[0],true
);
407 return var_export($globals->$t[0]->$t[1],true
);
412 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
415 function at_to_globals($tpl_source, &$smarty)
417 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
421 // {{{ function trimwhitespace
423 function trimwhitespace($source, &$smarty)
425 $tags = '(script|pre|textarea)';
426 preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
427 $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
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);
438 // {{{ function wiki_include
440 function wiki_include($source, &$smarty)
443 return preg_replace('/\{include( [^}]*)? wiki=([^} ]+)(.*?)\}/ui',
444 '{include\1 file="' . $globals->spoolroot
. '/spool/wiki.d/cache_\2.tpl"\3 included=1}',
448 function core_include($source, &$smarty)
451 return preg_replace('/\{include( [^}]*)? core=([^} ]+)(.*?)\}/ui',
452 '{include\1 file="' . $globals->spoolroot
. '/core/templates/\2"\3}',
457 //{{{ function hasPerm
459 function if_rewrites($source, &$smarty)
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}'),
474 function form_force_encodings($source, &$smarty)
476 return preg_replace('/<form[^\w]/',
477 '\0 accept-charset="utf-8" ',
482 // {{{ function hide_emails
484 function _hide_email($source)
486 $source = str_replace("\n", '', $source);
487 return '<script type="text/javascript">//<![CDATA[' . "\n" .
488 'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
492 function hide_emails($source, &$smarty)
494 if (!strpos($source, '@')) {
498 //prevent email replacement in <script> and <textarea>
499 $tags = '(script|textarea|select)';
500 preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
501 $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
503 //catch all emails in <a href="mailto:...">
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);
507 //prevant replacement in tag attributes
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);
512 $source = preg_replace('!([-a-z0-9_+.]+@[-a-z0-9_.]+)!iue', '_hide_email("\1")', $source);
513 $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source);
516 $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source);
517 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
524 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: