Rework all sources :
[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 public function trig($message)
32 {
33 $this->error[] = $message;
34 }
35
36 public function kill($message)
37 {
38 $this->trig($message);
39 return $this->run();
40 }
41
42 public function setPage($page)
43 {
44 $this->page = $page;
45 }
46
47 public function registerAction($action_code, array $pages = null)
48 {
49 $this->actions[] = array('text' => $action_code, 'pages' => $pages);
50 return true;
51 }
52
53 public function registerPage($name, $text, $template = null)
54 {
55 $this->pages[$name] = array('text' => $text, 'template' => $template);
56 return true;
57 }
58
59 public function run()
60 {
61 $this->registerPage('subscribe', _b_('Abonnements'), null);
62 $this->registerPage('forums', _b_('Les forums'), null);
63 if (!is_null(Banana::$group)) {
64 $this->registerPage('thread', Banana::$group, null);
65 if (!is_null(Banana::$artid)) {
66 $this->registerPage('message', _b_('Message'), null);
67 if ($this->page == 'cancel') {
68 $this->registerPage('cancel', _b_('Annulation'), null);
69 } elseif ($this->page == 'new') {
70 $this->registerPage('new', _b_('RĂ©pondre'), null);
71 }
72 } elseif ($this->page == 'new') {
73 $this->registerPage('new', _b_('Nouveau'), null);
74 }
75 }
76 foreach ($this->actions as $key=>&$action) {
77 if (!is_null($action['pages']) && !in_array($this->page, $action['pages'])) {
78 unset($this->actions[$key]);
79 }
80 }
81 $this->assign('group', Banana::$group);
82 $this->assign('artid', Banana::$artid);
83 $this->assign('part', Banana::$part);
84 $this->assign('first', Banana::$first);
85 $this->assign('action', Banana::$action);
86 $this->assign('profile', Banana::$profile);
87 $this->assign('spool', Banana::$spool);
88 $this->assign('protocole', Banana::$protocole);
89
90 $this->assign('errors', $this->error);
91 $this->assign('page', $this->page);
92 $this->assign('pages', $this->pages);
93 $this->assign('actions', $this->actions);
94
95 $this->register_function('url', array($this, 'makeUrl'));
96 $this->register_function('link', array($this, 'makeLink'));
97 $this->register_function('imglink', array($this, 'makeImgLink'));
98 $this->register_function('img', array($this, 'makeImg'));
99 if (!Banana::$debug_smarty) {
100 $error_level = error_reporting(0);
101 }
102 $text = $this->fetch('banana-base.tpl');
103 $text = banana_utf8entities($text);
104 if (!Banana::$debug_smarty) {
105 error_reporting($error_level);
106 }
107 return $text;
108 }
109
110 public function makeUrl($params, &$smarty = null)
111 {
112 if (function_exists('hook_makeLink')
113 && $res = hook_makeLink($params)) {
114 return $res;
115 }
116 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
117 $host = $_SERVER['HTTP_HOST'];
118 $file = $_SERVER['PHP_SELF'];
119
120 if (count($params) != 0) {
121 $get = '?';
122 foreach ($params as $key=>$value) {
123 if (strlen($get) != 1) {
124 $get .= '&';
125 }
126 $get .= $key . '=' . $value;
127 }
128 } else {
129 $get = '';
130 }
131 return $proto . $host . $file . $get;
132 }
133
134 public function makeLink($params, &$smarty = null)
135 {
136 $catch = array('text', 'popup', 'class', 'accesskey');
137 foreach ($catch as $key) {
138 ${$key} = isset($params[$key]) ? $params[$key] : null;
139 unset($params[$key]);
140 }
141 $link = $this->makeUrl($params, &$smarty);
142 if (is_null($text)) {
143 $text = $link;
144 }
145 if (!is_null($accesskey)) {
146 $popup .= ' (raccourci : ' . $accesskey . ')';
147 }
148 if (!is_null($popup)) {
149 $popup = ' title="' . $popup . '"';
150 }
151 if (!is_null($class)) {
152 $class = ' class="' . $class . '"';
153 }
154 $target = null;
155 if (isset($params['action']) && $params['action'] == 'view') {
156 $target = ' target="_blank"';
157 }
158 if (!is_null($accesskey)) {
159 $accesskey = ' accesskey="' . $accesskey . '"';
160 }
161 return '<a href="' . htmlentities($link) . '"'
162 . $target . $popup . $class . $accesskey
163 . '>' . $text . '</a>';
164 }
165
166 public function makeImg($params, &$smarty = null)
167 {
168 $catch = array('img', 'alt', 'height', 'width');
169 foreach ($catch as $key) {
170 ${$key} = isset($params[$key]) ? $params[$key] : null;
171 }
172 $img .= ".gif";
173 if (function_exists('hook_makeImg')
174 && $res = hook_makeImg($img, $alt, $height, $width)) {
175 return $res;
176 }
177
178 if (!is_null($width)) {
179 $width = ' width="' . $width . '"';
180 }
181 if (!is_null($height)) {
182 $height = ' height="' . $height . '"';
183 }
184
185 $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
186 $host = $_SERVER['HTTP_HOST'];
187 $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img;
188 $url = $proto . $host . $file;
189
190 return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
191 }
192
193 public function makeImgLink($params, &$smarty = null)
194 {
195 $params['alt'] = _b_($params['alt']);
196 $params['popup'] = $params['alt'];
197 $params['text'] = $this->makeImg($params, $smarty);
198 return $this->makeLink($params, $smarty);
199 }
200
201 /** Redirect to the page with the given parameter
202 * @ref makeLink
203 */
204 public function redirect($params = array())
205 {
206 header('Location: ' . $this->makeUrl($params));
207 }
208 }
209
210 // {{{ function banana_trimwhitespace
211
212 function banana_trimwhitespace($source, &$smarty)
213 {
214 $tags = array('script', 'pre', 'textarea');
215
216 foreach ($tags as $tag) {
217 preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, ${$tag});
218 $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
219 }
220
221 // remove all leading spaces, tabs and carriage returns NOT
222 // preceeded by a php close tag.
223 $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
224
225 foreach ($tags as $tag) {
226 $source = preg_replace("!&&&{$tag}&&&!e", 'array_shift(${$tag}[0])', $source);
227 }
228
229 return $source;
230 }
231
232 // }}}
233
234
235 // vim:set et sw=4 sts=4 ts=4:
236 ?>