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); |
154 | $this->assign('title_prefix', Banana::$feed_namePrefix); |
155 | $this->assign('language', $lg); |
156 | $this->register_function('rss_date', 'rss_date'); |
157 | return $this->_run($tpl); |
158 | } |
159 | |
160 | /** Code generation |
161 | */ |
162 | private function _run($tpl) |
163 | { |
7027794f |
164 | $this->assign('group', Banana::$group); |
165 | $this->assign('artid', Banana::$artid); |
166 | $this->assign('part', Banana::$part); |
167 | $this->assign('first', Banana::$first); |
168 | $this->assign('action', Banana::$action); |
169 | $this->assign('profile', Banana::$profile); |
170 | $this->assign('spool', Banana::$spool); |
171 | $this->assign('protocole', Banana::$protocole); |
8bfa4595 |
172 | $this->assign('showboxlist', Banana::$spool_boxlist); |
e95f5b5e |
173 | $this->assign('showthread', Banana::$msgshow_withthread); |
174 | $this->assign('withtabs' , Banana::$withtabs); |
e02edbfe |
175 | $this->assign('feed_format', Banana::$feed_format); |
176 | $this->assign('feed_active', Banana::$feed_active); |
7027794f |
177 | |
b18124be |
178 | $this->register_function('url', array($this, 'makeUrl')); |
179 | $this->register_function('link', array($this, 'makeLink')); |
180 | $this->register_function('imglink', array($this, 'makeImgLink')); |
181 | $this->register_function('img', array($this, 'makeImg')); |
a0b2e16c |
182 | $this->register_modifier('b', '_b_'); |
4eeddd59 |
183 | |
7027794f |
184 | $this->assign('errors', $this->error); |
185 | $this->assign('page', $this->page); |
186 | $this->assign('pages', $this->pages); |
187 | $this->assign('actions', $this->actions); |
188 | |
7027794f |
189 | if (!Banana::$debug_smarty) { |
190 | $error_level = error_reporting(0); |
191 | } |
b18124be |
192 | $text = $this->fetch($tpl); |
7027794f |
193 | $text = banana_utf8entities($text); |
194 | if (!Banana::$debug_smarty) { |
195 | error_reporting($error_level); |
196 | } |
197 | return $text; |
198 | } |
199 | |
2f0aa8ce |
200 | /** Build a URL in Banana |
201 | * @param params ARRAY location datas |
202 | * @param smarty OBJECT Smarty instance associated (null if none) |
203 | * @return URL of the page associated with the given parameters |
204 | * |
205 | * Usual parameters are : |
206 | * - group : the box name |
207 | * - artid : the current message id (index of message-id) |
208 | * - part : part id to show (may be a content-id, xface or a mime-type for text) |
209 | * - first : first linear-index to show in spool view |
210 | * - action: like subscribe, cancel, new |
211 | * - all others params are allowed, but not parsed by the base implementation of banana |
212 | * |
213 | * smarty funciton : {url param1=... param2=...} |
214 | */ |
215 | public function makeUrl(array $params, &$smarty = null) |
7027794f |
216 | { |
217 | if (function_exists('hook_makeLink') |
218 | && $res = hook_makeLink($params)) { |
219 | return $res; |
220 | } |
221 | $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; |
222 | $host = $_SERVER['HTTP_HOST']; |
223 | $file = $_SERVER['PHP_SELF']; |
224 | |
225 | if (count($params) != 0) { |
226 | $get = '?'; |
227 | foreach ($params as $key=>$value) { |
228 | if (strlen($get) != 1) { |
229 | $get .= '&'; |
230 | } |
231 | $get .= $key . '=' . $value; |
232 | } |
233 | } else { |
234 | $get = ''; |
235 | } |
236 | return $proto . $host . $file . $get; |
237 | } |
238 | |
2f0aa8ce |
239 | /** Build a link to a Banana page |
240 | * @param params ARRAY location datas |
241 | * @param smarty OBJECT Smarty instance associated (null if none) |
242 | * @return Link to the page associated with the given parameters |
243 | * |
244 | * Support all @ref makeURL parameters, but catch the following: |
245 | * - text : if set, defined the text of the link (if not set, the URL is used |
246 | * - popup : title of the link (showed as a tooltip on most browsers) |
247 | * - class : specific style class for the markup |
248 | * - accesskey: keyboard key to trigger the link |
249 | * None of this parameters is needed |
250 | * |
251 | * Smarty function : {link param1=... param2=...} |
252 | */ |
253 | public function makeLink(array $params, &$smarty = null) |
7027794f |
254 | { |
255 | $catch = array('text', 'popup', 'class', 'accesskey'); |
256 | foreach ($catch as $key) { |
257 | ${$key} = isset($params[$key]) ? $params[$key] : null; |
258 | unset($params[$key]); |
259 | } |
260 | $link = $this->makeUrl($params, &$smarty); |
261 | if (is_null($text)) { |
262 | $text = $link; |
263 | } |
264 | if (!is_null($accesskey)) { |
265 | $popup .= ' (raccourci : ' . $accesskey . ')'; |
266 | } |
267 | if (!is_null($popup)) { |
2f0aa8ce |
268 | $popup = ' title="' . banana_htmlentities($popup) . '"'; |
7027794f |
269 | } |
270 | if (!is_null($class)) { |
271 | $class = ' class="' . $class . '"'; |
272 | } |
7027794f |
273 | if (!is_null($accesskey)) { |
274 | $accesskey = ' accesskey="' . $accesskey . '"'; |
275 | } |
2f0aa8ce |
276 | return '<a href="' . banana_htmlentities($link) . '"' |
277 | . $popup . $class . $accesskey |
7027794f |
278 | . '>' . $text . '</a>'; |
279 | } |
280 | |
2f0aa8ce |
281 | /** Build a link to one of the banana built-in images |
282 | * @param params ARRAY image datas |
283 | * @param smarty OBJECT Smarty instance associated (null if none) |
284 | * @return Img tag |
285 | * |
286 | * Supported parameters are |
287 | * - img : name of the image (without its extension) |
288 | * - alt : alternative text |
289 | * - height and width : dimensions of the images |
290 | * img and alt are needed |
291 | * |
292 | * Smarty function: {img img=... alt=... [height=...] [width=...]} |
293 | */ |
294 | public function makeImg(array $params, &$smarty = null) |
7027794f |
295 | { |
296 | $catch = array('img', 'alt', 'height', 'width'); |
297 | foreach ($catch as $key) { |
298 | ${$key} = isset($params[$key]) ? $params[$key] : null; |
299 | } |
300 | $img .= ".gif"; |
301 | if (function_exists('hook_makeImg') |
302 | && $res = hook_makeImg($img, $alt, $height, $width)) { |
303 | return $res; |
304 | } |
305 | |
306 | if (!is_null($width)) { |
307 | $width = ' width="' . $width . '"'; |
308 | } |
309 | if (!is_null($height)) { |
310 | $height = ' height="' . $height . '"'; |
311 | } |
312 | |
313 | $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; |
314 | $host = $_SERVER['HTTP_HOST']; |
315 | $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img; |
316 | $url = $proto . $host . $file; |
317 | |
318 | return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />'; |
319 | } |
2f0aa8ce |
320 | |
321 | /** Build a link with an image as text |
322 | * @param params ARRAY image and location data |
323 | * @param smarty OBJECT Smarty instance associated (null if none) |
324 | * @return an image within an link |
325 | * |
326 | * All @ref makeImg and @ref makeLink parameters are supported |
327 | * if text is set, the text will be appended after the image in the link |
328 | * |
329 | * Smarty function : {imglink img=... alt=... [param1=...]} |
330 | */ |
331 | public function makeImgLink(array $params, &$smarty = null) |
7027794f |
332 | { |
2f0aa8ce |
333 | if (!isset($params['popup'])) { |
e02edbfe |
334 | $params['popup'] = @$params['alt']; |
2f0aa8ce |
335 | } |
336 | $img = $this->makeImg($params, $smarty); |
337 | if (isset($params['text'])) { |
338 | $img .= ' ' . $params['text']; |
339 | } |
340 | $params['text'] = $img; |
e02edbfe |
341 | unset($params['alt']); |
342 | unset($params['img']); |
343 | unset($params['width']); |
344 | unset($params['height']); |
7027794f |
345 | return $this->makeLink($params, $smarty); |
346 | } |
347 | |
348 | /** Redirect to the page with the given parameter |
2f0aa8ce |
349 | * @ref makeURL |
7027794f |
350 | */ |
2f0aa8ce |
351 | public function redirect(array $params = array()) |
7027794f |
352 | { |
353 | header('Location: ' . $this->makeUrl($params)); |
354 | } |
355 | } |
356 | |
357 | // {{{ function banana_trimwhitespace |
358 | |
359 | function banana_trimwhitespace($source, &$smarty) |
360 | { |
361 | $tags = array('script', 'pre', 'textarea'); |
362 | |
363 | foreach ($tags as $tag) { |
364 | preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, ${$tag}); |
365 | $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source); |
366 | } |
367 | |
368 | // remove all leading spaces, tabs and carriage returns NOT |
369 | // preceeded by a php close tag. |
370 | $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source); |
371 | |
372 | foreach ($tags as $tag) { |
373 | $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source); |
374 | } |
375 | |
376 | return $source; |
377 | } |
378 | |
379 | // }}} |
e02edbfe |
380 | // {{{ function rss_date |
381 | |
382 | function rss_date($t) |
383 | { |
384 | return date('r', $t); |
385 | } |
7027794f |
386 | |
e02edbfe |
387 | // }}} |
7027794f |
388 | |
598a1c53 |
389 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
7027794f |
390 | ?> |