migrate list creation.
[platal.git] / modules / xnetlists.php
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),
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),
44 'grp/lists/sync' => $this->make_hook('sync', AUTH_MDP),
45
46 /* hack: lists uses that */
47 'profile' => $this->make_hook('profile', AUTH_PUBLIC),
48 );
49 }
50
51 function prepare_client(&$page)
52 {
53 global $globals;
54
55 require_once 'lists.inc.php';
56
57 $this->client =& lists_xmlrpc(Session::getInt('uid'),
58 Session::get('password'),
59 $globals->asso('mail_domain'));
60
61 $page->useMenu();
62 $page->assign('asso', $globals->asso());
63 $page->setType($globals->asso('cat'));
64 }
65
66 function handler_lists(&$page)
67 {
68 global $globals;
69
70 $this->prepare_client($page);
71
72 $page->changeTpl('xnet/groupe/listes.tpl');
73
74 if (Get::has('del')) {
75 $this->client->unsubscribe(Get::get('del'));
76 redirect('lists');
77 }
78 if (Get::has('add')) {
79 $this->client->subscribe(Get::get('add'));
80 redirect('lists');
81 }
82
83 if (Post::has('del_alias') && may_update()) {
84 $alias = Post::get('del_alias');
85 // prevent group admin from erasing aliases from other groups
86 $alias = substr($alias, 0, strpos($alias, '@')).'@'.$globals->asso('mail_domain');
87 $globals->xdb->query(
88 'DELETE FROM x4dat.virtual_redirect, x4dat.virtual
89 USING x4dat.virtual AS v
90 LEFT JOIN x4dat.virtual_redirect USING(vid)
91 WHERE v.alias={?}', $alias);
92 $page->trig(Post::get('del_alias')." supprimé !");
93 }
94
95 $listes = $this->client->get_lists();
96 $page->assign('listes',$listes);
97
98 $alias = $globals->xdb->iterator(
99 'SELECT alias,type
100 FROM x4dat.virtual
101 WHERE alias
102 LIKE {?} AND type="user"
103 ORDER BY alias', '%@'.$globals->asso('mail_domain'));
104 $page->assign('alias', $alias);
105
106 $page->assign('may_update', may_update());
107 }
108
109 function handler_create(&$page)
110 {
111 global $globals;
112
113 $this->prepare_client($page);
114
115 $page->changeTpl('xnet/groupe/listes-create.tpl');
116 $page->assign('force_list_super', may_update());
117
118 if (!Post::has('submit')) {
119 return;
120 }
121
122 if (!Post::has('liste')) {
123 $page->trig_run('champs «addresse souhaitée» vide');
124 }
125
126 $liste = Post::get('liste');
127
128 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $liste)) {
129 $page->trig_run('le nom de la liste ne doit contenir que des lettres, chiffres et tirets');
130 }
131
132 $new = $liste.'@'.$globals->asso('mail_domain');
133 $res = $globals->xdb->query('SELECT COUNT(*) FROM x4dat.virtual WHERE alias={?}', $new);
134 $n = $res->fetchOneCell();
135
136 if($n) {
137 $page->trig_run('cet alias est déjà pris');
138 }
139 if(!Post::get('desc')) {
140 $page->trig_run('le sujet est vide');
141 }
142
143 require_once('platal/xmlrpc-client.inc.php');
144 require_once('lists.inc.php');
145 $ret = $this->client->create_list(
146 $liste, Post::get('desc'), Post::get('advertise'),
147 Post::get('modlevel'), Post::get('inslevel'),
148 array(Session::get('forlife')), array());
149
150 $dom = strtolower($globals->asso("mail_domain"));
151 $red = $dom.'_'.$liste;
152
153 if (!$ret) {
154 $page->kill("Un problème est survenu, contacter "
155 ."<a href='mailto:support@m4x.org'>support@m4x.org</a>");
156 return;
157 }
158 $globals->xdb->execute('INSERT INTO x4dat.virtual (alias,type)
159 VALUES({?},{?})', $liste.'@'.$dom, 'list');
160 $globals->xdb->execute('INSERT INTO x4dat.virtual_redirect (vid,redirect)
161 VALUES ({?}, {?})', mysql_insert_id(),
162 "$red+post@listes.polytechnique.org");
163 $globals->xdb->execute('INSERT INTO x4dat.virtual (alias,type)
164 VALUES({?},{?})', $liste.'-owner@'.$dom, 'list');
165 $globals->xdb->execute('INSERT INTO x4dat.virtual_redirect (vid,redirect)
166 VALUES ({?}, {?})', mysql_insert_id(),
167 "$red+owner@listes.polytechnique.org");
168 $globals->xdb->execute('INSERT INTO x4dat.virtual (alias,type)
169 VALUES({?},{?})', $liste.'-admin@'.$dom, 'list');
170 $globals->xdb->execute('INSERT INTO x4dat.virtual_redirect (vid,redirect)
171 VALUES ({?}, {?})', mysql_insert_id(),
172 "$red+admin@listes.polytechnique.org");
173 $globals->xdb->execute('INSERT INTO x4dat.virtual (alias,type)
174 VALUES({?},{?})', $liste.'-bounces@'.$dom, 'list');
175 $globals->xdb->execute('INSERT INTO x4dat.virtual_redirect (vid,redirect)
176 VALUES ({?}, {?})', mysql_insert_id(),
177 "$red+bounces@listes.polytechnique.org");
178
179 global $platal;
180 redirect(smarty_function_rel().'/'.$platal->ns.'lists/admin/'.$liste);
181 }
182
183 function handler_sync(&$page, $liste = null)
184 {
185 global $globals;
186
187 $this->prepare_client($page);
188
189 $page->changeTpl('xnet/groupe/listes-sync.tpl');
190
191 if (Env::has('add')) {
192 $this->client->mass_subscribe($liste, array_keys(Env::getMixed('add')));
193 }
194
195 list(,$members) = $this->client->get_members($liste);
196 $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
197 $subscribers = array_unique(array_merge($subscribers, $mails));
198
199 $not_in_group_x = array();
200 $not_in_group_ext = array();
201
202 $ann = $globals->xdb->iterator(
203 "SELECT IF(m.origine='X',IF(u.nom_usage<>'', u.nom_usage, u.nom) ,m.nom) AS nom,
204 IF(m.origine='X',u.prenom,m.prenom) AS prenom,
205 IF(m.origine='X',u.promo,'extérieur') AS promo,
206 IF(m.origine='X',CONCAT(a.alias, '@polytechnique.org'),m.email) AS email,
207 IF(m.origine='X',FIND_IN_SET('femme', u.flags),0) AS femme,
208 m.perms='admin' AS admin,
209 m.origine='X' AS x
210 FROM groupex.membres AS m
211 LEFT JOIN auth_user_md5 AS u ON ( u.user_id = m.uid )
212 LEFT JOIN aliases AS a ON ( a.id = m.uid AND a.type='a_vie' )
213 WHERE m.asso_id = {?}", $globals->asso('id'));
214
215 $not_in_list = array();
216
217 while ($tmp = $ann->next()) {
218 if (!in_array($tmp['email'], $subscribers)) {
219 $not_in_list[] = $tmp;
220 }
221 }
222
223 $page->assign('not_in_list', $not_in_list);
224 }
225
226 function handler_profile(&$page, $user = null)
227 {
228 redirect('https://www.polytechnique.org/profile/'.$user);
229 }
230 }
231
232 ?>