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