Notifies that rights restriction on Xnet does not apply to MLs.
[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 if (S::suid()) {
109 $page->trigWarning("Attention&nbsp;: l'affichage des listes de diffusion ne tient pas compte de l'option « Voir le site comme&hellip; ».");
110 }
111
112 if (count($listes) > 0 && !$globals->asso('has_ml')) {
113 XDB::execute("UPDATE groups
114 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
115 WHERE id = {?}",
116 $globals->asso('id'));
117 }
118 }
119
120 function handler_create($page)
121 {
122 global $globals;
123
124 if (!$globals->asso('mail_domain')) {
125 return PL_NOT_FOUND;
126 }
127 $this->prepare_client($page);
128 $page->changeTpl('xnetlists/create.tpl');
129
130 if (!Post::has('submit')) {
131 return;
132 } else {
133 S::assert_xsrf_token();
134 }
135
136 if (!Post::has('liste') || !Post::t('liste')) {
137 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
138 return;
139 }
140
141 $list = strtolower(Post::t('liste'));
142 if (!preg_match("/^[a-zA-Z0-9\-]*$/", $list)) {
143 $page->trigError('le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets');
144 return;
145 }
146
147 require_once 'emails.inc.php';
148 if (list_exist($list, $globals->asso('mail_domain'))) {
149 $page->trigError('Cet alias est déjà pris.');
150 return;
151 }
152 if (!Post::t('desc')) {
153 $page->trigError('Le sujet est vide.');
154 return;
155 }
156
157 $success = $this->client->create_list($list, utf8_decode(Post::t('desc')), Post::t('advertise'),
158 Post::t('modlevel'), Post::t('inslevel'),
159 array(S::user()->forlifeEmail()), array(S::user()->forlifeEmail()));
160
161 if (!$success) {
162 $page->kill("Un problème est survenu, contacter "
163 ."<a href='mailto:support@m4x.org'>support@m4x.org</a>");
164 return;
165 }
166 create_list($list, $globals->asso('mail_domain'));
167
168 XDB::execute("UPDATE groups
169 SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
170 WHERE id = {?}",
171 $globals->asso('id'));
172
173 pl_redirect('lists/admin/' . $list);
174 }
175
176 function handler_sync($page, $liste = null)
177 {
178 global $globals;
179
180 if (!$globals->asso('mail_domain')) {
181 return PL_NOT_FOUND;
182 }
183 $this->prepare_client($page);
184 $page->changeTpl('xnetlists/sync.tpl');
185
186 if (Env::has('add')) {
187 S::assert_xsrf_token();
188 $this->client->mass_subscribe($liste, array_keys(Env::v('add')));
189 }
190
191 list(,$members) = $this->client->get_members($liste);
192 $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
193 $subscribers = array_unique($mails);
194
195 $ann = XDB::fetchColumn('SELECT uid
196 FROM group_members
197 WHERE asso_id = {?}', $globals->asso('id'));
198 $users = User::getBulkUsersWithUIDs($ann);
199
200 $not_in_list = array();
201 foreach ($users as $user) {
202 if (!in_array(strtolower($user->forlifeEmail()), $subscribers)) {
203 $not_in_list[] = $user;
204 }
205 }
206
207 $page->assign('not_in_list', $not_in_list);
208 }
209
210 function handler_aadmin($page, $lfull = null)
211 {
212 global $globals;
213
214 if (!$globals->asso('mail_domain') || is_null($lfull)) {
215 return PL_NOT_FOUND;
216 }
217 $page->changeTpl('xnetlists/alias-admin.tpl');
218
219 require_once 'emails.inc.php';
220 list($local_part, $domain) = explode('@', $lfull);
221 if ($globals->asso('mail_domain') != $domain || !preg_match("/^[a-zA-Z0-9\-\.]*$/", $local_part)) {
222 $page->trigErrorRedirect('Le nom de l\'alias est erroné.', $globals->asso('diminutif') . '/lists');
223 }
224
225
226 if (Env::has('add_member')) {
227 S::assert_xsrf_token();
228
229 if (add_to_list_alias(Env::t('add_member'), $local_part, $domain)) {
230 $page->trigSuccess('Ajout réussit.');
231 } else {
232 $page->trigError('Ajout infructueux.');
233 }
234 }
235
236 if (Env::has('del_member')) {
237 S::assert_xsrf_token();
238
239 if (delete_from_list_alias(Env::t('del_member'), $local_part, $domain)) {
240 $page->trigSuccess('Suppression réussie.');
241 } else {
242 $page->trigError('Suppression infructueuse.');
243 }
244 }
245
246 $page->assign('members', list_alias_members($local_part, $domain));
247 }
248
249 function handler_acreate($page)
250 {
251 global $globals;
252
253 if (!$globals->asso('mail_domain')) {
254 return PL_NOT_FOUND;
255 }
256 $page->changeTpl('xnetlists/alias-create.tpl');
257
258 if (!Post::has('submit')) {
259 return;
260 } else {
261 S::assert_xsrf_token();
262 }
263
264 if (!Post::has('liste')) {
265 $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
266 return;
267 }
268 $list = Post::v('liste');
269 if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $list)) {
270 $page->trigError('Le nom de l\'alias ne doit contenir que des lettres,'
271 .' chiffres, tirets et points.');
272 return;
273 }
274
275 require_once 'emails.inc.php';
276 if (list_exist($list, $globals->asso('mail_domain'))) {
277 $page->trigError('Cet alias est déjà pris.');
278 return;
279 }
280
281 add_to_list_alias(S::i('uid'), $list, $globals->asso('mail_domain'));
282 pl_redirect('alias/admin/' . $list . '@' . $globals->asso('mail_domain'));
283 }
284
285 function handler_profile($page, $user = null)
286 {
287 http_redirect('https://www.polytechnique.org/profile/'.$user);
288 }
289 }
290
291 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
292 ?>