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