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