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