Happy New Year!
[platal.git] / include / banana / hooks.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 require_once 'banana/banana.inc.php';
23
24 function hook_formatDisplayHeader($_header, $_text, $in_spool = false)
25 {
26 switch ($_header) {
27 case 'from': case 'to': case 'cc': case 'reply-to':
28 $addresses = preg_split("/ *, */", $_text);
29 $text = '';
30 foreach ($addresses as $address) {
31 $address = BananaMessage::formatFrom(trim($address), Banana::$message->getHeaderValue('subject'));
32 if ($_header == 'from') {
33 if ($id = Banana::$message->getHeaderValue('x-org-id')) {
34 return $address . ' <a href="profile/' . $id . '" class="popup2" title="' . $id . '">'
35 . '<img src="images/icons/user_suit.gif" title="fiche" alt="" /></a>';
36 } elseif ($id = Banana::$message->getHeaderValue('x-org-mail')) {
37 list($id, $domain) = explode('@', $id);
38 return $address . ' <a href="profile/' . $id . '" class="popup2" title="' . $id . '">'
39 . '<img src="images/icons/user_suit.gif" title="fiche" alt="" /></a>';
40 } else {
41 return $address;
42 }
43 }
44 if (!empty($text)) {
45 $text .= ', ';
46 }
47 $text .= $address;
48 }
49 return $text;
50
51 case 'subject':
52 $link = null;
53 $text = stripslashes($_text);
54 if (preg_match('/^(.+?)\s*\[=> (.*?)\]\s*$/u', $text, $matches)) {
55 $text = $matches[1];
56 $group = $matches[2];
57 if (Banana::$group == $group) {
58 $link = ' [=>&nbsp;' . $group . ']';
59 } else {
60 $link = ' [=>&nbsp;' . Banana::$page->makeLink(array('group' => $group, 'text' => $group)) . ']';
61 }
62 }
63 $text = banana_catchFormats(banana_htmlentities($text));
64 if ($in_spool) {
65 return array($text, $link);
66 }
67 return $text . $link;
68 }
69 return null;
70 }
71
72 function hook_platalRSS($group)
73 {
74 if ($group) {
75 $group .= '/';
76 } else {
77 $group = '';
78 }
79 return '/rss/' . $group . S::v('hruid') . '/' . S::v('core_rss_hash') . '/rss.xml';
80 }
81
82 function hook_platalMessageLink($params)
83 {
84 $base = '';
85 if (isset($params['first'])) {
86 return $base . '/from/' . $params['first'];
87 }
88 if (isset($params['artid'])) {
89 if (@$params['part'] == 'xface') {
90 $base .= '/xface';
91 } elseif (@$params['action'] == 'new') {
92 $base .= '/reply';
93 } elseif (@$params['action'] == 'cancel') {
94 $base .= '/cancel';
95 } elseif (@$params['part']) {
96 if (strpos($params['part'], '.') !== false) {
97 $params['artid'] .= '?part=' . urlencode($params['part']);
98 $base = '/read';
99 } else {
100 $base .= '/' . str_replace('/', '.', $params['part']);
101 }
102 } else {
103 $base .= '/read';
104 }
105 return $base . '/' . $params['artid'];
106 }
107
108 if (@$params['action'] == 'new') {
109 return $base . '/new';
110 }
111 return $base;
112 }
113
114 function hook_makeImg($img, $alt, $height, $width)
115 {
116 global $globals;
117 $url = $globals->baseurl . '/images/banana/' . $img;
118
119 if (!is_null($width)) {
120 $width = ' width="' . $width . '"';
121 }
122 if (!is_null($height)) {
123 $height = ' height="' . $height . '"';
124 }
125
126 return '<img src="' . $url . '"' . $height . $width . ' alt="' . $alt . '" />';
127 }
128
129 if (!function_exists('hook_makeLink')) {
130 function hook_makeLink($params)
131 {
132 global $globals, $platal;
133 $xnet = !empty($GLOBALS['IS_XNET_SITE']);
134 $feed = (@$params['action'] == 'rss' || @$params['action'] == 'rss2' || @$params['action'] == 'atom');
135 if (Banana::$protocole->name() == 'NNTP' && !$xnet) {
136 $base = $globals->baseurl . '/banana';
137 if ($feed) {
138 return $base . hook_platalRSS(@$params['group']);
139 }
140 if (isset($params['page'])) {
141 return $base . '/' . $params['page'];
142 }
143 if (@$params['action'] == 'subscribe') {
144 return $base . '/subscribe';
145 }
146
147 if (!isset($params['group'])) {
148 return $base;
149 }
150 $base .= '/' . $params['group'];
151 } else if (Banana::$protocole->name() == 'NNTP' && $xnet) {
152 if ($feed) {
153 return $globals->baseurl . '/banana' . hook_platalRSS(@$params['group']);
154 }
155 $base = $globals->baseurl . '/' . $platal->ns . 'forum';
156 } else if (Banana::$protocole->name() == 'MLArchives') {
157 if ($feed) {
158 return $globals->baseurl . '/' . $platal->ns . hook_platalRSS(MLBanana::$listname);
159 } elseif (php_sapi_name() == 'cli') {
160 $base = "http://listes.polytechnique.org/archives/" . str_replace('@', '_', $params['group']);
161 } else {
162 $base = $globals->baseurl . '/' . $platal->ns . 'lists/archives/' . MLBanana::$listname;
163 }
164 }
165 $base = $base . hook_platalMessageLink($params);
166 if (@$params['action'] == 'showext') {
167 $base .= '?action=showext';
168 }
169 return $base;
170 }
171 }
172
173 function hook_hasXFace($headers)
174 {
175 return isset($headers['x-org-id']) || isset($headers['x-org-mail']);
176 }
177
178 function hook_getXFace($headers)
179 {
180 $login = @$headers['x-org-id'];
181 if (!$login) {
182 @list($login, ) = explode('@', $headers['x-org-mail']);
183 }
184 if (!$login) {
185 return false;
186 }
187 if (isset($headers['x-face'])) {
188 $res = XDB::query("SELECT p.uid
189 FROM forums.profils AS p
190 INNER JOIN aliases AS a ON (p.uid = a.id)
191 WHERE FIND_IN_SET('xface', p.flags) AND a.alias = {?}",
192 $login);
193 if ($res->numRows()) {
194 return false;
195 }
196 }
197 global $globals;
198 http_redirect($global->baseurl . '/photo/' . $login);
199 }
200
201 function hook_makeJs($src)
202 {
203 Platal::page()->addJsLink("$src.js");
204 return ' ';
205 }
206
207 function make_Organization()
208 {
209 global $globals;
210 $perms = S::v('perms');
211 $group = $globals->asso('nom');
212 if (S::has_perms()) {
213 return "Administrateur de Polytechnique.org";
214 } else if ($group && $perms->hasFlag('groupadmin')) {
215 return "Animateur de $group";
216 } else if ($group && $perms->hasFlag('groupmember')) {
217 return "Membre de $group";
218 }
219 return "Utilisateur de Polytechnique.org";
220 }
221
222 function get_banana_params(array &$get, $group = null, $action = null, $artid = null)
223 {
224 if ($group == 'forums') {
225 $group = null;
226 } else if ($group == 'thread') {
227 $group = S::v('banana_group');
228 } else if ($group == 'message') {
229 $action = 'read';
230 $group = S::v('banana_group');
231 $artid = S::i('banana_artid');
232 } else if ($action == 'message') {
233 $action = 'read';
234 $artid = S::i('banana_artid');
235 } else if ($group == 'subscribe' || $group == 'subscription') {
236 $group = null;
237 $action = null;
238 $get['action'] = 'subscribe';
239 } else if ($group == 'profile') {
240 $group = null;
241 $action = null;
242 $get['action'] = 'profile';
243 }
244 if (!is_null($group)) {
245 $get['group'] = $group;
246 }
247 if (!is_null($action)) {
248 if ($action == 'new') {
249 $get['action'] = 'new';
250 } elseif (!is_null($artid)) {
251 $get['artid'] = $artid;
252 if ($action == 'reply') {
253 $get['action'] = 'new';
254 } elseif ($action == 'cancel') {
255 $get['action'] = $action;
256 } elseif ($action == 'from') {
257 $get['first'] = $artid;
258 unset($get['artid']);
259 } elseif ($action == 'read') {
260 $get['part'] = @$_GET['part'];
261 } elseif ($action == 'source') {
262 $get['part'] = 'source';
263 } elseif ($action == 'xface') {
264 $get['part'] = 'xface';
265 } elseif ($action) {
266 $get['part'] = str_replace('.', '/', $action);
267 }
268 if (Get::v('action') == 'showext') {
269 $get['action'] = 'showext';
270 }
271 }
272 }
273 }
274
275 class PlatalBananaPage extends BananaPage
276 {
277 protected $handler;
278 protected $base;
279
280 public function __construct()
281 {
282 global $platal;
283 Banana::$withtabs = false;
284 $this->handler = 'BananaHandler';
285 $this->base = $platal->pl_self(0);
286 parent::__construct();
287 }
288
289 protected function prepare()
290 {
291 $tpl = parent::prepare();
292 global $wiz;
293 $wiz = new PlWizard('Banana', PlPage::getCoreTpl('plwizard.tpl'), true, false);
294 foreach ($this->pages as $name=>&$mpage) {
295 $wiz->addPage($this->handler, $mpage['text'], $name);
296 }
297 $wiz->apply(Platal::page(), $this->base, $this->page);
298 return $tpl;
299 }
300 }
301
302 class BananaHandler
303 {
304 public function __construct(PlWizard &$wiz)
305 {
306 }
307
308 public function template()
309 {
310 return 'banana/index.tpl';
311 }
312
313 public function prepare(PlPage &$page, $id)
314 {
315 }
316
317 public function process()
318 {
319 return PlWizard::CURRENT_PAGE;
320 }
321 }
322
323 function run_banana(&$page, $class, array $args)
324 {
325 $banana = new $class(S::user(), $args);
326 $page->assign('banana', $banana->run());
327 $page->addCssInline($banana->css());
328 $page->addCssLink('banana.css');
329 $rss = $banana->feed();
330 if ($rss) {
331 if (Banana::$group) {
332 $page->setRssLink('Banana :: ' . Banana::$group, $rss);
333 } else {
334 $page->setRssLink('Banana :: Abonnements', $rss);
335 }
336 }
337 $bt = $banana->backtrace();
338 if ($bt) {
339 new PlBacktrace(Banana::$protocole->name(), $banana->backtrace(), 'response', 'time');
340 }
341 }
342
343 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
344 ?>