Do htmlentities on posts subject
[platal.git] / modules / banana / banana.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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));
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_checkcancel($_headers)
73 {
74 return ($_headers['x-org-id'] == S::v('forlife') or S::has_perms());
75 }
76
77 function hook_makeLink($params) {
78 global $globals;
79 $base = $globals->baseurl . '/banana';
80 if (isset($params['page'])) {
81 return $base . '/' . $params['page'];
82 }
83 if (@$params['action'] == 'subscribe') {
84 return $base . '/subscription';
85 }
86 if (isset($params['xface'])) {
87 return $base . '/xface/' . strtr(base64_encode($params['xface']), '+/', '.:');
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'])) {
99 if (@$params['part'] == 'xface') {
100 $base .= '/xface';
101 } elseif (@$params['action'] == 'new') {
102 $base .= '/reply';
103 } elseif (@$params['action'] == 'cancel') {
104 $base .= '/cancel';
105 } else {
106 $base .= '/read';
107 }
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 }
113 }
114
115 if (@$params['action'] == 'new') {
116 return $base . '/new';
117 }
118 return $base;
119 }
120
121 function hook_makeImg($img, $alt, $height, $width)
122 {
123 $url = 'images/banana/' . $img;
124
125 if (!is_null($width)) {
126 $width = ' width="' . $width . '"';
127 }
128 if (!is_null($height)) {
129 $height = ' height="' . $height . '"';
130 }
131
132 return '<img src="' . $url . '"' . $height . $width . ' alt="' . $alt . '" />';
133 }
134
135 class PlatalBanana extends Banana
136 {
137 function __construct($params = null)
138 {
139 global $globals;
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 }
146
147 public function run()
148 {
149 global $platal, $globals;
150
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'));
163 if (!(list($nom,$mail,$sig,$disp,$maj) = $req->fetchOneRow())) {
164 $nom = S::v('prenom')." ".S::v('nom');
165 $mail = S::v('forlife')."@polytechnique.org";
166 $sig = $nom." (".S::v('promo').")";
167 $disp = 0;
168 $maj = 1;
169 }
170 if ($maj) {
171 $time = time();
172 }
173
174 // Build user profile
175 $req = XDB::query("
176 SELECT nom
177 FROM {$globals->banana->table_prefix}abos
178 LEFT JOIN {$globals->banana->table_prefix}list ON list.fid=abos.fid
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"
189 if (!is_null($time)) {
190 XDB::execute("UPDATE auth_user_quick
191 SET banana_last = FROM_UNIXTIME({?})
192 WHERE user_id={?}",
193 $time, S::i('uid'));
194 }
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();
208 }
209
210 protected function action_saveSubs($groups)
211 {
212 global $globals;
213 $uid = S::v('uid');
214
215 Banana::$profile['subscribe'] = array();
216 XDB::execute("DELETE FROM {$globals->banana->table_prefix}abos WHERE uid={?}", $uid);
217 if (!count($groups)) {
218 return true;
219 }
220
221 $req = XDB::iterRow("SELECT fid,nom FROM {$globals->banana->table_prefix}list");
222 $fids = array();
223 while (list($fid,$fnom) = $req->next()) {
224 $fids[$fnom] = $fid;
225 }
226
227 $diff = array_diff($groups, array_keys($fids));
228 foreach ($diff as $g) {
229 XDB::execute("INSERT INTO {$globals->banana->table_prefix}list (nom) VALUES ({?})", $g);
230 $fids[$g] = XDB::insertId();
231 }
232
233 foreach ($groups as $g) {
234 XDB::execute("INSERT INTO {$globals->banana->table_prefix}abos (fid,uid) VALUES ({?},{?})",
235 $fids[$g], $uid);
236 Banana::$profile['subscribe'][] = $g;
237 }
238 }
239 }
240
241 ?>