Fixes display and (de)activation of email storages (Closes #1477).
authorStéphane Jacob <sj@m4x.org>
Fri, 29 Apr 2011 08:31:20 +0000 (10:31 +0200)
committerStéphane Jacob <sj@m4x.org>
Fri, 29 Apr 2011 08:31:20 +0000 (10:31 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
ChangeLog
include/emails.inc.php
include/reminder/email_backup.inc.php
modules/email.php
modules/register.php

index 5397244..7913578 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -10,6 +10,7 @@ Bug/Wish:
     * Emails:
         - #1110: Improves email combobox                                   -JAC
         - #1459: Lists and administrates aliases on main domain            -JAC
+        - #1477: Fixes display and (de)activation of email storages        -JAC
 
     * Events:
         - #1475: Fixes date storage for next reminder                      -JAC
index 19c172e..db35a5d 100644 (file)
@@ -374,6 +374,9 @@ function ids_from_mails(array $emails)
 // The Bogo class represents a spam filtering level in plat/al architecture.
 class Bogo
 {
+    const MAIN_DEFAULT = 'default';
+    const IMAP_DEFAULT = 'let_spams';
+
     public static $states = array(
         0 => 'default',
         1 => 'let_spams',
@@ -514,10 +517,14 @@ class Email
     public function activate()
     {
         if ($this->inactive) {
-            XDB::execute('UPDATE  email_redirect_account
-                             SET  broken_level = IF(flags = \'broken\', broken_level - 1, broken_level), flags = \'active\'
-                           WHERE  uid = {?} AND redirect = {?}',
-                         $this->user->id(), $this->email);
+            if ($is_storage = in_array($this->type, self::get_allowed_storages($this->user))) {
+                self::activate_storage($this->user, $this->type, $this->action);
+            } else {
+                XDB::execute('UPDATE  email_redirect_account
+                                 SET  broken_level = IF(flags = \'broken\', broken_level - 1, broken_level), flags = \'active\'
+                               WHERE  uid = {?} AND redirect = {?}',
+                             $this->user->id(), $this->email);
+            }
             S::logger()->log('email_on', $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : ''));
             $this->inactive = false;
             $this->active   = true;
@@ -528,10 +535,14 @@ class Email
     public function deactivate()
     {
         if ($this->active) {
-            XDB::execute('UPDATE  email_redirect_account
-                             SET  flags = \'inactive\'
-                           WHERE  uid = {?} AND redirect = {?}',
-                         $this->user->id(), $this->email);
+            if ($is_storage = in_array($this->type, self::get_allowed_storages($this->user))) {
+                self::deactivate_storage($this->user, $this->type);
+            } else {
+                XDB::execute('UPDATE  email_redirect_account
+                                 SET  flags = \'inactive\'
+                               WHERE  uid = {?} AND redirect = {?}',
+                             $this->user->id(), $this->email);
+            }
             S::logger()->log('email_off', $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : "") );
             $this->inactive = true;
             $this->active   = false;
@@ -617,7 +628,7 @@ class Email
     }
 
     // Returns the list of allowed storages for the @p user.
-    static private function get_allowed_storages(User $user)
+    static public function get_allowed_storages(User $user)
     {
         global $globals;
         $storages = array();
@@ -639,23 +650,33 @@ class Email
         return $storages;
     }
 
-    static public function activate_storage(User $user, $storage)
+    static public function make_storage_redirection(User $user, $storage)
+    {
+        return $user->hruid . '@' . self::$storage_domains[$storage] . '.' . Platal::globals()->mail->domain;
+    }
+
+    static public function activate_storage(User $user, $storage, $action = null)
     {
         Platal::assert(in_array($storage, self::get_allowed_storages($user)), 'Unknown storage.');
 
-        if (!self::is_active_storage($user, $storage)) {
-            global $globals;
+        // We first need to retrieve the value for the antispam filter if not
+        // provided: it is either the user's redirections common value, or if
+        // they differ, our default value.
+        if (is_null($action)) {
+            $bogo = new Bogo($user);
+            $action = ($bogo->single_state ? Bogo::$states[$bogo->state] : Bogo::MAIN_DEFAULT);
+        }
 
-            XDB::execute('INSERT INTO  email_redirect_account (uid, type, redirect, flags)
-                               VALUES  ({?}, {?}, {?}, \'active\')',
-                         $user->id(), $storage,
-                         $user->hruid . '@' . self::$storage_domains[$storage] . '.' . $globals->mail->domain);
+        if (!self::is_active_storage($user, $storage)) {
+            XDB::execute('INSERT INTO  email_redirect_account (uid, type, action, redirect, flags)
+                               VALUES  ({?}, {?}, {?}, {?}, \'active\')',
+                         $user->id(), $storage, $action, self::make_storage_redirection($user, $storage));
         }
     }
 
     static public function deactivate_storage(User $user, $storage)
     {
-        if (in_array($storage, self::$storage_domains)) {
+        if (in_array($storage, self::get_allowed_storages($user))) {
             XDB::execute('DELETE FROM  email_redirect_account
                                 WHERE  uid = {?} AND type = {?}',
                          $user->id(), $storage);
@@ -664,7 +685,7 @@ class Email
 
     static public function is_active_storage(User $user, $storage)
     {
-        if (!in_array($storage, self::$storage_domains)) {
+        if (!in_array($storage, self::get_allowed_storages($user))) {
             return false;
         }
         $res = XDB::fetchOneCell('SELECT  COUNT(*)
@@ -697,6 +718,30 @@ class Redirect
         while ($row = $res->next()) {
             $this->emails[] = new Email($user, $row);
         }
+
+        if ($storages = Email::get_allowed_storages($user)) {
+            // We first need to retrieve the value for the antispam filter: it is
+            // either the user's redirections common value, or if they differ, our
+            // default value.
+            $bogo = new Bogo($user);
+            $filter = ($bogo->single_state ? Bogo::$states[$bogo->state] : Bogo::MAIN_DEFAULT);
+
+            foreach ($storages as $storage) {
+                if (!Email::is_active_storage($user, $storage)) {
+                    $this->emails[] = new Email($user, array(
+                            'redirect'      => Email::make_storage_redirection($user, $storage),
+                            'rewrite'       => '',
+                            'type'          => $storage,
+                            'action'        => $filter,
+                            'broken_date'   => 0,
+                            'broken_level'  => '0000-00-00',
+                            'last'          => '0000-00-00',
+                            'flags'         => 'inactive',
+                            'hash'          => '',
+                            'allow_rewrite' => 0));
+                }
+            }
+        }
     }
 
     public function other_active($email)
@@ -741,7 +786,7 @@ class Redirect
         // either the user's redirections common value, or if they differ, our
         // default value.
         $bogo = new Bogo($this->user);
-        $filter = ($bogo->single_state ? Bogo::$states[$bogo->state] : Bogo::$states[0]);
+        $filter = ($bogo->single_state ? Bogo::$states[$bogo->state] : Bogo::MAIN_DEFAULT);
         // If the email was already present for this user, we reset it to the default values, we thus use REPLACE INTO.
         XDB::execute('REPLACE INTO  email_redirect_account (uid, redirect, flags, action)
                             VALUES  ({?}, {?}, \'active\', {?})',
index 6336b8b..9264d37 100644 (file)
@@ -25,7 +25,7 @@ class ReminderEmailBackup extends Reminder
     {
         if ($action == 'yes') {
             require_once 'emails.inc.php';
-            Email::activate_storage($this->user, 'imap');
+            Email::activate_storage($this->user, 'imap', Bogo::IMAP_DEFAULT);
             $this->UpdateOnYes();
         }
 
index 15d1e31..41d4bf9 100644 (file)
@@ -615,12 +615,12 @@ class EmailModule extends PLModule
         require_once 'emails.inc.php';
         $page->assign('ok', false);
         if (S::logged() && (is_null($user) || $user->id() == S::i('uid'))) {
-            Email::activate_storage(S::user(), 'imap');
+            Email::activate_storage(S::user(), 'imap', Bogo::IMAP_DEFAULT);
             $page->assign('ok', true);
             $page->assign('yourself', S::user()->displayName());
             $page->assign('sexe', S::user()->isFemale());
         } else if (!S::logged() && $user) {
-            Email::activate_storage($user, 'imap');
+            Email::activate_storage($user, 'imap', Bogo::IMAP_DEFAULT);
             $page->assign('ok', true);
             $page->assign('yourself', $user->displayName());
             $page->assign('sexe', $user->isFemale());
index 50df85e..bc54a13 100644 (file)
@@ -389,7 +389,7 @@ class RegisterModule extends PLModule
                     NewsLetter::forGroup(NewsLetter::GROUP_XORG)->subscribe($user);
                     break;
                 case 'imap':
-                    Email::activate_storage($user, 'imap');
+                    Email::activate_storage($user, 'imap', Bogo::IMAP_DEFAULT);
                     break;
                 case 'ml_promo':
                     if ($isX) {