92d45099ffec56450c3fa7dc2158fd2d164fc8bd
2 /********************************************************************************
3 * banana/page.inc.php : class for group lists
4 * ------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 require_once 'smarty/libs/Smarty.class.php';
12 class BananaPage
extends Smarty
14 private $error = array();
17 private $pages = array();
18 private $actions = array();
20 public function __construct()
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');
31 /** Add an error message
32 * @param message STRING html code of the error to display
34 public function trig($message)
36 $this->error
[] = $message;
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
43 public function kill($message)
45 $this->trig($message);
46 $this->assign('killed', true
);
50 /** Set the current page
51 * @param page STRING page name
53 public function setPage($page)
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
63 public function registerAction($action_code, array $pages = null
)
65 $this->actions
[] = array('text' => $action_code, 'pages' => $pages);
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
75 public function registerPage($name, $text, $template = null
)
77 $this->pages
[$name] = array('text' => $text, 'template' => $template);
81 /** Generate XHTML code
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
);
96 } elseif ($this->page
== 'new') {
97 $this->registerPage('new', _b_('Nouveau'), null
);
100 foreach ($this->actions
as $key=>&$action) {
101 if (!is_null($action['pages']) && !in_array($this->page
, $action['pages'])) {
102 unset($this->actions
[$key]);
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);
114 $this->assign('errors', $this->error
);
115 $this->assign('page', $this->page
);
116 $this->assign('pages', $this->pages
);
117 $this->assign('actions', $this->actions
);
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);
126 $text = $this->fetch('banana-base.tpl');
127 $text = banana_utf8entities($text);
128 if (!Banana
::$debug_smarty) {
129 error_reporting($error_level);
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
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
147 * smarty funciton : {url param1=... param2=...}
149 public function makeUrl(array $params, &$smarty = null
)
151 if (function_exists('hook_makeLink')
152 && $res = hook_makeLink($params)) {
155 $proto = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
156 $host = $_SERVER['HTTP_HOST'];
157 $file = $_SERVER['PHP_SELF'];
159 if (count($params) != 0) {
161 foreach ($params as $key=>$value) {
162 if (strlen($get) != 1) {
165 $get .= $key . '=' . $value;
170 return $proto . $host . $file . $get;
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
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
185 * Smarty function : {link param1=... param2=...}
187 public function makeLink(array $params, &$smarty = null
)
189 $catch = array('text', 'popup', 'class', 'accesskey');
190 foreach ($catch as $key) {
191 $
{$key} = isset($params[$key]) ?
$params[$key] : null
;
192 unset($params[$key]);
194 $link = $this->makeUrl($params, &$smarty);
195 if (is_null($text)) {
198 if (!is_null($accesskey)) {
199 $popup .= ' (raccourci : ' . $accesskey . ')';
201 if (!is_null($popup)) {
202 $popup = ' title="' . banana_htmlentities($popup) . '"';
204 if (!is_null($class)) {
205 $class = ' class="' . $class . '"';
207 if (!is_null($accesskey)) {
208 $accesskey = ' accesskey="' . $accesskey . '"';
210 return '<a href="' . banana_htmlentities($link) . '"'
211 . $popup . $class . $accesskey
212 . '>' . $text . '</a>';
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)
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
226 * Smarty function: {img img=... alt=... [height=...] [width=...]}
228 public function makeImg(array $params, &$smarty = null
)
230 $catch = array('img', 'alt', 'height', 'width');
231 foreach ($catch as $key) {
232 $
{$key} = isset($params[$key]) ?
$params[$key] : null
;
235 if (function_exists('hook_makeImg')
236 && $res = hook_makeImg($img, $alt, $height, $width)) {
240 if (!is_null($width)) {
241 $width = ' width="' . $width . '"';
243 if (!is_null($height)) {
244 $height = ' height="' . $height . '"';
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;
252 return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
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
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
263 * Smarty function : {imglink img=... alt=... [param1=...]}
265 public function makeImgLink(array $params, &$smarty = null
)
267 $params['alt'] = _b_($params['alt']);
268 if (!isset($params['popup'])) {
269 $params['popup'] = $params['alt'];
271 $img = $this->makeImg($params, $smarty);
272 if (isset($params['text'])) {
273 $img .= ' ' . $params['text'];
275 $params['text'] = $img;
276 return $this->makeLink($params, $smarty);
279 /** Redirect to the page with the given parameter
282 public function redirect(array $params = array())
284 header('Location: ' . $this->makeUrl($params));
288 // {{{ function banana_trimwhitespace
290 function banana_trimwhitespace($source, &$smarty)
292 $tags = array('script', 'pre', 'textarea');
294 foreach ($tags as $tag) {
295 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, $
{$tag});
296 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
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);
303 foreach ($tags as $tag) {
304 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
313 // vim:set et sw=4 sts=4 ts=4: