2 /********************************************************************************
3 * banana/page.inc.php : class for group lists
4 * ------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 if (@!include_once('Smarty.class.php')) {
11 require_once 'smarty/libs/Smarty.class.php';
14 class BananaPage
extends Smarty
16 protected $error = array();
17 protected $page = null
;
19 protected $pages = array();
20 protected $killed = array();
21 protected $actions = array();
24 protected $json_params = array();
28 public function __construct($mode = null
)
32 $this->mode
= strtolower($mode);
33 $this->compile_check
= Banana
::$debug_smarty;
34 $this->template_dir
= dirname(__FILE__
) . '/templates/';
35 $this->compile_dir
= Banana
::$spool_root . '/templates_c/';
36 $this->register_prefilter('banana_trimwhitespace');
37 if (!is_dir($this->compile_dir
)) {
38 mkdir($this->compile_dir
);
42 /** Add an error message
43 * @param message STRING html code of the error to display
45 public function trig($message)
47 $this->error
[] = $message;
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
54 public function kill($message)
56 $this->trig($message);
57 $this->assign('killed', true
);
61 /** Set the current page
62 * @param page STRING page name
64 public function setPage($page)
70 /** Assign a variable in the page.
72 public function assign($var, $value)
74 if ($this->mode
=== 'json') {
75 $this->json_params
[$var] = $value;
77 parent
::assign($var, $value);
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
86 public function registerAction($action_code, array $pages = null
)
88 $this->actions
[] = array('text' => $action_code, 'pages' => $pages);
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
98 public function registerPage($name, $text, $template = null
)
100 $this->pages
[$name] = array('text' => $text, 'template' => $template);
105 * @param page STRING page name to kill
107 public function killPage($page)
109 $this->killed
[] = $page;
112 /** Add Inline CSS to put in the page headers
113 * @param css CSS code
115 public function addCssInline($css)
120 /** Preparte the page generation
121 * @return template to use
123 protected function prepare()
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)) {
130 if (Banana
::$spool) {
131 $subject = Banana
::$spool->overview
[Banana
::$artid]->subject
;
132 } else if (Banana
::$message) {
133 $subject = Banana
::$message->getHeaderValue('subject');
135 $subject = _b_('Message');
137 if (strlen($subject) > 30) {
138 $subject = substr($subject, 0, 30) . '…';
140 $this->registerPage('message', $subject, null
);
141 if ($this->page
== 'cancel') {
142 $this->registerPage('cancel', _b_('Annulation'), null
);
143 } elseif ($this->page
== 'new') {
144 $this->registerPage('new', _b_('Répondre'), null
);
146 } elseif ($this->page
== 'new') {
147 $this->registerPage('new', _b_('Nouveau'), null
);
150 foreach ($this->killed
as $page) {
151 unset($this->pages
[$page]);
153 foreach ($this->actions
as $key=>&$action) {
154 if (!is_null($action['pages']) && !in_array($this->page
, $action['pages'])) {
155 unset($this->actions
[$key]);
159 return 'banana-base.tpl';
162 /** Generate XHTML code
164 public function run()
166 if ($this->mode
=== 'json') {
167 return json_encode($this->json_params
);
169 $tpl = $this->prepare();
170 if (!isset($this->pages
[$this->page
])) {
171 $this->trig(_b_('La page demandée n\'existe pas'));
172 $this->actions
= array();
176 return $this->_run($tpl);
179 /** Generate feed XML code
181 public function feed()
183 @list
($lg) = explode('_', Banana
::$profile['locale']);
184 $tpl = 'banana-feed-' . Banana
::$feed_format . '.tpl';
185 $this->assign('copyright', Banana
::$feed_copyright);
186 $this->assign('generator', Banana
::$feed_generator);
187 $this->assign('email', Banana
::$feed_email);
188 $this->assign('title_prefix', Banana
::$feed_namePrefix);
189 $this->assign('language', $lg);
190 $this->register_function('rss_date', 'rss_date');
191 header('Content-Type: application/rss+xml; charset=utf-8');
192 echo $this->_run($tpl, false
);
198 private function _run($tpl, $ent = true
)
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);
208 $this->assign('showboxlist', Banana
::$spool_boxlist);
209 $this->assign('showthread', Banana
::$msgshow_withthread);
210 $this->assign('withtabs' , Banana
::$withtabs);
211 $this->assign('feed_format', Banana
::$feed_format);
212 $this->assign('feed_active', Banana
::$feed_active);
213 $this->assign('with_javascript', Banana
::$msgshow_javascript);
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'));
219 $this->register_modifier('b', '_b_');
221 $this->assign('errors', $this->error
);
222 $this->assign('page', $this->page
);
223 $this->assign('pages', $this->pages
);
224 $this->assign('actions', $this->actions
);
225 $this->register_modifier('banana_utf8entities', 'banana_utf8entities');
226 $this->register_modifier('banana_entities', 'banana_entities');
229 $this->default_modifiers
= Array('@banana_entities');
232 if (!Banana
::$debug_smarty) {
233 $error_level = error_reporting(0);
235 $text = $this->fetch($tpl);
236 if (!Banana
::$debug_smarty) {
237 error_reporting($error_level);
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
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
255 * smarty funciton : {url param1=... param2=...}
257 public function makeUrl(array $params, $smarty = null
)
259 if (function_exists('hook_makeLink')
260 && $res = hook_makeLink($params)) {
263 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
264 $host = Banana
::$baseurl ? Banana
::$baseurl : $_SERVER['SERVER_NAME'];
265 $file = $_SERVER['PHP_SELF'];
267 if (count($params) != 0) {
269 foreach ($params as $key=>$value) {
270 if (strlen($get) != 1) {
273 $get .= $key . '=' . $value;
278 return $proto . $host . $file . $get;
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
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
293 * Smarty function : {link param1=... param2=...}
295 public function makeLink(array $params, $smarty = null
)
297 $catch = array('text', 'popup', 'class', 'accesskey', 'style');
298 foreach ($catch as $key) {
299 $
{$key} = isset($params[$key]) ?
$params[$key] : null
;
300 unset($params[$key]);
302 $link = $this->makeUrl($params, &$smarty);
303 if (is_null($text)) {
306 if (!is_null($accesskey)) {
307 $popup .= ' (raccourci : ' . $accesskey . ')';
309 if (!is_null($popup)) {
310 $popup = ' title="' . banana_entities($popup) . '"';
312 if (!is_null($class)) {
313 $class = ' class="' . $class . '"';
315 if (!is_null($style)) {
316 $style = ' style="' . $style . '"';
318 if (!is_null($accesskey)) {
319 $accesskey = ' accesskey="' . $accesskey . '"';
321 return '<a href="' . banana_entities($link) . '"'
322 . $popup . $class . $style . $accesskey
323 . '>' . $text . '</a>';
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)
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
337 * Smarty function: {img img=... alt=... [height=...] [width=...]}
339 public function makeImg(array $params, $smarty = null
)
341 $catch = array('img', 'alt', 'height', 'width');
342 foreach ($catch as $key) {
343 $
{$key} = isset($params[$key]) ?
$params[$key] : null
;
346 if (function_exists('hook_makeImg')
347 && $res = hook_makeImg($img, $alt, $height, $width)) {
351 if (!is_null($width)) {
352 $width = ' width="' . $width . '"';
354 if (!is_null($height)) {
355 $height = ' height="' . $height . '"';
358 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
359 $host = Banana
::$baseurl ? Banana
::$baseurl : $_SERVER['SERVER_NAME'];
360 $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img;
361 $url = $proto . $host . $file;
363 return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
366 /** Build a link to one of the banana built-in javascript
367 * @param src STRING javascript name
368 * @return Javascript tag
370 public function makeJs($src)
372 if (!Banana
::$msgshow_javascript) {
375 if (function_exists('hook_makeJs')
376 && $res = hook_makeJs($src)) {
380 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
381 $host = Banana
::$baseurl ? Banana
::$baseurl : $_SERVER['SERVER_NAME'];
382 $file = dirname($_SERVER['PHP_SELF']) . '/javascript/' . $src . '.js';
383 $url = $proto . $host . $file;
385 return '<script type="text/javascript" src="' . $url . '"/></script>';
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
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
396 * Smarty function : {imglink img=... alt=... [param1=...]}
398 public function makeImgLink(array $params, $smarty = null
)
400 if (!isset($params['popup'])) {
401 $params['popup'] = @$params['alt'];
403 $img = $this->makeImg($params, $smarty);
404 if (isset($params['text'])) {
405 $img .= ' ' . $params['text'];
407 $params['text'] = $img;
408 unset($params['alt']);
409 unset($params['img']);
410 unset($params['width']);
411 unset($params['height']);
412 return $this->makeLink($params, $smarty);
415 /** Redirect to the page with the given parameter
418 public function redirect(array $params = array())
420 header('Location: ' . $this->makeUrl($params));
425 // {{{ function banana_trimwhitespace
427 function banana_trimwhitespace($source, $smarty)
429 $tags = array('script', 'pre', 'textarea');
431 foreach ($tags as $tag) {
432 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, $
{$tag});
433 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
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);
440 foreach ($tags as $tag) {
441 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
448 // {{{ function rss_date
450 function rss_date($t)
452 return date('r', $t);
457 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: