7027794f |
1 | <?php |
2 | /******************************************************************************** |
3 | * banana/page.inc.php : class for group lists |
4 | * ------------------------ |
5 | * |
6 | * This file is part of the banana distribution |
7 | * Copyright: See COPYING files that comes with this distribution |
8 | ********************************************************************************/ |
9 | |
10 | require_once 'smarty/libs/Smarty.class.php'; |
11 | |
12 | class BananaPage extends Smarty |
13 | { |
14 | private $error = array(); |
15 | private $page = null; |
16 | |
17 | private $pages = array(); |
4eeddd59 |
18 | private $killed = array(); |
7027794f |
19 | private $actions = array(); |
20 | |
9c118ac9 |
21 | public $css = ''; |
22 | |
7027794f |
23 | public function __construct() |
24 | { |
25 | $this->Smarty(); |
26 | |
27 | $this->compile_check = Banana::$debug_smarty; |
28 | $this->template_dir = dirname(__FILE__) . '/templates/'; |
c82c827e |
29 | $this->compile_dir = Banana::$spool_root . '/templates_c/'; |
7027794f |
30 | $this->register_prefilter('banana_trimwhitespace'); |
31 | |
32 | } |
33 | |
2f0aa8ce |
34 | /** Add an error message |
35 | * @param message STRING html code of the error to display |
36 | */ |
7027794f |
37 | public function trig($message) |
38 | { |
39 | $this->error[] = $message; |
40 | } |
41 | |
2f0aa8ce |
42 | /** Kill the current page (generate an error message and skip page generation) |
43 | * @param message STRING html code of the error message to display |
44 | @ @return XHTML code of the page |
45 | */ |
7027794f |
46 | public function kill($message) |
47 | { |
48 | $this->trig($message); |
2f0aa8ce |
49 | $this->assign('killed', true); |
7027794f |
50 | return $this->run(); |
51 | } |
52 | |
2f0aa8ce |
53 | /** Set the current page |
54 | * @param page STRING page name |
55 | */ |
7027794f |
56 | public function setPage($page) |
57 | { |
58 | $this->page = $page; |
4eeddd59 |
59 | return true; |
7027794f |
60 | } |
61 | |
2f0aa8ce |
62 | /** Register an action to show on banana page |
63 | * @param action_code HTML code of the action |
64 | * @param pages ARRAY pages where to show the action (null == every pages) |
65 | * @return true if success |
66 | */ |
7027794f |
67 | public function registerAction($action_code, array $pages = null) |
68 | { |
69 | $this->actions[] = array('text' => $action_code, 'pages' => $pages); |
70 | return true; |
71 | } |
72 | |
2f0aa8ce |
73 | /** Register a new page |
74 | * @param name Name of the page |
75 | * @param text Text for the tab of the page |
76 | * @param template Template path for the page if null, the page is not handled by banana |
77 | * @return true if success |
78 | */ |
7027794f |
79 | public function registerPage($name, $text, $template = null) |
80 | { |
81 | $this->pages[$name] = array('text' => $text, 'template' => $template); |
82 | return true; |
83 | } |
b18124be |
84 | |
4eeddd59 |
85 | /** Remove a page |
86 | * @param page STRING page name to kill |
87 | */ |
88 | public function killPage($page) |
89 | { |
90 | $this->killed[] = $page; |
91 | } |
92 | |
9c118ac9 |
93 | /** Add Inline CSS to put in the page headers |
94 | * @param css CSS code |
95 | */ |
96 | public function addCssInline($css) |
97 | { |
98 | $this->css .= $css; |
99 | } |
100 | |
b18124be |
101 | /** Preparte the page generation |
102 | * @return template to use |
2f0aa8ce |
103 | */ |
b18124be |
104 | protected function prepare() |
7027794f |
105 | { |
106 | $this->registerPage('subscribe', _b_('Abonnements'), null); |
107 | $this->registerPage('forums', _b_('Les forums'), null); |
108 | if (!is_null(Banana::$group)) { |
109 | $this->registerPage('thread', Banana::$group, null); |
110 | if (!is_null(Banana::$artid)) { |
111 | $this->registerPage('message', _b_('Message'), null); |
112 | if ($this->page == 'cancel') { |
113 | $this->registerPage('cancel', _b_('Annulation'), null); |
114 | } elseif ($this->page == 'new') { |
598a1c53 |
115 | $this->registerPage('new', _b_('Répondre'), null); |
7027794f |
116 | } |
117 | } elseif ($this->page == 'new') { |
118 | $this->registerPage('new', _b_('Nouveau'), null); |
119 | } |
120 | } |
4eeddd59 |
121 | foreach ($this->killed as $page) { |
122 | unset($this->pages[$page]); |
123 | } |
7027794f |
124 | foreach ($this->actions as $key=>&$action) { |
125 | if (!is_null($action['pages']) && !in_array($this->page, $action['pages'])) { |
126 | unset($this->actions[$key]); |
127 | } |
128 | } |
b18124be |
129 | |
130 | return 'banana-base.tpl'; |
131 | } |
132 | |
133 | /** Generate XHTML code |
134 | */ |
135 | public function run() |
136 | { |
137 | $tpl = $this->prepare(); |
4eeddd59 |
138 | if (!isset($this->pages[$this->page])) { |
598a1c53 |
139 | $this->trig(_b_('La page demandée n\'existe pas')); |
4eeddd59 |
140 | $this->actions = array(); |
141 | $this->page = null; |
142 | } |
b18124be |
143 | |
e02edbfe |
144 | return $this->_run($tpl); |
145 | } |
146 | |
147 | /** Generate feed XML code |
148 | */ |
149 | public function feed() |
150 | { |
151 | @list($lg) = explode('_', Banana::$profile['locale']); |
152 | $tpl = 'banana-feed-' . Banana::$feed_format . '.tpl'; |
153 | $this->assign('copyright', Banana::$feed_copyright); |
cd58d954 |
154 | $this->assign('generator', Banana::$feed_generator); |
155 | $this->assign('email', Banana::$feed_email); |
e02edbfe |
156 | $this->assign('title_prefix', Banana::$feed_namePrefix); |
157 | $this->assign('language', $lg); |
158 | $this->register_function('rss_date', 'rss_date'); |
dfb752b1 |
159 | header('Content-Type: application/rss+xml; charset=utf-8'); |
cd58d954 |
160 | echo $this->_run($tpl, false); |
dfb752b1 |
161 | exit; |
e02edbfe |
162 | } |
163 | |
164 | /** Code generation |
165 | */ |
cd58d954 |
166 | private function _run($tpl, $ent = true) |
e02edbfe |
167 | { |
7027794f |
168 | $this->assign('group', Banana::$group); |
169 | $this->assign('artid', Banana::$artid); |
170 | $this->assign('part', Banana::$part); |
171 | $this->assign('first', Banana::$first); |
172 | $this->assign('action', Banana::$action); |
173 | $this->assign('profile', Banana::$profile); |
174 | $this->assign('spool', Banana::$spool); |
175 | $this->assign('protocole', Banana::$protocole); |
8bfa4595 |
176 | $this->assign('showboxlist', Banana::$spool_boxlist); |
e95f5b5e |
177 | $this->assign('showthread', Banana::$msgshow_withthread); |
178 | $this->assign('withtabs' , Banana::$withtabs); |
e02edbfe |
179 | $this->assign('feed_format', Banana::$feed_format); |
180 | $this->assign('feed_active', Banana::$feed_active); |
7027794f |
181 | |
b18124be |
182 | $this->register_function('url', array($this, 'makeUrl')); |
183 | $this->register_function('link', array($this, 'makeLink')); |
184 | $this->register_function('imglink', array($this, 'makeImgLink')); |
185 | $this->register_function('img', array($this, 'makeImg')); |
a0b2e16c |
186 | $this->register_modifier('b', '_b_'); |
7995f07b |
187 | $this->register_modifier('htmlentities', 'banana_htmlentities'); |
4eeddd59 |
188 | |
7027794f |
189 | $this->assign('errors', $this->error); |
190 | $this->assign('page', $this->page); |
191 | $this->assign('pages', $this->pages); |
192 | $this->assign('actions', $this->actions); |
193 | |
7027794f |
194 | if (!Banana::$debug_smarty) { |
195 | $error_level = error_reporting(0); |
196 | } |
b18124be |
197 | $text = $this->fetch($tpl); |
cd58d954 |
198 | if ($ent) { |
199 | $text = banana_utf8entities($text); |
200 | } |
7027794f |
201 | if (!Banana::$debug_smarty) { |
202 | error_reporting($error_level); |
203 | } |
204 | return $text; |
205 | } |
206 | |
2f0aa8ce |
207 | /** Build a URL in Banana |
208 | * @param params ARRAY location datas |
209 | * @param smarty OBJECT Smarty instance associated (null if none) |
210 | * @return URL of the page associated with the given parameters |
211 | * |
212 | * Usual parameters are : |
213 | * - group : the box name |
214 | * - artid : the current message id (index of message-id) |
215 | * - part : part id to show (may be a content-id, xface or a mime-type for text) |
216 | * - first : first linear-index to show in spool view |
217 | * - action: like subscribe, cancel, new |
218 | * - all others params are allowed, but not parsed by the base implementation of banana |
219 | * |
220 | * smarty funciton : {url param1=... param2=...} |
221 | */ |
222 | public function makeUrl(array $params, &$smarty = null) |
7027794f |
223 | { |
224 | if (function_exists('hook_makeLink') |
225 | && $res = hook_makeLink($params)) { |
226 | return $res; |
227 | } |
228 | $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; |
229 | $host = $_SERVER['HTTP_HOST']; |
230 | $file = $_SERVER['PHP_SELF']; |
231 | |
232 | if (count($params) != 0) { |
233 | $get = '?'; |
234 | foreach ($params as $key=>$value) { |
235 | if (strlen($get) != 1) { |
236 | $get .= '&'; |
237 | } |
238 | $get .= $key . '=' . $value; |
239 | } |
240 | } else { |
241 | $get = ''; |
242 | } |
243 | return $proto . $host . $file . $get; |
244 | } |
245 | |
2f0aa8ce |
246 | /** Build a link to a Banana page |
247 | * @param params ARRAY location datas |
248 | * @param smarty OBJECT Smarty instance associated (null if none) |
249 | * @return Link to the page associated with the given parameters |
250 | * |
251 | * Support all @ref makeURL parameters, but catch the following: |
252 | * - text : if set, defined the text of the link (if not set, the URL is used |
253 | * - popup : title of the link (showed as a tooltip on most browsers) |
254 | * - class : specific style class for the markup |
255 | * - accesskey: keyboard key to trigger the link |
256 | * None of this parameters is needed |
257 | * |
258 | * Smarty function : {link param1=... param2=...} |
259 | */ |
260 | public function makeLink(array $params, &$smarty = null) |
7027794f |
261 | { |
262 | $catch = array('text', 'popup', 'class', 'accesskey'); |
263 | foreach ($catch as $key) { |
264 | ${$key} = isset($params[$key]) ? $params[$key] : null; |
265 | unset($params[$key]); |
266 | } |
267 | $link = $this->makeUrl($params, &$smarty); |
268 | if (is_null($text)) { |
269 | $text = $link; |
270 | } |
271 | if (!is_null($accesskey)) { |
272 | $popup .= ' (raccourci : ' . $accesskey . ')'; |
273 | } |
274 | if (!is_null($popup)) { |
2f0aa8ce |
275 | $popup = ' title="' . banana_htmlentities($popup) . '"'; |
7027794f |
276 | } |
277 | if (!is_null($class)) { |
278 | $class = ' class="' . $class . '"'; |
279 | } |
7027794f |
280 | if (!is_null($accesskey)) { |
281 | $accesskey = ' accesskey="' . $accesskey . '"'; |
282 | } |
2f0aa8ce |
283 | return '<a href="' . banana_htmlentities($link) . '"' |
284 | . $popup . $class . $accesskey |
7027794f |
285 | . '>' . $text . '</a>'; |
286 | } |
287 | |
2f0aa8ce |
288 | /** Build a link to one of the banana built-in images |
289 | * @param params ARRAY image datas |
290 | * @param smarty OBJECT Smarty instance associated (null if none) |
291 | * @return Img tag |
292 | * |
293 | * Supported parameters are |
294 | * - img : name of the image (without its extension) |
295 | * - alt : alternative text |
296 | * - height and width : dimensions of the images |
297 | * img and alt are needed |
298 | * |
299 | * Smarty function: {img img=... alt=... [height=...] [width=...]} |
300 | */ |
301 | public function makeImg(array $params, &$smarty = null) |
7027794f |
302 | { |
303 | $catch = array('img', 'alt', 'height', 'width'); |
304 | foreach ($catch as $key) { |
305 | ${$key} = isset($params[$key]) ? $params[$key] : null; |
306 | } |
307 | $img .= ".gif"; |
308 | if (function_exists('hook_makeImg') |
309 | && $res = hook_makeImg($img, $alt, $height, $width)) { |
310 | return $res; |
311 | } |
312 | |
313 | if (!is_null($width)) { |
314 | $width = ' width="' . $width . '"'; |
315 | } |
316 | if (!is_null($height)) { |
317 | $height = ' height="' . $height . '"'; |
318 | } |
319 | |
320 | $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; |
321 | $host = $_SERVER['HTTP_HOST']; |
322 | $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img; |
323 | $url = $proto . $host . $file; |
324 | |
325 | return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />'; |
326 | } |
2f0aa8ce |
327 | |
96e1e874 |
328 | /** Build a link to one of the banana built-in javascript |
329 | * @param src STRING javascript name |
330 | * @return Javascript tag |
331 | */ |
848a4944 |
332 | public function makeJs($src) |
96e1e874 |
333 | { |
334 | if (function_exists('hook_makeJs') |
335 | && $res = hook_makeJs($src)) { |
336 | return $res; |
337 | } |
338 | |
339 | $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; |
340 | $host = $_SERVER['HTTP_HOST']; |
341 | $file = dirname($_SERVER['PHP_SELF']) . '/javascript/' . $src . '.js'; |
342 | $url = $proto . $host . $file; |
343 | |
344 | return '<script type="text/javascript" src="' . $url . '"/></script>'; |
345 | } |
346 | |
2f0aa8ce |
347 | /** Build a link with an image as text |
348 | * @param params ARRAY image and location data |
349 | * @param smarty OBJECT Smarty instance associated (null if none) |
350 | * @return an image within an link |
351 | * |
352 | * All @ref makeImg and @ref makeLink parameters are supported |
353 | * if text is set, the text will be appended after the image in the link |
354 | * |
355 | * Smarty function : {imglink img=... alt=... [param1=...]} |
356 | */ |
357 | public function makeImgLink(array $params, &$smarty = null) |
7027794f |
358 | { |
2f0aa8ce |
359 | if (!isset($params['popup'])) { |
e02edbfe |
360 | $params['popup'] = @$params['alt']; |
2f0aa8ce |
361 | } |
362 | $img = $this->makeImg($params, $smarty); |
363 | if (isset($params['text'])) { |
364 | $img .= ' ' . $params['text']; |
365 | } |
366 | $params['text'] = $img; |
e02edbfe |
367 | unset($params['alt']); |
368 | unset($params['img']); |
369 | unset($params['width']); |
370 | unset($params['height']); |
7027794f |
371 | return $this->makeLink($params, $smarty); |
372 | } |
373 | |
374 | /** Redirect to the page with the given parameter |
2f0aa8ce |
375 | * @ref makeURL |
7027794f |
376 | */ |
2f0aa8ce |
377 | public function redirect(array $params = array()) |
7027794f |
378 | { |
379 | header('Location: ' . $this->makeUrl($params)); |
380 | } |
381 | } |
382 | |
383 | // {{{ function banana_trimwhitespace |
384 | |
385 | function banana_trimwhitespace($source, &$smarty) |
386 | { |
387 | $tags = array('script', 'pre', 'textarea'); |
388 | |
389 | foreach ($tags as $tag) { |
390 | preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, ${$tag}); |
391 | $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source); |
392 | } |
393 | |
394 | // remove all leading spaces, tabs and carriage returns NOT |
395 | // preceeded by a php close tag. |
396 | $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source); |
397 | |
398 | foreach ($tags as $tag) { |
399 | $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source); |
400 | } |
401 | |
402 | return $source; |
403 | } |
404 | |
405 | // }}} |
e02edbfe |
406 | // {{{ function rss_date |
407 | |
408 | function rss_date($t) |
409 | { |
410 | return date('r', $t); |
411 | } |
7027794f |
412 | |
e02edbfe |
413 | // }}} |
7027794f |
414 | |
598a1c53 |
415 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
7027794f |
416 | ?> |