| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
| 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 | |
| 22 | if (!@include_once 'smarty/libs/Smarty.class.php') { |
| 23 | require_once 'smarty/Smarty.class.php'; |
| 24 | } |
| 25 | |
| 26 | abstract class PlPage extends Smarty |
| 27 | { |
| 28 | private $_page_type; |
| 29 | private $_tpl; |
| 30 | private $_errors; |
| 31 | private $_failure; |
| 32 | private $_jsonVars; |
| 33 | |
| 34 | // {{{ function PlPage() |
| 35 | |
| 36 | public function __construct() |
| 37 | { |
| 38 | parent::Smarty(); |
| 39 | |
| 40 | global $globals; |
| 41 | |
| 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/'; |
| 51 | |
| 52 | $this->compile_check = !empty($globals->debug); |
| 53 | |
| 54 | $this->_errors = array('errors' => array()); |
| 55 | $this->_jsonVars = array(); |
| 56 | $this->_failure = false; |
| 57 | |
| 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"); |
| 61 | } |
| 62 | $this->register_prefilter('at_to_globals'); |
| 63 | $this->addJsLink('jquery.js'); |
| 64 | } |
| 65 | |
| 66 | // }}} |
| 67 | // {{{ function changeTpl() |
| 68 | |
| 69 | public function changeTpl($tpl, $type = SKINNED) |
| 70 | { |
| 71 | $this->_tpl = $tpl; |
| 72 | $this->_page_type = $type; |
| 73 | $this->assign('pl_tpl', $tpl); |
| 74 | } |
| 75 | |
| 76 | // }}} |
| 77 | // {{{ function getCoreTpl() |
| 78 | |
| 79 | public static function getCoreTpl($tpl) |
| 80 | { |
| 81 | global $globals; |
| 82 | return $globals->spoolroot . '/core/templates/' . $tpl; |
| 83 | } |
| 84 | |
| 85 | // }}} |
| 86 | // {{{ function coreTpl() |
| 87 | |
| 88 | /** Use a template from the core. |
| 89 | */ |
| 90 | public function coreTpl($tpl, $type = SKINNED) |
| 91 | { |
| 92 | global $globals; |
| 93 | $this->changeTpl(self::getCoreTpl($tpl), $type); |
| 94 | } |
| 95 | |
| 96 | // }}} |
| 97 | // {{{ function raw() |
| 98 | |
| 99 | public function raw() |
| 100 | { |
| 101 | global $globals; |
| 102 | $this->assign('globals', $globals); |
| 103 | return $this->fetch($this->_tpl); |
| 104 | } |
| 105 | |
| 106 | // }}} |
| 107 | // {{{ function _run() |
| 108 | |
| 109 | protected function _run($skin) |
| 110 | { |
| 111 | global $globals, $platal, $TIME_BEGIN; |
| 112 | |
| 113 | Platal::session()->close(); |
| 114 | |
| 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); |
| 125 | |
| 126 | if (Env::has('json') && count($this->_jsonVars)) { |
| 127 | return $this->jsonDisplay(); |
| 128 | } |
| 129 | |
| 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; |
| 136 | } |
| 137 | |
| 138 | switch ($this->_page_type) { |
| 139 | case NO_SKIN: |
| 140 | if (!($globals->debug & DEBUG_SMARTY)) { |
| 141 | error_reporting(0); |
| 142 | } |
| 143 | $this->display($this->_tpl); |
| 144 | exit; |
| 145 | |
| 146 | case SIMPLE: |
| 147 | $this->assign('simple', true); |
| 148 | |
| 149 | case SKINNED: |
| 150 | $this->register_modifier('escape_html', 'escape_html'); |
| 151 | $this->default_modifiers = Array('@escape_html'); |
| 152 | } |
| 153 | if (S::i('auth') <= AUTH_PUBLIC) { |
| 154 | $this->register_outputfilter('hide_emails'); |
| 155 | } |
| 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"); |
| 160 | } |
| 161 | |
| 162 | if (!$globals->debug) { |
| 163 | error_reporting(0); |
| 164 | $this->display($skin); |
| 165 | exit; |
| 166 | } |
| 167 | |
| 168 | $this->assign('validate', true); |
| 169 | if (!($globals->debug & DEBUG_SMARTY)) { |
| 170 | error_reporting(0); |
| 171 | } |
| 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')), |
| 182 | $result); |
| 183 | } else { |
| 184 | $result = str_replace('@@BACKTRACE@@', '', $result); |
| 185 | } |
| 186 | |
| 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); |
| 191 | fclose($fd); |
| 192 | |
| 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 />'; |
| 197 | if ($m[1]) { |
| 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 />"; |
| 200 | } |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | echo str_replace("@HOOK@", $ttime.$replc, $result); |
| 207 | exit; |
| 208 | } |
| 209 | |
| 210 | abstract public function run(); |
| 211 | |
| 212 | // }}} |
| 213 | // {{{ function nb_errs() |
| 214 | |
| 215 | public function nb_errs() |
| 216 | { |
| 217 | return count($this->_errors['errors']); |
| 218 | } |
| 219 | |
| 220 | // }}} |
| 221 | // {{{ function trig() |
| 222 | |
| 223 | private function trig($msg, $type = 'errors') |
| 224 | { |
| 225 | if (!isset($this->_errors[$type])) { |
| 226 | $this->_errors[$type] = array(); |
| 227 | } |
| 228 | $this->_errors[$type][] = $msg; |
| 229 | } |
| 230 | |
| 231 | public function trigError($msg) |
| 232 | { |
| 233 | $this->trig($msg, 'errors'); |
| 234 | } |
| 235 | |
| 236 | public function trigWarning($msg) |
| 237 | { |
| 238 | $this->trig($msg, 'warnings'); |
| 239 | } |
| 240 | |
| 241 | public function trigSuccess($msg) |
| 242 | { |
| 243 | $this->trig($msg, 'success'); |
| 244 | } |
| 245 | |
| 246 | // }}} |
| 247 | // {{{ function kill() |
| 248 | |
| 249 | public function kill($msg) |
| 250 | { |
| 251 | // PHP is used on command line... do not run the whole page stuff. |
| 252 | if (php_sapi_name() == 'cli') { |
| 253 | echo $msg . "\n"; |
| 254 | exit; |
| 255 | } |
| 256 | |
| 257 | global $platal; |
| 258 | |
| 259 | $this->trigError($msg); |
| 260 | $this->_failure = true; |
| 261 | $this->run(); |
| 262 | } |
| 263 | |
| 264 | // }}} |
| 265 | // {{{ function setTitle |
| 266 | |
| 267 | public function setTitle($title) |
| 268 | { |
| 269 | global $globals; |
| 270 | if (isset($globals->core->sitename)) { |
| 271 | $title = $globals->core->sitename . ' :: ' . $title; |
| 272 | } |
| 273 | $this->assign('pl_title', $title); |
| 274 | } |
| 275 | |
| 276 | // }}} |
| 277 | // {{{ function addJsLink |
| 278 | |
| 279 | public function addJsLink($path) |
| 280 | { |
| 281 | $this->append('pl_js', $path); |
| 282 | } |
| 283 | |
| 284 | // }}} |
| 285 | // {{{ function addCssLink |
| 286 | |
| 287 | public function addCssLink($path) |
| 288 | { |
| 289 | $this->append('pl_css', $path); |
| 290 | } |
| 291 | |
| 292 | // }}} |
| 293 | // {{{ function addLink |
| 294 | |
| 295 | public function addLink($rel, $path) |
| 296 | { |
| 297 | $this->append('pl_link', array('rel' => $rel, 'href' => $path)); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | // }}} |
| 302 | // {{{ function addCssInline |
| 303 | |
| 304 | public function addCssInline($css) |
| 305 | { |
| 306 | if (!empty($css)) { |
| 307 | $this->append('pl_inline_css', $css); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // }}} |
| 312 | // {{{ function setRssLink |
| 313 | |
| 314 | public function setRssLink($title, $path) |
| 315 | { |
| 316 | $this->assign('pl_rss', array('title' => $title, 'href' => $path)); |
| 317 | } |
| 318 | |
| 319 | // }}} |
| 320 | // {{{ function jsonDisplay |
| 321 | protected function jsonDisplay() |
| 322 | { |
| 323 | header("Content-type: text/javascript; charset=utf-8"); |
| 324 | array_walk_recursive($this->_jsonVars, "escape_xorgDB"); |
| 325 | $jsonbegin = Env::v('jsonBegin'); |
| 326 | $jsonend = Env::v('jsonEnd'); |
| 327 | if (Env::has('jsonVar')) { |
| 328 | $jsonbegin = Env::v('jsonVar').' = '; |
| 329 | $jsonend = ';'; |
| 330 | } elseif (Env::has('jsonFunc')) { |
| 331 | $jsonbegin = Env::v('jsonFunc').'('; |
| 332 | $jsonend = ');'; |
| 333 | } |
| 334 | echo $jsonbegin, json_encode($this->_jsonVars), $jsonend; |
| 335 | exit; |
| 336 | } |
| 337 | // }}} |
| 338 | // {{{ function jsonAssign |
| 339 | public function jsonAssign($var, $value) |
| 340 | { |
| 341 | $this->_jsonVars[$var] = $value; |
| 342 | } |
| 343 | |
| 344 | // }}} |
| 345 | } |
| 346 | |
| 347 | function escape_xorgDB(&$item, $key) |
| 348 | { |
| 349 | if ($item instanceof XOrgDBIterator) { |
| 350 | $expanded = array(); |
| 351 | while ($a = $item->next()) { |
| 352 | $expanded[] = $a; |
| 353 | } |
| 354 | $item = $expanded; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | // {{{ function escape_html () |
| 359 | |
| 360 | /** |
| 361 | * default smarty plugin, used to auto-escape dangerous html. |
| 362 | * |
| 363 | * < --> < |
| 364 | * > --> > |
| 365 | * " --> " |
| 366 | * & not followed by some entity --> & |
| 367 | */ |
| 368 | function escape_html($string) |
| 369 | { |
| 370 | if (is_string($string)) { |
| 371 | return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); |
| 372 | } else { |
| 373 | return $string; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // }}} |
| 378 | // {{{ function at_to_globals() |
| 379 | |
| 380 | /** |
| 381 | * helper |
| 382 | */ |
| 383 | |
| 384 | function _to_globals($s) { |
| 385 | global $globals; |
| 386 | $t = explode('.',$s); |
| 387 | if (count($t) == 1) { |
| 388 | return var_export($globals->$t[0],true); |
| 389 | } else { |
| 390 | return var_export($globals->$t[0]->$t[1],true); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * compilation plugin used to import $globals confing through #globals.foo.bar# directives |
| 396 | */ |
| 397 | |
| 398 | function at_to_globals($tpl_source, &$smarty) |
| 399 | { |
| 400 | return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source); |
| 401 | } |
| 402 | |
| 403 | // }}} |
| 404 | // {{{ function trimwhitespace |
| 405 | |
| 406 | function trimwhitespace($source, &$smarty) |
| 407 | { |
| 408 | $tags = '(script|pre|textarea)'; |
| 409 | preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches); |
| 410 | $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source); |
| 411 | |
| 412 | // remove all leading spaces, tabs and carriage returns NOT |
| 413 | // preceeded by a php close tag. |
| 414 | $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source); |
| 415 | $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source); |
| 416 | |
| 417 | return $source; |
| 418 | } |
| 419 | |
| 420 | // }}} |
| 421 | // {{{ function wiki_include |
| 422 | |
| 423 | function wiki_include($source, &$smarty) |
| 424 | { |
| 425 | global $globals; |
| 426 | return preg_replace('/\{include( [^}]*)? wiki=([^} ]+)(.*?)\}/ui', |
| 427 | '{include\1 file="' . $globals->spoolroot . '/spool/wiki.d/cache_\2.tpl"\3 included=1}', |
| 428 | $source); |
| 429 | } |
| 430 | |
| 431 | function core_include($source, &$smarty) |
| 432 | { |
| 433 | global $globals; |
| 434 | return preg_replace('/\{include( [^}]*)? core=([^} ]+)(.*?)\}/ui', |
| 435 | '{include\1 file="' . $globals->spoolroot . '/core/templates/\2"\3}', |
| 436 | $source); |
| 437 | } |
| 438 | |
| 439 | // }}} |
| 440 | //{{{ function hasPerm |
| 441 | |
| 442 | function if_has_perms($source, &$smarty) |
| 443 | { |
| 444 | $source = preg_replace('/\{if([^}]*) (\!?)hasPerms\(([^)]+)\)([^}]*)\}/', |
| 445 | '{if\1 \2$smarty.session.perms->hasFlagCombination(\3)\4}', |
| 446 | $source); |
| 447 | return preg_replace('/\{if([^}]*) (\!?)hasPerm\(([^)]+)\)([^}]*)\}/', |
| 448 | '{if\1 \2($smarty.session.perms && $smarty.session.perms->hasFlag(\3))\4}', |
| 449 | $source); |
| 450 | } |
| 451 | |
| 452 | // }}} |
| 453 | // {{{ |
| 454 | |
| 455 | function form_force_encodings($source, &$smarty) |
| 456 | { |
| 457 | return preg_replace('/<form[^\w]/', |
| 458 | '\0 accept-charset="utf-8" ', |
| 459 | $source); |
| 460 | } |
| 461 | |
| 462 | // }}} |
| 463 | // {{{ function hide_emails |
| 464 | |
| 465 | function _hide_email($source) |
| 466 | { |
| 467 | $source = str_replace("\n", '', $source); |
| 468 | return '<script type="text/javascript">//<![CDATA[' . "\n" . |
| 469 | 'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" . |
| 470 | '//]]></script>'; |
| 471 | } |
| 472 | |
| 473 | function hide_emails($source, &$smarty) |
| 474 | { |
| 475 | if (!strpos($source, '@')) { |
| 476 | return $source; |
| 477 | } |
| 478 | |
| 479 | //prevent email replacement in <script> and <textarea> |
| 480 | $tags = '(script|textarea|select)'; |
| 481 | preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches); |
| 482 | $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source); |
| 483 | |
| 484 | //catch all emails in <a href="mailto:..."> |
| 485 | preg_match_all("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", $source, $ahref); |
| 486 | $source = preg_replace("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", '&&&ahref&&&', $source); |
| 487 | |
| 488 | //prevant replacement in tag attributes |
| 489 | preg_match_all("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!ius", $source, $misc); |
| 490 | $source = preg_replace("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!ius", '&&&misc&&&', $source); |
| 491 | |
| 492 | //catch ! |
| 493 | $source = preg_replace('!([-a-z0-9_+.]+@[-a-z0-9_.]+)!iue', '_hide_email("\1")', $source); |
| 494 | $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source); |
| 495 | |
| 496 | // restore data |
| 497 | $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source); |
| 498 | $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source); |
| 499 | |
| 500 | return $source; |
| 501 | } |
| 502 | |
| 503 | // }}} |
| 504 | |
| 505 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 506 | ?> |