2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class EmailModule
extends PLModule
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
),
33 'emails/antispam/submit' => $this->make_hook('submit', AUTH_COOKIE
),
34 'emails/test' => $this->make_hook('test', AUTH_COOKIE
, 'user', NO_AUTH
),
36 'admin/emails/duplicated' => $this->make_hook('duplicated', AUTH_MDP
, 'admin'),
37 'admin/emails/watch' => $this->make_hook('duplicated', AUTH_MDP
, 'admin'),
38 'admin/emails/lost' => $this->make_hook('lost', AUTH_MDP
, 'admin'),
42 function handler_emails(&$page, $action = null
, $email = null
)
46 $page->changeTpl('emails/index.tpl');
47 $page->assign('xorg_title','Polytechnique.org - Mes emails');
51 if ($action == 'best' && $email) {
52 // bestalias is the first bit : 1
53 // there will be maximum 8 bits in flags : 255
54 XDB
::execute("UPDATE aliases SET flags=flags & (255 - 1) WHERE id={?}", $uid);
55 XDB
::execute("UPDATE aliases SET flags=flags | 1 WHERE id={?} AND alias={?}",
59 // on regarde si on a affaire à un homonyme
60 $sql = "SELECT alias, (type='a_vie') AS a_vie,
61 (alias REGEXP '\\\\.[0-9]{2}$') AS cent_ans,
62 FIND_IN_SET('bestalias',flags) AS best, expire
64 WHERE id = {?} AND type!='homonyme'
65 ORDER BY LENGTH(alias)";
66 $page->assign('aliases', XDB
::iterator($sql, $uid));
68 $homonyme = XDB
::query("SELECT alias FROM aliases INNER JOIN homonymes ON (id = homonyme_id) WHERE user_id = {?} AND type = 'homonyme'", $uid);
69 $page->assign('homonyme', $homonyme->fetchOneCell());
73 WHERE uid = {?} AND FIND_IN_SET('active', flags)";
74 $page->assign('mails', XDB
::iterator($sql, $uid));
77 // on regarde si l'utilisateur a un alias et si oui on l'affiche !
78 $forlife = S
::v('forlife');
82 INNER JOIN virtual_redirect AS vr USING(vid)
83 WHERE (redirect={?} OR redirect={?})
84 AND alias LIKE '%@{$globals->mail->alias_dom}'",
85 $forlife.'@'.$globals->mail
->domain
, $forlife.'@'.$globals->mail
->domain2
);
86 $page->assign('melix', $res->fetchOneCell());
89 function handler_alias(&$page, $action = null
, $value = null
)
91 require_once 'validations.inc.php';
95 $page->changeTpl('emails/alias.tpl');
96 $page->assign('xorg_title','Polytechnique.org - Alias melix.net');
99 $forlife = S
::v('forlife');
101 $page->assign('demande', AliasReq
::get_request($uid));
103 if ($action == 'delete' && $value) {
104 //Suppression d'un alias
106 'DELETE virtual, virtual_redirect
108 INNER JOIN virtual_redirect USING (vid)
109 WHERE alias = {?} AND (redirect = {?} OR redirect = {?})', $value,
110 $forlife.'@'.$globals->mail
->domain
, $forlife.'@'.$globals->mail
->domain2
);
113 //Récupération des alias éventuellement existants
115 "SELECT alias, emails_alias_pub
116 FROM auth_user_quick, virtual
117 INNER JOIN virtual_redirect USING(vid)
118 WHERE ( redirect={?} OR redirect= {?} )
119 AND alias LIKE '%@{$globals->mail->alias_dom}' AND user_id = {?}",
120 $forlife.'@'.$globals->mail
->domain
,
121 $forlife.'@'.$globals->mail
->domain2
, S
::v('uid'));
122 list($alias, $visibility) = $res->fetchOneRow();
123 $page->assign('actuel', $alias);
125 if ($action == 'ask' && Env
::has('alias') and Env
::has('raison')) {
126 //Si l'utilisateur vient de faire une damande
128 $alias = Env
::v('alias');
129 $raison = Env
::v('raison');
130 $public = (Env
::v('public', 'off') == 'on')?
"public":"private";
132 $page->assign('r_alias', $alias);
133 $page->assign('r_raison', $raison);
134 if ($public == 'public') {
135 $page->assign('r_public', true
);
138 //Quelques vérifications sur l'alias (caractères spéciaux)
139 if (!preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $alias)) {
140 $page->trig("L'adresse demandée n'est pas valide.
141 Vérifie qu'elle comporte entre 3 et 20 caractères
142 et qu'elle ne contient que des lettres non accentuées,
143 des chiffres ou les caractères - et .");
146 //vérifier que l'alias n'est pas déja pris
147 $res = XDB
::query('SELECT COUNT(*) FROM virtual WHERE alias={?}',
148 $alias.'@'.$globals->mail
->alias_dom
);
149 if ($res->fetchOneCell() > 0) {
150 $page->trig("L'alias $alias@{$globals->mail->alias_dom} a déja été attribué.
151 Tu ne peux donc pas l'obtenir.");
155 //vérifier que l'alias n'est pas déja en demande
156 $it = new ValidateIterator ();
157 while($req = $it->next()) {
158 if ($req->type
== "alias" and $req->alias
== $alias . '@' . $globals->mail
->alias_dom
) {
159 $page->trig("L'alias $alias@{$globals->mail->alias_dom} a déja été demandé.
160 Tu ne peux donc pas l'obtenir pour l'instant.");
165 //Insertion de la demande dans la base, écrase les requêtes précédente
166 $myalias = new AliasReq($uid, $alias, $raison, $public);
168 $page->assign('success',$alias);
172 elseif ($action == 'set'
173 && ($value == 'public' ||
$value == 'private'))
175 if ($value == 'public') {
176 XDB
::execute("UPDATE auth_user_quick SET emails_alias_pub = 'public'
177 WHERE user_id = {?}", S
::v('uid'));
179 XDB
::execute("UPDATE auth_user_quick SET emails_alias_pub = 'private'
180 WHERE user_id = {?}", S
::v('uid'));
183 $visibility = $value;
186 $page->assign('mail_public', ($visibility == 'public'));
189 function handler_redirect(&$page, $action = null
, $email = null
)
193 require_once 'emails.inc.php';
195 $page->changeTpl('emails/redirect.tpl');
198 $forlife = S
::v('forlife');
200 $page->assign('eleve', S
::i('promo') >= date("Y") - 5);
202 $redirect = new Redirect(S
::v('uid'));
204 // FS#703 : $_GET is urldecoded twice, hence
205 // + (the data) => %2B (in the url) => + (first decoding) => ' ' (second decoding)
206 // Since there can be no spaces in emails, we can fix this with :
207 $email = str_replace(' ', '+', $email);
209 if ($action == 'remove' && $email) {
210 $retour = $redirect->delete_email($email);
211 $page->assign('retour', $retour);
214 if ($action == 'active' && $email) {
215 $redirect->modify_one_email($email, true
);
218 if ($action == 'inactive' && $email) {
219 $redirect->modify_one_email($email, false
);
222 if ($action == 'rewrite' && $email) {
223 $rewrite = @func_get_arg
(3);
224 $redirect->modify_one_email_redirect($email, $rewrite);
227 if (Env
::has('emailop')) {
228 $actifs = Env
::v('emails_actifs', Array());
229 print_r(Env
::v('emails_rewrite'));
230 if (Env
::v('emailop') == "ajouter" && Env
::has('email')) {
231 $page->assign('retour', $redirect->add_email(Env
::v('email')));
232 } elseif (empty($actifs)) {
233 $page->assign('retour', ERROR_INACTIVE_REDIRECTION
);
234 } elseif (is_array($actifs)) {
235 $page->assign('retour', $redirect->modify_email($actifs,
236 Env
::v('emails_rewrite',Array())));
243 INNER JOIN virtual_redirect USING(vid)
244 WHERE (redirect={?} OR redirect={?})
245 AND alias LIKE '%@{$globals->mail->alias_dom}'",
246 $forlife.'@'.$globals->mail
->domain
, $forlife.'@'.$globals->mail
->domain2
);
247 $melix = $res->fetchOneCell();
249 list($melix) = explode('@', $melix);
250 $page->assign('melix',$melix);
256 WHERE id={?} AND (type='a_vie' OR type='alias')
257 ORDER BY !FIND_IN_SET('usage',flags), LENGTH(alias)", $uid);
259 $page->assign('alias', $res->fetchAllAssoc());
260 $page->assign('emails',$redirect->emails
);
263 function handler_antispam(&$page, $statut_filtre = null
)
265 require_once 'emails.inc.php';
266 require_once('wiki.inc.php');
267 wiki_require_page('Xorg.Antispam');
269 $page->changeTpl('emails/antispam.tpl');
271 $bogo = new Bogo(S
::v('uid'));
272 if (isset($statut_filtre)) {
273 $bogo->change(S
::v('uid'), $statut_filtre +
0);
275 $page->assign('filtre',$bogo->level());
278 function handler_submit(&$page)
280 require_once('wiki.inc.php');
281 wiki_require_page('Xorg.Mails');
282 $page->changeTpl('emails/submit_spam.tpl');
284 if (Post
::has('send_email')) {
285 $upload = PlUpload
::get($_FILES['mail'], S
::v('forlife'), 'spam.submit', true
);
287 $page->trig('Une erreur a été rencontrée lors du transfert du fichier');
290 $mime = $upload->contentType();
291 if ($mime != 'text/x-mail' && $mime != 'message/rfc822') {
293 $page->trig('Le fichier ne contient pas un mail complet');
297 $box = Post
::v('type') . '@' . $globals->mail
->domain
;
298 $mailer = new PlMailer();
299 $mailer->addTo($box);
300 $mailer->setFrom('"' . S
::v('prenom') . ' ' . S
::v('nom') . '" <web@' . $globals->mail
->domain
. '>');
301 $mailer->setTxtBody(Post
::v('type') . ' soumis par ' . S
::v('forlife') . ' via le web');
302 $mailer->addUploadAttachment($upload, Post
::v('type') . '.mail');
304 $page->trig('Le message a été transmis à ' . $box);
309 function handler_send(&$page)
312 $page->changeTpl('emails/send.tpl');
313 $page->addJsLink('ajax.js');
315 $page->assign('xorg_title','Polytechnique.org - Envoyer un email');
317 // action si on recoit un formulaire
318 if (Post
::has('save')) {
319 unset($_POST['save']);
320 if (trim(preg_replace('/-- .*/', '', Post
::v('contenu'))) != "") {
321 $_POST['to_contacts'] = explode(';', @$_POST['to_contacts']);
322 $_POST['cc_contacts'] = explode(';', @$_POST['cc_contacts']);
323 $data = serialize($_POST);
324 XDB
::execute("REPLACE INTO email_send_save
325 VALUES ({?}, {?})", S
::i('uid'), $data);
328 } else if (Env
::v('submit') == 'Envoyer') {
329 function getEmails($aliases)
331 if (!is_array($aliases)) {
334 $rel = Env
::v('contacts');
336 foreach ($aliases as $alias) {
337 $ret[$alias] = $rel[$alias];
339 return join(', ', $ret);
342 foreach ($_FILES as &$file) {
343 if ($file['name'] && !PlUpload
::get($file, S
::v('forlife'), 'emails.send', false
)) {
344 $page->trig("Impossible de télécharger '" . pl_entities($file['name']) . "'");
348 XDB
::execute("DELETE FROM email_send_save
349 WHERE uid = {?}", S
::i('uid'));
351 $to2 = getEmails(Env
::v('to_contacts'));
352 $cc2 = getEmails(Env
::v('cc_contacts'));
353 $txt = str_replace('^M', '', Env
::v('contenu'));
355 $subj = Env
::v('sujet');
356 $from = Env
::v('from');
357 $cc = trim(Env
::v('cc'));
358 $bcc = trim(Env
::v('bcc'));
360 if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) {
361 $page->trig("Indique au moins un destinataire.");
362 $page->assign('uploaded_f', PlUpload
::listFilenames(S
::v('forlife'), 'emails.send'));
364 $mymail = new PlMailer();
365 $mymail->setFrom($from);
366 $mymail->setSubject($subj);
367 if (!empty($to)) { $mymail->addTo($to); }
368 if (!empty($cc)) { $mymail->addCc($cc); }
369 if (!empty($bcc)) { $mymail->addBcc($bcc); }
370 if (!empty($to2)) { $mymail->addTo($to2); }
371 if (!empty($cc2)) { $mymail->addCc($cc2); }
372 $files =& PlUpload
::listFiles(S
::v('forlife'), 'emails.send');
373 foreach ($files as $name=>&$upload) {
374 $mymail->addUploadAttachment($upload, $name);
376 if (Env
::v('nowiki')) {
377 $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
379 $mymail->setWikiBody($txt);
381 if ($mymail->send()) {
382 $page->trig("Ton mail a bien été envoyé.");
383 $_REQUEST = array('bcc' => S
::v('bestalias').'@'.$globals->mail
->domain
);
384 PlUpload
::clear(S
::v('forlife'), 'emails.send');
386 $page->trig("Erreur lors de l'envoi du courriel, réessaye.");
387 $page->assign('uploaded_f', PlUpload
::listFilenames(S
::v('forlife'), 'emails.send'));
391 $res = XDB
::query("SELECT data
393 WHERE uid = {?}", S
::i('uid'));
394 if ($res->numRows() == 0) {
395 PlUpload
::clear(S
::v('forlife'), 'emails.send');
396 $_REQUEST['bcc'] = S
::v('bestalias').'@'.$globals->mail
->domain
;
398 $data = unserialize($res->fetchOneCell());
399 $_REQUEST = array_merge($_REQUEST, $data);
404 "SELECT u.prenom, u.nom, u.promo, a.alias as forlife
405 FROM auth_user_md5 AS u
406 INNER JOIN contacts AS c ON (u.user_id = c.contact)
407 INNER JOIN aliases AS a ON (u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
409 ORDER BY u.nom, u.prenom", S
::v('uid'));
410 $page->assign('contacts', $res->fetchAllAssoc());
411 $page->assign('maxsize', ini_get('post_max_size') . 'o');
414 function handler_test(&$page, $forlife = null
)
417 if (!S
::has_perms() ||
!$forlife) {
418 $forlife = S
::v('bestalias');
420 $mailer = new PlMailer('emails/test.mail.tpl');
421 $mailer->assign('email', $forlife . '@' . $globals->mail
->domain
);
422 $iterator = XDB
::iterator("SELECT email
424 INNER JOIN aliases AS a ON (e.uid = a.id)
425 WHERE FIND_IN_SET('active', e.flags) AND a.alias = {?}",
427 $mailer->assign('redirects', $iterator);
428 $res = XDB
::query("SELECT FIND_IN_SET('femme', u.flags), prenom
429 FROM auth_user_md5 AS u
430 INNER JOIN aliases AS a ON (a.id = u.user_id)
431 WHERE a.alias = {?}", $forlife);
432 list($sexe, $prenom) = $res->fetchOneRow();
433 $mailer->assign('sexe', $sexe);
434 $mailer->assign('prenom', $prenom);
439 function handler_broken(&$page, $warn = null
, $email = null
)
441 require_once 'emails.inc.php';
442 require_once('wiki.inc.php');
443 wiki_require_page('Xorg.PatteCassée');
447 $page->changeTpl('emails/broken.tpl');
449 if ($warn == 'warn' && $email) {
450 $email = valide_email($email);
451 // vérifications d'usage
453 "SELECT e.uid, a.alias
455 INNER JOIN auth_user_md5 AS u ON e.uid = u.user_id
456 INNER JOIN aliases AS a ON (e.uid = a.id AND type!='homonyme'
457 AND FIND_IN_SET('bestalias',a.flags))
458 WHERE e.email={?}", $email);
460 if (list($uid, $dest) = $sel->fetchOneRow()) {
462 $message = "Bonjour !
464 Ce mail a été généré automatiquement par le service de patte cassée de
465 Polytechnique.org car un autre utilisateur, ".S
::v('prenom').' '.S
::v('nom').",
466 nous a signalé qu'en t'envoyant un mail, il avait reçu un message d'erreur
467 indiquant que ton adresse de redirection $email
468 ne fonctionnait plus !
470 Nous te suggérons de vérifier cette adresse, et le cas échéant de mettre
471 à jour sur le site <{$globals->baseurl}/emails> tes adresses
474 Pour plus de rensignements sur le service de patte cassée, n'hésites pas à
475 consulter la page <{$globals->baseurl}/emails/broken>.
478 A bientôt sur Polytechnique.org !
479 L'équipe d'administration <support@" . $globals->mail
->domain
. '>';
481 $mail = new PlMailer();
482 $mail->setFrom('"Polytechnique.org" <support@' . $globals->mail
->domain
. '>');
483 $mail->addTo("$dest@" . $globals->mail
->domain
);
484 $mail->setSubject("Une de tes adresse de redirection Polytechnique.org ne marche plus !!");
485 $mail->setTxtBody($message);
487 $page->trig("Mail envoyé ! :o)");
489 } elseif (Post
::has('email')) {
490 $email = valide_email(Post
::v('email'));
492 list(,$fqdn) = explode('@', $email);
493 $fqdn = strtolower($fqdn);
494 if ($fqdn == 'polytechnique.org' ||
$fqdn == 'melix.org'
495 ||
$fqdn == 'm4x.org' ||
$fqdn == 'melix.net')
497 $page->assign('neuneu', true
);
499 $page->assign('email',$email);
501 "SELECT e1.uid, e1.panne != 0 AS panne, count(e2.uid) AS nb_mails,
502 u.nom, u.prenom, u.promo, a.alias AS forlife
504 LEFT JOIN emails as e2 ON(e1.uid = e2.uid
505 AND FIND_IN_SET('active', e2.flags)
506 AND e1.email != e2.email)
507 INNER JOIN auth_user_md5 as u ON(e1.uid = u.user_id)
508 INNER JOIN aliases AS a ON (a.id = e1.uid AND a.type = 'a_vie')
510 GROUP BY e1.uid", $email);
511 if ($x = $sel->fetchOneAssoc()) {
512 // on écrit dans la base que l'adresse est cassée
514 XDB
::execute("UPDATE emails
518 WHERE email = {?}", $email);
520 XDB
::execute("UPDATE emails
522 WHERE email = {?} AND panne_level = 0", $email);
524 $page->assign_by_ref('x', $x);
530 function handler_duplicated(&$page, $action = 'list', $email = null
)
532 $page->changeTpl('emails/duplicated.tpl');
534 $states = array('pending' => 'En attente...',
535 'safe' => 'Pas d\'inquiétude',
536 'unsafe' => 'Recherches en cours',
537 'dangerous' => 'Usurpations par cette adresse');
538 $page->assign('states', $states);
540 switch (Post
::v('action')) {
542 if (trim(Post
::v('emailN')) != '') {
543 Xdb
::execute('INSERT IGNORE INTO emails_watch (email, state, detection, last, uid, description)
544 VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
545 trim(Post
::v('emailN')), Post
::v('stateN'), S
::i('uid'), Post
::v('descriptionN'));
550 Xdb
::execute('UPDATE emails_watch
551 SET state = {?}, last = NOW(), uid = {?}, description = {?}
552 WHERE email = {?}', Post
::v('stateN'), S
::i('uid'), Post
::v('descriptionN'), Post
::v('emailN'));
556 if ($action == 'delete' && !is_null($email)) {
557 Xdb
::execute('DELETE FROM emails_watch WHERE email = {?}', $email);
560 if ($action != 'create' && $action != 'edit') {
563 $page->assign('action', $action);
565 if ($action == 'list') {
566 $sql = "SELECT w.email, w.detection, w.state, a.alias AS forlife
567 FROM emails_watch AS w
568 LEFT JOIN emails AS e USING(email)
569 LEFT JOIN aliases AS a ON (a.id = e.uid AND a.type = 'a_vie')
570 ORDER BY w.state, w.email, a.alias";
571 $it = Xdb
::iterRow($sql);
575 while (list($email, $date, $state, $forlife) = $it->next()) {
576 if (count($props) == 0 ||
$props['mail'] != $email) {
577 if (count($props) > 0) {
580 $props = array('mail' => $email,
581 'detection' => $date,
583 'users' => array($forlife));
585 $props['users'][] = $forlife;
588 if (count($props) > 0) {
591 $page->assign('table', $table);
592 } elseif ($action == 'edit') {
593 $sql = "SELECT w.detection, w.state, w.last, w.description,
594 a1.alias AS edit, a2.alias AS forlife
595 FROM emails_watch AS w
596 LEFT JOIN aliases AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie')
597 LEFT JOIN emails AS e ON (w.email = e.email)
598 LEFT JOIN aliases AS a2 ON (a2.id = e.uid AND a2.type = 'a_vie')
601 $it = Xdb
::iterRow($sql, $email);
604 while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) {
605 if (count($props) == 0) {
606 $props = array('mail' => $email,
607 'detection' => $detection,
610 'description' => $description,
612 'users' => array($forlife));
614 $props['users'][] = $forlife;
617 $page->assign('doublon', $props);
620 function handler_lost(&$page, $action = 'list', $email = null
)
622 $page->changeTpl('emails/lost.tpl');
624 $page->assign('lost_emails', XDB
::iterator('
625 SELECT u.user_id, a.alias
626 FROM auth_user_md5 AS u
627 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = "a_vie")
628 LEFT JOIN emails AS e ON (u.user_id=e.uid AND FIND_IN_SET("active",e.flags))
629 WHERE e.uid IS NULL AND u.deces = 0
630 ORDER BY u.promo DESC, u.nom, u.prenom'));
634 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: