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 require_once 'smarty/libs/Smarty.class.php';
12 class BananaPage
extends Smarty
14 private $error = array();
17 private $pages = array();
18 private $killed = array();
19 private $actions = array();
23 public function __construct()
27 $this->compile_check
= Banana
::$debug_smarty;
28 $this->template_dir
= dirname(__FILE__
) . '/templates/';
29 $this->compile_dir
= Banana
::$spool_root . '/templates_c/';
30 $this->register_prefilter('banana_trimwhitespace');
34 /** Add an error message
35 * @param message STRING html code of the error to display
37 public function trig($message)
39 $this->error
[] = $message;
42 /** Kill the current page (generate an error message and skip page generation)
43 * @param message STRING html code of the error message to display
44 @ @return XHTML code of the page
46 public function kill($message)
48 $this->trig($message);
49 $this->assign('killed', true
);
53 /** Set the current page
54 * @param page STRING page name
56 public function setPage($page)
62 /** Register an action to show on banana page
63 * @param action_code HTML code of the action
64 * @param pages ARRAY pages where to show the action (null == every pages)
65 * @return true if success
67 public function registerAction($action_code, array $pages = null
)
69 $this->actions
[] = array('text' => $action_code, 'pages' => $pages);
73 /** Register a new page
74 * @param name Name of the page
75 * @param text Text for the tab of the page
76 * @param template Template path for the page if null, the page is not handled by banana
77 * @return true if success
79 public function registerPage($name, $text, $template = null
)
81 $this->pages
[$name] = array('text' => $text, 'template' => $template);
86 * @param page STRING page name to kill
88 public function killPage($page)
90 $this->killed
[] = $page;
93 /** Add Inline CSS to put in the page headers
96 public function addCssInline($css)
101 /** Preparte the page generation
102 * @return template to use
104 protected function prepare()
106 $this->registerPage('subscribe', _b_('Abonnements'), null
);
107 $this->registerPage('forums', _b_('Les forums'), null
);
108 if (!is_null(Banana
::$group)) {
109 $this->registerPage('thread', Banana
::$group, null
);
110 if (!is_null(Banana
::$artid)) {
111 $this->registerPage('message', _b_('Message'), null
);
112 if ($this->page
== 'cancel') {
113 $this->registerPage('cancel', _b_('Annulation'), null
);
114 } elseif ($this->page
== 'new') {
115 $this->registerPage('new', _b_('Répondre'), null
);
117 } elseif ($this->page
== 'new') {
118 $this->registerPage('new', _b_('Nouveau'), null
);
121 foreach ($this->killed
as $page) {
122 unset($this->pages
[$page]);
124 foreach ($this->actions
as $key=>&$action) {
125 if (!is_null($action['pages']) && !in_array($this->page
, $action['pages'])) {
126 unset($this->actions
[$key]);
130 return 'banana-base.tpl';
133 /** Generate XHTML code
135 public function run()
137 $tpl = $this->prepare();
138 if (!isset($this->pages
[$this->page
])) {
139 $this->trig(_b_('La page demandée n\'existe pas'));
140 $this->actions
= array();
144 return $this->_run($tpl);
147 /** Generate feed XML code
149 public function feed()
151 @list
($lg) = explode('_', Banana
::$profile['locale']);
152 $tpl = 'banana-feed-' . Banana
::$feed_format . '.tpl';
153 $this->assign('copyright', Banana
::$feed_copyright);
154 $this->assign('generator', Banana
::$feed_generator);
155 $this->assign('email', Banana
::$feed_email);
156 $this->assign('title_prefix', Banana
::$feed_namePrefix);
157 $this->assign('language', $lg);
158 $this->register_function('rss_date', 'rss_date');
159 header('Content-Type: application/rss+xml; charset=utf-8');
160 echo $this->_run($tpl, false
);
166 private function _run($tpl, $ent = true
)
168 $this->assign('group', Banana
::$group);
169 $this->assign('artid', Banana
::$artid);
170 $this->assign('part', Banana
::$part);
171 $this->assign('first', Banana
::$first);
172 $this->assign('action', Banana
::$action);
173 $this->assign('profile', Banana
::$profile);
174 $this->assign('spool', Banana
::$spool);
175 $this->assign('protocole', Banana
::$protocole);
176 $this->assign('showboxlist', Banana
::$spool_boxlist);
177 $this->assign('showthread', Banana
::$msgshow_withthread);
178 $this->assign('withtabs' , Banana
::$withtabs);
179 $this->assign('feed_format', Banana
::$feed_format);
180 $this->assign('feed_active', Banana
::$feed_active);
182 $this->register_function('url', array($this, 'makeUrl'));
183 $this->register_function('link', array($this, 'makeLink'));
184 $this->register_function('imglink', array($this, 'makeImgLink'));
185 $this->register_function('img', array($this, 'makeImg'));
186 $this->register_modifier('b', '_b_');
188 $this->assign('errors', $this->error
);
189 $this->assign('page', $this->page
);
190 $this->assign('pages', $this->pages
);
191 $this->assign('actions', $this->actions
);
192 $this->register_modifier('banana_utf8entities', 'banana_utf8entities');
193 $this->register_modifier('banana_entities', 'banana_entities');
196 $this->default_modifiers
= Array('@banana_entities');
199 if (!Banana
::$debug_smarty) {
200 $error_level = error_reporting(0);
202 $text = $this->fetch($tpl);
203 if (!Banana
::$debug_smarty) {
204 error_reporting($error_level);
209 /** Build a URL in Banana
210 * @param params ARRAY location datas
211 * @param smarty OBJECT Smarty instance associated (null if none)
212 * @return URL of the page associated with the given parameters
214 * Usual parameters are :
215 * - group : the box name
216 * - artid : the current message id (index of message-id)
217 * - part : part id to show (may be a content-id, xface or a mime-type for text)
218 * - first : first linear-index to show in spool view
219 * - action: like subscribe, cancel, new
220 * - all others params are allowed, but not parsed by the base implementation of banana
222 * smarty funciton : {url param1=... param2=...}
224 public function makeUrl(array $params, &$smarty = null
)
226 if (function_exists('hook_makeLink')
227 && $res = hook_makeLink($params)) {
230 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
231 $host = $_SERVER['HTTP_HOST'];
232 $file = $_SERVER['PHP_SELF'];
234 if (count($params) != 0) {
236 foreach ($params as $key=>$value) {
237 if (strlen($get) != 1) {
240 $get .= $key . '=' . $value;
245 return $proto . $host . $file . $get;
248 /** Build a link to a Banana page
249 * @param params ARRAY location datas
250 * @param smarty OBJECT Smarty instance associated (null if none)
251 * @return Link to the page associated with the given parameters
253 * Support all @ref makeURL parameters, but catch the following:
254 * - text : if set, defined the text of the link (if not set, the URL is used
255 * - popup : title of the link (showed as a tooltip on most browsers)
256 * - class : specific style class for the markup
257 * - accesskey: keyboard key to trigger the link
258 * None of this parameters is needed
260 * Smarty function : {link param1=... param2=...}
262 public function makeLink(array $params, &$smarty = null
)
264 $catch = array('text', 'popup', 'class', 'accesskey');
265 foreach ($catch as $key) {
266 $
{$key} = isset($params[$key]) ?
$params[$key] : null
;
267 unset($params[$key]);
269 $link = $this->makeUrl($params, &$smarty);
270 if (is_null($text)) {
273 if (!is_null($accesskey)) {
274 $popup .= ' (raccourci : ' . $accesskey . ')';
276 if (!is_null($popup)) {
277 $popup = ' title="' . banana_entities($popup) . '"';
279 if (!is_null($class)) {
280 $class = ' class="' . $class . '"';
282 if (!is_null($accesskey)) {
283 $accesskey = ' accesskey="' . $accesskey . '"';
285 return '<a href="' . banana_entities($link) . '"'
286 . $popup . $class . $accesskey
287 . '>' . $text . '</a>';
290 /** Build a link to one of the banana built-in images
291 * @param params ARRAY image datas
292 * @param smarty OBJECT Smarty instance associated (null if none)
295 * Supported parameters are
296 * - img : name of the image (without its extension)
297 * - alt : alternative text
298 * - height and width : dimensions of the images
299 * img and alt are needed
301 * Smarty function: {img img=... alt=... [height=...] [width=...]}
303 public function makeImg(array $params, &$smarty = null
)
305 $catch = array('img', 'alt', 'height', 'width');
306 foreach ($catch as $key) {
307 $
{$key} = isset($params[$key]) ?
$params[$key] : null
;
310 if (function_exists('hook_makeImg')
311 && $res = hook_makeImg($img, $alt, $height, $width)) {
315 if (!is_null($width)) {
316 $width = ' width="' . $width . '"';
318 if (!is_null($height)) {
319 $height = ' height="' . $height . '"';
322 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
323 $host = $_SERVER['HTTP_HOST'];
324 $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img;
325 $url = $proto . $host . $file;
327 return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
330 /** Build a link to one of the banana built-in javascript
331 * @param src STRING javascript name
332 * @return Javascript tag
334 public function makeJs($src)
336 if (function_exists('hook_makeJs')
337 && $res = hook_makeJs($src)) {
341 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
342 $host = $_SERVER['HTTP_HOST'];
343 $file = dirname($_SERVER['PHP_SELF']) . '/javascript/' . $src . '.js';
344 $url = $proto . $host . $file;
346 return '<script type="text/javascript" src="' . $url . '"/></script>';
349 /** Build a link with an image as text
350 * @param params ARRAY image and location data
351 * @param smarty OBJECT Smarty instance associated (null if none)
352 * @return an image within an link
354 * All @ref makeImg and @ref makeLink parameters are supported
355 * if text is set, the text will be appended after the image in the link
357 * Smarty function : {imglink img=... alt=... [param1=...]}
359 public function makeImgLink(array $params, &$smarty = null
)
361 if (!isset($params['popup'])) {
362 $params['popup'] = @$params['alt'];
364 $img = $this->makeImg($params, $smarty);
365 if (isset($params['text'])) {
366 $img .= ' ' . $params['text'];
368 $params['text'] = $img;
369 unset($params['alt']);
370 unset($params['img']);
371 unset($params['width']);
372 unset($params['height']);
373 return $this->makeLink($params, $smarty);
376 /** Redirect to the page with the given parameter
379 public function redirect(array $params = array())
381 header('Location: ' . $this->makeUrl($params));
385 // {{{ function banana_trimwhitespace
387 function banana_trimwhitespace($source, &$smarty)
389 $tags = array('script', 'pre', 'textarea');
391 foreach ($tags as $tag) {
392 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, $
{$tag});
393 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
396 // remove all leading spaces, tabs and carriage returns NOT
397 // preceeded by a php close tag.
398 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
400 foreach ($tags as $tag) {
401 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
408 // {{{ function rss_date
410 function rss_date($t)
412 return date('r', $t);
417 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: