Fixes vim mode line.
[banana.git] / banana / page.inc.php
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
10 if (@!include_once('Smarty.class.php')) {
11 require_once 'smarty/libs/Smarty.class.php';
12 }
13
14 class BananaPage extends Smarty
15 {
16 protected $error = array();
17 protected $page = null;
18
19 protected $pages = array();
20 protected $killed = array();
21 protected $actions = array();
22
23 protected $mode;
24 protected $json_params = array();
25
26 public $css = '';
27
28 public function __construct($mode = null)
29 {
30 parent::Smarty();
31
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);
39 }
40 }
41
42 /** Add an error message
43 * @param message STRING html code of the error to display
44 */
45 public function trig($message)
46 {
47 $this->error[] = $message;
48 }
49
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
53 */
54 public function kill($message)
55 {
56 $this->trig($message);
57 $this->assign('killed', true);
58 return $this->run();
59 }
60
61 /** Set the current page
62 * @param page STRING page name
63 */
64 public function setPage($page)
65 {
66 $this->page = $page;
67 return true;
68 }
69
70 /** Assign a variable in the page.
71 */
72 public function assign($var, $value)
73 {
74 if ($this->mode === 'json') {
75 $this->json_params[$var] = $value;
76 } else {
77 parent::assign($var, $value);
78 }
79 }
80
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
85 */
86 public function registerAction($action_code, array $pages = null)
87 {
88 $this->actions[] = array('text' => $action_code, 'pages' => $pages);
89 return true;
90 }
91
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
97 */
98 public function registerPage($name, $text, $template = null)
99 {
100 $this->pages[$name] = array('text' => $text, 'template' => $template);
101 return true;
102 }
103
104 /** Remove a page
105 * @param page STRING page name to kill
106 */
107 public function killPage($page)
108 {
109 $this->killed[] = $page;
110 }
111
112 /** Add Inline CSS to put in the page headers
113 * @param css CSS code
114 */
115 public function addCssInline($css)
116 {
117 $this->css .= $css;
118 }
119
120 /** Preparte the page generation
121 * @return template to use
122 */
123 protected function prepare()
124 {
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');
134 } else {
135 $subject = _b_('Message');
136 }
137 if (strlen($subject) > 30) {
138 $subject = substr($subject, 0, 30) . '…';
139 }
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);
145 }
146 } elseif ($this->page == 'new') {
147 $this->registerPage('new', _b_('Nouveau'), null);
148 }
149 }
150 foreach ($this->killed as $page) {
151 unset($this->pages[$page]);
152 }
153 foreach ($this->actions as $key=>&$action) {
154 if (!is_null($action['pages']) && !in_array($this->page, $action['pages'])) {
155 unset($this->actions[$key]);
156 }
157 }
158
159 return 'banana-base.tpl';
160 }
161
162 /** Generate XHTML code
163 */
164 public function run()
165 {
166 if ($this->mode === 'json') {
167 return json_encode($this->json_params);
168 }
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();
173 $this->page = null;
174 }
175
176 return $this->_run($tpl);
177 }
178
179 /** Generate feed XML code
180 */
181 public function feed()
182 {
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);
193 exit;
194 }
195
196 /** Code generation
197 */
198 private function _run($tpl, $ent = true)
199 {
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);
214
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_');
220
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');
227
228 if ($ent) {
229 $this->default_modifiers = Array('@banana_entities');
230 }
231
232 if (!Banana::$debug_smarty) {
233 $error_level = error_reporting(0);
234 }
235 $text = $this->fetch($tpl);
236 if (!Banana::$debug_smarty) {
237 error_reporting($error_level);
238 }
239 return $text;
240 }
241
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
246 *
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
254 *
255 * smarty funciton : {url param1=... param2=...}
256 */
257 public function makeUrl(array $params, $smarty = null)
258 {
259 if (function_exists('hook_makeLink')
260 && $res = hook_makeLink($params)) {
261 return $res;
262 }
263 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
264 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
265 $file = $_SERVER['PHP_SELF'];
266
267 if (count($params) != 0) {
268 $get = '?';
269 foreach ($params as $key=>$value) {
270 if (strlen($get) != 1) {
271 $get .= '&';
272 }
273 $get .= $key . '=' . $value;
274 }
275 } else {
276 $get = '';
277 }
278 return $proto . $host . $file . $get;
279 }
280
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
285 *
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
292 *
293 * Smarty function : {link param1=... param2=...}
294 */
295 public function makeLink(array $params, $smarty = null)
296 {
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]);
301 }
302 $link = $this->makeUrl($params, $smarty);
303 if (is_null($text)) {
304 $text = $link;
305 }
306 if (!is_null($accesskey)) {
307 $popup .= ' (raccourci : ' . $accesskey . ')';
308 }
309 if (!is_null($popup)) {
310 $popup = ' title="' . banana_entities($popup) . '"';
311 }
312 if (!is_null($class)) {
313 $class = ' class="' . $class . '"';
314 }
315 if (!is_null($style)) {
316 $style = ' style="' . $style . '"';
317 }
318 if (!is_null($accesskey)) {
319 $accesskey = ' accesskey="' . $accesskey . '"';
320 }
321 return '<a href="' . banana_entities($link) . '"'
322 . $popup . $class . $style . $accesskey
323 . '>' . $text . '</a>';
324 }
325
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)
329 * @return Img tag
330 *
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
336 *
337 * Smarty function: {img img=... alt=... [height=...] [width=...]}
338 */
339 public function makeImg(array $params, $smarty = null)
340 {
341 $catch = array('img', 'alt', 'height', 'width');
342 foreach ($catch as $key) {
343 ${$key} = isset($params[$key]) ? $params[$key] : null;
344 }
345 $img .= ".gif";
346 if (function_exists('hook_makeImg')
347 && $res = hook_makeImg($img, $alt, $height, $width)) {
348 return $res;
349 }
350
351 if (!is_null($width)) {
352 $width = ' width="' . $width . '"';
353 }
354 if (!is_null($height)) {
355 $height = ' height="' . $height . '"';
356 }
357
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;
362
363 return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
364 }
365
366 /** Build a link to one of the banana built-in javascript
367 * @param src STRING javascript name
368 * @return Javascript tag
369 */
370 public function makeJs($src)
371 {
372 if (!Banana::$msgshow_javascript) {
373 return '';
374 }
375 if (function_exists('hook_makeJs')
376 && $res = hook_makeJs($src)) {
377 return $res;
378 }
379
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;
384
385 return '<script type="text/javascript" src="' . $url . '"/></script>';
386 }
387
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
392 *
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
395 *
396 * Smarty function : {imglink img=... alt=... [param1=...]}
397 */
398 public function makeImgLink(array $params, $smarty = null)
399 {
400 if (!isset($params['popup'])) {
401 $params['popup'] = @$params['alt'];
402 }
403 $img = $this->makeImg($params, $smarty);
404 if (isset($params['text'])) {
405 $img .= ' ' . $params['text'];
406 }
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);
413 }
414
415 /** Redirect to the page with the given parameter
416 * @ref makeURL
417 */
418 public function redirect(array $params = array())
419 {
420 header('Location: ' . $this->makeUrl($params));
421 exit;
422 }
423 }
424
425 // {{{ function banana_trimwhitespace
426
427 function banana_trimwhitespace($source, $smarty)
428 {
429 $tags = array('script', 'pre', 'textarea');
430
431 foreach ($tags as $tag) {
432 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, ${$tag});
433 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
434 }
435
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);
439
440 foreach ($tags as $tag) {
441 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
442 }
443
444 return $source;
445 }
446
447 // }}}
448 // {{{ function rss_date
449
450 function rss_date($t)
451 {
452 return date('r', $t);
453 }
454
455 // }}}
456
457 // vim:set et sw=4 sts=4 ts=4 fenc=utf-8:
458 ?>