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