Commit | Line | Data |
---|---|---|
7b9d64a8 | 1 | <?php |
2 | /*************************************************************************** | |
9f5bd98e | 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
7b9d64a8 | 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 | ||
adb07f6f | 22 | Platal::load('lists'); |
7b9d64a8 | 23 | |
24 | class XnetListsModule extends ListsModule | |
25 | { | |
26 | var $client; | |
27 | ||
28 | function handlers() | |
29 | { | |
30 | return array( | |
020c8ed0 SJ |
31 | '%grp/lists' => $this->make_hook('lists', AUTH_MDP, 'groupmember'), |
32 | '%grp/lists/create' => $this->make_hook('create', AUTH_MDP, 'groupmember'), | |
7b9d64a8 | 33 | |
020c8ed0 SJ |
34 | '%grp/lists/members' => $this->make_hook('members', AUTH_COOKIE), |
35 | '%grp/lists/csv' => $this->make_hook('csv', AUTH_COOKIE), | |
36 | '%grp/lists/annu' => $this->make_hook('annu', AUTH_COOKIE), | |
37 | '%grp/lists/archives' => $this->make_hook('archives', AUTH_COOKIE), | |
38 | '%grp/lists/archives/rss' => $this->make_hook('rss', AUTH_PUBLIC), | |
7b9d64a8 | 39 | |
020c8ed0 SJ |
40 | '%grp/lists/moderate' => $this->make_hook('moderate', AUTH_MDP), |
41 | '%grp/lists/admin' => $this->make_hook('admin', AUTH_MDP), | |
42 | '%grp/lists/options' => $this->make_hook('options', AUTH_MDP), | |
43 | '%grp/lists/delete' => $this->make_hook('delete', AUTH_MDP), | |
7b9d64a8 | 44 | |
020c8ed0 SJ |
45 | '%grp/lists/soptions' => $this->make_hook('soptions', AUTH_MDP), |
46 | '%grp/lists/check' => $this->make_hook('check', AUTH_MDP), | |
47 | '%grp/lists/sync' => $this->make_hook('sync', AUTH_MDP), | |
7b9d64a8 | 48 | |
020c8ed0 SJ |
49 | '%grp/alias/admin' => $this->make_hook('aadmin', AUTH_MDP, 'groupadmin'), |
50 | '%grp/alias/create' => $this->make_hook('acreate', AUTH_MDP, 'groupadmin'), | |
d1fcf09c | 51 | |
7b9d64a8 | 52 | /* hack: lists uses that */ |
020c8ed0 | 53 | 'profile' => $this->make_hook('profile', AUTH_PUBLIC), |
7b9d64a8 | 54 | ); |
55 | } | |
56 | ||
7f1ff426 | 57 | function prepare_client(&$page, $user = null) |
7b9d64a8 | 58 | { |
59 | global $globals; | |
adb07f6f | 60 | Platal::load('lists', 'lists.inc.php'); |
7b9d64a8 | 61 | |
7f1ff426 FB |
62 | if (is_null($user)) { |
63 | $user =& S::user(); | |
64 | } | |
65 | $this->client = new MMList($user, $globals->asso('mail_domain')); | |
7b9d64a8 | 66 | |
7b9d64a8 | 67 | $page->assign('asso', $globals->asso()); |
68 | $page->setType($globals->asso('cat')); | |
092945b4 | 69 | |
70 | return $globals->asso('mail_domain'); | |
7b9d64a8 | 71 | } |
72 | ||
9fdacf8d | 73 | function handler_lists(&$page) |
74 | { | |
75 | global $globals; | |
76 | ||
c77e45f1 | 77 | if (!$globals->asso('mail_domain')) { |
78 | return PL_NOT_FOUND; | |
79 | } | |
9fdacf8d | 80 | $this->prepare_client($page); |
1490093c | 81 | $page->changeTpl('xnetlists/index.tpl'); |
9fdacf8d | 82 | |
83 | if (Get::has('del')) { | |
8d73663d | 84 | S::assert_xsrf_token(); |
5e2307dc | 85 | $this->client->unsubscribe(Get::v('del')); |
8b00e0e0 | 86 | pl_redirect('lists'); |
9fdacf8d | 87 | } |
88 | if (Get::has('add')) { | |
8d73663d | 89 | S::assert_xsrf_token(); |
5e2307dc | 90 | $this->client->subscribe(Get::v('add')); |
8b00e0e0 | 91 | pl_redirect('lists'); |
9fdacf8d | 92 | } |
93 | ||
94 | if (Post::has('del_alias') && may_update()) { | |
8d73663d VZ |
95 | S::assert_xsrf_token(); |
96 | ||
5e2307dc | 97 | $alias = Post::v('del_alias'); |
9fdacf8d | 98 | // prevent group admin from erasing aliases from other groups |
99 | $alias = substr($alias, 0, strpos($alias, '@')).'@'.$globals->asso('mail_domain'); | |
08cce2ff | 100 | XDB::query( |
c55cca6e | 101 | 'DELETE FROM r, v |
00112b2e VZ |
102 | USING virtual AS v |
103 | LEFT JOIN virtual_redirect AS r USING(vid) | |
9fdacf8d | 104 | WHERE v.alias={?}', $alias); |
6bb2f79a | 105 | $page->trigSuccess(Post::v('del_alias')." supprimé !"); |
9fdacf8d | 106 | } |
107 | ||
108 | $listes = $this->client->get_lists(); | |
41c0150b | 109 | $page->assign('listes', $listes); |
9fdacf8d | 110 | |
08cce2ff | 111 | $alias = XDB::iterator( |
9fdacf8d | 112 | 'SELECT alias,type |
00112b2e | 113 | FROM virtual |
9fdacf8d | 114 | WHERE alias |
115 | LIKE {?} AND type="user" | |
116 | ORDER BY alias', '%@'.$globals->asso('mail_domain')); | |
117 | $page->assign('alias', $alias); | |
118 | ||
119 | $page->assign('may_update', may_update()); | |
41c0150b SJ |
120 | |
121 | if (count($listes) > 0 && !$globals->asso('has_ml')) { | |
eb41eda9 | 122 | XDB::execute("UPDATE groups |
41c0150b SJ |
123 | SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml') |
124 | WHERE id = {?}", | |
125 | $globals->asso('id')); | |
126 | } | |
9fdacf8d | 127 | } |
128 | ||
80f44cfe | 129 | function handler_create(&$page) |
130 | { | |
131 | global $globals; | |
132 | ||
c77e45f1 | 133 | if (!$globals->asso('mail_domain')) { |
134 | return PL_NOT_FOUND; | |
135 | } | |
80f44cfe | 136 | $this->prepare_client($page); |
3fe218e4 | 137 | $page->changeTpl('xnetlists/create.tpl'); |
80f44cfe | 138 | |
139 | if (!Post::has('submit')) { | |
140 | return; | |
8d73663d VZ |
141 | } else { |
142 | S::assert_xsrf_token(); | |
80f44cfe | 143 | } |
144 | ||
304d3602 | 145 | if (!Post::has('liste') || !Post::v('liste')) { |
6bb2f79a | 146 | $page->trigError('Le champs « adresse souhaitée » est vide.'); |
c9110c6c | 147 | return; |
80f44cfe | 148 | } |
149 | ||
88f7a3f1 | 150 | $liste = strtolower(Post::v('liste')); |
80f44cfe | 151 | |
152 | if (!preg_match("/^[a-zA-Z0-9\-]*$/", $liste)) { | |
a7d35093 | 153 | $page->trigError('le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets'); |
c9110c6c | 154 | return; |
80f44cfe | 155 | } |
156 | ||
157 | $new = $liste.'@'.$globals->asso('mail_domain'); | |
00112b2e | 158 | $res = XDB::query('SELECT alias FROM virtual WHERE alias={?}', $new); |
80f44cfe | 159 | |
92144f3e | 160 | if ($res->numRows()) { |
a7d35093 | 161 | $page->trigError('cet alias est déjà pris'); |
c9110c6c | 162 | return; |
80f44cfe | 163 | } |
5e2307dc | 164 | if (!Post::v('desc')) { |
a7d35093 | 165 | $page->trigError('le sujet est vide'); |
c9110c6c | 166 | return; |
80f44cfe | 167 | } |
168 | ||
80f44cfe | 169 | $ret = $this->client->create_list( |
92144f3e | 170 | $liste, utf8_decode(Post::v('desc')), Post::v('advertise'), |
5e2307dc | 171 | Post::v('modlevel'), Post::v('inslevel'), |
2c141369 | 172 | array(S::user()->forlifeEmail()), array(S::user()->forlifeEmail())); |
80f44cfe | 173 | |
174 | $dom = strtolower($globals->asso("mail_domain")); | |
175 | $red = $dom.'_'.$liste; | |
176 | ||
177 | if (!$ret) { | |
a7de4ef7 | 178 | $page->kill("Un problème est survenu, contacter " |
80f44cfe | 179 | ."<a href='mailto:support@m4x.org'>support@m4x.org</a>"); |
180 | return; | |
181 | } | |
7d427b10 FB |
182 | foreach (array('', 'owner', 'admin', 'bounces', 'unsubscribe') as $app) { |
183 | $mdir = $app == '' ? '+post' : '+' . $app; | |
18a039bd FB |
184 | if (!empty($app)) { |
185 | $app = '-' . $app; | |
186 | } | |
00112b2e | 187 | XDB::execute('INSERT INTO virtual (alias,type) |
7d427b10 | 188 | VALUES({?},{?})', $liste. $app . '@'.$dom, 'list'); |
00112b2e | 189 | XDB::execute('INSERT INTO virtual_redirect (vid,redirect) |
7d427b10 FB |
190 | VALUES ({?}, {?})', XDB::insertId(), |
191 | $red . $mdir . '@listes.polytechnique.org'); | |
192 | } | |
41c0150b | 193 | |
eb41eda9 | 194 | XDB::execute("UPDATE groups |
41c0150b SJ |
195 | SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml') |
196 | WHERE id = {?}", | |
197 | $globals->asso('id')); | |
198 | ||
8b00e0e0 | 199 | pl_redirect('lists/admin/'.$liste); |
80f44cfe | 200 | } |
201 | ||
9fdacf8d | 202 | function handler_sync(&$page, $liste = null) |
203 | { | |
204 | global $globals; | |
205 | ||
c77e45f1 | 206 | if (!$globals->asso('mail_domain')) { |
207 | return PL_NOT_FOUND; | |
208 | } | |
9fdacf8d | 209 | $this->prepare_client($page); |
3fe218e4 | 210 | $page->changeTpl('xnetlists/sync.tpl'); |
9fdacf8d | 211 | |
212 | if (Env::has('add')) { | |
8d73663d | 213 | S::assert_xsrf_token(); |
5e2307dc | 214 | $this->client->mass_subscribe($liste, array_keys(Env::v('add'))); |
9fdacf8d | 215 | } |
216 | ||
217 | list(,$members) = $this->client->get_members($liste); | |
218 | $mails = array_map(create_function('$arr', 'return $arr[1];'), $members); | |
cf5e8ef1 | 219 | $subscribers = array_unique($mails); |
9fdacf8d | 220 | |
221 | $not_in_group_x = array(); | |
222 | $not_in_group_ext = array(); | |
223 | ||
832e6fcb | 224 | $ann = XDB::fetchColumn('SELECT uid |
eb41eda9 | 225 | FROM group_members |
832e6fcb | 226 | WHERE asso_id = {?}', $globals->asso('id')); |
ff82994d | 227 | $users = User::getBulkUsersWithUIDs($ann); |
9fdacf8d | 228 | |
229 | $not_in_list = array(); | |
832e6fcb | 230 | foreach ($users as $user) { |
cb8a8977 FB |
231 | if (!in_array(strtolower($user->forlifeEmail()), $subscribers)) { |
232 | $not_in_list[] = $user; | |
9fdacf8d | 233 | } |
234 | } | |
235 | ||
236 | $page->assign('not_in_list', $not_in_list); | |
237 | } | |
238 | ||
d1fcf09c | 239 | function handler_aadmin(&$page, $lfull = null) |
240 | { | |
c77e45f1 | 241 | global $globals; |
242 | ||
243 | if (!$globals->asso('mail_domain') || is_null($lfull)) { | |
d1fcf09c | 244 | return PL_NOT_FOUND; |
245 | } | |
1490093c | 246 | $page->changeTpl('xnetlists/alias-admin.tpl'); |
d1fcf09c | 247 | |
248 | if (Env::has('add_member')) { | |
8d73663d VZ |
249 | S::assert_xsrf_token(); |
250 | ||
fd4ab3c7 FB |
251 | $add = Env::t('add_member'); |
252 | $user = User::getSilent($add); | |
253 | if ($user) { | |
254 | $add = $user->forlifeEmail(); | |
255 | } else if (!User::isForeignEmailAddress($add)) { | |
256 | $add = null; | |
d1fcf09c | 257 | } |
fd4ab3c7 | 258 | if (!empty($add)) { |
5660032a SJ |
259 | XDB::execute('INSERT INTO virtual_redirect (vid, redirect) |
260 | SELECT vid, {?} | |
261 | FROM virtual | |
fd4ab3c7 FB |
262 | WHERE alias = {?}', strtolower($add), $lfull); |
263 | $page->trigSuccess($add . ' ajouté.'); | |
d1fcf09c | 264 | } else { |
e46cf8c4 | 265 | $page->trigError($add . " n'existe pas."); |
d1fcf09c | 266 | } |
267 | } | |
268 | ||
269 | if (Env::has('del_member')) { | |
8d73663d | 270 | S::assert_xsrf_token(); |
08cce2ff | 271 | XDB::query( |
00112b2e VZ |
272 | "DELETE FROM virtual_redirect |
273 | USING virtual_redirect | |
274 | INNER JOIN virtual USING(vid) | |
5e2307dc | 275 | WHERE redirect={?} AND alias={?}", Env::v('del_member'), $lfull); |
8b00e0e0 | 276 | pl_redirect('alias/admin/'.$lfull); |
d1fcf09c | 277 | } |
278 | ||
7cdecf88 | 279 | global $globals; |
cb8a8977 FB |
280 | $emails = XDB::fetchColumn('SELECT redirect |
281 | FROM virtual_redirect AS vr | |
282 | INNER JOIN virtual AS v USING(vid) | |
283 | WHERE v.alias = {?} | |
284 | ORDER BY redirect', $lfull); | |
285 | $mem = array(); | |
286 | foreach ($emails as $email) { | |
287 | $user = User::getSilent($email); | |
288 | if ($user) { | |
289 | $mem[] = array('user' => $user, 'email' => $email); | |
290 | } else { | |
291 | $mem[] = array('email' => $email); | |
292 | } | |
293 | } | |
294 | $page->assign('mem', $mem); | |
d1fcf09c | 295 | } |
296 | ||
297 | function handler_acreate(&$page) | |
298 | { | |
299 | global $globals; | |
300 | ||
c77e45f1 | 301 | if (!$globals->asso('mail_domain')) { |
302 | return PL_NOT_FOUND; | |
303 | } | |
1490093c | 304 | $page->changeTpl('xnetlists/alias-create.tpl'); |
d1fcf09c | 305 | |
306 | if (!Post::has('submit')) { | |
307 | return; | |
8d73663d VZ |
308 | } else { |
309 | S::assert_xsrf_token(); | |
d1fcf09c | 310 | } |
311 | ||
312 | if (!Post::has('liste')) { | |
6bb2f79a | 313 | $page->trigError('Le champs « adresse souhaitée » est vide.'); |
d1fcf09c | 314 | return; |
315 | } | |
5e2307dc | 316 | $liste = Post::v('liste'); |
d1fcf09c | 317 | if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $liste)) { |
a7d35093 FB |
318 | $page->trigError('le nom de l\'alias ne doit contenir que des lettres,' |
319 | .' chiffres, tirets et points'); | |
d1fcf09c | 320 | return; |
321 | } | |
322 | ||
323 | $new = $liste.'@'.$globals->asso('mail_domain'); | |
00112b2e | 324 | $res = XDB::query('SELECT COUNT(*) FROM virtual WHERE alias = {?}', $new); |
d1fcf09c | 325 | $n = $res->fetchOneCell(); |
c9110c6c | 326 | if ($n) { |
a7d35093 | 327 | $page->trigError('cet alias est déjà pris'); |
d1fcf09c | 328 | return; |
329 | } | |
330 | ||
00112b2e | 331 | XDB::query('INSERT INTO virtual (alias,type) VALUES({?}, "user")', $new); |
d1fcf09c | 332 | |
8b00e0e0 | 333 | pl_redirect("alias/admin/$new"); |
d1fcf09c | 334 | } |
335 | ||
7b9d64a8 | 336 | function handler_profile(&$page, $user = null) |
337 | { | |
8b00e0e0 | 338 | http_redirect('https://www.polytechnique.org/profile/'.$user); |
7b9d64a8 | 339 | } |
340 | } | |
341 | ||
a7de4ef7 | 342 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
7b9d64a8 | 343 | ?> |