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