Merge branch 'xorg/maint' into xorg/master
[platal.git] / modules / xnetlists.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 Platal::load('lists');
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, 'groupmember'),
32 '%grp/lists/create' => $this->make_hook('create', AUTH_MDP, 'groupmember'),
33
34 '%grp/lists/members' => $this->make_hook('members', AUTH_COOKIE),
35 '%grp/lists/csv' => $this->make_hook('csv', AUTH_COOKIE),
36 '%grp/lists/annu' => $this->make_hook('annu', AUTH_COOKIE),
37 '%grp/lists/archives' => $this->make_hook('archives', AUTH_COOKIE),
38 '%grp/lists/archives/rss' => $this->make_hook('rss', AUTH_PUBLIC),
39
40 '%grp/lists/moderate' => $this->make_hook('moderate', AUTH_MDP),
41 '%grp/lists/admin' => $this->make_hook('admin', AUTH_MDP),
42 '%grp/lists/options' => $this->make_hook('options', AUTH_MDP),
43 '%grp/lists/delete' => $this->make_hook('delete', AUTH_MDP),
44
45 '%grp/lists/soptions' => $this->make_hook('soptions', AUTH_MDP),
46 '%grp/lists/check' => $this->make_hook('check', AUTH_MDP),
47 '%grp/lists/sync' => $this->make_hook('sync', AUTH_MDP),
48
49 '%grp/alias/admin' => $this->make_hook('aadmin', AUTH_MDP, 'groupadmin'),
50 '%grp/alias/create' => $this->make_hook('acreate', AUTH_MDP, 'groupadmin'),
51
52 /* hack: lists uses that */
53 'profile' => $this->make_hook('profile', AUTH_PUBLIC),
54 );
55 }
56
57 function prepare_client($page, $user = null)
58 {
59 global $globals;
60 Platal::load('lists', 'lists.inc.php');
61
62 if (is_null($user)) {
63 $user =& S::user();
64 }
65 $this->client = new MMList($user, $globals->asso('mail_domain'));
66
67 $page->assign('asso', $globals->asso());
68 $page->setType($globals->asso('cat'));
69
70 return $globals->asso('mail_domain');
71 }
72
73 function handler_lists($page)
74 {
75 global $globals;
76 require_once 'emails.inc.php';
77
78 if (!$globals->asso('mail_domain')) {
79 return PL_NOT_FOUND;
80 }
81 $this->prepare_client($page);
82 $page->changeTpl('xnetlists/index.tpl');
83
84 if (Get::has('del')) {
85 S::assert_xsrf_token();
86 $this->client->unsubscribe(Get::v('del'));
87 pl_redirect('lists');
88 }
89 if (Get::has('add')) {
90 S::assert_xsrf_token();
91 $this->client->subscribe(Get::v('add'));
92 pl_redirect('lists');
93 }
94
95 if (Post::has('del_alias') && may_update()) {
96 S::assert_xsrf_token();
97
98 $alias = Post::t('del_alias');
99 list($local_part, ) = explode('@', $alias);
100 delete_list_alias($local_part, $globals->asso('mail_domain'));
101 $page->trigSuccess($alias . ' supprimé&nbsp;!');
102 }
103
104 $listes = $this->client->get_lists();
105 $page->assign('listes', $listes);
106 $page->assign('aliases', iterate_list_alias($globals->asso('mail_domain')));
107 $page->assign('may_update', may_update());
108
109 if (count($listes) > 0 && !$globals->asso('has_ml')) {
110 XDB::execute("UPDATE groups
111 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
112 WHERE id = {?}",
113 $globals->asso('id'));
114 }
115 }
116
117 function handler_create($page)
118 {
119 global $globals;
120
121 if (!$globals->asso('mail_domain')) {
122 return PL_NOT_FOUND;
123 }
124 $this->prepare_client($page);
125 $page->changeTpl('xnetlists/create.tpl');
126
127 if (!Post::has('submit')) {
128 return;
129 } else {
130 S::assert_xsrf_token();
131 }
132
133 if (!Post::has('liste') || !Post::t('liste')) {
134 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
135 return;
136 }
137
138 $list = strtolower(Post::t('liste'));
139 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $list)) {
140 $page->trigError('le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets');
141 return;
142 }
143
144 require_once 'emails.inc.php';
145 if (list_exist($list, $globals->asso('mail_domain'))) {
146 $page->trigError('Cet alias est déjà pris.');
147 return;
148 }
149 if (!Post::t('desc')) {
150 $page->trigError('Le sujet est vide.');
151 return;
152 }
153
154 $success = $this->client->create_list($list, utf8_decode(Post::t('desc')), Post::t('advertise'),
155 Post::t('modlevel'), Post::t('inslevel'),
156 array(S::user()->forlifeEmail()), array(S::user()->forlifeEmail()));
157
158 if (!$success) {
159 $page->kill("Un problème est survenu, contacter "
160 ."<a href='mailto:support@m4x.org'>support@m4x.org</a>");
161 return;
162 }
163 create_list($list, $globals->asso('mail_domain'));
164
165 XDB::execute("UPDATE groups
166 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
167 WHERE id = {?}",
168 $globals->asso('id'));
169
170 pl_redirect('lists/admin/' . $list);
171 }
172
173 function handler_sync($page, $liste = null)
174 {
175 global $globals;
176
177 if (!$globals->asso('mail_domain')) {
178 return PL_NOT_FOUND;
179 }
180 $this->prepare_client($page);
181 $page->changeTpl('xnetlists/sync.tpl');
182
183 if (Env::has('add')) {
184 S::assert_xsrf_token();
185 $this->client->mass_subscribe($liste, array_keys(Env::v('add')));
186 }
187
188 list(,$members) = $this->client->get_members($liste);
189 $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
190 $subscribers = array_unique($mails);
191
192 $ann = XDB::fetchColumn('SELECT uid
193 FROM group_members
194 WHERE asso_id = {?}', $globals->asso('id'));
195 $users = User::getBulkUsersWithUIDs($ann);
196
197 $not_in_list = array();
198 foreach ($users as $user) {
199 if (!in_array(strtolower($user->forlifeEmail()), $subscribers)) {
200 $not_in_list[] = $user;
201 }
202 }
203
204 $page->assign('not_in_list', $not_in_list);
205 }
206
207 function handler_aadmin($page, $lfull = null)
208 {
209 global $globals;
210
211 if (!$globals->asso('mail_domain') || is_null($lfull)) {
212 return PL_NOT_FOUND;
213 }
214 $page->changeTpl('xnetlists/alias-admin.tpl');
215
216 require_once 'emails.inc.php';
217 list($local_part, $domain) = explode('@', $lfull);
218 if (Env::has('add_member')) {
219 S::assert_xsrf_token();
220
221 $email = Env::t('add_member');
222 $user = User::getSilent($email);
223 if ($user) {
224 add_to_list_alias($user, $local_part, $domain);
225 $page->trigSuccess($email . ' ajouté.');
226 } else {
227 $page->trigError($email . " n'existe pas.");
228 }
229 }
230
231 if (Env::has('del_member')) {
232 S::assert_xsrf_token();
233
234 $user = User::getSilent(Env::t('del_member'));
235 delete_from_list_alias($user, $local_part, $domain);
236 $page->trigSuccess($user->fullName() . ' supprimé.');
237 }
238
239 $page->assign('members', list_alias_members($local_part, $domain));
240 }
241
242 function handler_acreate($page)
243 {
244 global $globals;
245
246 if (!$globals->asso('mail_domain')) {
247 return PL_NOT_FOUND;
248 }
249 $page->changeTpl('xnetlists/alias-create.tpl');
250
251 if (!Post::has('submit')) {
252 return;
253 } else {
254 S::assert_xsrf_token();
255 }
256
257 if (!Post::has('liste')) {
258 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
259 return;
260 }
261 $list = Post::v('liste');
262 if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $list)) {
263 $page->trigError('Le nom de l\'alias ne doit contenir que des lettres,'
264 .' chiffres, tirets et points.');
265 return;
266 }
267
268 require_once 'emails.inc.php';
269 if (list_exist($list, $globals->asso('mail_domain'))) {
270 $page->trigError('Cet alias est déjà pris.');
271 return;
272 }
273
274 add_to_list_alias(S::user(), $list, $globals->asso('mail_domain'));
275 pl_redirect('alias/admin/' . $list . '@' . $globals->asso('mail_domain'));
276 }
277
278 function handler_profile($page, $user = null)
279 {
280 http_redirect('https://www.polytechnique.org/profile/'.$user);
281 }
282 }
283
284 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
285 ?>