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: iso-8859-15, latin9, us-ascii, ascii");
110 if (!$globals->debug
) {
112 $this->display($skin);
116 if ($globals->debug
& 1) {
117 $this->assign('db_trace', XDB
::trace_format($this, 'skin/common.database-debug.tpl'));
120 $this->assign('validate', true
);
122 $result = $this->fetch($skin);
123 $ttime = sprintf('Temps total: %.02fs<br />', microtime_float() - $TIME_BEGIN);
124 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
126 if ($globals->debug
& 2) {
128 $fd = fopen($this->compile_dir
."/valid.html","w");
129 fwrite($fd, $result);
132 exec($globals->spoolroot
."/bin/devel/xhtml.validate.pl ".$this->compile_dir
."/valid.html", $val);
133 foreach ($val as $h) {
134 if (preg_match("/^X-W3C-Validator-Errors: (\d+)$/", $h, $m)) {
135 $replc = '<span style="color: #080;">HTML OK</span><br />';
137 $replc = "<span class='erreur'><a href='http://validator.w3.org/check?uri={$globals->baseurl}"
138 ."/valid.html&ss=1#result'>{$m[1]} ERREUR(S) !!!</a></span><br />";
145 echo str_replace("@HOOK@", $ttime.$replc, $result);
150 // {{{ function nb_errs()
154 return count($this->_errors
);
158 // {{{ function trig()
162 $this->_errors
[] = $msg;
166 // {{{ function kill()
172 $this->assign('platal', $platal);
174 $this->_failure
= true
;
179 // {{{ function addJsLink
181 function addJsLink($path)
183 $this->append('xorg_js', $path);
187 // {{{ function addCssLink
189 function addCssLink($path)
191 $this->append('xorg_css', $path);
195 // {{{ function addCssInline
197 function addCssInline($css)
200 $this->append('xorg_inline_css', $css);
205 // {{{ function setRssLink
207 function setRssLink($title, $path)
209 $this->assign('xorg_rss', array('title' => $title, 'href' => $path));
215 // {{{ function escape_html ()
218 * default smarty plugin, used to auto-escape dangerous html.
223 * & not followed by some entity --> &
225 function escape_html($string)
227 if (is_string($string)) {
228 $transtbl = Array('<' => '<', '>' => '>', '"' => '"', '\'' => ''');
229 return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/", "&" , strtr($string, $transtbl));
236 // {{{ function at_to_globals()
242 function _to_globals($s) {
244 $t = explode('.',$s);
245 if (count($t) == 1) {
246 return var_export($globals->$t[0],true
);
248 return var_export($globals->$t[0]->$t[1],true
);
253 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
256 function at_to_globals($tpl_source, &$smarty)
258 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
262 // {{{ function trimwhitespace
264 function trimwhitespace($source, &$smarty)
266 $tags = array('script', 'pre', 'textarea');
268 foreach ($tags as $tag) {
269 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, $
{$tag});
270 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
273 // remove all leading spaces, tabs and carriage returns NOT
274 // preceeded by a php close tag.
275 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
277 foreach ($tags as $tag) {
278 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
287 function form_force_encodings($source, &$smarty)
289 return preg_replace('/<form[^\w]/',
290 '\0 accept-charset="iso-8859-15 latin9 us-ascii ascii" ',
295 // {{{ function hide_emails
297 function _hide_email($source)
299 $source = str_replace("\n", '', $source);
300 return '<script type="text/javascript">//<![CDATA[' . "\n" .
301 'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
305 function hide_emails($source, &$smarty)
307 //prevent email replacement in <script> and <textarea>
308 $tags = array('script', 'textarea', 'select');
310 foreach ($tags as $tag) {
311 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, $
{$tag});
312 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
315 //catch all emails in <a href="mailto:...">
316 preg_match_all("!<a[^>]+href=[\"'][^\"']*[-a-z0-9_.]+@[-a-z0-9_.]+[^\"']*[\"'][^>]*>.*?</a>!is", $source, $ahref);
317 $source = preg_replace("!<a[^>]+href=[\"'][^\"']*[-a-z0-9_.]+@[-a-z0-9_.]+[^\"']*[\"'][^>]*>.*?</a>!is", '&&&ahref&&&', $source);
319 //prevant replacement in tag attributes
320 preg_match_all("!<[^>]+[-a-z0-9_.]+@[-a-z0-9_.]+[^>]+>!is", $source, $misc);
321 $source = preg_replace("!<[^>]+[-a-z0-9_.]+@[-a-z0-9_.]+[^>]+>!is", '&&&misc&&&', $source);
324 $source = preg_replace('!([-a-z0-9_.]+@[-a-z0-9_.]+)!ie', '_hide_email("\1")', $source);
325 $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source);
328 $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source);
329 foreach ($tags as $tag) {
330 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
338 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: