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