Merge commit 'origin/master' into fusionax
[platal.git] / modules / email.php
index ccf08bf..787bbae 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2009 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -24,20 +24,23 @@ class EmailModule extends PLModule
     function handlers()
     {
         return array(
-            'emails' => $this->make_hook('emails', AUTH_COOKIE),
-            'emails/alias'    => $this->make_hook('alias', AUTH_MDP),
-            'emails/antispam' => $this->make_hook('antispam', AUTH_MDP),
-            'emails/broken'   => $this->make_hook('broken', AUTH_COOKIE),
-            'emails/redirect' => $this->make_hook('redirect', AUTH_MDP),
-            'emails/send'     => $this->make_hook('send', AUTH_MDP),
-            'emails/antispam/submit'  => $this->make_hook('submit', AUTH_COOKIE),
-            'emails/test'     => $this->make_hook('test', AUTH_COOKIE, 'user', NO_AUTH),
-
-            'emails/imap/in'  => $this->make_hook('imap_in', AUTH_PUBLIC),
-
-            'admin/emails/duplicated' => $this->make_hook('duplicated', AUTH_MDP, 'admin'),
-            'admin/emails/watch'      => $this->make_hook('duplicated', AUTH_MDP, 'admin'),
-            'admin/emails/lost'       => $this->make_hook('lost', AUTH_MDP, 'admin'),
+            'emails'                  => $this->make_hook('emails',      AUTH_COOKIE),
+            'emails/alias'            => $this->make_hook('alias',       AUTH_MDP),
+            'emails/antispam'         => $this->make_hook('antispam',    AUTH_MDP),
+            'emails/broken'           => $this->make_hook('broken',      AUTH_COOKIE),
+            'emails/redirect'         => $this->make_hook('redirect',    AUTH_MDP),
+            'emails/send'             => $this->make_hook('send',        AUTH_MDP),
+            'emails/antispam/submit'  => $this->make_hook('submit',      AUTH_COOKIE),
+            'emails/test'             => $this->make_hook('test',        AUTH_COOKIE, 'user', NO_AUTH),
+
+            'emails/rewrite/in'       => $this->make_hook('rewrite_in',  AUTH_PUBLIC),
+            'emails/rewrite/out'      => $this->make_hook('rewrite_out', AUTH_PUBLIC),
+
+            'emails/imap/in'          => $this->make_hook('imap_in',     AUTH_PUBLIC),
+
+            'admin/emails/duplicated' => $this->make_hook('duplicated',  AUTH_MDP,    'admin'),
+            'admin/emails/watch'      => $this->make_hook('duplicated',  AUTH_MDP,    'admin'),
+            'admin/emails/lost'       => $this->make_hook('lost',        AUTH_MDP,    'admin'),
         );
     }
 
@@ -232,9 +235,9 @@ class EmailModule extends PLModule
         $email = str_replace(' ', '+', $email);
 
         // Apply email redirection change requests.
+        $result = SUCCESS;
         if ($action == 'remove' && $email) {
-            $retour = $redirect->delete_email($email);
-            $page->assign('retour', $retour);
+            $result = $redirect->delete_email($email);
         }
 
         if ($action == 'active' && $email) {
@@ -256,15 +259,37 @@ class EmailModule extends PLModule
             $actifs = Env::v('emails_actifs', Array());
             print_r(Env::v('emails_rewrite'));
             if (Env::v('emailop') == "ajouter" && Env::has('email')) {
-                $page->assign('retour', $redirect->add_email(Env::v('email')));
+                $new_email = Env::v('email');
+                if ($new_email == "new@example.org") {
+                    $new_email = Env::v('email_new');
+                }
+                $result = $redirect->add_email($new_email);
+                if ($result == ERROR_INVALID_EMAIL) {
+                    $page->assign('email', $new_email);
+                }
+                $page->assign('retour', $result);
             } elseif (empty($actifs)) {
-                $page->assign('retour', ERROR_INACTIVE_REDIRECTION);
+                $result = ERROR_INACTIVE_REDIRECTION;
             } elseif (is_array($actifs)) {
-                $page->assign('retour', $redirect->modify_email($actifs,
-                    Env::v('emails_rewrite',Array())));
+                $result = $redirect->modify_email($actifs, Env::v('emails_rewrite', Array()));
             }
         }
 
+        switch ($result) {
+          case ERROR_INACTIVE_REDIRECTION:
+            $page->trigError('Tu ne peux pas avoir aucune adresse de redirection active, sinon ton adresse '
+                             . $user->forlifeEmail() . ' ne fonctionnerait plus.');
+            break;
+          case ERROR_INVALID_EMAIL:
+            $page->trigError('Erreur: l\'email n\'est pas valide.');
+            break;
+          case ERROR_LOOP_EMAIL:
+            $page->trigError('Erreur : ' . $user->forlifeEmail()
+                             . ' ne doit pas être renvoyé vers lui-même, ni vers son équivalent en '
+                             . $globals->mail->domain2 . ' ni vers polytechnique.edu.');
+            break;
+        }
+
         // Fetch the @alias_dom email alias, if any.
         $res = XDB::query(
                 "SELECT  alias
@@ -294,6 +319,9 @@ class EmailModule extends PLModule
         // Display GoogleApps acount information.
         require_once 'googleapps.inc.php';
         $page->assign('googleapps', GoogleAppsAccount::account_status($user->id()));
+
+        require_once 'emails.combobox.inc.php';
+        fill_email_combobox($page);
     }
 
     function handler_antispam(&$page, $statut_filtre = null)
@@ -471,7 +499,7 @@ class EmailModule extends PLModule
         }
 
         // Retrieves the User object for the test email recipient.
-        if (S::has_perms() && $hruid) {
+        if (S::admin() && $hruid) {
             $user = User::getSilent($hruid);
         } else {
             $user = S::user();
@@ -492,6 +520,75 @@ class EmailModule extends PLModule
         exit;
     }
 
+    function handler_rewrite_in(&$page, $mail, $hash)
+    {
+        $page->changeTpl('emails/rewrite.tpl');
+        $page->assign('option', 'in');
+        if (empty($mail) || empty($hash)) {
+            return PL_NOT_FOUND;
+        }
+        $pos = strrpos($mail, '_');
+        if ($pos === false) {
+            return PL_NOT_FOUND;
+        }
+        $mail{$pos} = '@';
+        $res = XDB::query("SELECT  COUNT(*)
+                             FROM  emails
+                            WHERE  email = {?} AND hash = {?}",
+                          $mail, $hash);
+        $count = intval($res->fetchOneCell());
+        if ($count > 0) {
+            XDB::query("UPDATE  emails
+                           SET  allow_rewrite = true, hash = NULL
+                         WHERE  email = {?} AND hash = {?}",
+                         $mail, $hash);
+            $page->trigSuccess("Réécriture activée pour l'adresse " . $mail);
+            return;
+        }
+        return PL_NOT_FOUND;
+    }
+
+    function handler_rewrite_out(&$page, $mail, $hash)
+    {
+        $page->changeTpl('emails/rewrite.tpl');
+        $page->assign('option', 'out');
+        if (empty($mail) || empty($hash)) {
+            return PL_NOT_FOUND;
+        }
+        $pos = strrpos($mail, '_');
+        if ($pos === false) {
+            return PL_NOT_FOUND;
+        }
+        $mail{$pos} = '@';
+        $res = XDB::query("SELECT  COUNT(*)
+                             FROM  emails
+                            WHERE  email = {?} AND hash = {?}",
+                          $mail, $hash);
+        $count = intval($res->fetchOneCell());
+        if ($count > 0) {
+            global $globals;
+            $res = XDB::query("SELECT  e.email, e.rewrite, a.alias
+                                 FROM  emails AS e
+                           INNER JOIN  aliases AS a ON (a.id = e.uid AND a.type = 'a_vie')
+                                WHERE  e.email = {?} AND e.hash = {?}",
+                              $mail, $hash);
+            XDB::query("UPDATE  emails
+                           SET  allow_rewrite = false, hash = NULL
+                         WHERE  email = {?} AND hash = {?}",
+                        $mail, $hash);
+            list($mail, $rewrite, $forlife) = $res->fetchOneRow();
+            $mail = new PlMailer();
+            $mail->setFrom("webmaster@" . $globals->mail->domain);
+            $mail->addTo("support@" .  $globals->mail->domain);
+            $mail->setSubject("Tentative de détournement de correspondance via le rewrite");
+            $mail->setTxtBody("$forlife a tenté un rewrite de $mail vers $rewrite. Cette demande a été rejetée via le web");
+            $mail->send();
+            $page->trigWarning("Un mail d'alerte a été envoyé à l'équipe de " . $globals->core->sitename);
+            return;
+        }
+        return PL_NOT_FOUND;
+    }
+
     function handler_imap_in(&$page, $hash = null, $login = null)
     {
         $page->changeTpl('emails/imap_register.tpl');