Fixes deprecated features in PHP 5.3.x.
[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 protected $error = array();
17 protected $page = null;
18
19 protected $pages = array();
20 protected $killed = array();
21 protected $actions = array();
22
23 public $css = '';
24
25 public function __construct()
26 {
27 parent::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 if (!is_dir($this->compile_dir)) {
34 mkdir($this->compile_dir);
35 }
36 }
37
38 /** Add an error message
39 * @param message STRING html code of the error to display
40 */
41 public function trig($message)
42 {
43 $this->error[] = $message;
44 }
45
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 */
50 public function kill($message)
51 {
52 $this->trig($message);
53 $this->assign('killed', true);
54 return $this->run();
55 }
56
57 /** Set the current page
58 * @param page STRING page name
59 */
60 public function setPage($page)
61 {
62 $this->page = $page;
63 return true;
64 }
65
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
70 */
71 public function registerAction($action_code, array $pages = null)
72 {
73 $this->actions[] = array('text' => $action_code, 'pages' => $pages);
74 return true;
75 }
76
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 */
83 public function registerPage($name, $text, $template = null)
84 {
85 $this->pages[$name] = array('text' => $text, 'template' => $template);
86 return true;
87 }
88
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
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
105 /** Preparte the page generation
106 * @return template to use
107 */
108 protected function prepare()
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)) {
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);
126 if ($this->page == 'cancel') {
127 $this->registerPage('cancel', _b_('Annulation'), null);
128 } elseif ($this->page == 'new') {
129 $this->registerPage('new', _b_('Répondre'), null);
130 }
131 } elseif ($this->page == 'new') {
132 $this->registerPage('new', _b_('Nouveau'), null);
133 }
134 }
135 foreach ($this->killed as $page) {
136 unset($this->pages[$page]);
137 }
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 }
143
144 return 'banana-base.tpl';
145 }
146
147 /** Generate XHTML code
148 */
149 public function run()
150 {
151 $tpl = $this->prepare();
152 if (!isset($this->pages[$this->page])) {
153 $this->trig(_b_('La page demandée n\'existe pas'));
154 $this->actions = array();
155 $this->page = null;
156 }
157
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);
168 $this->assign('generator', Banana::$feed_generator);
169 $this->assign('email', Banana::$feed_email);
170 $this->assign('title_prefix', Banana::$feed_namePrefix);
171 $this->assign('language', $lg);
172 $this->register_function('rss_date', 'rss_date');
173 header('Content-Type: application/rss+xml; charset=utf-8');
174 echo $this->_run($tpl, false);
175 exit;
176 }
177
178 /** Code generation
179 */
180 private function _run($tpl, $ent = true)
181 {
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);
190 $this->assign('showboxlist', Banana::$spool_boxlist);
191 $this->assign('showthread', Banana::$msgshow_withthread);
192 $this->assign('withtabs' , Banana::$withtabs);
193 $this->assign('feed_format', Banana::$feed_format);
194 $this->assign('feed_active', Banana::$feed_active);
195 $this->assign('with_javascript', Banana::$msgshow_javascript);
196
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'));
201 $this->register_modifier('b', '_b_');
202
203 $this->assign('errors', $this->error);
204 $this->assign('page', $this->page);
205 $this->assign('pages', $this->pages);
206 $this->assign('actions', $this->actions);
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 }
213
214 if (!Banana::$debug_smarty) {
215 $error_level = error_reporting(0);
216 }
217 $text = $this->fetch($tpl);
218 if (!Banana::$debug_smarty) {
219 error_reporting($error_level);
220 }
221 return $text;
222 }
223
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
228 *
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 */
239 public function makeUrl(array $params, $smarty = null)
240 {
241 if (function_exists('hook_makeLink')
242 && $res = hook_makeLink($params)) {
243 return $res;
244 }
245 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
246 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
247 $file = $_SERVER['PHP_SELF'];
248
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 = '';
259 }
260 return $proto . $host . $file . $get;
261 }
262
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 */
277 public function makeLink(array $params, $smarty = null)
278 {
279 $catch = array('text', 'popup', 'class', 'accesskey', 'style');
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)) {
292 $popup = ' title="' . banana_entities($popup) . '"';
293 }
294 if (!is_null($class)) {
295 $class = ' class="' . $class . '"';
296 }
297 if (!is_null($style)) {
298 $style = ' style="' . $style . '"';
299 }
300 if (!is_null($accesskey)) {
301 $accesskey = ' accesskey="' . $accesskey . '"';
302 }
303 return '<a href="' . banana_entities($link) . '"'
304 . $popup . $class . $style . $accesskey
305 . '>' . $text . '</a>';
306 }
307
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 */
321 public function makeImg(array $params, $smarty = null)
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://';
341 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
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 }
347
348 /** Build a link to one of the banana built-in javascript
349 * @param src STRING javascript name
350 * @return Javascript tag
351 */
352 public function makeJs($src)
353 {
354 if (!Banana::$msgshow_javascript) {
355 return '';
356 }
357 if (function_exists('hook_makeJs')
358 && $res = hook_makeJs($src)) {
359 return $res;
360 }
361
362 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
363 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
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 }
369
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 */
380 public function makeImgLink(array $params, $smarty = null)
381 {
382 if (!isset($params['popup'])) {
383 $params['popup'] = @$params['alt'];
384 }
385 $img = $this->makeImg($params, $smarty);
386 if (isset($params['text'])) {
387 $img .= ' ' . $params['text'];
388 }
389 $params['text'] = $img;
390 unset($params['alt']);
391 unset($params['img']);
392 unset($params['width']);
393 unset($params['height']);
394 return $this->makeLink($params, $smarty);
395 }
396
397 /** Redirect to the page with the given parameter
398 * @ref makeURL
399 */
400 public function redirect(array $params = array())
401 {
402 header('Location: ' . $this->makeUrl($params));
403 exit;
404 }
405 }
406
407 // {{{ function banana_trimwhitespace
408
409 function banana_trimwhitespace($source, $smarty)
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 // }}}
430 // {{{ function rss_date
431
432 function rss_date($t)
433 {
434 return date('r', $t);
435 }
436
437 // }}}
438
439 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
440 ?>