Add @m4x.org to have real mail
[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
b7582015 38 function handler_emails(&$page, $action = null, $email = null)
81b5b746 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
b7582015 47 if ($action == 'best' && $email) {
81b5b746 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={?}",
b7582015 52 $uid, $email);
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) {
b7582015 201 $retour = $redirect->delete_email($email);
202 $page->assign('retour', $retour);
81b5b746 203 }
b7582015 204
205 if ($action == 'active' && $email) {
206 $redirect->modify_one_email($email, true);
207 }
208
209 if ($action == 'inactive' && $email) {
210 $redirect->modify_one_email($email, false);
211 }
212
213 if ($action == 'rewrite' && $email) {
214 $rewrite = @func_get_arg(3);
215 $redirect->modify_one_email_redirect($email, $rewrite);
216 }
217
81b5b746 218 if (Env::has('emailop')) {
5e2307dc 219 $actifs = Env::v('emails_actifs', Array());
b7582015 220 print_r(Env::v('emails_rewrite'));
5e2307dc 221 if (Env::v('emailop') == "ajouter" && Env::has('email')) {
222 $page->assign('retour', $redirect->add_email(Env::v('email')));
81b5b746 223 } elseif (empty($actifs)) {
224 $page->assign('retour', ERROR_INACTIVE_REDIRECTION);
225 } elseif (is_array($actifs)) {
226 $page->assign('retour', $redirect->modify_email($actifs,
5e2307dc 227 Env::v('emails_rewrite',Array())));
81b5b746 228 }
229 }
230
08cce2ff 231 $res = XDB::query(
81b5b746 232 "SELECT alias
233 FROM virtual
234 INNER JOIN virtual_redirect USING(vid)
235 WHERE (redirect={?} OR redirect={?})
236 AND alias LIKE '%@{$globals->mail->alias_dom}'",
237 $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2);
238 $melix = $res->fetchOneCell();
239 if ($melix) {
240 list($melix) = explode('@', $melix);
241 $page->assign('melix',$melix);
242 }
243
08cce2ff 244 $res = XDB::query(
81b5b746 245 "SELECT alias,expire
246 FROM aliases
247 WHERE id={?} AND (type='a_vie' OR type='alias')
248 ORDER BY !FIND_IN_SET('usage',flags), LENGTH(alias)", $uid);
249 $page->assign('alias', $res->fetchAllAssoc());
250 $page->assign('emails',$redirect->emails);
81b5b746 251 }
252
b7582015 253 function handler_antispam(&$page, $statut_filtre = null)
81b5b746 254 {
255 require_once 'emails.inc.php';
256
257 $page->changeTpl('emails/antispam.tpl');
258
cab08090 259 $bogo = new Bogo(S::v('uid'));
b7582015 260 if (isset($statut_filtre)) {
261 $bogo->change(S::v('uid'), $statut_filtre + 0);
81b5b746 262 }
263 $page->assign('filtre',$bogo->level());
81b5b746 264 }
265
266 function handler_send(&$page)
267 {
268 global $globals;
269
270 $page->changeTpl('emails/send.tpl');
271
272 $page->assign('xorg_title','Polytechnique.org - Envoyer un email');
273
274 // action si on recoit un formulaire
5e2307dc 275 if (Env::v('submit') == 'Envoyer')
81b5b746 276 {
5e2307dc 277 $to2 = join(', ', Env::v('contacts', Array()));
278 $txt = str_replace('^M', '', Env::v('contenu'));
279 $to = Env::v('to');
280 $subj = Env::v('sujet');
281 $from = Env::v('from');
282 $cc = Env::v('cc');
283 $bcc = Env::v('bcc');
81b5b746 284
285 if (empty($to) && empty($cc) && empty($to2)) {
286 $page->trig("Indique au moins un destinataire.");
287 } else {
1e33266a 288 $mymail = new PlMailer();
81b5b746 289 $mymail->setFrom($from);
290 $mymail->setSubject($subj);
291 if (!empty($to)) { $mymail->addTo($to); }
292 if (!empty($cc)) { $mymail->addCc($cc); }
293 if (!empty($bcc)) { $mymail->addBcc($bcc); }
294 if (!empty($to2)) { $mymail->addTo($to2); }
295 $mymail->setTxtBody(wordwrap($txt,72,"\n"));
296 if ($mymail->send()) {
297 $page->trig("Ton mail a bien été envoyé.");
cab08090 298 $_REQUEST = array('bcc' => S::v('bestalias').'@'.$globals->mail->domain);
81b5b746 299 } else {
300 $page->trig("Erreur lors de l'envoi du courriel, réessaye.");
301 }
302 }
303 } else {
cab08090 304 $_REQUEST['bcc'] = S::v('bestalias').'@'.$globals->mail->domain;
81b5b746 305 }
306
08cce2ff 307 $res = XDB::query(
81b5b746 308 "SELECT u.prenom, u.nom, u.promo, a.alias as forlife
309 FROM auth_user_md5 AS u
310 INNER JOIN contacts AS c ON (u.user_id = c.contact)
311 INNER JOIN aliases AS a ON (u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
312 WHERE c.uid = {?}
cab08090 313 ORDER BY u.nom, u.prenom", S::v('uid'));
81b5b746 314 $page->assign('contacts', $res->fetchAllAssoc());
81b5b746 315 }
316
317 function handler_broken(&$page, $warn = null, $email = null)
318 {
319 require_once 'emails.inc.php';
320
321 global $globals;
322
323 $page->changeTpl('emails/broken.tpl');
324
325 if ($warn == 'warn' && $email) {
326 $email = valide_email($email);
327 // vérifications d'usage
08cce2ff 328 $sel = XDB::query(
81b5b746 329 "SELECT e.uid, a.alias
330 FROM emails AS e
331 INNER JOIN auth_user_md5 AS u ON e.uid = u.user_id
332 INNER JOIN aliases AS a ON (e.uid = a.id AND type!='homonyme'
333 AND FIND_IN_SET('bestalias',a.flags))
334 WHERE e.email={?}", $email);
335
336 if (list($uid, $dest) = $sel->fetchOneRow()) {
337 // envoi du mail
338 $message = "Bonjour !
339
340Ce mail a été généré automatiquement par le service de patte cassée de
cab08090 341Polytechnique.org car un autre utilisateur, ".S::v('prenom').' '.S::v('nom').",
81b5b746 342nous a signalé qu'en t'envoyant un mail, il avait reçu un message d'erreur
343indiquant que ton adresse de redirection $email
344ne fonctionnait plus !
345
346Nous te suggérons de vérifier cette adresse, et le cas échéant de mettre
2c388954 347à jour sur le site <{$globals->baseurl}/emails> tes adresses
81b5b746 348de redirection...
349
350Pour plus de rensignements sur le service de patte cassée, n'hésites pas à
2c388954 351consulter la page <{$globals->baseurl}/emails/broken>.
81b5b746 352
353
354A bientôt sur Polytechnique.org !
355L'équipe d'administration <support@polytechnique.org>";
356
1e33266a 357 $mail = new PlMailer();
81b5b746 358 $mail->setFrom('"Polytechnique.org" <support@polytechnique.org>');
359 $mail->addTo("$dest@polytechnique.org");
360 $mail->setSubject("Une de tes adresse de redirection Polytechnique.org ne marche plus !!");
361 $mail->setTxtBody($message);
362 $mail->send();
363 $page->trig("Mail envoyé ! :o)");
364 }
365 } elseif (Post::has('email')) {
5e2307dc 366 $email = valide_email(Post::v('email'));
81b5b746 367
368 list(,$fqdn) = explode('@', $email);
369 $fqdn = strtolower($fqdn);
370 if ($fqdn == 'polytechnique.org' || $fqdn == 'melix.org'
371 || $fqdn == 'm4x.org' || $fqdn == 'melix.net')
372 {
373 $page->assign('neuneu', true);
374 } else {
375 $page->assign('email',$email);
08cce2ff 376 $sel = XDB::query(
81b5b746 377 "SELECT e1.uid, e1.panne != 0 AS panne, count(e2.uid) AS nb_mails,
378 u.nom, u.prenom, u.promo
379 FROM emails as e1
380 LEFT JOIN emails as e2 ON(e1.uid = e2.uid
381 AND FIND_IN_SET('active', e2.flags)
382 AND e1.email != e2.email)
383 INNER JOIN auth_user_md5 as u ON(e1.uid = u.user_id)
384 WHERE e1.email = {?}
385 GROUP BY e1.uid", $email);
386 if ($x = $sel->fetchOneAssoc()) {
387 // on écrit dans la base que l'adresse est cassée
388 if (!$x['panne']) {
74938c82 389 XDB::execute("UPDATE emails
390 SET panne=NOW(),
391 last=NOW(),
392 panne_level = 1
393 WHERE email = {?}", $email);
394 } else {
395 XDB::execute("UPDATE emails
396 SET panne_level = 1
397 WHERE email = {?} AND panne_level = 0");
81b5b746 398 }
399 $page->assign_by_ref('x', $x);
400 }
401 }
402 }
403 }
9e570fe0 404
405 function handler_duplicated(&$page, $action = 'list', $email = null)
406 {
407 $page->changeTpl('emails/duplicated.tpl');
408
409 $states = array('pending' => 'En attente...',
410 'safe' => 'Pas d\'inquiétude',
411 'unsafe' => 'Recherches en cours',
412 'dangerous' => 'Usurpations par cette adresse');
413 $page->assign('states', $states);
414
415 switch (Post::v('action')) {
416 case 'create':
417 if (trim(Post::v('emailN')) != '') {
418 Xdb::execute('INSERT IGNORE INTO emails_watch (email, state, detection, last, uid, description)
419 VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
420 trim(Post::v('emailN')), Post::v('stateN'), S::i('uid'), Post::v('descriptionN'));
421 };
422 break;
423
424 case 'edit':
425 Xdb::execute('UPDATE emails_watch
426 SET state = {?}, last = NOW(), uid = {?}, description = {?}
427 WHERE email = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'), Post::v('emailN'));
428 break;
429
430 default:
431 if ($action == 'delete' && !is_null($email)) {
432 Xdb::execute('DELETE FROM emails_watch WHERE email = {?}', $email);
433 }
434 }
435 if ($action != 'create' && $action != 'edit') {
436 $action = 'list';
437 }
438 $page->assign('action', $action);
439
440 if ($action == 'list') {
441 $sql = "SELECT w.email, w.detection, w.state, a.alias AS forlife
442 FROM emails_watch AS w
ca6d07f4 443 LEFT JOIN emails AS e USING(email)
444 LEFT JOIN aliases AS a ON (a.id = e.uid AND a.type = 'a_vie')
9e570fe0 445 ORDER BY w.state, w.email, a.alias";
446 $it = Xdb::iterRow($sql);
447
448 $table = array();
449 $props = array();
450 while (list($email, $date, $state, $forlife) = $it->next()) {
451 if (count($props) == 0 || $props['mail'] != $email) {
452 if (count($props) > 0) {
453 $table[] = $props;
454 }
455 $props = array('mail' => $email,
456 'detection' => $date,
457 'state' => $state,
458 'users' => array($forlife));
459 } else {
460 $props['users'][] = $forlife;
461 }
462 }
463 if (count($props) > 0) {
464 $table[] = $props;
465 }
466 $page->assign('table', $table);
467 } elseif ($action == 'edit') {
468 $sql = "SELECT w.detection, w.state, w.last, w.description,
469 a1.alias AS edit, a2.alias AS forlife
470 FROM emails_watch AS w
9615c895 471 LEFT JOIN aliases AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie')
472 LEFT JOIN emails AS e ON (w.email = e.email)
ca6d07f4 473 LEFT JOIN aliases AS a2 ON (a2.id = e.uid AND a2.type = 'a_vie')
9e570fe0 474 WHERE w.email = {?}
475 ORDER BY a2.alias";
476 $it = Xdb::iterRow($sql, $email);
477
478 $props = array();
479 while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) {
480 if (count($props) == 0) {
481 $props = array('mail' => $email,
482 'detection' => $detection,
483 'state' => $state,
484 'last' => $last,
485 'description' => $description,
486 'edit' => $edit,
487 'users' => array($forlife));
488 } else {
489 $props['users'][] = $forlife;
490 }
491 }
492 $page->assign('doublon', $props);
493 }
494 }
81b5b746 495}
496
497?>