Add javascript flag in templates
[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
1d57a0f1 10if (@!include_once('Smarty.class.php')) {
e83d5aa0 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);
c8e034f7 185 $this->assign('with_javascript', Banana::$msgshow_javascript);
7027794f 186
b18124be 187 $this->register_function('url', array($this, 'makeUrl'));
188 $this->register_function('link', array($this, 'makeLink'));
189 $this->register_function('imglink', array($this, 'makeImgLink'));
190 $this->register_function('img', array($this, 'makeImg'));
a0b2e16c 191 $this->register_modifier('b', '_b_');
4eeddd59 192
7027794f 193 $this->assign('errors', $this->error);
194 $this->assign('page', $this->page);
195 $this->assign('pages', $this->pages);
196 $this->assign('actions', $this->actions);
4f7c063d 197 $this->register_modifier('banana_utf8entities', 'banana_utf8entities');
198 $this->register_modifier('banana_entities', 'banana_entities');
199
200 if ($ent) {
201 $this->default_modifiers = Array('@banana_entities');
202 }
7027794f 203
7027794f 204 if (!Banana::$debug_smarty) {
205 $error_level = error_reporting(0);
206 }
b18124be 207 $text = $this->fetch($tpl);
7027794f 208 if (!Banana::$debug_smarty) {
209 error_reporting($error_level);
210 }
211 return $text;
212 }
213
2f0aa8ce 214 /** Build a URL in Banana
215 * @param params ARRAY location datas
216 * @param smarty OBJECT Smarty instance associated (null if none)
217 * @return URL of the page associated with the given parameters
218 *
219 * Usual parameters are :
220 * - group : the box name
221 * - artid : the current message id (index of message-id)
222 * - part : part id to show (may be a content-id, xface or a mime-type for text)
223 * - first : first linear-index to show in spool view
224 * - action: like subscribe, cancel, new
225 * - all others params are allowed, but not parsed by the base implementation of banana
226 *
227 * smarty funciton : {url param1=... param2=...}
228 */
229 public function makeUrl(array $params, &$smarty = null)
7027794f 230 {
231 if (function_exists('hook_makeLink')
232 && $res = hook_makeLink($params)) {
233 return $res;
234 }
235 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
068c6301 236 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
7027794f 237 $file = $_SERVER['PHP_SELF'];
238
239 if (count($params) != 0) {
240 $get = '?';
241 foreach ($params as $key=>$value) {
242 if (strlen($get) != 1) {
243 $get .= '&';
244 }
245 $get .= $key . '=' . $value;
246 }
247 } else {
248 $get = '';
249 }
250 return $proto . $host . $file . $get;
251 }
252
2f0aa8ce 253 /** Build a link to a Banana page
254 * @param params ARRAY location datas
255 * @param smarty OBJECT Smarty instance associated (null if none)
256 * @return Link to the page associated with the given parameters
257 *
258 * Support all @ref makeURL parameters, but catch the following:
259 * - text : if set, defined the text of the link (if not set, the URL is used
260 * - popup : title of the link (showed as a tooltip on most browsers)
261 * - class : specific style class for the markup
262 * - accesskey: keyboard key to trigger the link
263 * None of this parameters is needed
264 *
265 * Smarty function : {link param1=... param2=...}
266 */
267 public function makeLink(array $params, &$smarty = null)
7027794f 268 {
dce7d862 269 $catch = array('text', 'popup', 'class', 'accesskey', 'style');
7027794f 270 foreach ($catch as $key) {
271 ${$key} = isset($params[$key]) ? $params[$key] : null;
272 unset($params[$key]);
273 }
274 $link = $this->makeUrl($params, &$smarty);
275 if (is_null($text)) {
276 $text = $link;
277 }
278 if (!is_null($accesskey)) {
279 $popup .= ' (raccourci : ' . $accesskey . ')';
280 }
281 if (!is_null($popup)) {
4f7c063d 282 $popup = ' title="' . banana_entities($popup) . '"';
7027794f 283 }
284 if (!is_null($class)) {
285 $class = ' class="' . $class . '"';
286 }
dce7d862 287 if (!is_null($style)) {
288 $style = ' style="' . $style . '"';
289 }
7027794f 290 if (!is_null($accesskey)) {
291 $accesskey = ' accesskey="' . $accesskey . '"';
292 }
4f7c063d 293 return '<a href="' . banana_entities($link) . '"'
dce7d862 294 . $popup . $class . $style . $accesskey
7027794f 295 . '>' . $text . '</a>';
296 }
297
2f0aa8ce 298 /** Build a link to one of the banana built-in images
299 * @param params ARRAY image datas
300 * @param smarty OBJECT Smarty instance associated (null if none)
301 * @return Img tag
302 *
303 * Supported parameters are
304 * - img : name of the image (without its extension)
305 * - alt : alternative text
306 * - height and width : dimensions of the images
307 * img and alt are needed
308 *
309 * Smarty function: {img img=... alt=... [height=...] [width=...]}
310 */
311 public function makeImg(array $params, &$smarty = null)
7027794f 312 {
313 $catch = array('img', 'alt', 'height', 'width');
314 foreach ($catch as $key) {
315 ${$key} = isset($params[$key]) ? $params[$key] : null;
316 }
317 $img .= ".gif";
318 if (function_exists('hook_makeImg')
319 && $res = hook_makeImg($img, $alt, $height, $width)) {
320 return $res;
321 }
322
323 if (!is_null($width)) {
324 $width = ' width="' . $width . '"';
325 }
326 if (!is_null($height)) {
327 $height = ' height="' . $height . '"';
328 }
329
330 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
67f43f06 331 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
7027794f 332 $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img;
333 $url = $proto . $host . $file;
334
335 return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
336 }
2f0aa8ce 337
96e1e874 338 /** Build a link to one of the banana built-in javascript
339 * @param src STRING javascript name
340 * @return Javascript tag
341 */
848a4944 342 public function makeJs($src)
96e1e874 343 {
727b37b8 344 if (!Banana::$msgshow_javascript) {
345 return '';
346 }
96e1e874 347 if (function_exists('hook_makeJs')
348 && $res = hook_makeJs($src)) {
349 return $res;
350 }
351
352 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
67f43f06 353 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
96e1e874 354 $file = dirname($_SERVER['PHP_SELF']) . '/javascript/' . $src . '.js';
355 $url = $proto . $host . $file;
356
357 return '<script type="text/javascript" src="' . $url . '"/></script>';
358 }
359
2f0aa8ce 360 /** Build a link with an image as text
361 * @param params ARRAY image and location data
362 * @param smarty OBJECT Smarty instance associated (null if none)
363 * @return an image within an link
364 *
365 * All @ref makeImg and @ref makeLink parameters are supported
366 * if text is set, the text will be appended after the image in the link
367 *
368 * Smarty function : {imglink img=... alt=... [param1=...]}
369 */
370 public function makeImgLink(array $params, &$smarty = null)
7027794f 371 {
2f0aa8ce 372 if (!isset($params['popup'])) {
e02edbfe 373 $params['popup'] = @$params['alt'];
2f0aa8ce 374 }
375 $img = $this->makeImg($params, $smarty);
376 if (isset($params['text'])) {
377 $img .= ' ' . $params['text'];
378 }
379 $params['text'] = $img;
e02edbfe 380 unset($params['alt']);
381 unset($params['img']);
382 unset($params['width']);
383 unset($params['height']);
7027794f 384 return $this->makeLink($params, $smarty);
385 }
386
387 /** Redirect to the page with the given parameter
2f0aa8ce 388 * @ref makeURL
7027794f 389 */
2f0aa8ce 390 public function redirect(array $params = array())
7027794f 391 {
392 header('Location: ' . $this->makeUrl($params));
56b35f9c 393 exit;
7027794f 394 }
395}
396
397// {{{ function banana_trimwhitespace
398
399function banana_trimwhitespace($source, &$smarty)
400{
401 $tags = array('script', 'pre', 'textarea');
402
403 foreach ($tags as $tag) {
404 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, ${$tag});
405 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
406 }
407
408 // remove all leading spaces, tabs and carriage returns NOT
409 // preceeded by a php close tag.
410 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
411
412 foreach ($tags as $tag) {
413 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
414 }
415
416 return $source;
417}
418
419// }}}
e02edbfe 420// {{{ function rss_date
421
422function rss_date($t)
423{
424 return date('r', $t);
425}
7027794f 426
e02edbfe 427// }}}
7027794f 428
598a1c53 429// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
7027794f 430?>