Fix notices
[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 private $error = array();
17 private $page = null;
18
19 private $pages = array();
20 private $killed = array();
21 private $actions = array();
22
23 public $css = '';
24
25 public function __construct()
26 {
27 $this->Smarty();
28
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);
35 }
36 }
37
38 /** Add an error message
39 * @param message STRING html code of the error to display
40 */
41 public function trig($message)
42 {
43 $this->error[] = $message;
44 }
45
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 */
50 public function kill($message)
51 {
52 $this->trig($message);
53 $this->assign('killed', true);
54 return $this->run();
55 }
56
57 /** Set the current page
58 * @param page STRING page name
59 */
60 public function setPage($page)
61 {
62 $this->page = $page;
63 return true;
64 }
65
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 */
71 public function registerAction($action_code, array $pages = null)
72 {
73 $this->actions[] = array('text' => $action_code, 'pages' => $pages);
74 return true;
75 }
76
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 */
83 public function registerPage($name, $text, $template = null)
84 {
85 $this->pages[$name] = array('text' => $text, 'template' => $template);
86 return true;
87 }
88
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
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
105 /** Preparte the page generation
106 * @return template to use
107 */
108 protected function prepare()
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') {
119 $this->registerPage('new', _b_('Répondre'), null);
120 }
121 } elseif ($this->page == 'new') {
122 $this->registerPage('new', _b_('Nouveau'), null);
123 }
124 }
125 foreach ($this->killed as $page) {
126 unset($this->pages[$page]);
127 }
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 }
133
134 return 'banana-base.tpl';
135 }
136
137 /** Generate XHTML code
138 */
139 public function run()
140 {
141 $tpl = $this->prepare();
142 if (!isset($this->pages[$this->page])) {
143 $this->trig(_b_('La page demandée n\'existe pas'));
144 $this->actions = array();
145 $this->page = null;
146 }
147
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);
158 $this->assign('generator', Banana::$feed_generator);
159 $this->assign('email', Banana::$feed_email);
160 $this->assign('title_prefix', Banana::$feed_namePrefix);
161 $this->assign('language', $lg);
162 $this->register_function('rss_date', 'rss_date');
163 header('Content-Type: application/rss+xml; charset=utf-8');
164 echo $this->_run($tpl, false);
165 exit;
166 }
167
168 /** Code generation
169 */
170 private function _run($tpl, $ent = true)
171 {
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);
180 $this->assign('showboxlist', Banana::$spool_boxlist);
181 $this->assign('showthread', Banana::$msgshow_withthread);
182 $this->assign('withtabs' , Banana::$withtabs);
183 $this->assign('feed_format', Banana::$feed_format);
184 $this->assign('feed_active', Banana::$feed_active);
185
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'));
190 $this->register_modifier('b', '_b_');
191
192 $this->assign('errors', $this->error);
193 $this->assign('page', $this->page);
194 $this->assign('pages', $this->pages);
195 $this->assign('actions', $this->actions);
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 }
202
203 if (!Banana::$debug_smarty) {
204 $error_level = error_reporting(0);
205 }
206 $text = $this->fetch($tpl);
207 if (!Banana::$debug_smarty) {
208 error_reporting($error_level);
209 }
210 return $text;
211 }
212
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)
229 {
230 if (function_exists('hook_makeLink')
231 && $res = hook_makeLink($params)) {
232 return $res;
233 }
234 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
235 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
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
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)
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)) {
281 $popup = ' title="' . banana_entities($popup) . '"';
282 }
283 if (!is_null($class)) {
284 $class = ' class="' . $class . '"';
285 }
286 if (!is_null($accesskey)) {
287 $accesskey = ' accesskey="' . $accesskey . '"';
288 }
289 return '<a href="' . banana_entities($link) . '"'
290 . $popup . $class . $accesskey
291 . '>' . $text . '</a>';
292 }
293
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)
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://';
327 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
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 }
333
334 /** Build a link to one of the banana built-in javascript
335 * @param src STRING javascript name
336 * @return Javascript tag
337 */
338 public function makeJs($src)
339 {
340 if (!Banana::$msgshow_javascript) {
341 return '';
342 }
343 if (function_exists('hook_makeJs')
344 && $res = hook_makeJs($src)) {
345 return $res;
346 }
347
348 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
349 $host = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
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
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)
367 {
368 if (!isset($params['popup'])) {
369 $params['popup'] = @$params['alt'];
370 }
371 $img = $this->makeImg($params, $smarty);
372 if (isset($params['text'])) {
373 $img .= ' ' . $params['text'];
374 }
375 $params['text'] = $img;
376 unset($params['alt']);
377 unset($params['img']);
378 unset($params['width']);
379 unset($params['height']);
380 return $this->makeLink($params, $smarty);
381 }
382
383 /** Redirect to the page with the given parameter
384 * @ref makeURL
385 */
386 public function redirect(array $params = array())
387 {
388 header('Location: ' . $this->makeUrl($params));
389 exit;
390 }
391 }
392
393 // {{{ function banana_trimwhitespace
394
395 function banana_trimwhitespace($source, &$smarty)
396 {
397 $tags = array('script', 'pre', 'textarea');
398
399 foreach ($tags as $tag) {
400 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, ${$tag});
401 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
402 }
403
404 // remove all leading spaces, tabs and carriage returns NOT
405 // preceeded by a php close tag.
406 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
407
408 foreach ($tags as $tag) {
409 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
410 }
411
412 return $source;
413 }
414
415 // }}}
416 // {{{ function rss_date
417
418 function rss_date($t)
419 {
420 return date('r', $t);
421 }
422
423 // }}}
424
425 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
426 ?>