more simplifications
[platal.git] / classes / Page.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
50a40a33 3 * Copyright (C) 2003-2006 Polytechnique.org *
0337d704 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
f12af8ad 22require_once 'smarty/libs/Smarty.class.php';
0337d704 23
f12af8ad 24class PlatalPage extends Smarty
0337d704 25{
26 // {{{ properties
a3a049fc 27
0337d704 28 var $_page_type;
29 var $_tpl;
30 var $_errors;
860fdaec 31 var $_failure;
0337d704 32
33 // defaults
34 var $caching = false;
35 var $config_overwrite = false;
36 var $use_sub_dirs = false;
37
38 // }}}
39 // {{{ function PlatalPage()
40
62a66dfc 41 function PlatalPage($tpl, $type = SKINNED)
0337d704 42 {
43 global $globals;
44
f12af8ad 45 $this->Smarty();
796aea34 46
0337d704 47 $this->template_dir = $globals->spoolroot."/templates/";
7faf6e6a 48 $this->compile_dir = $globals->spoolroot."/spool/templates_c/";
0337d704 49 array_unshift($this->plugins_dir, $globals->spoolroot."/plugins/");
50 $this->config_dir = $globals->spoolroot."/configs/";
51
52 $this->compile_check = !empty($globals->debug);
53
0337d704 54 $this->_page_type = $type;
55 $this->_tpl = $tpl;
860fdaec 56 $this->_errors = array();
57 $this->_failure = false;
0337d704 58
0337d704 59 $this->register_prefilter('at_to_globals');
60 $this->register_prefilter('trimwhitespace');
c99ef281 61 $this->addJsLink('xorg.js');
0337d704 62 }
63
64 // }}}
65 // {{{ function changeTpl()
66
62a66dfc 67 function changeTpl($tpl, $type = SKINNED)
0337d704 68 {
69 $this->_tpl = $tpl;
70 $this->_page_type = $type;
0337d704 71 $this->_page_type = $type;
72 $this->assign('xorg_tpl', $tpl);
73 }
74
75 // }}}
76 // {{{ function _run()
77
78 function _run($skin)
79 {
80 global $globals, $TIME_BEGIN;
36f114ef 81
82 session_write_close();
83
860fdaec 84 $this->assign("xorg_errors", $this->_errors);
85 $this->assign("xorg_failure", $this->_failure);
a3a049fc 86
62a66dfc 87 switch ($this->_page_type) {
88 case NO_SKIN:
0337d704 89 $this->display($this->_tpl);
90 exit;
62a66dfc 91
92 case SIMPLE:
c6db254a 93 $this->assign('simple', true);
62a66dfc 94 case SKINNED:
801fcad8 95 $this->register_modifier('escape_html', 'escape_html');
96 $this->default_modifiers = Array('@escape_html');
0337d704 97 }
b4315e15 98
99 if (!$globals->debug) {
100 $this->display($skin);
101 exit;
102 }
a3a049fc 103
b4315e15 104 if ($globals->debug & 1) {
f1ca33de 105 $this->assign('db_trace', XDB::trace_format($this, 'database-debug.tpl'));
b4315e15 106 }
0337d704 107
7da8ef90 108 $this->assign('validate', true);
b4315e15 109 $result = $this->fetch($skin);
36f114ef 110 $ttime .= sprintf('Temps total: %.02fs<br />', microtime_float() - $TIME_BEGIN);
b4315e15 111 $replc = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
0337d704 112
b4315e15 113 if ($globals->debug & 2) {
114
115 $fd = fopen($this->compile_dir."/valid.html","w");
116 fwrite($fd, $result);
117 fclose($fd);
a3a049fc 118
b4315e15 119 exec($globals->spoolroot."/bin/devel/xhtml.validate.pl ".$this->compile_dir."/valid.html", $val);
120 foreach ($val as $h) {
121 if (preg_match("/^X-W3C-Validator-Errors: (\d+)$/", $h, $m)) {
122 $replc = '<span style="color: #080;">HTML OK</span><br />';
123 if ($m[1]) {
124 $replc = "<span class='erreur'><a href='http://validator.w3.org/check?uri={$globals->baseurl}"
125 ."/valid.html&amp;ss=1#result'>{$m[1]} ERREUR(S) !!!</a></span><br />";
0337d704 126 }
b4315e15 127 break;
0337d704 128 }
129 }
0337d704 130 }
131
b4315e15 132 echo str_replace("@HOOK@", $ttime.$replc, $result);
0337d704 133 exit;
134 }
135
136 // }}}
137 // {{{ function run()
138
139 function run()
140 {
141 die ("implement me");
142 }
143
144 // }}}
145 // {{{ function nb_errs()
146
147 function nb_errs()
148 {
860fdaec 149 return count($this->_errors);
0337d704 150 }
151
152 // }}}
153 // {{{ function trig()
154
155 function trig($msg)
156 {
860fdaec 157 $this->_errors[] = $msg;
0337d704 158 }
159
160 // }}}
0337d704 161 // {{{ function kill()
162
163 function kill($msg)
164 {
0fcbe8d0 165 $this->trig($msg);
166 $this->_failure = true;
0337d704 167 $this->run();
168 }
169
170 // }}}
0337d704 171 // {{{ function loadModule()
a3a049fc 172
0337d704 173 function loadModule($modname)
174 {
175 require_once("$modname.inc.php");
176 }
177
178 // }}}
179 // {{{ function addJsLink
180
181 function addJsLink($path)
182 {
183 $this->append('xorg_js', $path);
184 }
185
186 // }}}
187 // {{{ function addCssLink
188
189 function addCssLink($path)
190 {
191 $this->append('xorg_css', $path);
192 }
193
194 // }}}
0337d704 195}
196
b76f0797 197// {{{ function escape_html ()
198
199/**
200 * default smarty plugin, used to auto-escape dangerous html.
201 *
202 * < --> &lt;
203 * > --> &gt;
204 * " --> &quot;
205 * & not followed by some entity --> &amp;
206 */
207function escape_html($string)
208{
209 if (is_string($string)) {
210 $transtbl = Array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;', '\'' => '&#39;');
211 return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/", "&amp;" , strtr($string, $transtbl));
212 } else {
213 return $string;
214 }
215}
216
217// }}}
218// {{{ function at_to_globals()
219
220/**
221 * helper
222 */
223
224function _to_globals($s) {
225 global $globals;
226 $t = explode('.',$s);
227 if (count($t) == 1) {
228 return var_export($globals->$t[0],true);
229 } else {
230 return var_export($globals->$t[0]->$t[1],true);
231 }
232}
233
234/**
235 * compilation plugin used to import $globals confing through #globals.foo.bar# directives
236 */
237
238function at_to_globals($tpl_source, &$smarty)
239{
240 return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
241}
242
243// }}}
244// {{{ function trimwhitespace
245
246function trimwhitespace($source, &$smarty)
247{
248 $tags = array('script', 'pre', 'textarea');
249
250 foreach ($tags as $tag) {
251 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, ${$tag});
252 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
253 }
254
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);
258
259 foreach ($tags as $tag) {
260 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
261 }
262
263 return $source;
264}
265
266// }}}
267
0337d704 268// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
269?>