7d313efd5f3a8f229f72de04a06ab32feccb8c96
[banana.git] / banana / page.inc.php
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 if (!include_once('Smarty.class.php')) {
11 require_once 'smarty/libs/Smarty.class.php';
12 }
13
14 class BananaPage extends Smarty
15 {
16 private $error = array();
17 private $page = null;
18
19 private $pages = array();
20 private $killed = array();
21 private $actions = array();
22
23 public $css = '';
24
25 public function __construct()
26 {
27 $this->Smarty();
28
29 $this->compile_check = Banana::$debug_smarty;
30 $this->template_dir = dirname(__FILE__) . '/templates/';
31 $this->compile_dir = Banana::$spool_root . '/templates_c/';
32 $this->register_prefilter('banana_trimwhitespace');
33
34 }
35
36 /** Add an error message
37 * @param message STRING html code of the error to display
38 */
39 public function trig($message)
40 {
41 $this->error[] = $message;
42 }
43
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 */
48 public function kill($message)
49 {
50 $this->trig($message);
51 $this->assign('killed', true);
52 return $this->run();
53 }
54
55 /** Set the current page
56 * @param page STRING page name
57 */
58 public function setPage($page)
59 {
60 $this->page = $page;
61 return true;
62 }
63
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 */
69 public function registerAction($action_code, array $pages = null)
70 {
71 $this->actions[] = array('text' => $action_code, 'pages' => $pages);
72 return true;
73 }
74
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 */
81 public function registerPage($name, $text, $template = null)
82 {
83 $this->pages[$name] = array('text' => $text, 'template' => $template);
84 return true;
85 }
86
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
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
103 /** Preparte the page generation
104 * @return template to use
105 */
106 protected function prepare()
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') {
117 $this->registerPage('new', _b_('Répondre'), null);
118 }
119 } elseif ($this->page == 'new') {
120 $this->registerPage('new', _b_('Nouveau'), null);
121 }
122 }
123 foreach ($this->killed as $page) {
124 unset($this->pages[$page]);
125 }
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 }
131
132 return 'banana-base.tpl';
133 }
134
135 /** Generate XHTML code
136 */
137 public function run()
138 {
139 $tpl = $this->prepare();
140 if (!isset($this->pages[$this->page])) {
141 $this->trig(_b_('La page demandée n\'existe pas'));
142 $this->actions = array();
143 $this->page = null;
144 }
145
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);
156 $this->assign('generator', Banana::$feed_generator);
157 $this->assign('email', Banana::$feed_email);
158 $this->assign('title_prefix', Banana::$feed_namePrefix);
159 $this->assign('language', $lg);
160 $this->register_function('rss_date', 'rss_date');
161 header('Content-Type: application/rss+xml; charset=utf-8');
162 echo $this->_run($tpl, false);
163 exit;
164 }
165
166 /** Code generation
167 */
168 private function _run($tpl, $ent = true)
169 {
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);
178 $this->assign('showboxlist', Banana::$spool_boxlist);
179 $this->assign('showthread', Banana::$msgshow_withthread);
180 $this->assign('withtabs' , Banana::$withtabs);
181 $this->assign('feed_format', Banana::$feed_format);
182 $this->assign('feed_active', Banana::$feed_active);
183
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'));
188 $this->register_modifier('b', '_b_');
189
190 $this->assign('errors', $this->error);
191 $this->assign('page', $this->page);
192 $this->assign('pages', $this->pages);
193 $this->assign('actions', $this->actions);
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 }
200
201 if (!Banana::$debug_smarty) {
202 $error_level = error_reporting(0);
203 }
204 $text = $this->fetch($tpl);
205 if (!Banana::$debug_smarty) {
206 error_reporting($error_level);
207 }
208 return $text;
209 }
210
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)
227 {
228 if (function_exists('hook_makeLink')
229 && $res = hook_makeLink($params)) {
230 return $res;
231 }
232 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
233 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
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
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)
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)) {
279 $popup = ' title="' . banana_entities($popup) . '"';
280 }
281 if (!is_null($class)) {
282 $class = ' class="' . $class . '"';
283 }
284 if (!is_null($accesskey)) {
285 $accesskey = ' accesskey="' . $accesskey . '"';
286 }
287 return '<a href="' . banana_entities($link) . '"'
288 . $popup . $class . $accesskey
289 . '>' . $text . '</a>';
290 }
291
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)
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://';
325 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
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 }
331
332 /** Build a link to one of the banana built-in javascript
333 * @param src STRING javascript name
334 * @return Javascript tag
335 */
336 public function makeJs($src)
337 {
338 if (!Banana::$msgshow_javascript) {
339 return '';
340 }
341 if (function_exists('hook_makeJs')
342 && $res = hook_makeJs($src)) {
343 return $res;
344 }
345
346 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
347 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
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
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)
365 {
366 if (!isset($params['popup'])) {
367 $params['popup'] = @$params['alt'];
368 }
369 $img = $this->makeImg($params, $smarty);
370 if (isset($params['text'])) {
371 $img .= ' ' . $params['text'];
372 }
373 $params['text'] = $img;
374 unset($params['alt']);
375 unset($params['img']);
376 unset($params['width']);
377 unset($params['height']);
378 return $this->makeLink($params, $smarty);
379 }
380
381 /** Redirect to the page with the given parameter
382 * @ref makeURL
383 */
384 public function redirect(array $params = array())
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 // }}}
413 // {{{ function rss_date
414
415 function rss_date($t)
416 {
417 return date('r', $t);
418 }
419
420 // }}}
421
422 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
423 ?>