Use Banana to preview the mails for ML moderation
[platal.git] / modules / banana / banana.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 Polytechnique.org *
0337d704 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
57e834ac 22require_once 'banana/banana.inc.php';
0337d704 23
5f4a0314 24function hook_formatDisplayHeader($_header, $_text, $in_spool = false)
aafe5c09 25{
5f4a0314 26 switch ($_header) {
27 case 'from': case 'to': case 'cc': case 'reply-to':
b8bf029a 28 $addresses = preg_split("/ *, */", $_text);
29 $text = '';
30 foreach ($addresses as $address) {
31 $address = BananaMessage::formatFrom(trim($address));
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 }
5f4a0314 43 }
b8bf029a 44 if (!empty($text)) {
45 $text .= ', ';
46 }
47 $text .= $address;
5f4a0314 48 }
b8bf029a 49 return $text;
5f4a0314 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 }
c473df2f 63 $text = banana_catchFormats(banana_htmlentities($text));
5f4a0314 64 if ($in_spool) {
65 return array($text, $link);
66 }
67 return $text . $link;
68 }
69 return null;
aafe5c09 70}
71
5f4a0314 72function hook_checkcancel($_headers)
38d87ed9 73{
5f4a0314 74 return ($_headers['x-org-id'] == S::v('forlife') or S::has_perms());
38d87ed9 75}
76
3d9b1d30 77function hook_makeLink($params) {
9a102e9d 78 global $globals;
79 $base = $globals->baseurl . '/banana';
5f4a0314 80 if (isset($params['page'])) {
81 return $base . '/' . $params['page'];
82 }
83 if (@$params['action'] == 'subscribe') {
f107d806 84 return $base . '/subscription';
85 }
86 if (isset($params['xface'])) {
7e6495d4 87 return $base . '/xface/' . strtr(base64_encode($params['xface']), '+/', '.:');
f107d806 88 }
89
90 if (!isset($params['group'])) {
91 return $base;
92 }
93 $base .= '/' . $params['group'];
94
95 if (isset($params['first'])) {
96 return $base . '/from/' . $params['first'];
97 }
98 if (isset($params['artid'])) {
5f4a0314 99 if (@$params['part'] == 'xface') {
100 $base .= '/xface';
101 } elseif (@$params['action'] == 'new') {
f107d806 102 $base .= '/reply';
1a013db7 103 } elseif (@$params['action'] == 'cancel') {
f107d806 104 $base .= '/cancel';
105 } else {
106 $base .= '/read';
107 }
5f4a0314 108 if (isset($params['part']) && $params['part'] != 'xface') {
109 return $base . '/' . $params['artid'] . '?part=' . urlencode($params['part']);
110 } else {
111 return $base . '/' . $params['artid'];
112 }
f107d806 113 }
57e834ac 114
1a013db7 115 if (@$params['action'] == 'new') {
f107d806 116 return $base . '/new';
117 }
118 return $base;
3d9b1d30 119}
120
cce828e5 121function hook_makeImg($img, $alt, $height, $width)
63e3b9ae 122{
7da8ef90 123 $url = 'images/banana/' . $img;
63e3b9ae 124
125 if (!is_null($width)) {
126 $width = ' width="' . $width . '"';
127 }
cce828e5 128 if (!is_null($height)) {
129 $height = ' height="' . $height . '"';
130 }
57e834ac 131
cce828e5 132 return '<img src="' . $url . '"' . $height . $width . ' alt="' . $alt . '" />';
63e3b9ae 133}
134
0337d704 135class PlatalBanana extends Banana
136{
5f4a0314 137 function __construct($params = null)
0337d704 138 {
139 global $globals;
5f4a0314 140 Banana::$msgedit_canattach = false;
141 array_push(Banana::$msgparse_headers, 'x-org-id', 'x-org-mail');
142 Banana::$nntp_host = 'news://web_'.S::v('forlife')
143 . ":{$globals->banana->password}@{$globals->banana->server}:{$globals->banana->port}/";
144 parent::__construct($params);
145 }
7da8ef90 146
5f4a0314 147 public function run()
148 {
149 global $platal, $globals;
0337d704 150
5f4a0314 151 // Update last unread time
152 $time = null;
153 if (!is_null($this->params) && isset($this->params['updateall'])) {
154 $time = intval($this->params['updateall']);
155 $_SESSION['banana_last'] = $time;
156 }
157
158 // Get user profile from SQL
159 $req = XDB::query("SELECT nom, mail, sig,
160 FIND_IN_SET('threads',flags), FIND_IN_SET('automaj',flags)
161 FROM {$globals->banana->table_prefix}profils
162 WHERE uid={?}", S::i('uid'));
0337d704 163 if (!(list($nom,$mail,$sig,$disp,$maj) = $req->fetchOneRow())) {
cab08090 164 $nom = S::v('prenom')." ".S::v('nom');
165 $mail = S::v('forlife')."@polytechnique.org";
166 $sig = $nom." (".S::v('promo').")";
0337d704 167 $disp = 0;
168 $maj = 1;
169 }
0337d704 170 if ($maj) {
5f4a0314 171 $time = time();
0337d704 172 }
173
5f4a0314 174 // Build user profile
175 $req = XDB::query("
176 SELECT nom
0337d704 177 FROM {$globals->banana->table_prefix}abos
178 LEFT JOIN {$globals->banana->table_prefix}list ON list.fid=abos.fid
5f4a0314 179 WHERE uid={?}", S::i('uid'));
180 Banana::$profile['headers']['From'] = utf8_encode("$nom <$mail>");
181 Banana::$profile['headers']['Organization'] = 'Utilisateur de Polytechnique.org';
182 Banana::$profile['signature'] = utf8_encode($sig);
183 Banana::$profile['display'] = $disp;
184 Banana::$profile['autoup'] = $maj;
185 Banana::$profile['lastnews'] = S::v('banana_last');
186 Banana::$profile['subscribe'] = $req->fetchColumn();
187
188 // Update the "unread limit"
85b9d118 189 if (!is_null($time)) {
5f4a0314 190 XDB::execute("UPDATE auth_user_quick
191 SET banana_last = FROM_UNIXTIME({?})
192 WHERE user_id={?}",
193 $time, S::i('uid'));
0337d704 194 }
5f4a0314 195
196 // Register custom Banana links and tabs
197 if (!Banana::$profile['autoup']) {
198 Banana::$page->registerAction('<a href=\'javascript:dynpostkv("'
199 . $platal->path . '", "updateall", ' . time() . ')\'>'
200 . 'Marquer tous les messages comme lus'
201 . '</a>', array('forums', 'thread', 'message'));
202 }
203 Banana::$page->registerPage('profile', utf8_encode('Préférences'), null);
204
205
206 // Run Banana
207 return parent::run();
0337d704 208 }
209
5f4a0314 210 protected function action_saveSubs($groups)
0337d704 211 {
212 global $globals;
cab08090 213 $uid = S::v('uid');
0337d704 214
5f4a0314 215 Banana::$profile['subscribe'] = array();
08cce2ff 216 XDB::execute("DELETE FROM {$globals->banana->table_prefix}abos WHERE uid={?}", $uid);
5f4a0314 217 if (!count($groups)) {
0337d704 218 return true;
219 }
57e834ac 220
08cce2ff 221 $req = XDB::iterRow("SELECT fid,nom FROM {$globals->banana->table_prefix}list");
0337d704 222 $fids = array();
223 while (list($fid,$fnom) = $req->next()) {
224 $fids[$fnom] = $fid;
225 }
226
5f4a0314 227 $diff = array_diff($groups, array_keys($fids));
0337d704 228 foreach ($diff as $g) {
08cce2ff 229 XDB::execute("INSERT INTO {$globals->banana->table_prefix}list (nom) VALUES ({?})", $g);
8b83a166 230 $fids[$g] = XDB::insertId();
0337d704 231 }
232
5f4a0314 233 foreach ($groups as $g) {
a8f6aabb 234 XDB::execute("INSERT INTO {$globals->banana->table_prefix}abos (fid,uid) VALUES ({?},{?})",
235 $fids[$g], $uid);
5f4a0314 236 Banana::$profile['subscribe'][] = $g;
0337d704 237 }
238 }
239}
240
241?>