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