Backport [1175] and [1176]
[platal.git] / modules / email.php
CommitLineData
81b5b746 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
22class EmailModule extends PLModule
23{
81b5b746 24 function handlers()
25 {
26 return array(
27 'emails' => $this->make_hook('emails', AUTH_COOKIE),
28 'emails/alias' => $this->make_hook('alias', AUTH_MDP),
29 'emails/antispam' => $this->make_hook('antispam', AUTH_MDP),
30 'emails/broken' => $this->make_hook('broken', AUTH_COOKIE),
31 'emails/redirect' => $this->make_hook('redirect', AUTH_MDP),
32 'emails/send' => $this->make_hook('send', AUTH_MDP),
9e570fe0 33
34 'admin/emails/duplicated' => $this->make_hook('duplicated', AUTH_MDP, 'admin')
81b5b746 35 );
36 }
37
38 function handler_emails(&$page)
39 {
40 global $globals;
41
42 $page->changeTpl('emails/index.tpl');
43 $page->assign('xorg_title','Polytechnique.org - Mes emails');
44
cab08090 45 $uid = S::v('uid');
81b5b746 46
47 if (Post::has('best')) {
48 // bestalias is the first bit : 1
49 // there will be maximum 8 bits in flags : 255
08cce2ff 50 XDB::execute("UPDATE aliases SET flags=flags & (255 - 1) WHERE id={?}", $uid);
51 XDB::execute("UPDATE aliases SET flags=flags | 1 WHERE id={?} AND alias={?}",
5e2307dc 52 $uid, Post::v('best'));
81b5b746 53 }
54
55 // on regarde si on a affaire à un homonyme
56 $sql = "SELECT alias, (type='a_vie') AS a_vie,
57 (alias REGEXP '\\\\.[0-9]{2}$') AS cent_ans,
58 FIND_IN_SET('bestalias',flags) AS best, expire
59 FROM aliases
60 WHERE id = {?} AND type!='homonyme'
61 ORDER BY LENGTH(alias)";
08cce2ff 62 $page->assign('aliases', XDB::iterator($sql, $uid));
81b5b746 63
31170a38 64 $homonyme = XDB::query("SELECT alias FROM aliases INNER JOIN homonymes ON (id = homonyme_id) WHERE user_id = {?} AND type = 'homonyme'", $uid);
65 $page->assign('homonyme', $homonyme->fetchOneCell());
66
81b5b746 67 $sql = "SELECT email
68 FROM emails
69 WHERE uid = {?} AND FIND_IN_SET('active', flags)";
08cce2ff 70 $page->assign('mails', XDB::iterator($sql, $uid));
81b5b746 71
72
73 // on regarde si l'utilisateur a un alias et si oui on l'affiche !
cab08090 74 $forlife = S::v('forlife');
08cce2ff 75 $res = XDB::query(
81b5b746 76 "SELECT alias
77 FROM virtual AS v
78 INNER JOIN virtual_redirect AS vr USING(vid)
79 WHERE (redirect={?} OR redirect={?})
80 AND alias LIKE '%@{$globals->mail->alias_dom}'",
81 $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2);
82 $page->assign('melix', $res->fetchOneCell());
81b5b746 83 }
84
85 function handler_alias(&$page, $action = null, $value = null)
86 {
87 require_once 'validations.inc.php';
88
89 global $globals;
90
91 $page->changeTpl('emails/alias.tpl');
92 $page->assign('xorg_title','Polytechnique.org - Alias melix.net');
93
cab08090 94 $uid = S::v('uid');
95 $forlife = S::v('forlife');
81b5b746 96
97 $page->assign('demande', AliasReq::get_request($uid));
98
99 if ($action == 'suppr' && $value) {
100 //Suppression d'un alias
08cce2ff 101 XDB::execute(
81b5b746 102 'DELETE virtual, virtual_redirect
103 FROM virtual
104 INNER JOIN virtual_redirect USING (vid)
105 WHERE alias = {?} AND (redirect = {?} OR redirect = {?})', $value,
106 $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2);
107 }
108
109 //Récupération des alias éventuellement existants
08cce2ff 110 $res = XDB::query(
81b5b746 111 "SELECT alias, emails_alias_pub
112 FROM auth_user_quick, virtual
113 INNER JOIN virtual_redirect USING(vid)
114 WHERE ( redirect={?} OR redirect= {?} )
115 AND alias LIKE '%@{$globals->mail->alias_dom}' AND user_id = {?}",
116 $forlife.'@'.$globals->mail->domain,
cab08090 117 $forlife.'@'.$globals->mail->domain2, S::v('uid'));
81b5b746 118 list($alias, $visibility) = $res->fetchOneRow();
119 $page->assign('actuel', $alias);
120
121 if ($action == 'ask' && Env::has('alias') and Env::has('raison')) {
122 //Si l'utilisateur vient de faire une damande
123
5e2307dc 124 $alias = Env::v('alias');
125 $raison = Env::v('raison');
126 $public = (Env::v('public', 'off') == 'on')?"public":"private";
81b5b746 127
128 $page->assign('r_alias', $alias);
129 $page->assign('r_raison', $raison);
130 if ($public == 'public') {
131 $page->assign('r_public', true);
132 }
133
134 //Quelques vérifications sur l'alias (caractères spéciaux)
135 if (!preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $alias)) {
136 $page->trig("L'adresse demandée n'est pas valide.
137 Vérifie qu'elle comporte entre 3 et 20 caractères
138 et qu'elle ne contient que des lettres non accentuées,
139 des chiffres ou les caractères - et .");
fd8f77de 140 return;
81b5b746 141 } else {
142 //vérifier que l'alias n'est pas déja pris
08cce2ff 143 $res = XDB::query('SELECT COUNT(*) FROM virtual WHERE alias={?}',
81b5b746 144 $alias.'@'.$globals->mail->alias_dom);
145 if ($res->fetchOneCell() > 0) {
146 $page->trig("L'alias $alias@{$globals->mail->alias_dom} a déja été attribué.
147 Tu ne peux donc pas l'obtenir.");
fd8f77de 148 return;
81b5b746 149 }
150
151 //vérifier que l'alias n'est pas déja en demande
152 $it = new ValidateIterator ();
153 while($req = $it->next()) {
154 if ($req->type == "alias" and $req->alias == $alias) {
155 $page->trig("L'alias $alias@{$globals->mail->alias_dom} a déja été demandé.
156 Tu ne peux donc pas l'obtenir pour l'instant.");
fd8f77de 157 return ;
81b5b746 158 }
159 }
160
161 //Insertion de la demande dans la base, écrase les requêtes précédente
162 $myalias = new AliasReq($uid, $alias, $raison, $public);
163 $myalias->submit();
164 $page->assign('success',$alias);
fd8f77de 165 return;
81b5b746 166 }
167 }
168 elseif ($action == 'set'
169 && ($value == 'public' || $value == 'private'))
170 {
171 if ($value == 'public') {
08cce2ff 172 XDB::execute("UPDATE auth_user_quick SET emails_alias_pub = 'public'
cab08090 173 WHERE user_id = {?}", S::v('uid'));
81b5b746 174 } else {
08cce2ff 175 XDB::execute("UPDATE auth_user_quick SET emails_alias_pub = 'private'
cab08090 176 WHERE user_id = {?}", S::v('uid'));
81b5b746 177 }
178
179 $visibility = $value;
180 }
181
182 $page->assign('mail_public', ($visibility == 'public'));
81b5b746 183 }
184
185 function handler_redirect(&$page, $action = null, $email = null)
186 {
187 global $globals;
188
189 require_once 'emails.inc.php';
190
191 $page->changeTpl('emails/redirect.tpl');
192
cab08090 193 $uid = S::v('uid');
194 $forlife = S::v('forlife');
81b5b746 195
3b83212e 196 $page->assign('eleve', S::i('promo') >= date("Y") - 5);
197
cab08090 198 $redirect = new Redirect(S::v('uid'));
81b5b746 199
200 if ($action == 'remove' && $email) {
201 $page->assign('retour', $redirect->delete_email($email));
202 }
203
204 if (Env::has('emailop')) {
5e2307dc 205 $actifs = Env::v('emails_actifs', Array());
206 if (Env::v('emailop') == "ajouter" && Env::has('email')) {
207 $page->assign('retour', $redirect->add_email(Env::v('email')));
81b5b746 208 } elseif (empty($actifs)) {
209 $page->assign('retour', ERROR_INACTIVE_REDIRECTION);
210 } elseif (is_array($actifs)) {
211 $page->assign('retour', $redirect->modify_email($actifs,
5e2307dc 212 Env::v('emails_rewrite',Array())));
81b5b746 213 }
214 }
215
08cce2ff 216 $res = XDB::query(
81b5b746 217 "SELECT alias
218 FROM virtual
219 INNER JOIN virtual_redirect USING(vid)
220 WHERE (redirect={?} OR redirect={?})
221 AND alias LIKE '%@{$globals->mail->alias_dom}'",
222 $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2);
223 $melix = $res->fetchOneCell();
224 if ($melix) {
225 list($melix) = explode('@', $melix);
226 $page->assign('melix',$melix);
227 }
228
08cce2ff 229 $res = XDB::query(
81b5b746 230 "SELECT alias,expire
231 FROM aliases
232 WHERE id={?} AND (type='a_vie' OR type='alias')
233 ORDER BY !FIND_IN_SET('usage',flags), LENGTH(alias)", $uid);
234 $page->assign('alias', $res->fetchAllAssoc());
235 $page->assign('emails',$redirect->emails);
81b5b746 236 }
237
238 function handler_antispam(&$page)
239 {
240 require_once 'emails.inc.php';
241
242 $page->changeTpl('emails/antispam.tpl');
243
cab08090 244 $bogo = new Bogo(S::v('uid'));
81b5b746 245 if (Env::has('statut_filtre')) {
5e2307dc 246 $bogo->change(S::v('uid'), Env::i('statut_filtre'));
81b5b746 247 }
248 $page->assign('filtre',$bogo->level());
81b5b746 249 }
250
251 function handler_send(&$page)
252 {
253 global $globals;
254
255 $page->changeTpl('emails/send.tpl');
256
257 $page->assign('xorg_title','Polytechnique.org - Envoyer un email');
258
259 // action si on recoit un formulaire
5e2307dc 260 if (Env::v('submit') == 'Envoyer')
81b5b746 261 {
5e2307dc 262 $to2 = join(', ', Env::v('contacts', Array()));
263 $txt = str_replace('^M', '', Env::v('contenu'));
264 $to = Env::v('to');
265 $subj = Env::v('sujet');
266 $from = Env::v('from');
267 $cc = Env::v('cc');
268 $bcc = Env::v('bcc');
81b5b746 269
270 if (empty($to) && empty($cc) && empty($to2)) {
271 $page->trig("Indique au moins un destinataire.");
272 } else {
273 require_once("diogenes/diogenes.hermes.inc.php");
274
275 $mymail = new HermesMailer();
276 $mymail->setFrom($from);
277 $mymail->setSubject($subj);
278 if (!empty($to)) { $mymail->addTo($to); }
279 if (!empty($cc)) { $mymail->addCc($cc); }
280 if (!empty($bcc)) { $mymail->addBcc($bcc); }
281 if (!empty($to2)) { $mymail->addTo($to2); }
ea3930d0 282 $mymail->addHeader('X-XOrg-Login', S::v('bestalias') . '@' . $globals->mail->domain);
81b5b746 283 $mymail->setTxtBody(wordwrap($txt,72,"\n"));
284 if ($mymail->send()) {
285 $page->trig("Ton mail a bien été envoyé.");
cab08090 286 $_REQUEST = array('bcc' => S::v('bestalias').'@'.$globals->mail->domain);
81b5b746 287 } else {
288 $page->trig("Erreur lors de l'envoi du courriel, réessaye.");
289 }
290 }
291 } else {
cab08090 292 $_REQUEST['bcc'] = S::v('bestalias').'@'.$globals->mail->domain;
81b5b746 293 }
294
08cce2ff 295 $res = XDB::query(
81b5b746 296 "SELECT u.prenom, u.nom, u.promo, a.alias as forlife
297 FROM auth_user_md5 AS u
298 INNER JOIN contacts AS c ON (u.user_id = c.contact)
299 INNER JOIN aliases AS a ON (u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
300 WHERE c.uid = {?}
cab08090 301 ORDER BY u.nom, u.prenom", S::v('uid'));
81b5b746 302 $page->assign('contacts', $res->fetchAllAssoc());
81b5b746 303 }
304
305 function handler_broken(&$page, $warn = null, $email = null)
306 {
307 require_once 'emails.inc.php';
308
309 global $globals;
310
311 $page->changeTpl('emails/broken.tpl');
312
313 if ($warn == 'warn' && $email) {
314 $email = valide_email($email);
315 // vérifications d'usage
08cce2ff 316 $sel = XDB::query(
81b5b746 317 "SELECT e.uid, a.alias
318 FROM emails AS e
319 INNER JOIN auth_user_md5 AS u ON e.uid = u.user_id
320 INNER JOIN aliases AS a ON (e.uid = a.id AND type!='homonyme'
321 AND FIND_IN_SET('bestalias',a.flags))
322 WHERE e.email={?}", $email);
323
324 if (list($uid, $dest) = $sel->fetchOneRow()) {
325 // envoi du mail
326 $message = "Bonjour !
327
328Ce mail a été généré automatiquement par le service de patte cassée de
cab08090 329Polytechnique.org car un autre utilisateur, ".S::v('prenom').' '.S::v('nom').",
81b5b746 330nous a signalé qu'en t'envoyant un mail, il avait reçu un message d'erreur
331indiquant que ton adresse de redirection $email
332ne fonctionnait plus !
333
334Nous te suggérons de vérifier cette adresse, et le cas échéant de mettre
2c388954 335à jour sur le site <{$globals->baseurl}/emails> tes adresses
81b5b746 336de redirection...
337
338Pour plus de rensignements sur le service de patte cassée, n'hésites pas à
2c388954 339consulter la page <{$globals->baseurl}/emails/broken>.
81b5b746 340
341
342A bientôt sur Polytechnique.org !
343L'équipe d'administration <support@polytechnique.org>";
344
345 require_once("diogenes/diogenes.hermes.inc.php");
346 $mail = new HermesMailer();
347 $mail->setFrom('"Polytechnique.org" <support@polytechnique.org>');
348 $mail->addTo("$dest@polytechnique.org");
349 $mail->setSubject("Une de tes adresse de redirection Polytechnique.org ne marche plus !!");
350 $mail->setTxtBody($message);
351 $mail->send();
352 $page->trig("Mail envoyé ! :o)");
353 }
354 } elseif (Post::has('email')) {
5e2307dc 355 $email = valide_email(Post::v('email'));
81b5b746 356
357 list(,$fqdn) = explode('@', $email);
358 $fqdn = strtolower($fqdn);
359 if ($fqdn == 'polytechnique.org' || $fqdn == 'melix.org'
360 || $fqdn == 'm4x.org' || $fqdn == 'melix.net')
361 {
362 $page->assign('neuneu', true);
363 } else {
364 $page->assign('email',$email);
08cce2ff 365 $sel = XDB::query(
81b5b746 366 "SELECT e1.uid, e1.panne != 0 AS panne, count(e2.uid) AS nb_mails,
367 u.nom, u.prenom, u.promo
368 FROM emails as e1
369 LEFT JOIN emails as e2 ON(e1.uid = e2.uid
370 AND FIND_IN_SET('active', e2.flags)
371 AND e1.email != e2.email)
372 INNER JOIN auth_user_md5 as u ON(e1.uid = u.user_id)
373 WHERE e1.email = {?}
374 GROUP BY e1.uid", $email);
375 if ($x = $sel->fetchOneAssoc()) {
376 // on écrit dans la base que l'adresse est cassée
377 if (!$x['panne']) {
74938c82 378 XDB::execute("UPDATE emails
379 SET panne=NOW(),
380 last=NOW(),
381 panne_level = 1
382 WHERE email = {?}", $email);
383 } else {
384 XDB::execute("UPDATE emails
385 SET panne_level = 1
386 WHERE email = {?} AND panne_level = 0");
81b5b746 387 }
388 $page->assign_by_ref('x', $x);
389 }
390 }
391 }
392 }
9e570fe0 393
394 function handler_duplicated(&$page, $action = 'list', $email = null)
395 {
396 $page->changeTpl('emails/duplicated.tpl');
397
398 $states = array('pending' => 'En attente...',
399 'safe' => 'Pas d\'inquiétude',
400 'unsafe' => 'Recherches en cours',
401 'dangerous' => 'Usurpations par cette adresse');
402 $page->assign('states', $states);
403
404 switch (Post::v('action')) {
405 case 'create':
406 if (trim(Post::v('emailN')) != '') {
407 Xdb::execute('INSERT IGNORE INTO emails_watch (email, state, detection, last, uid, description)
408 VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
409 trim(Post::v('emailN')), Post::v('stateN'), S::i('uid'), Post::v('descriptionN'));
410 };
411 break;
412
413 case 'edit':
414 Xdb::execute('UPDATE emails_watch
415 SET state = {?}, last = NOW(), uid = {?}, description = {?}
416 WHERE email = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'), Post::v('emailN'));
417 break;
418
419 default:
420 if ($action == 'delete' && !is_null($email)) {
421 Xdb::execute('DELETE FROM emails_watch WHERE email = {?}', $email);
422 }
423 }
424 if ($action != 'create' && $action != 'edit') {
425 $action = 'list';
426 }
427 $page->assign('action', $action);
428
429 if ($action == 'list') {
430 $sql = "SELECT w.email, w.detection, w.state, a.alias AS forlife
431 FROM emails_watch AS w
ca6d07f4 432 LEFT JOIN emails AS e USING(email)
433 LEFT JOIN aliases AS a ON (a.id = e.uid AND a.type = 'a_vie')
9e570fe0 434 ORDER BY w.state, w.email, a.alias";
435 $it = Xdb::iterRow($sql);
436
437 $table = array();
438 $props = array();
439 while (list($email, $date, $state, $forlife) = $it->next()) {
440 if (count($props) == 0 || $props['mail'] != $email) {
441 if (count($props) > 0) {
442 $table[] = $props;
443 }
444 $props = array('mail' => $email,
445 'detection' => $date,
446 'state' => $state,
447 'users' => array($forlife));
448 } else {
449 $props['users'][] = $forlife;
450 }
451 }
452 if (count($props) > 0) {
453 $table[] = $props;
454 }
455 $page->assign('table', $table);
456 } elseif ($action == 'edit') {
457 $sql = "SELECT w.detection, w.state, w.last, w.description,
458 a1.alias AS edit, a2.alias AS forlife
459 FROM emails_watch AS w
9615c895 460 LEFT JOIN aliases AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie')
461 LEFT JOIN emails AS e ON (w.email = e.email)
ca6d07f4 462 LEFT JOIN aliases AS a2 ON (a2.id = e.uid AND a2.type = 'a_vie')
9e570fe0 463 WHERE w.email = {?}
464 ORDER BY a2.alias";
465 $it = Xdb::iterRow($sql, $email);
466
467 $props = array();
468 while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) {
469 if (count($props) == 0) {
470 $props = array('mail' => $email,
471 'detection' => $detection,
472 'state' => $state,
473 'last' => $last,
474 'description' => $description,
475 'edit' => $edit,
476 'users' => array($forlife));
477 } else {
478 $props['users'][] = $forlife;
479 }
480 }
481 $page->assign('doublon', $props);
482 }
483 }
81b5b746 484}
485
486?>