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