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