8b2602c775bb4d577d599039d0b7188ce46fb468
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();
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 if (Banana
::$spool) {
116 $subject = Banana
::$spool->overview
[Banana
::$artid]->subject
;
117 } else if (Banana
::$message) {
118 $subject = Banana
::$message->getHeaderValue('subject');
120 $subject = _b_('Message');
122 if (strlen($subject) > 30) {
123 $subject = substr($subject, 0, 30) . '…';
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
);
131 } elseif ($this->page
== 'new') {
132 $this->registerPage('new', _b_('Nouveau'), null
);
135 foreach ($this->killed
as $page) {
136 unset($this->pages
[$page]);
138 foreach ($this->actions
as $key=>&$action) {
139 if (!is_null($action['pages']) && !in_array($this->page
, $action['pages'])) {
140 unset($this->actions
[$key]);
144 return 'banana-base.tpl';
147 /** Generate XHTML code
149 public function run()
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();
158 return $this->_run($tpl);
161 /** Generate feed XML code
163 public function feed()
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
);
180 private function _run($tpl, $ent = true
)
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);
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_');
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');
211 $this->default_modifiers
= Array('@banana_entities');
214 if (!Banana
::$debug_smarty) {
215 $error_level = error_reporting(0);
217 $text = $this->fetch($tpl);
218 if (!Banana
::$debug_smarty) {
219 error_reporting($error_level);
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
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
237 * smarty funciton : {url param1=... param2=...}
239 public function makeUrl(array $params, $smarty = null
)
241 if (function_exists('hook_makeLink')
242 && $res = hook_makeLink($params)) {
245 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
246 $host = Banana
::$baseurl ? Banana
::$baseurl : $_SERVER['SERVER_NAME'];
247 $file = $_SERVER['PHP_SELF'];
249 if (count($params) != 0) {
251 foreach ($params as $key=>$value) {
252 if (strlen($get) != 1) {
255 $get .= $key . '=' . $value;
260 return $proto . $host . $file . $get;
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
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
275 * Smarty function : {link param1=... param2=...}
277 public function makeLink(array $params, $smarty = null
)
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]);
284 $link = $this->makeUrl($params, &$smarty);
285 if (is_null($text)) {
288 if (!is_null($accesskey)) {
289 $popup .= ' (raccourci : ' . $accesskey . ')';
291 if (!is_null($popup)) {
292 $popup = ' title="' . banana_entities($popup) . '"';
294 if (!is_null($class)) {
295 $class = ' class="' . $class . '"';
297 if (!is_null($style)) {
298 $style = ' style="' . $style . '"';
300 if (!is_null($accesskey)) {
301 $accesskey = ' accesskey="' . $accesskey . '"';
303 return '<a href="' . banana_entities($link) . '"'
304 . $popup . $class . $style . $accesskey
305 . '>' . $text . '</a>';
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)
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
319 * Smarty function: {img img=... alt=... [height=...] [width=...]}
321 public function makeImg(array $params, $smarty = null
)
323 $catch = array('img', 'alt', 'height', 'width');
324 foreach ($catch as $key) {
325 $
{$key} = isset($params[$key]) ?
$params[$key] : null
;
328 if (function_exists('hook_makeImg')
329 && $res = hook_makeImg($img, $alt, $height, $width)) {
333 if (!is_null($width)) {
334 $width = ' width="' . $width . '"';
336 if (!is_null($height)) {
337 $height = ' height="' . $height . '"';
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;
345 return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
348 /** Build a link to one of the banana built-in javascript
349 * @param src STRING javascript name
350 * @return Javascript tag
352 public function makeJs($src)
354 if (!Banana
::$msgshow_javascript) {
357 if (function_exists('hook_makeJs')
358 && $res = hook_makeJs($src)) {
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;
367 return '<script type="text/javascript" src="' . $url . '"/></script>';
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
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
378 * Smarty function : {imglink img=... alt=... [param1=...]}
380 public function makeImgLink(array $params, $smarty = null
)
382 if (!isset($params['popup'])) {
383 $params['popup'] = @$params['alt'];
385 $img = $this->makeImg($params, $smarty);
386 if (isset($params['text'])) {
387 $img .= ' ' . $params['text'];
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);
397 /** Redirect to the page with the given parameter
400 public function redirect(array $params = array())
402 header('Location: ' . $this->makeUrl($params));
407 // {{{ function banana_trimwhitespace
409 function banana_trimwhitespace($source, $smarty)
411 $tags = array('script', 'pre', 'textarea');
413 foreach ($tags as $tag) {
414 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, $
{$tag});
415 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
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);
422 foreach ($tags as $tag) {
423 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
430 // {{{ function rss_date
432 function rss_date($t)
434 return date('r', $t);
439 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: