Update example files
[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 require_once 'smarty/libs/Smarty.class.php';
11
12 class BananaPage extends Smarty
13 {
14 private $error = array();
15 private $page = null;
16
17 private $pages = array();
18 private $killed = array();
19 private $actions = array();
20
21 public $css = '';
22
23 public function __construct()
24 {
25 $this->Smarty();
26
27 $this->compile_check = Banana::$debug_smarty;
28 $this->template_dir = dirname(__FILE__) . '/templates/';
29 $this->compile_dir = Banana::$spool_root . '/templates_c/';
30 $this->register_prefilter('banana_trimwhitespace');
31
32 }
33
34 /** Add an error message
35 * @param message STRING html code of the error to display
36 */
37 public function trig($message)
38 {
39 $this->error[] = $message;
40 }
41
42 /** Kill the current page (generate an error message and skip page generation)
43 * @param message STRING html code of the error message to display
44 @ @return XHTML code of the page
45 */
46 public function kill($message)
47 {
48 $this->trig($message);
49 $this->assign('killed', true);
50 return $this->run();
51 }
52
53 /** Set the current page
54 * @param page STRING page name
55 */
56 public function setPage($page)
57 {
58 $this->page = $page;
59 return true;
60 }
61
62 /** Register an action to show on banana page
63 * @param action_code HTML code of the action
64 * @param pages ARRAY pages where to show the action (null == every pages)
65 * @return true if success
66 */
67 public function registerAction($action_code, array $pages = null)
68 {
69 $this->actions[] = array('text' => $action_code, 'pages' => $pages);
70 return true;
71 }
72
73 /** Register a new page
74 * @param name Name of the page
75 * @param text Text for the tab of the page
76 * @param template Template path for the page if null, the page is not handled by banana
77 * @return true if success
78 */
79 public function registerPage($name, $text, $template = null)
80 {
81 $this->pages[$name] = array('text' => $text, 'template' => $template);
82 return true;
83 }
84
85 /** Remove a page
86 * @param page STRING page name to kill
87 */
88 public function killPage($page)
89 {
90 $this->killed[] = $page;
91 }
92
93 /** Add Inline CSS to put in the page headers
94 * @param css CSS code
95 */
96 public function addCssInline($css)
97 {
98 $this->css .= $css;
99 }
100
101 /** Preparte the page generation
102 * @return template to use
103 */
104 protected function prepare()
105 {
106 $this->registerPage('subscribe', _b_('Abonnements'), null);
107 $this->registerPage('forums', _b_('Les forums'), null);
108 if (!is_null(Banana::$group)) {
109 $this->registerPage('thread', Banana::$group, null);
110 if (!is_null(Banana::$artid)) {
111 $this->registerPage('message', _b_('Message'), null);
112 if ($this->page == 'cancel') {
113 $this->registerPage('cancel', _b_('Annulation'), null);
114 } elseif ($this->page == 'new') {
115 $this->registerPage('new', _b_('Répondre'), null);
116 }
117 } elseif ($this->page == 'new') {
118 $this->registerPage('new', _b_('Nouveau'), null);
119 }
120 }
121 foreach ($this->killed as $page) {
122 unset($this->pages[$page]);
123 }
124 foreach ($this->actions as $key=>&$action) {
125 if (!is_null($action['pages']) && !in_array($this->page, $action['pages'])) {
126 unset($this->actions[$key]);
127 }
128 }
129
130 return 'banana-base.tpl';
131 }
132
133 /** Generate XHTML code
134 */
135 public function run()
136 {
137 $tpl = $this->prepare();
138 if (!isset($this->pages[$this->page])) {
139 $this->trig(_b_('La page demandée n\'existe pas'));
140 $this->actions = array();
141 $this->page = null;
142 }
143
144 return $this->_run($tpl);
145 }
146
147 /** Generate feed XML code
148 */
149 public function feed()
150 {
151 @list($lg) = explode('_', Banana::$profile['locale']);
152 $tpl = 'banana-feed-' . Banana::$feed_format . '.tpl';
153 $this->assign('copyright', Banana::$feed_copyright);
154 $this->assign('title_prefix', Banana::$feed_namePrefix);
155 $this->assign('language', $lg);
156 $this->register_function('rss_date', 'rss_date');
157 header('Content-Type: application/rss+xml; charset=utf-8');
158 echo $this->_run($tpl);
159 exit;
160 }
161
162 /** Code generation
163 */
164 private function _run($tpl)
165 {
166 $this->assign('group', Banana::$group);
167 $this->assign('artid', Banana::$artid);
168 $this->assign('part', Banana::$part);
169 $this->assign('first', Banana::$first);
170 $this->assign('action', Banana::$action);
171 $this->assign('profile', Banana::$profile);
172 $this->assign('spool', Banana::$spool);
173 $this->assign('protocole', Banana::$protocole);
174 $this->assign('showboxlist', Banana::$spool_boxlist);
175 $this->assign('showthread', Banana::$msgshow_withthread);
176 $this->assign('withtabs' , Banana::$withtabs);
177 $this->assign('feed_format', Banana::$feed_format);
178 $this->assign('feed_active', Banana::$feed_active);
179
180 $this->register_function('url', array($this, 'makeUrl'));
181 $this->register_function('link', array($this, 'makeLink'));
182 $this->register_function('imglink', array($this, 'makeImgLink'));
183 $this->register_function('img', array($this, 'makeImg'));
184 $this->register_modifier('b', '_b_');
185
186 $this->assign('errors', $this->error);
187 $this->assign('page', $this->page);
188 $this->assign('pages', $this->pages);
189 $this->assign('actions', $this->actions);
190
191 if (!Banana::$debug_smarty) {
192 $error_level = error_reporting(0);
193 }
194 $text = $this->fetch($tpl);
195 $text = banana_utf8entities($text);
196 if (!Banana::$debug_smarty) {
197 error_reporting($error_level);
198 }
199 return $text;
200 }
201
202 /** Build a URL in Banana
203 * @param params ARRAY location datas
204 * @param smarty OBJECT Smarty instance associated (null if none)
205 * @return URL of the page associated with the given parameters
206 *
207 * Usual parameters are :
208 * - group : the box name
209 * - artid : the current message id (index of message-id)
210 * - part : part id to show (may be a content-id, xface or a mime-type for text)
211 * - first : first linear-index to show in spool view
212 * - action: like subscribe, cancel, new
213 * - all others params are allowed, but not parsed by the base implementation of banana
214 *
215 * smarty funciton : {url param1=... param2=...}
216 */
217 public function makeUrl(array $params, &$smarty = null)
218 {
219 if (function_exists('hook_makeLink')
220 && $res = hook_makeLink($params)) {
221 return $res;
222 }
223 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
224 $host = $_SERVER['HTTP_HOST'];
225 $file = $_SERVER['PHP_SELF'];
226
227 if (count($params) != 0) {
228 $get = '?';
229 foreach ($params as $key=>$value) {
230 if (strlen($get) != 1) {
231 $get .= '&';
232 }
233 $get .= $key . '=' . $value;
234 }
235 } else {
236 $get = '';
237 }
238 return $proto . $host . $file . $get;
239 }
240
241 /** Build a link to a Banana page
242 * @param params ARRAY location datas
243 * @param smarty OBJECT Smarty instance associated (null if none)
244 * @return Link to the page associated with the given parameters
245 *
246 * Support all @ref makeURL parameters, but catch the following:
247 * - text : if set, defined the text of the link (if not set, the URL is used
248 * - popup : title of the link (showed as a tooltip on most browsers)
249 * - class : specific style class for the markup
250 * - accesskey: keyboard key to trigger the link
251 * None of this parameters is needed
252 *
253 * Smarty function : {link param1=... param2=...}
254 */
255 public function makeLink(array $params, &$smarty = null)
256 {
257 $catch = array('text', 'popup', 'class', 'accesskey');
258 foreach ($catch as $key) {
259 ${$key} = isset($params[$key]) ? $params[$key] : null;
260 unset($params[$key]);
261 }
262 $link = $this->makeUrl($params, &$smarty);
263 if (is_null($text)) {
264 $text = $link;
265 }
266 if (!is_null($accesskey)) {
267 $popup .= ' (raccourci : ' . $accesskey . ')';
268 }
269 if (!is_null($popup)) {
270 $popup = ' title="' . banana_htmlentities($popup) . '"';
271 }
272 if (!is_null($class)) {
273 $class = ' class="' . $class . '"';
274 }
275 if (!is_null($accesskey)) {
276 $accesskey = ' accesskey="' . $accesskey . '"';
277 }
278 return '<a href="' . banana_htmlentities($link) . '"'
279 . $popup . $class . $accesskey
280 . '>' . $text . '</a>';
281 }
282
283 /** Build a link to one of the banana built-in images
284 * @param params ARRAY image datas
285 * @param smarty OBJECT Smarty instance associated (null if none)
286 * @return Img tag
287 *
288 * Supported parameters are
289 * - img : name of the image (without its extension)
290 * - alt : alternative text
291 * - height and width : dimensions of the images
292 * img and alt are needed
293 *
294 * Smarty function: {img img=... alt=... [height=...] [width=...]}
295 */
296 public function makeImg(array $params, &$smarty = null)
297 {
298 $catch = array('img', 'alt', 'height', 'width');
299 foreach ($catch as $key) {
300 ${$key} = isset($params[$key]) ? $params[$key] : null;
301 }
302 $img .= ".gif";
303 if (function_exists('hook_makeImg')
304 && $res = hook_makeImg($img, $alt, $height, $width)) {
305 return $res;
306 }
307
308 if (!is_null($width)) {
309 $width = ' width="' . $width . '"';
310 }
311 if (!is_null($height)) {
312 $height = ' height="' . $height . '"';
313 }
314
315 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
316 $host = $_SERVER['HTTP_HOST'];
317 $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img;
318 $url = $proto . $host . $file;
319
320 return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
321 }
322
323 /** Build a link with an image as text
324 * @param params ARRAY image and location data
325 * @param smarty OBJECT Smarty instance associated (null if none)
326 * @return an image within an link
327 *
328 * All @ref makeImg and @ref makeLink parameters are supported
329 * if text is set, the text will be appended after the image in the link
330 *
331 * Smarty function : {imglink img=... alt=... [param1=...]}
332 */
333 public function makeImgLink(array $params, &$smarty = null)
334 {
335 if (!isset($params['popup'])) {
336 $params['popup'] = @$params['alt'];
337 }
338 $img = $this->makeImg($params, $smarty);
339 if (isset($params['text'])) {
340 $img .= ' ' . $params['text'];
341 }
342 $params['text'] = $img;
343 unset($params['alt']);
344 unset($params['img']);
345 unset($params['width']);
346 unset($params['height']);
347 return $this->makeLink($params, $smarty);
348 }
349
350 /** Redirect to the page with the given parameter
351 * @ref makeURL
352 */
353 public function redirect(array $params = array())
354 {
355 header('Location: ' . $this->makeUrl($params));
356 }
357 }
358
359 // {{{ function banana_trimwhitespace
360
361 function banana_trimwhitespace($source, &$smarty)
362 {
363 $tags = array('script', 'pre', 'textarea');
364
365 foreach ($tags as $tag) {
366 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, ${$tag});
367 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
368 }
369
370 // remove all leading spaces, tabs and carriage returns NOT
371 // preceeded by a php close tag.
372 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
373
374 foreach ($tags as $tag) {
375 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
376 }
377
378 return $source;
379 }
380
381 // }}}
382 // {{{ function rss_date
383
384 function rss_date($t)
385 {
386 return date('r', $t);
387 }
388
389 // }}}
390
391 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
392 ?>