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