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