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->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 switch ($this->_page_type
) {
88 $this->display($this->_tpl
);
92 $this->assign('simple', true
);
95 $this->register_modifier('escape_html', 'escape_html');
96 $this->default_modifiers
= Array('@escape_html');
98 $this->register_outputfilter('hide_emails');
99 $this->addJsLink('wiki.js');
100 header("Accept-Charset: iso-8859-15, latin9, us-ascii, ascii");
102 if (!$globals->debug
) {
104 $this->display($skin);
108 if ($globals->debug
& 1) {
109 $this->assign('db_trace', XDB
::trace_format($this, 'skin/common.database-debug.tpl'));
112 $this->assign('validate', true
);
114 $result = $this->fetch($skin);
115 $ttime = sprintf('Temps total: %.02fs<br />', microtime_float() - $TIME_BEGIN);
116 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
118 if ($globals->debug
& 2) {
120 $fd = fopen($this->compile_dir
."/valid.html","w");
121 fwrite($fd, $result);
124 exec($globals->spoolroot
."/bin/devel/xhtml.validate.pl ".$this->compile_dir
."/valid.html", $val);
125 foreach ($val as $h) {
126 if (preg_match("/^X-W3C-Validator-Errors: (\d+)$/", $h, $m)) {
127 $replc = '<span style="color: #080;">HTML OK</span><br />';
129 $replc = "<span class='erreur'><a href='http://validator.w3.org/check?uri={$globals->baseurl}"
130 ."/valid.html&ss=1#result'>{$m[1]} ERREUR(S) !!!</a></span><br />";
137 echo str_replace("@HOOK@", $ttime.$replc, $result);
142 // {{{ function nb_errs()
146 return count($this->_errors
);
150 // {{{ function trig()
154 $this->_errors
[] = $msg;
158 // {{{ function kill()
164 $this->assign('platal', $platal);
166 $this->_failure
= true
;
171 // {{{ function addJsLink
173 function addJsLink($path)
175 $this->append('xorg_js', $path);
179 // {{{ function addCssLink
181 function addCssLink($path)
183 $this->append('xorg_css', $path);
187 // {{{ function setRssLink
189 function setRssLink($title, $path)
191 $this->assign('xorg_rss', array('title' => $title, 'href' => $path));
197 // {{{ function escape_html ()
200 * default smarty plugin, used to auto-escape dangerous html.
205 * & not followed by some entity --> &
207 function escape_html($string)
209 if (is_string($string)) {
210 $transtbl = Array('<' => '<', '>' => '>', '"' => '"', '\'' => ''');
211 return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/", "&" , strtr($string, $transtbl));
218 // {{{ function at_to_globals()
224 function _to_globals($s) {
226 $t = explode('.',$s);
227 if (count($t) == 1) {
228 return var_export($globals->$t[0],true
);
230 return var_export($globals->$t[0]->$t[1],true
);
235 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
238 function at_to_globals($tpl_source, &$smarty)
240 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
244 // {{{ function trimwhitespace
246 function trimwhitespace($source, &$smarty)
248 $tags = array('script', 'pre', 'textarea');
250 foreach ($tags as $tag) {
251 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, $
{$tag});
252 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
255 // remove all leading spaces, tabs and carriage returns NOT
256 // preceeded by a php close tag.
257 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
259 foreach ($tags as $tag) {
260 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
269 function form_force_encodings($source, &$smarty)
271 return preg_replace('/<form[^\w]/',
272 '\0 accept-charset="iso-8859-15 latin9 us-ascii ascii" ',
277 // {{{ function hide_emails
279 function _hide_email($source)
281 $source = str_replace("\n", '', $source);
282 return '<script type="text/javascript">//<![CDATA[' . "\n" .
283 'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
287 function hide_emails($source, &$smarty)
289 //prevent email replacement in <script> and <textarea>
290 $tags = array('script', 'textarea', 'select');
292 foreach ($tags as $tag) {
293 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, $
{$tag});
294 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
297 //catch all emails in <a href="mailto:...">
298 preg_match_all("!<a[^>]+href=[\"'][^\"']*[-a-z0-9_.]+@[-a-z0-9_.]+[^\"']*[\"'][^>]*>.*?</a>!is", $source, $ahref);
299 $source = preg_replace("!<a[^>]+href=[\"'][^\"']*[-a-z0-9_.]+@[-a-z0-9_.]+[^\"']*[\"'][^>]*>.*?</a>!is", '&&&ahref&&&', $source);
301 //prevant replacement in tag attributes
302 preg_match_all("!<[^>]+[-a-z0-9_.]+@[-a-z0-9_.]+[^>]+>!is", $source, $misc);
303 $source = preg_replace("!<[^>]+[-a-z0-9_.]+@[-a-z0-9_.]+[^>]+>!is", '&&&misc&&&', $source);
306 $source = preg_replace('!([-a-z0-9_.]+@[-a-z0-9_.]+)!ie', '_hide_email("\1")', $source);
307 $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source);
310 $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source);
311 foreach ($tags as $tag) {
312 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
320 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: