0568d330b22607f4928040253dfef32e304da56c
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 require_once 'smarty/libs/Smarty.class.php';
24 class PlatalPage
extends Smarty
33 var $config_overwrite = false
;
34 var $use_sub_dirs = false
;
36 // {{{ function PlatalPage()
38 function PlatalPage($tpl, $type = SKINNED
)
44 $this->template_dir
= $globals->spoolroot
."/templates/";
45 $this->compile_dir
= $globals->spoolroot
."/spool/templates_c/";
46 array_unshift($this->plugins_dir
, $globals->spoolroot
."/plugins/");
47 $this->config_dir
= $globals->spoolroot
."/configs/";
49 $this->compile_check
= !empty($globals->debug
);
51 $this->_page_type
= $type;
53 $this->_errors
= array();
54 $this->_failure
= false
;
56 $this->register_prefilter('at_to_globals');
57 $this->register_prefilter('trimwhitespace');
58 $this->register_prefilter('form_force_encodings');
59 $this->addJsLink('xorg.js');
63 // {{{ function changeTpl()
65 function changeTpl($tpl, $type = SKINNED
)
68 $this->_page_type
= $type;
69 $this->assign('xorg_tpl', $tpl);
73 // {{{ function _run()
77 global $globals, $TIME_BEGIN;
79 session_write_close();
81 $this->assign('xorg_errors', $this->_errors
);
82 $this->assign('xorg_failure', $this->_failure
);
83 $this->assign('globals', $globals);
85 if (Env
::v('display') == 'light') {
86 $this->_page_type
= SIMPLE
;
87 } elseif (Env
::v('display') == 'raw') {
88 $this->_page_type
= NO_SKIN
;
89 } elseif (Env
::v('display') == 'full') {
90 $this->_page_typ
= SKINNED
;
93 switch ($this->_page_type
) {
96 $this->display($this->_tpl
);
100 $this->assign('simple', true
);
103 $this->register_modifier('escape_html', 'escape_html');
104 $this->default_modifiers
= Array('@escape_html');
106 $this->register_outputfilter('hide_emails');
107 $this->addJsLink('wiki.js');
108 header("Accept-Charset: utf-8");
110 if (!$globals->debug
) {
112 $this->display($skin);
116 if ($globals->debug
& 1) {
117 PlBacktrace
::clean();
118 $this->assign_by_ref('backtraces', PlBacktrace
::$bt);
121 $this->assign('validate', true
);
123 $result = $this->fetch($skin);
124 $ttime = sprintf('Temps total: %.02fs - Mémoire totale : %.02fKo<br />', microtime(true
) - $TIME_BEGIN
125 , memory_get_peak_usage(true
) / 1024);
126 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
128 if ($globals->debug
& 2) {
129 $fd = fopen($this->compile_dir
."/valid.html","w");
130 fwrite($fd, $result);
133 exec($globals->spoolroot
."/bin/devel/xhtml.validate.pl ".$this->compile_dir
."/valid.html", $val);
134 foreach ($val as $h) {
135 if (preg_match("/^X-W3C-Validator-Errors: (\d+)$/", $h, $m)) {
136 $replc = '<span style="color: #080;">HTML OK</span><br />';
138 $replc = "<span class='erreur'><a href='http://validator.w3.org/check?uri={$globals->baseurl}"
139 ."/valid.html&ss=1#result'>{$m[1]} ERREUR(S) !!!</a></span><br />";
146 echo str_replace("@HOOK@", $ttime.$replc, $result);
151 // {{{ function nb_errs()
155 return count($this->_errors
);
159 // {{{ function trig()
163 $this->_errors
[] = $msg;
167 // {{{ function kill()
173 $this->assign('platal', $platal);
175 $this->_failure
= true
;
180 // {{{ function addJsLink
182 function addJsLink($path)
184 $this->append('xorg_js', $path);
188 // {{{ function addCssLink
190 function addCssLink($path)
192 $this->append('xorg_css', $path);
196 // {{{ function addCssInline
198 function addCssInline($css)
201 $this->append('xorg_inline_css', $css);
206 // {{{ function setRssLink
208 function setRssLink($title, $path)
210 $this->assign('xorg_rss', array('title' => $title, 'href' => $path));
216 // {{{ function escape_html ()
219 * default smarty plugin, used to auto-escape dangerous html.
224 * & not followed by some entity --> &
226 function escape_html($string)
228 if (is_string($string)) {
229 $transtbl = Array('<' => '<', '>' => '>', '"' => '"', '\'' => ''');
230 return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/", "&" , strtr($string, $transtbl));
237 // {{{ function at_to_globals()
243 function _to_globals($s) {
245 $t = explode('.',$s);
246 if (count($t) == 1) {
247 return var_export($globals->$t[0],true
);
249 return var_export($globals->$t[0]->$t[1],true
);
254 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
257 function at_to_globals($tpl_source, &$smarty)
259 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
263 // {{{ function trimwhitespace
265 function trimwhitespace($source, &$smarty)
267 $tags = '(script|pre|textarea)';
268 preg_match_all("!<$tags.*?>.*?</$tags>!ius", $source, $tagsmatches);
269 $source = preg_replace("!<$tags.*?>.*?</$tags>!ius", "&&&tags&&&", $source);
271 // remove all leading spaces, tabs and carriage returns NOT
272 // preceeded by a php close tag.
273 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
274 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
282 function form_force_encodings($source, &$smarty)
284 return preg_replace('/<form[^\w]/',
285 '\0 accept-charset="utf-8" ',
290 // {{{ function hide_emails
292 function _hide_email($source)
294 $source = str_replace("\n", '', $source);
295 return '<script type="text/javascript">//<![CDATA[' . "\n" .
296 'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
300 function hide_emails($source, &$smarty)
302 //prevent email replacement in <script> and <textarea>
303 $tags = '(script|textarea|select)';
304 preg_match_all("!<$tags.*?>.*?</$tags>!ius", $source, $tagsmatches);
305 $source = preg_replace("!<$tags.*?>.*?</$tags>!ius", "&&&tags&&&", $source);
307 //catch all emails in <a href="mailto:...">
308 preg_match_all("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", $source, $ahref);
309 $source = preg_replace("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", '&&&ahref&&&', $source);
311 //prevant replacement in tag attributes
312 preg_match_all("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!is", $source, $misc);
313 $source = preg_replace("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!is", '&&&misc&&&', $source);
316 $source = preg_replace('!([-a-z0-9_+.]+@[-a-z0-9_.]+)!ie', '_hide_email("\1")', $source);
317 $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source);
320 $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source);
321 $source = preg_replace("!&&&tags&&&!e", 'array_shift($tagsmatches[0])', $source);
328 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: