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 private $error = array();
19 private $pages = array();
20 private $killed = array();
21 private $actions = array();
25 public function __construct()
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
);
38 /** Add an error message
39 * @param message STRING html code of the error to display
41 public function trig($message)
43 $this->error
[] = $message;
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
50 public function kill($message)
52 $this->trig($message);
53 $this->assign('killed', true
);
57 /** Set the current page
58 * @param page STRING page name
60 public function setPage($page)
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
71 public function registerAction($action_code, array $pages = null
)
73 $this->actions
[] = array('text' => $action_code, 'pages' => $pages);
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
83 public function registerPage($name, $text, $template = null
)
85 $this->pages
[$name] = array('text' => $text, 'template' => $template);
90 * @param page STRING page name to kill
92 public function killPage($page)
94 $this->killed
[] = $page;
97 /** Add Inline CSS to put in the page headers
100 public function addCssInline($css)
105 /** Preparte the page generation
106 * @return template to use
108 protected function prepare()
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 $this->registerPage('message', _b_('Message'), null
);
116 if ($this->page
== 'cancel') {
117 $this->registerPage('cancel', _b_('Annulation'), null
);
118 } elseif ($this->page
== 'new') {
119 $this->registerPage('new', _b_('Répondre'), null
);
121 } elseif ($this->page
== 'new') {
122 $this->registerPage('new', _b_('Nouveau'), null
);
125 foreach ($this->killed
as $page) {
126 unset($this->pages
[$page]);
128 foreach ($this->actions
as $key=>&$action) {
129 if (!is_null($action['pages']) && !in_array($this->page
, $action['pages'])) {
130 unset($this->actions
[$key]);
134 return 'banana-base.tpl';
137 /** Generate XHTML code
139 public function run()
141 $tpl = $this->prepare();
142 if (!isset($this->pages
[$this->page
])) {
143 $this->trig(_b_('La page demandée n\'existe pas'));
144 $this->actions
= array();
148 return $this->_run($tpl);
151 /** Generate feed XML code
153 public function feed()
155 @list
($lg) = explode('_', Banana
::$profile['locale']);
156 $tpl = 'banana-feed-' . Banana
::$feed_format . '.tpl';
157 $this->assign('copyright', Banana
::$feed_copyright);
158 $this->assign('generator', Banana
::$feed_generator);
159 $this->assign('email', Banana
::$feed_email);
160 $this->assign('title_prefix', Banana
::$feed_namePrefix);
161 $this->assign('language', $lg);
162 $this->register_function('rss_date', 'rss_date');
163 header('Content-Type: application/rss+xml; charset=utf-8');
164 echo $this->_run($tpl, false
);
170 private function _run($tpl, $ent = true
)
172 $this->assign('group', Banana
::$group);
173 $this->assign('artid', Banana
::$artid);
174 $this->assign('part', Banana
::$part);
175 $this->assign('first', Banana
::$first);
176 $this->assign('action', Banana
::$action);
177 $this->assign('profile', Banana
::$profile);
178 $this->assign('spool', Banana
::$spool);
179 $this->assign('protocole', Banana
::$protocole);
180 $this->assign('showboxlist', Banana
::$spool_boxlist);
181 $this->assign('showthread', Banana
::$msgshow_withthread);
182 $this->assign('withtabs' , Banana
::$withtabs);
183 $this->assign('feed_format', Banana
::$feed_format);
184 $this->assign('feed_active', Banana
::$feed_active);
186 $this->register_function('url', array($this, 'makeUrl'));
187 $this->register_function('link', array($this, 'makeLink'));
188 $this->register_function('imglink', array($this, 'makeImgLink'));
189 $this->register_function('img', array($this, 'makeImg'));
190 $this->register_modifier('b', '_b_');
192 $this->assign('errors', $this->error
);
193 $this->assign('page', $this->page
);
194 $this->assign('pages', $this->pages
);
195 $this->assign('actions', $this->actions
);
196 $this->register_modifier('banana_utf8entities', 'banana_utf8entities');
197 $this->register_modifier('banana_entities', 'banana_entities');
200 $this->default_modifiers
= Array('@banana_entities');
203 if (!Banana
::$debug_smarty) {
204 $error_level = error_reporting(0);
206 $text = $this->fetch($tpl);
207 if (!Banana
::$debug_smarty) {
208 error_reporting($error_level);
213 /** Build a URL in Banana
214 * @param params ARRAY location datas
215 * @param smarty OBJECT Smarty instance associated (null if none)
216 * @return URL of the page associated with the given parameters
218 * Usual parameters are :
219 * - group : the box name
220 * - artid : the current message id (index of message-id)
221 * - part : part id to show (may be a content-id, xface or a mime-type for text)
222 * - first : first linear-index to show in spool view
223 * - action: like subscribe, cancel, new
224 * - all others params are allowed, but not parsed by the base implementation of banana
226 * smarty funciton : {url param1=... param2=...}
228 public function makeUrl(array $params, &$smarty = null
)
230 if (function_exists('hook_makeLink')
231 && $res = hook_makeLink($params)) {
234 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
235 $host = Banana
::$baseurl ? Banana
::$baseurl : $_SERVER['SERVER_NAME'];
236 $file = $_SERVER['PHP_SELF'];
238 if (count($params) != 0) {
240 foreach ($params as $key=>$value) {
241 if (strlen($get) != 1) {
244 $get .= $key . '=' . $value;
249 return $proto . $host . $file . $get;
252 /** Build a link to a Banana page
253 * @param params ARRAY location datas
254 * @param smarty OBJECT Smarty instance associated (null if none)
255 * @return Link to the page associated with the given parameters
257 * Support all @ref makeURL parameters, but catch the following:
258 * - text : if set, defined the text of the link (if not set, the URL is used
259 * - popup : title of the link (showed as a tooltip on most browsers)
260 * - class : specific style class for the markup
261 * - accesskey: keyboard key to trigger the link
262 * None of this parameters is needed
264 * Smarty function : {link param1=... param2=...}
266 public function makeLink(array $params, &$smarty = null
)
268 $catch = array('text', 'popup', 'class', 'accesskey');
269 foreach ($catch as $key) {
270 $
{$key} = isset($params[$key]) ?
$params[$key] : null
;
271 unset($params[$key]);
273 $link = $this->makeUrl($params, &$smarty);
274 if (is_null($text)) {
277 if (!is_null($accesskey)) {
278 $popup .= ' (raccourci : ' . $accesskey . ')';
280 if (!is_null($popup)) {
281 $popup = ' title="' . banana_entities($popup) . '"';
283 if (!is_null($class)) {
284 $class = ' class="' . $class . '"';
286 if (!is_null($accesskey)) {
287 $accesskey = ' accesskey="' . $accesskey . '"';
289 return '<a href="' . banana_entities($link) . '"'
290 . $popup . $class . $accesskey
291 . '>' . $text . '</a>';
294 /** Build a link to one of the banana built-in images
295 * @param params ARRAY image datas
296 * @param smarty OBJECT Smarty instance associated (null if none)
299 * Supported parameters are
300 * - img : name of the image (without its extension)
301 * - alt : alternative text
302 * - height and width : dimensions of the images
303 * img and alt are needed
305 * Smarty function: {img img=... alt=... [height=...] [width=...]}
307 public function makeImg(array $params, &$smarty = null
)
309 $catch = array('img', 'alt', 'height', 'width');
310 foreach ($catch as $key) {
311 $
{$key} = isset($params[$key]) ?
$params[$key] : null
;
314 if (function_exists('hook_makeImg')
315 && $res = hook_makeImg($img, $alt, $height, $width)) {
319 if (!is_null($width)) {
320 $width = ' width="' . $width . '"';
322 if (!is_null($height)) {
323 $height = ' height="' . $height . '"';
326 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
327 $host = Banana
::$baseurl ? Banana
::$baseurl : $_SERVER['SERVER_NAME'];
328 $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img;
329 $url = $proto . $host . $file;
331 return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
334 /** Build a link to one of the banana built-in javascript
335 * @param src STRING javascript name
336 * @return Javascript tag
338 public function makeJs($src)
340 if (!Banana
::$msgshow_javascript) {
343 if (function_exists('hook_makeJs')
344 && $res = hook_makeJs($src)) {
348 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
349 $host = Banana
::$baseurl ? Banana
::$baseurl : $_SERVER['SERVER_NAME'];
350 $file = dirname($_SERVER['PHP_SELF']) . '/javascript/' . $src . '.js';
351 $url = $proto . $host . $file;
353 return '<script type="text/javascript" src="' . $url . '"/></script>';
356 /** Build a link with an image as text
357 * @param params ARRAY image and location data
358 * @param smarty OBJECT Smarty instance associated (null if none)
359 * @return an image within an link
361 * All @ref makeImg and @ref makeLink parameters are supported
362 * if text is set, the text will be appended after the image in the link
364 * Smarty function : {imglink img=... alt=... [param1=...]}
366 public function makeImgLink(array $params, &$smarty = null
)
368 if (!isset($params['popup'])) {
369 $params['popup'] = @$params['alt'];
371 $img = $this->makeImg($params, $smarty);
372 if (isset($params['text'])) {
373 $img .= ' ' . $params['text'];
375 $params['text'] = $img;
376 unset($params['alt']);
377 unset($params['img']);
378 unset($params['width']);
379 unset($params['height']);
380 return $this->makeLink($params, $smarty);
383 /** Redirect to the page with the given parameter
386 public function redirect(array $params = array())
388 header('Location: ' . $this->makeUrl($params));
392 // {{{ function banana_trimwhitespace
394 function banana_trimwhitespace($source, &$smarty)
396 $tags = array('script', 'pre', 'textarea');
398 foreach ($tags as $tag) {
399 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, $
{$tag});
400 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
403 // remove all leading spaces, tabs and carriage returns NOT
404 // preceeded by a php close tag.
405 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
407 foreach ($tags as $tag) {
408 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
415 // {{{ function rss_date
417 function rss_date($t)
419 return date('r', $t);
424 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: