Integrates the PGP support with the web interface.
[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
e83d5aa0 10if (!include_once('Smarty.class.php')) {
11 require_once 'smarty/libs/Smarty.class.php';
12}
7027794f 13
14class BananaPage extends Smarty
15{
16 private $error = array();
17 private $page = null;
18
19 private $pages = array();
4eeddd59 20 private $killed = array();
7027794f 21 private $actions = array();
22
9c118ac9 23 public $css = '';
24
7027794f 25 public function __construct()
26 {
27 $this->Smarty();
28
29 $this->compile_check = Banana::$debug_smarty;
30 $this->template_dir = dirname(__FILE__) . '/templates/';
c82c827e 31 $this->compile_dir = Banana::$spool_root . '/templates_c/';
7027794f 32 $this->register_prefilter('banana_trimwhitespace');
a9676fc7 33 if (!is_dir($this->compile_dir)) {
34 mkdir($this->compile_dir);
35 }
7027794f 36 }
37
2f0aa8ce 38 /** Add an error message
39 * @param message STRING html code of the error to display
40 */
7027794f 41 public function trig($message)
42 {
43 $this->error[] = $message;
44 }
45
2f0aa8ce 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
49 */
7027794f 50 public function kill($message)
51 {
52 $this->trig($message);
2f0aa8ce 53 $this->assign('killed', true);
7027794f 54 return $this->run();
55 }
56
2f0aa8ce 57 /** Set the current page
58 * @param page STRING page name
59 */
7027794f 60 public function setPage($page)
61 {
62 $this->page = $page;
4eeddd59 63 return true;
7027794f 64 }
65
2f0aa8ce 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
70 */
7027794f 71 public function registerAction($action_code, array $pages = null)
72 {
73 $this->actions[] = array('text' => $action_code, 'pages' => $pages);
74 return true;
75 }
76
2f0aa8ce 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
82 */
7027794f 83 public function registerPage($name, $text, $template = null)
84 {
85 $this->pages[$name] = array('text' => $text, 'template' => $template);
86 return true;
87 }
b18124be 88
4eeddd59 89 /** Remove a page
90 * @param page STRING page name to kill
91 */
92 public function killPage($page)
93 {
94 $this->killed[] = $page;
95 }
96
9c118ac9 97 /** Add Inline CSS to put in the page headers
98 * @param css CSS code
99 */
100 public function addCssInline($css)
101 {
102 $this->css .= $css;
103 }
104
b18124be 105 /** Preparte the page generation
106 * @return template to use
2f0aa8ce 107 */
b18124be 108 protected function prepare()
7027794f 109 {
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') {
598a1c53 119 $this->registerPage('new', _b_('Répondre'), null);
7027794f 120 }
121 } elseif ($this->page == 'new') {
122 $this->registerPage('new', _b_('Nouveau'), null);
123 }
124 }
4eeddd59 125 foreach ($this->killed as $page) {
126 unset($this->pages[$page]);
127 }
7027794f 128 foreach ($this->actions as $key=>&$action) {
129 if (!is_null($action['pages']) && !in_array($this->page, $action['pages'])) {
130 unset($this->actions[$key]);
131 }
132 }
b18124be 133
134 return 'banana-base.tpl';
135 }
136
137 /** Generate XHTML code
138 */
139 public function run()
140 {
141 $tpl = $this->prepare();
4eeddd59 142 if (!isset($this->pages[$this->page])) {
598a1c53 143 $this->trig(_b_('La page demandée n\'existe pas'));
4eeddd59 144 $this->actions = array();
145 $this->page = null;
146 }
b18124be 147
e02edbfe 148 return $this->_run($tpl);
149 }
150
151 /** Generate feed XML code
152 */
153 public function feed()
154 {
155 @list($lg) = explode('_', Banana::$profile['locale']);
156 $tpl = 'banana-feed-' . Banana::$feed_format . '.tpl';
157 $this->assign('copyright', Banana::$feed_copyright);
cd58d954 158 $this->assign('generator', Banana::$feed_generator);
159 $this->assign('email', Banana::$feed_email);
e02edbfe 160 $this->assign('title_prefix', Banana::$feed_namePrefix);
161 $this->assign('language', $lg);
162 $this->register_function('rss_date', 'rss_date');
dfb752b1 163 header('Content-Type: application/rss+xml; charset=utf-8');
cd58d954 164 echo $this->_run($tpl, false);
dfb752b1 165 exit;
e02edbfe 166 }
167
168 /** Code generation
169 */
cd58d954 170 private function _run($tpl, $ent = true)
e02edbfe 171 {
7027794f 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);
8bfa4595 180 $this->assign('showboxlist', Banana::$spool_boxlist);
e95f5b5e 181 $this->assign('showthread', Banana::$msgshow_withthread);
182 $this->assign('withtabs' , Banana::$withtabs);
e02edbfe 183 $this->assign('feed_format', Banana::$feed_format);
184 $this->assign('feed_active', Banana::$feed_active);
7027794f 185
b18124be 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'));
a0b2e16c 190 $this->register_modifier('b', '_b_');
4eeddd59 191
7027794f 192 $this->assign('errors', $this->error);
193 $this->assign('page', $this->page);
194 $this->assign('pages', $this->pages);
195 $this->assign('actions', $this->actions);
4f7c063d 196 $this->register_modifier('banana_utf8entities', 'banana_utf8entities');
197 $this->register_modifier('banana_entities', 'banana_entities');
198
199 if ($ent) {
200 $this->default_modifiers = Array('@banana_entities');
201 }
7027794f 202
7027794f 203 if (!Banana::$debug_smarty) {
204 $error_level = error_reporting(0);
205 }
b18124be 206 $text = $this->fetch($tpl);
7027794f 207 if (!Banana::$debug_smarty) {
208 error_reporting($error_level);
209 }
210 return $text;
211 }
212
2f0aa8ce 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
217 *
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
225 *
226 * smarty funciton : {url param1=... param2=...}
227 */
228 public function makeUrl(array $params, &$smarty = null)
7027794f 229 {
230 if (function_exists('hook_makeLink')
231 && $res = hook_makeLink($params)) {
232 return $res;
233 }
234 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
068c6301 235 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
7027794f 236 $file = $_SERVER['PHP_SELF'];
237
238 if (count($params) != 0) {
239 $get = '?';
240 foreach ($params as $key=>$value) {
241 if (strlen($get) != 1) {
242 $get .= '&';
243 }
244 $get .= $key . '=' . $value;
245 }
246 } else {
247 $get = '';
248 }
249 return $proto . $host . $file . $get;
250 }
251
2f0aa8ce 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
256 *
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
263 *
264 * Smarty function : {link param1=... param2=...}
265 */
266 public function makeLink(array $params, &$smarty = null)
7027794f 267 {
268 $catch = array('text', 'popup', 'class', 'accesskey');
269 foreach ($catch as $key) {
270 ${$key} = isset($params[$key]) ? $params[$key] : null;
271 unset($params[$key]);
272 }
273 $link = $this->makeUrl($params, &$smarty);
274 if (is_null($text)) {
275 $text = $link;
276 }
277 if (!is_null($accesskey)) {
278 $popup .= ' (raccourci : ' . $accesskey . ')';
279 }
280 if (!is_null($popup)) {
4f7c063d 281 $popup = ' title="' . banana_entities($popup) . '"';
7027794f 282 }
283 if (!is_null($class)) {
284 $class = ' class="' . $class . '"';
285 }
7027794f 286 if (!is_null($accesskey)) {
287 $accesskey = ' accesskey="' . $accesskey . '"';
288 }
4f7c063d 289 return '<a href="' . banana_entities($link) . '"'
2f0aa8ce 290 . $popup . $class . $accesskey
7027794f 291 . '>' . $text . '</a>';
292 }
293
2f0aa8ce 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)
297 * @return Img tag
298 *
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
304 *
305 * Smarty function: {img img=... alt=... [height=...] [width=...]}
306 */
307 public function makeImg(array $params, &$smarty = null)
7027794f 308 {
309 $catch = array('img', 'alt', 'height', 'width');
310 foreach ($catch as $key) {
311 ${$key} = isset($params[$key]) ? $params[$key] : null;
312 }
313 $img .= ".gif";
314 if (function_exists('hook_makeImg')
315 && $res = hook_makeImg($img, $alt, $height, $width)) {
316 return $res;
317 }
318
319 if (!is_null($width)) {
320 $width = ' width="' . $width . '"';
321 }
322 if (!is_null($height)) {
323 $height = ' height="' . $height . '"';
324 }
325
326 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
67f43f06 327 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
7027794f 328 $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img;
329 $url = $proto . $host . $file;
330
331 return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
332 }
2f0aa8ce 333
96e1e874 334 /** Build a link to one of the banana built-in javascript
335 * @param src STRING javascript name
336 * @return Javascript tag
337 */
848a4944 338 public function makeJs($src)
96e1e874 339 {
727b37b8 340 if (!Banana::$msgshow_javascript) {
341 return '';
342 }
96e1e874 343 if (function_exists('hook_makeJs')
344 && $res = hook_makeJs($src)) {
345 return $res;
346 }
347
348 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
67f43f06 349 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
96e1e874 350 $file = dirname($_SERVER['PHP_SELF']) . '/javascript/' . $src . '.js';
351 $url = $proto . $host . $file;
352
353 return '<script type="text/javascript" src="' . $url . '"/></script>';
354 }
355
2f0aa8ce 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
360 *
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
363 *
364 * Smarty function : {imglink img=... alt=... [param1=...]}
365 */
366 public function makeImgLink(array $params, &$smarty = null)
7027794f 367 {
2f0aa8ce 368 if (!isset($params['popup'])) {
e02edbfe 369 $params['popup'] = @$params['alt'];
2f0aa8ce 370 }
371 $img = $this->makeImg($params, $smarty);
372 if (isset($params['text'])) {
373 $img .= ' ' . $params['text'];
374 }
375 $params['text'] = $img;
e02edbfe 376 unset($params['alt']);
377 unset($params['img']);
378 unset($params['width']);
379 unset($params['height']);
7027794f 380 return $this->makeLink($params, $smarty);
381 }
382
383 /** Redirect to the page with the given parameter
2f0aa8ce 384 * @ref makeURL
7027794f 385 */
2f0aa8ce 386 public function redirect(array $params = array())
7027794f 387 {
388 header('Location: ' . $this->makeUrl($params));
389 }
390}
391
392// {{{ function banana_trimwhitespace
393
394function banana_trimwhitespace($source, &$smarty)
395{
396 $tags = array('script', 'pre', 'textarea');
397
398 foreach ($tags as $tag) {
399 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, ${$tag});
400 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
401 }
402
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);
406
407 foreach ($tags as $tag) {
408 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
409 }
410
411 return $source;
412}
413
414// }}}
e02edbfe 415// {{{ function rss_date
416
417function rss_date($t)
418{
419 return date('r', $t);
420}
7027794f 421
e02edbfe 422// }}}
7027794f 423
598a1c53 424// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
7027794f 425?>