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