Add option for checking unknown emails for axletter
[platal.git] / modules / axletter.php
CommitLineData
8da0d3c1 1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
8da0d3c1 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
22class AXLetterModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
eb5a266d 27 'ax' => $this->make_hook('index', AUTH_COOKIE),
a0f05027 28 'ax/out' => $this->make_hook('out', AUTH_PUBLIC),
8da0d3c1 29 'ax/show' => $this->make_hook('show', AUTH_COOKIE),
30 'ax/edit' => $this->make_hook('submit', AUTH_MDP),
31 'ax/edit/cancel' => $this->make_hook('cancel', AUTH_MDP),
32 'ax/edit/valid' => $this->make_hook('valid', AUTH_MDP),
eb5a266d 33 'admin/axletter' => $this->make_hook('admin', AUTH_MDP, 'admin'),
8da0d3c1 34 );
35 }
36
030038cd 37 function handler_out(&$page, $hash = null)
a0f05027 38 {
39 if (!$hash) {
40 if (!S::logged()) {
41 return PL_DO_AUTH;
42 } else {
43 return $this->handler_index($page, 'out');
44 }
45 }
460d8f55 46 $this->load('axletter.inc.php');
a0f05027 47 $page->changeTpl('axletter/unsubscribe.tpl');
48 $page->assign('success', AXLetter::unsubscribe($hash, true));
49 }
50
51 function handler_index(&$page, $action = null)
8da0d3c1 52 {
460d8f55 53 $this->load('axletter.inc.php');
8da0d3c1 54
55 $page->changeTpl('axletter/index.tpl');
46f272fe 56 $page->setTitle('Envois de l\'AX');
8da0d3c1 57
58 switch ($action) {
8da0d3c1 59 case 'in': AXLetter::subscribe(); break;
a0f05027 60 case 'out': AXLetter::unsubscribe(); break;
8da0d3c1 61 }
62
63 $perm = AXLetter::hasPerms();
64 if ($perm) {
9b242f17 65 $res = XDB::query("SELECT * FROM axletter_ins");
66 $page->assign('count', $res->numRows());
a0f05027 67 $page->assign('new', AXLetter::awaiting());
8da0d3c1 68 }
69 $page->assign('axs', AXLetter::subscriptionState());
70 $page->assign('ax_list', AXLetter::listSent());
71 $page->assign('ax_rights', $perm);
72 }
73
74 function handler_submit(&$page, $action = null)
75 {
460d8f55 76 $this->load('axletter.inc.php');
8da0d3c1 77 if (!AXLetter::hasPerms()) {
78 return PL_FORBIDDEN;
79 }
80
81 $page->changeTpl('axletter/edit.tpl');
82
4ea58d4b
VZ
83 $saved = Post::i('saved');
84 $new = false;
85 $id = Post::i('id');
86 $short_name = trim(Post::v('short_name'));
87 $subject = trim(Post::v('subject'));
88 $title = trim(Post::v('title'));
89 $body = rtrim(Post::v('body'));
90 $signature = trim(Post::v('signature'));
91 $promo_min = Post::i('promo_min');
92 $promo_max = Post::i('promo_max');
4a08dc73 93 $subset_to = preg_split("/[ ,;\:\n\r]+/", Post::v('subset_to'), -1, PREG_SPLIT_NO_EMPTY);
180627f3 94 $subset = (count($subset_to) > 0);
1f58ab8e 95 $subset_rm = Post::b('subset_rm');
4ea58d4b
VZ
96 $echeance = Post::has('echeance_date') ?
97 preg_replace('/^(\d\d\d\d)(\d\d)(\d\d)$/', '\1-\2-\3', Post::v('echeance_date')) . ' ' . Post::v('echeance_time')
98 : Post::v('echeance');
8da0d3c1 99 $echeance_date = Post::v('echeance_date');
100 $echeance_time = Post::v('echeance_time');
101
102 if (!$id) {
103 $res = XDB::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)");
104 if ($res->numRows()) {
105 extract($res->fetchOneAssoc(), EXTR_OVERWRITE);
c1134ef2 106 $subset_to = ($subset ? explode("\n", $subset) : null);
180627f3 107 $subset = (count($subset_to) > 0);
8da0d3c1 108 $saved = true;
109 } else {
110 XDB::execute("INSERT INTO axletter SET id = NULL");
111 $id = XDB::insertId();
112 }
113 if (!$echeance || $echeance == '0000-00-00 00:00:00') {
114 $saved = false;
115 $new = true;
116 }
40d428d8
VZ
117 } elseif (Post::has('valid')) {
118 S::assert_xsrf_token();
119
8da0d3c1 120 if (!$subject && $title) {
121 $subject = $title;
122 }
123 if (!$title && $subject) {
124 $title = $subject;
125 }
126 if (!$subject || !$title || !$body) {
a7d35093 127 $page->trigError("L'article doit avoir un sujet et un contenu");
8da0d3c1 128 Post::kill('valid');
129 }
130 if (($promo_min > $promo_max && $promo_max != 0)||
131 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
132 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
133 {
a7d35093 134 $page->trigError("L'intervalle de promotions n'est pas valide");
8da0d3c1 135 Post::kill('valid');
136 }
4ea58d4b 137 if (empty($short_name)) {
a7d35093 138 $page->trigError("L'annonce doit avoir un nom raccourci pour simplifier la navigation dans les archives");
8da0d3c1 139 Post::kill('valid');
4ea58d4b 140 } elseif (!preg_match('/^[a-z][-a-z0-9]*[a-z0-9]$/', $short_name)) {
a7d35093 141 $page->trigError("Le nom raccourci n'est pas valide, il doit comporter au moins 2 caractères et n'être composé "
8da0d3c1 142 . "que de chiffres, lettres et tirets");
143 Post::kill('valid');
4ea58d4b
VZ
144 } elseif ($short_name != Post::v('old_short_name')) {
145 $res = XDB::query("SELECT id FROM axletter WHERE short_name = {?}", $short_name);
8da0d3c1 146 if ($res->numRows() && $res->fetchOneCell() != $id) {
a7d35093 147 $page->trigError("Le nom $short_name est déjà utilisé, merci d'en choisir un autre");
4ea58d4b
VZ
148 $short_name = Post::v('old_short_name');
149 if (empty($short_name)) {
8da0d3c1 150 Post::kill('valid');
151 }
152 }
153 }
154
155 switch (@Post::v('valid')) {
813c44f7
RB
156 case 'Vérifier':
157 // Same as 'preview', but performs a test of all provided emails
158 if ($subset) {
159 require_once 'emails.inc.php';
160 $ids = ids_from_mails($subset_to);
161 $has_error = false;
162 foreach ($subset_to as $e) {
163 if (!array_key_exists($e, $ids)) {
164 if (!$has_error) {
165 $page->trigError("Emails inconnus :");
166 $has_error = true;
167 }
168 $page->trigError($e);
169 }
170 }
171 }
172 // XXX : no break here, since Vérifier is a subcase of Aperçu.
a7de4ef7 173 case 'Aperçu':
460d8f55 174 $this->load('axletter.inc.php');
4ea58d4b 175 $al = new AXLetter(array($id, $short_name, $subject, $title, $body, $signature,
1f58ab8e 176 $promo_min, $promo_max, $subset, $subset_rm, $echeance, 0, 'new'));
8da0d3c1 177 $al->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
178 break;
179
180 case 'Confirmer':
181 XDB::execute("REPLACE INTO axletter
c6f2cafe 182 SET id = {?}, short_name = {?}, subject = {?}, title = {?}, body = {?},
1f58ab8e
RB
183 signature = {?}, promo_min = {?}, promo_max = {?}, echeance = {?}, subset = {?}, subset_rm = {?}",
184 $id, $short_name, $subject, $title, $body, $signature, $promo_min, $promo_max, $echeance, $subset ? implode("\n", $subset_to) : null, $subset_rm);
9b242f17 185 if (!$saved) {
115c90db 186 global $globals;
9b242f17 187 $mailer = new PlMailer();
1d55fe45 188 $mailer->setFrom("support@" . $globals->mail->domain);
faefdbb7
SJ
189 $mailer->setSubject("Un nouveau projet d'email de l'AX vient d'être proposé");
190 $mailer->setTxtBody("Un nouvel email vient d'être rédigé en prévision d'un envoi prochain. Vous pouvez "
9b242f17 191 . "le modifier jusqu'à ce qu'il soit verrouillé pour l'envoi\n\n"
faefdbb7 192 . "Le sujet de l'email : $subject\n"
9b242f17 193 . "L'échéance d'envoi est fixée à $echeance.\n"
faefdbb7 194 . "L'email pourra néanmoins partir avant cette échéance si un administrateur de "
9b242f17 195 . "Polytechnique.org le valide.\n\n"
faefdbb7 196 . "Pour modifier, valider ou annuler l'email :\n"
9b242f17 197 . "https://www.polytechnique.org/ax/edit\n"
198 . "-- \n"
199 . "Association Polytechnique.org\n");
200 $res = XDB::iterRow("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
201 u.prenom, a.alias AS bestalias
202 FROM axletter_rights AS ar
203 INNER JOIN auth_user_md5 AS u USING(user_id)
204 INNER JOIN aliases AS a ON (u.user_id = a.id
205 AND FIND_IN_SET('bestalias', a.flags))");
9b242f17 206 while (list($nom, $prenom, $alias) = $res->next()) {
207 $mailer->addTo("$nom $prenom <$alias@{$globals->mail->domain}>");
208 }
209 $mailer->send();
210 }
8da0d3c1 211 $saved = true;
212 $echeance_date = null;
213 $echeance_time = null;
214 pl_redirect('ax');
215 break;
216 }
217 }
218 $page->assign('id', $id);
4ea58d4b 219 $page->assign('short_name', $short_name);
8da0d3c1 220 $page->assign('subject', $subject);
221 $page->assign('title', $title);
222 $page->assign('body', $body);
223 $page->assign('signature', $signature);
224 $page->assign('promo_min', $promo_min);
225 $page->assign('promo_max', $promo_max);
2a3f4f57
RB
226 $page->assign('subset_to', implode("\n", $subset_to));
227 $page->assign('subset', $subset);
1f58ab8e 228 $page->assign('subset_rm', $subset_rm);
8da0d3c1 229 $page->assign('echeance', $echeance);
230 $page->assign('echeance_date', $echeance_date);
231 $page->assign('echeance_time', $echeance_time);
232 $page->assign('saved', $saved);
233 $page->assign('new', $new);
dd70cd28 234 $page->assign('is_xorg', S::admin());
8da0d3c1 235
236 if (!$saved) {
237 $select = '';
8da0d3c1 238 for ($i = 0 ; $i < 24 ; $i++) {
239 $stamp = sprintf('%02d:00:00', $i);
240 if ($stamp == $echeance_time) {
241 $sel = ' selected="selected"';
242 } else {
243 $sel = '';
244 }
245 $select .= "<option value=\"$stamp\"$sel>{$i}h</option>\n";
246 }
247 $page->assign('echeance_time', $select);
eaf30d86 248 }
8da0d3c1 249 }
250
251 function handler_cancel(&$page, $force = null)
252 {
460d8f55 253 $this->load('axletter.inc.php');
fde3e90e 254 if (!AXLetter::hasPerms() || !S::has_xsrf_token()) {
8da0d3c1 255 return PL_FORBIDDEN;
256 }
257
a0f05027 258 $al = AXLetter::awaiting();
fde3e90e 259 if (!$al) {
8da0d3c1 260 $page->kill("Aucune lettre en attente");
261 return;
262 }
8da0d3c1 263 if (!$al->invalid()) {
264 $page->kill("Une erreur est survenue lors de l'annulation de l'envoi");
265 return;
266 }
267
8f794f88 268 $page->killSuccess("L'envoi de l'annonce {$al->title()} est annulé.");
8da0d3c1 269 }
270
271 function handler_valid(&$page, $force = null)
272 {
460d8f55 273 $this->load('axletter.inc.php');
fde3e90e 274 if (!AXLetter::hasPerms() || !S::has_xsrf_token()) {
8da0d3c1 275 return PL_FORBIDDEN;
276 }
277
a0f05027 278 $al = AXLetter::awaiting();
279 if (!$al) {
8da0d3c1 280 $page->kill("Aucune lettre en attente");
281 return;
282 }
8da0d3c1 283 if (!$al->valid()) {
284 $page->kill("Une erreur est survenue lors de la validation de l'envoi");
285 return;
286 }
287
8f794f88 288 $page->killSuccess("L'envoi de l'annonce aura lieu dans l'heure qui vient.");
8da0d3c1 289 }
290
291 function handler_show(&$page, $nid = 'last')
292 {
460d8f55 293 $this->load('axletter.inc.php');
8da0d3c1 294 $page->changeTpl('axletter/show.tpl');
295
4a71cf67
SJ
296 try {
297 $nl = new AXLetter($nid);
298 if (Get::has('text')) {
299 $nl->toText($page, S::v('prenom'), S::v('nom'), S::v('femme'));
300 } else {
301 $nl->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
302 }
303 if (Post::has('send')) {
304 $nl->sendTo(S::user()->login(), S::user()->bestEmail(),
305 S::v('prenom'), S::v('nom'),
306 S::v('femme'), S::v('mail_fmt') != 'texte');
307 }
308 } catch (MailNotFound $e) {
309 return PL_NOT_FOUND;
8da0d3c1 310 }
311 }
0d75939a 312
313 function handler_admin(&$page, $action = null, $uid = null)
314 {
460d8f55 315 $this->load('axletter.inc.php');
0d75939a 316 if (Post::has('action')) {
317 $action = Post::v('action');
318 $uid = Post::v('uid');
319 }
40d428d8
VZ
320 if ($uid) {
321 S::assert_xsrf_token();
322
0d75939a 323 $uids = preg_split('/ *[,;\: ] */', $uid);
324 foreach ($uids as $uid) {
325 switch ($action) {
326 case 'add':
327 $res = AXLetter::grantPerms($uid);
328 break;
329 case 'del';
330 $res = AXLetter::revokePerms($uid);
331 break;
332 }
333 if (!$res) {
a7d35093 334 $page->trigError("Personne ne correspond à l'identifiant '$uid'");
0d75939a 335 }
336 }
337 }
338
339 $page->changeTpl('axletter/admin.tpl');
340 $res = XDB::iterator("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
90c614cd 341 u.prenom, u.promo, u.hruid
0d75939a 342 FROM axletter_rights AS ar
90c614cd 343 INNER JOIN auth_user_md5 AS u USING(user_id)");
0d75939a 344 $page->assign('admins', $res);
eaf30d86 345
0d75939a 346 $importer = new CSVImporter('axletter_ins');
347 $importer->registerFunction('user_id', 'email vers Id X.org', array($this, 'idFromMail'));
348 $importer->forceValue('hash', array($this, 'createHash'));
2f95cd8f 349 $importer->apply($page, "admin/axletter", array('user_id', 'email', 'prenom', 'nom', 'promo', 'flag', 'hash'));
0d75939a 350 }
351
f6eacab0 352 function idFromMail($line, $key, $relation = null)
0d75939a 353 {
354 static $field;
cbce77b7 355 global $globals;
0d75939a 356 if (!isset($field)) {
357 $field = array('email', 'mail', 'login', 'bestalias', 'forlife', 'flag');
358 foreach ($field as $fld) {
359 if (isset($line[$fld])) {
360 $field = $fld;
361 break;
362 }
363 }
364 }
365 $email = $line[$field];
366 if (strpos($email, '@') === false) {
367 $user = $email;
cbce77b7 368 $domain = $globals->mail->domain2;
0d75939a 369 } else {
eaf30d86 370 list($user, $domain) = explode('@', $email);
cbce77b7 371 }
372 if ($domain != $globals->mail->domain && $domain != $globals->mail->domain2
0d75939a 373 && $domain != $globals->mail->alias_dom && $domain != $globals->mail->alias_dom2) {
cbce77b7 374 $res = XDB::query("SELECT uid FROM emails WHERE email = {?}", $email);
375 if ($res->numRows() == 1) {
376 return $res->fetchOneCell();
0d75939a 377 }
cbce77b7 378 return '0';
379 }
380 list($user) = explode('+', $user);
381 list($user) = explode('_', $user);
382 if ($domain == $globals->mail->alias_dom || $domain == $globals->mail->alias_dom2) {
383 $res = XDB::query("SELECT a.id
384 FROM virtual AS v
385 INNER JOIN virtual_redirect AS r USING(vid)
386 INNER JOIN aliases AS a ON (a.type = 'a_vie'
387 AND r.redirect = CONCAT(a.alias, '@{$globals->mail->domain2}'))
388 WHERE v.alias = CONCAT({?}, '@{$globals->mail->alias_dom}')", $user);
389 $id = $res->fetchOneCell();
390 return $id ? $id : '0';
0d75939a 391 }
392 $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $user);
393 $id = $res->fetchOneCell();
394 return $id ? $id : '0';
395 }
396
f6eacab0 397 function createHash($line, $key, $relation)
0d75939a 398 {
5480a216 399 $hash = implode(time(), $line) . rand();
0d75939a 400 $hash = md5($hash);
401 return $hash;
402 }
8da0d3c1 403}
404
a7de4ef7 405// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
8da0d3c1 406?>