2667cd0a7abac3ff33d4709908bb6bdf99b34f50
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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->addJsLink('xorg.js');
62 // {{{ function changeTpl()
64 function changeTpl($tpl, $type = SKINNED
)
67 $this->_page_type
= $type;
68 $this->assign('xorg_tpl', $tpl);
72 // {{{ function _run()
76 global $globals, $TIME_BEGIN;
78 session_write_close();
80 $this->assign('xorg_errors', $this->_errors
);
81 $this->assign('xorg_failure', $this->_failure
);
82 $this->assign('globals', $globals);
84 switch ($this->_page_type
) {
87 $this->display($this->_tpl
);
91 $this->assign('simple', true
);
94 $this->register_modifier('escape_html', 'escape_html');
95 $this->default_modifiers
= Array('@escape_html');
97 $this->register_outputfilter('hide_emails');
98 $this->addJsLink('wiki.js');
100 if (!$globals->debug
) {
102 $this->display($skin);
106 if ($globals->debug
& 1) {
107 $this->assign('db_trace', XDB
::trace_format($this, 'database-debug.tpl'));
110 $this->assign('validate', true
);
112 $result = $this->fetch($skin);
113 $ttime = sprintf('Temps total: %.02fs<br />', microtime_float() - $TIME_BEGIN);
114 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
116 if ($globals->debug
& 2) {
118 $fd = fopen($this->compile_dir
."/valid.html","w");
119 fwrite($fd, $result);
122 exec($globals->spoolroot
."/bin/devel/xhtml.validate.pl ".$this->compile_dir
."/valid.html", $val);
123 foreach ($val as $h) {
124 if (preg_match("/^X-W3C-Validator-Errors: (\d+)$/", $h, $m)) {
125 $replc = '<span style="color: #080;">HTML OK</span><br />';
127 $replc = "<span class='erreur'><a href='http://validator.w3.org/check?uri={$globals->baseurl}"
128 ."/valid.html&ss=1#result'>{$m[1]} ERREUR(S) !!!</a></span><br />";
135 echo str_replace("@HOOK@", $ttime.$replc, $result);
140 // {{{ function nb_errs()
144 return count($this->_errors
);
148 // {{{ function trig()
152 $this->_errors
[] = $msg;
156 // {{{ function kill()
162 $this->assign('platal', $platal);
164 $this->_failure
= true
;
169 // {{{ function addJsLink
171 function addJsLink($path)
173 $this->append('xorg_js', $path);
177 // {{{ function addCssLink
179 function addCssLink($path)
181 $this->append('xorg_css', $path);
185 // {{{ function setRssLink
187 function setRssLink($title, $path)
189 $this->assign('xorg_rss', array('title' => $title, 'href' => $path));
195 // {{{ function escape_html ()
198 * default smarty plugin, used to auto-escape dangerous html.
203 * & not followed by some entity --> &
205 function escape_html($string)
207 if (is_string($string)) {
208 $transtbl = Array('<' => '<', '>' => '>', '"' => '"', '\'' => ''');
209 return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/", "&" , strtr($string, $transtbl));
216 // {{{ function at_to_globals()
222 function _to_globals($s) {
224 $t = explode('.',$s);
225 if (count($t) == 1) {
226 return var_export($globals->$t[0],true
);
228 return var_export($globals->$t[0]->$t[1],true
);
233 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
236 function at_to_globals($tpl_source, &$smarty)
238 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
242 // {{{ function trimwhitespace
244 function trimwhitespace($source, &$smarty)
246 $tags = array('script', 'pre', 'textarea');
248 foreach ($tags as $tag) {
249 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, $
{$tag});
250 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
253 // remove all leading spaces, tabs and carriage returns NOT
254 // preceeded by a php close tag.
255 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
257 foreach ($tags as $tag) {
258 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
265 // {{{ function hide_emails
267 function _hide_email($source)
269 return '<script type="text/javascript">Nix.decode("' . addslashes(str_rot13($source)) . '");</script>';
272 function hide_emails($source, &$smarty)
274 //prevent email replacement in <script> and <textarea>
275 $tags = array('script', 'textarea', 'select');
277 foreach ($tags as $tag) {
278 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, $
{$tag});
279 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
282 //catch all emails in <a href="mailto:...">
283 preg_match_all("!<a[^>]+href=[\"'][^\"']*[-a-z0-9_.]+@[-a-z0-9_.]+[^\"']*[\"'][^>]*>.*?</a>!is", $source, $ahref);
284 $source = preg_replace("!<a[^>]+href=[\"'][^\"']*[-a-z0-9_.]+@[-a-z0-9_.]+[^\"']*[\"'][^>]*>.*?</a>!is", '&&&ahref&&&', $source);
286 //prevant replacement in tag attributes
287 preg_match_all("!<[^>]+[-a-z0-9_.]+@[-a-z0-9_.]+[^>]+>!is", $source, $misc);
288 $source = preg_replace("!<[^>]+[-a-z0-9_.]+@[-a-z0-9_.]+[^>]+>!is", '&&&misc&&&', $source);
291 $source = preg_replace('!([-a-z0-9_.]+@[-a-z0-9_.]+)!ie', '_hide_email("\1")', $source);
292 $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source);
295 $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source);
296 foreach ($tags as $tag) {
297 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
305 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: