Adapt text to the sex of the user && Typo.
[platal.git] / modules / email.php
CommitLineData
81b5b746 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
81b5b746 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),
62ce6356 33 'emails/antispam/submit' => $this->make_hook('submit', AUTH_COOKIE),
9c0a8f8c 34 'emails/test' => $this->make_hook('test', AUTH_COOKIE, 'user', NO_AUTH),
9e570fe0 35
2ee43273
FB
36 'emails/imap/in' => $this->make_hook('imap_in', AUTH_PUBLIC),
37
7727ffc7 38 'admin/emails/duplicated' => $this->make_hook('duplicated', AUTH_MDP, 'admin'),
f141945d 39 'admin/emails/watch' => $this->make_hook('duplicated', AUTH_MDP, 'admin'),
5ff3f06b 40 'admin/emails/lost' => $this->make_hook('lost', AUTH_MDP, 'admin'),
81b5b746 41 );
42 }
43
b7582015 44 function handler_emails(&$page, $action = null, $email = null)
81b5b746 45 {
46 global $globals;
e97e9b8f 47 require_once 'emails.inc.php';
81b5b746 48
49 $page->changeTpl('emails/index.tpl');
50 $page->assign('xorg_title','Polytechnique.org - Mes emails');
51
cab08090 52 $uid = S::v('uid');
81b5b746 53
b7582015 54 if ($action == 'best' && $email) {
81b5b746 55 // bestalias is the first bit : 1
56 // there will be maximum 8 bits in flags : 255
08cce2ff 57 XDB::execute("UPDATE aliases SET flags=flags & (255 - 1) WHERE id={?}", $uid);
58 XDB::execute("UPDATE aliases SET flags=flags | 1 WHERE id={?} AND alias={?}",
b7582015 59 $uid, $email);
81b5b746 60 }
61
a7de4ef7 62 // on regarde si on a affaire à un homonyme
81b5b746 63 $sql = "SELECT alias, (type='a_vie') AS a_vie,
64 (alias REGEXP '\\\\.[0-9]{2}$') AS cent_ans,
65 FIND_IN_SET('bestalias',flags) AS best, expire
66 FROM aliases
67 WHERE id = {?} AND type!='homonyme'
68 ORDER BY LENGTH(alias)";
08cce2ff 69 $page->assign('aliases', XDB::iterator($sql, $uid));
81b5b746 70
e97e9b8f
VZ
71 $homonyme = XDB::query("SELECT alias FROM aliases INNER JOIN homonymes ON (id = homonyme_id) WHERE user_id = {?} AND type = 'homonyme'", $uid);
72 $page->assign('homonyme', $homonyme->fetchOneCell());
e1547442 73
aab4661d 74 // Affichage des redirections de l'utilisateur.
e97e9b8f
VZ
75 $redirect = new Redirect($uid);
76 $page->assign('mails', $redirect->active_emails());
81b5b746 77
78 // on regarde si l'utilisateur a un alias et si oui on l'affiche !
cab08090 79 $forlife = S::v('forlife');
08cce2ff 80 $res = XDB::query(
81b5b746 81 "SELECT alias
82 FROM virtual AS v
83 INNER JOIN virtual_redirect AS vr USING(vid)
eaf30d86 84 WHERE (redirect={?} OR redirect={?})
81b5b746 85 AND alias LIKE '%@{$globals->mail->alias_dom}'",
86 $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2);
87 $page->assign('melix', $res->fetchOneCell());
81b5b746 88 }
89
90 function handler_alias(&$page, $action = null, $value = null)
91 {
92 require_once 'validations.inc.php';
93
94 global $globals;
95
96 $page->changeTpl('emails/alias.tpl');
97 $page->assign('xorg_title','Polytechnique.org - Alias melix.net');
98
cab08090 99 $uid = S::v('uid');
100 $forlife = S::v('forlife');
81b5b746 101
102 $page->assign('demande', AliasReq::get_request($uid));
103
a5361008 104 if ($action == 'delete' && $value) {
81b5b746 105 //Suppression d'un alias
08cce2ff 106 XDB::execute(
81b5b746 107 'DELETE virtual, virtual_redirect
108 FROM virtual
109 INNER JOIN virtual_redirect USING (vid)
110 WHERE alias = {?} AND (redirect = {?} OR redirect = {?})', $value,
111 $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2);
112 }
113
a7de4ef7 114 //Récupération des alias éventuellement existants
08cce2ff 115 $res = XDB::query(
81b5b746 116 "SELECT alias, emails_alias_pub
117 FROM auth_user_quick, virtual
118 INNER JOIN virtual_redirect USING(vid)
119 WHERE ( redirect={?} OR redirect= {?} )
eaf30d86 120 AND alias LIKE '%@{$globals->mail->alias_dom}' AND user_id = {?}",
81b5b746 121 $forlife.'@'.$globals->mail->domain,
cab08090 122 $forlife.'@'.$globals->mail->domain2, S::v('uid'));
81b5b746 123 list($alias, $visibility) = $res->fetchOneRow();
124 $page->assign('actuel', $alias);
125
126 if ($action == 'ask' && Env::has('alias') and Env::has('raison')) {
127 //Si l'utilisateur vient de faire une damande
128
5e2307dc 129 $alias = Env::v('alias');
130 $raison = Env::v('raison');
131 $public = (Env::v('public', 'off') == 'on')?"public":"private";
81b5b746 132
133 $page->assign('r_alias', $alias);
134 $page->assign('r_raison', $raison);
135 if ($public == 'public') {
136 $page->assign('r_public', true);
137 }
138
a7de4ef7 139 //Quelques vérifications sur l'alias (caractères spéciaux)
81b5b746 140 if (!preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $alias)) {
a7de4ef7 141 $page->trig("L'adresse demandée n'est pas valide.
142 Vérifie qu'elle comporte entre 3 et 20 caractères
143 et qu'elle ne contient que des lettres non accentuées,
144 des chiffres ou les caractères - et .");
fd8f77de 145 return;
81b5b746 146 } else {
a7de4ef7 147 //vérifier que l'alias n'est pas déja pris
08cce2ff 148 $res = XDB::query('SELECT COUNT(*) FROM virtual WHERE alias={?}',
81b5b746 149 $alias.'@'.$globals->mail->alias_dom);
150 if ($res->fetchOneCell() > 0) {
a7de4ef7 151 $page->trig("L'alias $alias@{$globals->mail->alias_dom} a déja été attribué.
81b5b746 152 Tu ne peux donc pas l'obtenir.");
fd8f77de 153 return;
81b5b746 154 }
155
a7de4ef7 156 //vérifier que l'alias n'est pas déja en demande
81b5b746 157 $it = new ValidateIterator ();
158 while($req = $it->next()) {
1f07f6c2 159 if ($req->type == "alias" and $req->alias == $alias . '@' . $globals->mail->alias_dom) {
a7de4ef7 160 $page->trig("L'alias $alias@{$globals->mail->alias_dom} a déja été demandé.
81b5b746 161 Tu ne peux donc pas l'obtenir pour l'instant.");
fd8f77de 162 return ;
81b5b746 163 }
164 }
165
a7de4ef7 166 //Insertion de la demande dans la base, écrase les requêtes précédente
81b5b746 167 $myalias = new AliasReq($uid, $alias, $raison, $public);
168 $myalias->submit();
169 $page->assign('success',$alias);
fd8f77de 170 return;
81b5b746 171 }
172 }
173 elseif ($action == 'set'
174 && ($value == 'public' || $value == 'private'))
175 {
176 if ($value == 'public') {
08cce2ff 177 XDB::execute("UPDATE auth_user_quick SET emails_alias_pub = 'public'
cab08090 178 WHERE user_id = {?}", S::v('uid'));
81b5b746 179 } else {
08cce2ff 180 XDB::execute("UPDATE auth_user_quick SET emails_alias_pub = 'private'
cab08090 181 WHERE user_id = {?}", S::v('uid'));
81b5b746 182 }
183
184 $visibility = $value;
185 }
186
187 $page->assign('mail_public', ($visibility == 'public'));
81b5b746 188 }
189
190 function handler_redirect(&$page, $action = null, $email = null)
191 {
192 global $globals;
193
194 require_once 'emails.inc.php';
195
196 $page->changeTpl('emails/redirect.tpl');
197
cab08090 198 $uid = S::v('uid');
199 $forlife = S::v('forlife');
81b5b746 200
3b83212e 201 $page->assign('eleve', S::i('promo') >= date("Y") - 5);
202
cab08090 203 $redirect = new Redirect(S::v('uid'));
81b5b746 204
6d6b65bf 205 // FS#703 : $_GET is urldecoded twice, hence
206 // + (the data) => %2B (in the url) => + (first decoding) => ' ' (second decoding)
207 // Since there can be no spaces in emails, we can fix this with :
208 $email = str_replace(' ', '+', $email);
209
81b5b746 210 if ($action == 'remove' && $email) {
b7582015 211 $retour = $redirect->delete_email($email);
212 $page->assign('retour', $retour);
81b5b746 213 }
e1547442
FB
214
215 if ($action == 'active' && $email) {
216 $redirect->modify_one_email($email, true);
217 }
218
219 if ($action == 'inactive' && $email) {
220 $redirect->modify_one_email($email, false);
221 }
222
223 if ($action == 'rewrite' && $email) {
224 $rewrite = @func_get_arg(3);
225 $redirect->modify_one_email_redirect($email, $rewrite);
226 }
227
81b5b746 228 if (Env::has('emailop')) {
5e2307dc 229 $actifs = Env::v('emails_actifs', Array());
b7582015 230 print_r(Env::v('emails_rewrite'));
5e2307dc 231 if (Env::v('emailop') == "ajouter" && Env::has('email')) {
232 $page->assign('retour', $redirect->add_email(Env::v('email')));
81b5b746 233 } elseif (empty($actifs)) {
234 $page->assign('retour', ERROR_INACTIVE_REDIRECTION);
235 } elseif (is_array($actifs)) {
236 $page->assign('retour', $redirect->modify_email($actifs,
5e2307dc 237 Env::v('emails_rewrite',Array())));
81b5b746 238 }
239 }
240
08cce2ff 241 $res = XDB::query(
81b5b746 242 "SELECT alias
243 FROM virtual
244 INNER JOIN virtual_redirect USING(vid)
245 WHERE (redirect={?} OR redirect={?})
eaf30d86 246 AND alias LIKE '%@{$globals->mail->alias_dom}'",
81b5b746 247 $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2);
248 $melix = $res->fetchOneCell();
249 if ($melix) {
250 list($melix) = explode('@', $melix);
251 $page->assign('melix',$melix);
252 }
253
08cce2ff 254 $res = XDB::query(
81b5b746 255 "SELECT alias,expire
256 FROM aliases
257 WHERE id={?} AND (type='a_vie' OR type='alias')
258 ORDER BY !FIND_IN_SET('usage',flags), LENGTH(alias)", $uid);
bb0727ea 259
81b5b746 260 $page->assign('alias', $res->fetchAllAssoc());
261 $page->assign('emails',$redirect->emails);
aab4661d 262
f5c4bf30
VZ
263 require_once 'googleapps.inc.php';
264 $page->assign('googleapps', GoogleAppsAccount::account_status($uid));
81b5b746 265 }
266
b7582015 267 function handler_antispam(&$page, $statut_filtre = null)
81b5b746 268 {
269 require_once 'emails.inc.php';
841cfb97 270 require_once('wiki.inc.php');
271 wiki_require_page('Xorg.Antispam');
81b5b746 272
273 $page->changeTpl('emails/antispam.tpl');
274
cab08090 275 $bogo = new Bogo(S::v('uid'));
b7582015 276 if (isset($statut_filtre)) {
11ed26e5 277 $bogo->change($statut_filtre + 0);
81b5b746 278 }
279 $page->assign('filtre',$bogo->level());
81b5b746 280 }
281
62ce6356 282 function handler_submit(&$page)
283 {
4b9ba601 284 require_once('wiki.inc.php');
285 wiki_require_page('Xorg.Mails');
62ce6356 286 $page->changeTpl('emails/submit_spam.tpl');
287
288 if (Post::has('send_email')) {
15603084 289 $upload = PlUpload::get($_FILES['mail'], S::v('forlife'), 'spam.submit', true);
290 if (!$upload) {
a7de4ef7 291 $page->trig('Une erreur a été rencontrée lors du transfert du fichier');
62ce6356 292 return;
293 }
15603084 294 $mime = $upload->contentType();
62ce6356 295 if ($mime != 'text/x-mail' && $mime != 'message/rfc822') {
15603084 296 $upload->clear();
62ce6356 297 $page->trig('Le fichier ne contient pas un mail complet');
298 return;
299 }
300 global $globals;
301 $box = Post::v('type') . '@' . $globals->mail->domain;
302 $mailer = new PlMailer();
303 $mailer->addTo($box);
304 $mailer->setFrom('"' . S::v('prenom') . ' ' . S::v('nom') . '" <web@' . $globals->mail->domain . '>');
305 $mailer->setTxtBody(Post::v('type') . ' soumis par ' . S::v('forlife') . ' via le web');
15603084 306 $mailer->addUploadAttachment($upload, Post::v('type') . '.mail');
62ce6356 307 $mailer->send();
a7de4ef7 308 $page->trig('Le message a été transmis à ' . $box);
15603084 309 $upload->clear();
62ce6356 310 }
311 }
312
81b5b746 313 function handler_send(&$page)
314 {
315 global $globals;
81b5b746 316 $page->changeTpl('emails/send.tpl');
da419622 317 $page->addJsLink('ajax.js');
81b5b746 318
319 $page->assign('xorg_title','Polytechnique.org - Envoyer un email');
320
321 // action si on recoit un formulaire
3360c04f
FB
322 if (Post::has('save')) {
323 unset($_POST['save']);
324 if (trim(preg_replace('/-- .*/', '', Post::v('contenu'))) != "") {
325 $_POST['to_contacts'] = explode(';', @$_POST['to_contacts']);
326 $_POST['cc_contacts'] = explode(';', @$_POST['cc_contacts']);
327 $data = serialize($_POST);
328 XDB::execute("REPLACE INTO email_send_save
329 VALUES ({?}, {?})", S::i('uid'), $data);
330 }
331 exit;
332 } else if (Env::v('submit') == 'Envoyer') {
91a14f73 333 function getEmails($aliases)
334 {
335 if (!is_array($aliases)) {
336 return null;
337 }
338 $rel = Env::v('contacts');
339 $ret = array();
340 foreach ($aliases as $alias) {
341 $ret[$alias] = $rel[$alias];
342 }
343 return join(', ', $ret);
344 }
345
5e68682e 346 $error = false;
da419622 347 foreach ($_FILES as &$file) {
f3fb8eda 348 if ($file['name'] && !PlUpload::get($file, S::v('forlife'), 'emails.send', false)) {
5e68682e
FB
349 $page->trig(PlUpload::$lastError);
350 $error = true;
351 break;
da419622 352 }
353 }
354
5e68682e
FB
355 if (!$error) {
356 XDB::execute("DELETE FROM email_send_save
357 WHERE uid = {?}", S::i('uid'));
358
359 $to2 = getEmails(Env::v('to_contacts'));
360 $cc2 = getEmails(Env::v('cc_contacts'));
361 $txt = str_replace('^M', '', Env::v('contenu'));
362 $to = Env::v('to');
363 $subj = Env::v('sujet');
364 $from = Env::v('from');
365 $cc = trim(Env::v('cc'));
366 $bcc = trim(Env::v('bcc'));
367
368 if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) {
369 $page->trig("Indique au moins un destinataire.");
da419622 370 $page->assign('uploaded_f', PlUpload::listFilenames(S::v('forlife'), 'emails.send'));
5e68682e
FB
371 } else {
372 $mymail = new PlMailer();
373 $mymail->setFrom($from);
374 $mymail->setSubject($subj);
375 if (!empty($to)) { $mymail->addTo($to); }
376 if (!empty($cc)) { $mymail->addCc($cc); }
377 if (!empty($bcc)) { $mymail->addBcc($bcc); }
378 if (!empty($to2)) { $mymail->addTo($to2); }
379 if (!empty($cc2)) { $mymail->addCc($cc2); }
380 $files =& PlUpload::listFiles(S::v('forlife'), 'emails.send');
381 foreach ($files as $name=>&$upload) {
382 $mymail->addUploadAttachment($upload, $name);
383 }
384 if (Env::v('nowiki')) {
385 $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
386 } else {
387 $mymail->setWikiBody($txt);
388 }
389 if ($mymail->send()) {
390 $page->trig("Ton mail a bien été envoyé.");
391 $_REQUEST = array('bcc' => S::v('bestalias').'@'.$globals->mail->domain);
392 PlUpload::clear(S::v('forlife'), 'emails.send');
393 } else {
394 $page->trig("Erreur lors de l'envoi du courriel, réessaye.");
395 $page->assign('uploaded_f', PlUpload::listFilenames(S::v('forlife'), 'emails.send'));
396 }
81b5b746 397 }
398 }
399 } else {
3360c04f
FB
400 $res = XDB::query("SELECT data
401 FROM email_send_save
402 WHERE uid = {?}", S::i('uid'));
403 if ($res->numRows() == 0) {
404 PlUpload::clear(S::v('forlife'), 'emails.send');
405 $_REQUEST['bcc'] = S::v('bestalias').'@'.$globals->mail->domain;
406 } else {
407 $data = unserialize($res->fetchOneCell());
408 $_REQUEST = array_merge($_REQUEST, $data);
409 }
81b5b746 410 }
411
08cce2ff 412 $res = XDB::query(
81b5b746 413 "SELECT u.prenom, u.nom, u.promo, a.alias as forlife
414 FROM auth_user_md5 AS u
415 INNER JOIN contacts AS c ON (u.user_id = c.contact)
416 INNER JOIN aliases AS a ON (u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
417 WHERE c.uid = {?}
cab08090 418 ORDER BY u.nom, u.prenom", S::v('uid'));
81b5b746 419 $page->assign('contacts', $res->fetchAllAssoc());
5e68682e 420 $page->assign('maxsize', ini_get('upload_max_filesize') . 'o');
81b5b746 421 }
422
5ff3f06b
FB
423 function handler_test(&$page, $forlife = null)
424 {
425 global $globals;
e4ecada4
VZ
426 require_once 'emails.inc.php';
427
5ff3f06b
FB
428 if (!S::has_perms() || !$forlife) {
429 $forlife = S::v('bestalias');
430 }
e4ecada4
VZ
431
432 $res = XDB::query("SELECT FIND_IN_SET('femme', u.flags), prenom, user_id
5ff3f06b
FB
433 FROM auth_user_md5 AS u
434 INNER JOIN aliases AS a ON (a.id = u.user_id)
435 WHERE a.alias = {?}", $forlife);
e4ecada4
VZ
436 list($sexe, $prenom, $uid) = $res->fetchOneRow();
437 $redirect = new Redirect($uid);
438
439 $mailer = new PlMailer('emails/test.mail.tpl');
440 $mailer->assign('email', $forlife . '@' . $globals->mail->domain);
441 $mailer->assign('redirects', $redirect->active_emails());
5ff3f06b
FB
442 $mailer->assign('sexe', $sexe);
443 $mailer->assign('prenom', $prenom);
444 $mailer->send();
445 exit;
446 }
447
2ee43273
FB
448 function handler_imap_in(&$page, $hash = null, $login = null)
449 {
450 $page->changeTpl('emails/imap_register.tpl');
451 $id = null;
452 if (!empty($hash) || !empty($login)) {
e2cea47d 453 $req = XDB::query("SELECT u.prenom, FIND_IN_SET('femme', u.flags) AS sexe, a.id
2ee43273
FB
454 FROM aliases AS a
455 INNER JOIN newsletter_ins AS ni ON (a.id = ni.user_id)
456 INNER JOIN auth_user_md5 AS u ON (u.user_id = a.id)
457 WHERE a.alias = {?} AND ni.hash = {?}", $login, $hash);
e2cea47d 458 list($prenom, $sexe, $id) = $req->fetchOneRow();
2ee43273
FB
459 }
460
461 require_once('emails.inc.php');
462 $page->assign('ok', false);
463 if (S::logged() && (is_null($id) || $id == S::i('uid'))) {
464 $storage = new EmailStorage(S::i('uid'), 'imap');
465 $storage->activate();
466 $page->assign('ok', true);
467 $page->assign('prenom', S::v('prenom'));
e2cea47d 468 $page->assign('sexe', S::v('femme'));
2ee43273
FB
469 } else if (!S::logged() && $id) {
470 $storage = new EmailStorage($id, 'imap');
471 $storage->activate();
472 $page->assign('ok', true);
473 $page->assign('prenom', $prenom);
e2cea47d 474 $page->assign('sexe', $sexe);
2ee43273
FB
475 }
476 }
477
81b5b746 478 function handler_broken(&$page, $warn = null, $email = null)
479 {
480 require_once 'emails.inc.php';
4b9ba601 481 require_once('wiki.inc.php');
a7de4ef7 482 wiki_require_page('Xorg.PatteCassée');
81b5b746 483
484 global $globals;
485
486 $page->changeTpl('emails/broken.tpl');
487
488 if ($warn == 'warn' && $email) {
489 $email = valide_email($email);
a7de4ef7 490 // vérifications d'usage
08cce2ff 491 $sel = XDB::query(
81b5b746 492 "SELECT e.uid, a.alias
493 FROM emails AS e
494 INNER JOIN auth_user_md5 AS u ON e.uid = u.user_id
495 INNER JOIN aliases AS a ON (e.uid = a.id AND type!='homonyme'
496 AND FIND_IN_SET('bestalias',a.flags))
497 WHERE e.email={?}", $email);
498
499 if (list($uid, $dest) = $sel->fetchOneRow()) {
500 // envoi du mail
501 $message = "Bonjour !
502
a7de4ef7 503Ce mail a été généré automatiquement par le service de patte cassée de
cab08090 504Polytechnique.org car un autre utilisateur, ".S::v('prenom').' '.S::v('nom').",
a7de4ef7 505nous a signalé qu'en t'envoyant un mail, il avait reçu un message d'erreur
81b5b746 506indiquant que ton adresse de redirection $email
507ne fonctionnait plus !
508
a7de4ef7 509Nous te suggérons de vérifier cette adresse, et le cas échéant de mettre
510à jour sur le site <{$globals->baseurl}/emails> tes adresses
81b5b746 511de redirection...
512
a7de4ef7 513Pour plus de rensignements sur le service de patte cassée, n'hésites pas à
2c388954 514consulter la page <{$globals->baseurl}/emails/broken>.
81b5b746 515
516
a7de4ef7 517A bientôt sur Polytechnique.org !
1d55fe45 518L'équipe d'administration <support@" . $globals->mail->domain . '>';
81b5b746 519
1e33266a 520 $mail = new PlMailer();
1d55fe45 521 $mail->setFrom('"Polytechnique.org" <support@' . $globals->mail->domain . '>');
522 $mail->addTo("$dest@" . $globals->mail->domain);
81b5b746 523 $mail->setSubject("Une de tes adresse de redirection Polytechnique.org ne marche plus !!");
524 $mail->setTxtBody($message);
525 $mail->send();
a7de4ef7 526 $page->trig("Mail envoyé ! :o)");
81b5b746 527 }
528 } elseif (Post::has('email')) {
5e2307dc 529 $email = valide_email(Post::v('email'));
81b5b746 530
531 list(,$fqdn) = explode('@', $email);
532 $fqdn = strtolower($fqdn);
533 if ($fqdn == 'polytechnique.org' || $fqdn == 'melix.org'
534 || $fqdn == 'm4x.org' || $fqdn == 'melix.net')
535 {
536 $page->assign('neuneu', true);
537 } else {
538 $page->assign('email',$email);
08cce2ff 539 $sel = XDB::query(
81b5b746 540 "SELECT e1.uid, e1.panne != 0 AS panne, count(e2.uid) AS nb_mails,
4b9ba601 541 u.nom, u.prenom, u.promo, a.alias AS forlife
81b5b746 542 FROM emails as e1
eaf30d86 543 LEFT JOIN emails as e2 ON(e1.uid = e2.uid
81b5b746 544 AND FIND_IN_SET('active', e2.flags)
545 AND e1.email != e2.email)
546 INNER JOIN auth_user_md5 as u ON(e1.uid = u.user_id)
4b9ba601 547 INNER JOIN aliases AS a ON (a.id = e1.uid AND a.type = 'a_vie')
81b5b746 548 WHERE e1.email = {?}
549 GROUP BY e1.uid", $email);
550 if ($x = $sel->fetchOneAssoc()) {
a7de4ef7 551 // on écrit dans la base que l'adresse est cassée
81b5b746 552 if (!$x['panne']) {
74938c82 553 XDB::execute("UPDATE emails
554 SET panne=NOW(),
555 last=NOW(),
556 panne_level = 1
557 WHERE email = {?}", $email);
558 } else {
559 XDB::execute("UPDATE emails
560 SET panne_level = 1
4b9ba601 561 WHERE email = {?} AND panne_level = 0", $email);
81b5b746 562 }
563 $page->assign_by_ref('x', $x);
564 }
565 }
566 }
567 }
9e570fe0 568
569 function handler_duplicated(&$page, $action = 'list', $email = null)
570 {
571 $page->changeTpl('emails/duplicated.tpl');
572
573 $states = array('pending' => 'En attente...',
a7de4ef7 574 'safe' => 'Pas d\'inquiétude',
9e570fe0 575 'unsafe' => 'Recherches en cours',
576 'dangerous' => 'Usurpations par cette adresse');
577 $page->assign('states', $states);
578
579 switch (Post::v('action')) {
580 case 'create':
581 if (trim(Post::v('emailN')) != '') {
582 Xdb::execute('INSERT IGNORE INTO emails_watch (email, state, detection, last, uid, description)
583 VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
584 trim(Post::v('emailN')), Post::v('stateN'), S::i('uid'), Post::v('descriptionN'));
585 };
586 break;
587
588 case 'edit':
589 Xdb::execute('UPDATE emails_watch
590 SET state = {?}, last = NOW(), uid = {?}, description = {?}
591 WHERE email = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'), Post::v('emailN'));
592 break;
593
594 default:
595 if ($action == 'delete' && !is_null($email)) {
596 Xdb::execute('DELETE FROM emails_watch WHERE email = {?}', $email);
597 }
598 }
599 if ($action != 'create' && $action != 'edit') {
600 $action = 'list';
601 }
602 $page->assign('action', $action);
603
604 if ($action == 'list') {
605 $sql = "SELECT w.email, w.detection, w.state, a.alias AS forlife
606 FROM emails_watch AS w
ca6d07f4 607 LEFT JOIN emails AS e USING(email)
608 LEFT JOIN aliases AS a ON (a.id = e.uid AND a.type = 'a_vie')
9e570fe0 609 ORDER BY w.state, w.email, a.alias";
610 $it = Xdb::iterRow($sql);
611
612 $table = array();
613 $props = array();
614 while (list($email, $date, $state, $forlife) = $it->next()) {
615 if (count($props) == 0 || $props['mail'] != $email) {
616 if (count($props) > 0) {
617 $table[] = $props;
618 }
619 $props = array('mail' => $email,
620 'detection' => $date,
621 'state' => $state,
622 'users' => array($forlife));
623 } else {
624 $props['users'][] = $forlife;
625 }
626 }
627 if (count($props) > 0) {
628 $table[] = $props;
629 }
630 $page->assign('table', $table);
631 } elseif ($action == 'edit') {
632 $sql = "SELECT w.detection, w.state, w.last, w.description,
633 a1.alias AS edit, a2.alias AS forlife
634 FROM emails_watch AS w
eaf30d86 635 LEFT JOIN aliases AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie')
9615c895 636 LEFT JOIN emails AS e ON (w.email = e.email)
ca6d07f4 637 LEFT JOIN aliases AS a2 ON (a2.id = e.uid AND a2.type = 'a_vie')
9e570fe0 638 WHERE w.email = {?}
639 ORDER BY a2.alias";
640 $it = Xdb::iterRow($sql, $email);
641
642 $props = array();
643 while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) {
644 if (count($props) == 0) {
645 $props = array('mail' => $email,
646 'detection' => $detection,
647 'state' => $state,
648 'last' => $last,
649 'description' => $description,
650 'edit' => $edit,
651 'users' => array($forlife));
652 } else {
653 $props['users'][] = $forlife;
654 }
655 }
656 $page->assign('doublon', $props);
657 }
658 }
7727ffc7 659 function handler_lost(&$page, $action = 'list', $email = null)
660 {
661 $page->changeTpl('emails/lost.tpl');
eaf30d86 662
7727ffc7 663 $page->assign('lost_emails', XDB::iterator('
0e5ec860
VZ
664 SELECT u.user_id, a.alias
665 FROM auth_user_md5 AS u
666 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = "a_vie")
667 LEFT JOIN emails AS e ON (u.user_id=e.uid AND FIND_IN_SET("active",e.flags))
668 WHERE e.uid IS NULL AND
669 FIND_IN_SET("googleapps", u.mail_storage) = 0 AND
670 u.deces = 0
671 ORDER BY u.promo DESC, u.nom, u.prenom'));
7727ffc7 672 }
81b5b746 673}
674
a7de4ef7 675// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
81b5b746 676?>