X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=include%2Femails.inc.php;h=f9c131a17c431f183ebdfd05a1c2e92810c20c24;hb=08d7cc452e666835373023f2ca8416e75e2cb9c5;hp=357dd0d0616f1225df6edb5ee069fadd944ad1b2;hpb=11ed26e5a8fc25008a26c8842ce5281343fd704d;p=platal.git diff --git a/include/emails.inc.php b/include/emails.inc.php index 357dd0d..f9c131a 100644 --- a/include/emails.inc.php +++ b/include/emails.inc.php @@ -257,6 +257,105 @@ class EmailRedirection extends Email } } +// class EmailStorage {{{1 +// Implementation of Email for email storage backends from Polytechnique.org. +class EmailStorage extends Email +{ + // Shortname to realname mapping for known mail storage backends. + private $display_names = array( + 'imap' => 'Accès de secours aux emails (IMAP)', + 'googleapps' => 'Compte Google Apps', + ); + + // Retrieves the current list of actives storages. + private function get_storages() + { + $res = XDB::query("SELECT mail_storage + FROM auth_user_md5 + WHERE user_id = {?}", $this->uid); + return new PlFlagSet($res->fetchOneCell()); + } + + // Updates the list of active storages. + private function set_storages($storages) + { + XDB::execute("UPDATE auth_user_md5 + SET mail_storage = {?} + WHERE user_id = {?}", $storages, $this->uid); + } + + // Returns the list of allowed storages for the @p user. + static public function get_allowed_storages($uid) + { + global $globals; + $storages = array(); + + // Google Apps storage is available for users with valid Google Apps account. + require_once 'googleapps.inc.php'; + if ($globals->mailstorage->googleapps_domain && + GoogleAppsAccount::account_status($uid) == 'active') { + $storages[] = 'googleapps'; + } + + // IMAP storage is always visible to administrators, and is allowed for + // everyone when the service is marked as 'active'. + if ($globals->mailstorage->imap_active || S::has_perms()) { + $storages[] = 'imap'; + } + + return $storages; + } + + + public function __construct($uid, $name) + { + $this->uid = $uid; + $this->email = $name; + $this->display_email = (isset($this->display_names[$name]) ? $this->display_names[$name] : $name); + + $storages = $this->get_storages(); + $this->sufficient = ($name == 'googleapps'); + $this->active = $storages->hasFlag($name); + $this->broken = false; + $this->disabled = false; + $this->rewrite = ''; + $this->panne = $this->last = $this->panne_level = 0; + } + + public function activate() + { + if (!$this->active) { + $storages = $this->get_storages(); + $storages->addFlag($this->email); + $this->set_storages($storages); + $this->active = true; + } + } + + public function deactivate() + { + if ($this->active) { + $storages = $this->get_storages(); + $storages->rmFlag($this->email); + $this->set_storages($storages); + $this->active = false; + } + + } + + // Source rewrite can't be enabled for email storage addresses. + public function set_rewrite($rewrite) {} + + // Email storage are not supposed to be broken, hence not supposed to be + // cleaned-up. + public function clean_errors() {} + + // Capabilities. + public function has_rewrite() { return false; } + public function is_removable() { return false; } + public function has_disable() { return false; } +} + // class Redirect {{{1 // Redirect is a placeholder class for an user's active redirections (third-party // redirection email, or Polytechnique.org mail storages). @@ -275,7 +374,9 @@ class Redirect public function __construct($_uid) { $this->uid = $_uid; + $this->bogo = new Bogo($_uid); + // Adds third-party email redirections. $res = XDB::iterRow("SELECT email, flags, rewrite, panne, last, panne_level FROM emails WHERE uid = {?} AND flags != 'filter'", $_uid); @@ -283,7 +384,11 @@ class Redirect while ($row = $res->next()) { $this->emails[] = new EmailRedirection($_uid, $row); } - $this->bogo = new Bogo($_uid); + + // Adds local email storage backends. + foreach (EmailStorage::get_allowed_storages($_uid) as $storage) { + $this->emails[] = new EmailStorage($_uid, $storage); + } } // public function other_active() {{{2 @@ -481,69 +586,25 @@ class Redirect } return $mails; } -} - -// class MailStorage {{{1 -class MailStorage { - protected $uid; - protected $name; - protected $storage; - - public function __construct($_uid, $_name) - { - $this->uid = $_uid; - $this->name = $_name; - - $res = XDB::query("SELECT mail_storage - FROM auth_user_md5 - WHERE user_id = {?}", $this->uid); - $this->storages = new FlagSet($res->fetchOneCell()); - } - - public function disable() - { - $this->storages->rmFlag($this->name); - XDB::execute("UPDATE auth_user_md5 - SET mail_storage = {?} - WHERE user_id = {?}", $this->storages->flags(), $this->uid); - return true; - } - public function enable() - { - $this->storages->addFlag($this->name); - XDB::execute("UPDATE auth_user_md5 - SET mail_storage = {?} - WHERE user_id = {?}", $this->storages->flags(), $this->uid); - return true; - } + // function active_emails() {{{2 - public function active() + public function active_emails() { - return $this->storages->hasFlag($this->name); + $emails = array(); + foreach ($this->emails as $mail) { + if ($mail->active) { + $emails[] = $mail; + } + } + return $emails; } -} -class MailStorageIMAP extends MailStorage { - public function __construct($_uid) - { - parent::__construct($_uid, 'imap'); - } -} + // function get_uid() {{{2 -class MailStorageGoogleApps extends MailStorage { - public function __construct($_uid) + public function get_uid() { - parent::__construct($_uid, 'googleapps'); - } - - public function disable() { - $redirect = new Redirect(S::v('uid')); - if (!$redirect->other_active(NULL)) { - return false; - } - - return parent::disable(); + return $this->uid; } }