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