Moving to GitHub.
[platal.git] / include / emails.inc.php
index 5a05f61..f2f12e0 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -30,7 +30,20 @@ function require_email_update(User $user, $new_email)
 {
     Platal::assert(!is_null($user), 'User cannot be null.');
 
-    return !$user->checkPerms(User::PERM_MAIL) && $new_email != $user->forlifeEmail();
+    $is_new = !$user->checkPerms(User::PERM_MAIL) && $new_email != strtolower($user->email);
+    if ($new_email && $is_new) {
+        $already = XDB::fetchOneCell('SELECT  hruid
+                                        FROM  accounts
+                                       WHERE  email = {?} AND uid != {?}',
+                                     $new_email, $user->id());
+        if ($already) {
+            Platal::page()->trigError("L'email ne peut pas être utilisé pour ce compte car il correspond déjà au compte : "
+                                      . $already . ". Si l'utilisateur courant et cette personne ne sont en fait qu'une "
+                                      . "seul et même personne, ou en cas de problème, contacter : contact@polytechnique.org");
+            return false;
+        }
+    }
+    return $is_new;
 }
 
 function format_email_alias($email)
@@ -189,7 +202,7 @@ function mark_broken_email($email, $admin = false)
         return;
     }
 
-    $user = XDB::fetchOneAssoc('SELECT  r1.uid, r1.broken_level != 0 AS broken, COUNT(r2.uid) AS nb_mails,
+    $user = XDB::fetchOneAssoc('SELECT  r1.uid, a.hruid, a.full_name, r1.broken_level != 0 AS broken, COUNT(r2.uid) AS nb_mails,
                                         s.email AS alias, DATE_ADD(r1.last, INTERVAL 14 DAY) < CURDATE() as notify
                                   FROM  email_redirect_account AS r1
                             INNER JOIN  accounts               AS a  ON (a.uid = r1.uid)
@@ -298,11 +311,17 @@ function valide_email($str)
 // function isvalid_email_redirection() {{{1
 /** Checks if an email is a suitable redirection.
  * @param $email the email to check
+ * @param $user  the user asking for the redirection
  * @return BOOL
  */
-function isvalid_email_redirection($email)
+function isvalid_email_redirection($email, User $user)
 {
-    return isvalid_email($email) && !preg_match("/@polytechnique\.edu$/", $email) && User::isForeignEmailAddress($email);
+    $valid = isvalid_email($email) && User::isForeignEmailAddress($email);
+    if (!$user->hasProfile() || ($user->profile()->grad_year > date('Y') - 3)) {
+        return $valid && !preg_match("/@polytechnique\.edu$/", $email);
+    } else {
+        return $valid;
+    }
 }
 
 // function ids_from_mails() {{{1
@@ -522,7 +541,7 @@ class Email
     // Activates the email address as a redirection.
     public function activate()
     {
-        if ($this->inactive) {
+        if (!$this->active) {
             if (in_array($this->type, self::get_allowed_storages($this->user))) {
                 self::activate_storage($this->user, $this->type, $this->action);
             } else {
@@ -532,6 +551,8 @@ class Email
                              $this->user->id(), $this->email);
             }
             S::logger()->log('email_on', $this->email . ($this->user->id() != S::v('uid') ? "(admin on {$this->user->login()})" : ''));
+            $this->disabled = false;
+            $this->broken   = false;
             $this->inactive = false;
             $this->active   = true;
         }
@@ -785,7 +806,7 @@ class Redirect
         if (!isvalid_email($email_stripped)) {
             return ERROR_INVALID_EMAIL;
         }
-        if (!isvalid_email_redirection($email_stripped)) {
+        if (!isvalid_email_redirection($email_stripped, $this->user)) {
             return ERROR_LOOP_EMAIL;
         }
         // We first need to retrieve the value for the antispam filter: it is
@@ -797,6 +818,10 @@ class Redirect
         XDB::execute('REPLACE INTO  email_redirect_account (uid, redirect, flags, action)
                             VALUES  ({?}, {?}, \'active\', {?})',
                      $this->user->id(), $email, $filter);
+        // Replace this email by forlife email, if present in aliases and MLs.
+        $listClient = new MMList(S::user());
+        $listClient->change_user_email($email, $this->user->forlifeEmail());
+        update_alias_user($email, $this->user->forlifeEmail());
         if ($logger = S::v('log', null)) { // may be absent --> step4.php
             S::logger()->log('email_add', $email . ($this->user->id() != S::v('uid') ? " (admin on {$this->user->login()})" : ""));
         }
@@ -988,5 +1013,5 @@ class Redirect
     }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>