Adds levels to medals (Closes #1381).
[platal.git] / modules / xnetgrp.php
index 2b5966d..b5f5e57 100644 (file)
@@ -850,20 +850,10 @@ class XnetGrpModule extends PLModule
         // + (the data) => %2B (in the url) => + (first decoding) => ' ' (second decoding)
         // Since there can be no spaces in emails, we can fix this with :
         $email = str_replace(' ', '+', $email);
+        $is_valid_email = isvalid_email($email);
 
-        // Finds or creates account: first cases are for users with an account.
-        if (!User::isForeignEmailAddress($email)) {
-            // Standard account
-            $user = User::getSilent($email);
-        } else if (!isvalid_email($email)) {
-            // email might not be a regular email but an alias or a hruid
-            $user = User::getSilent($email);
-            if (!$user) {
-                // need a valid email address
-                $page->trigError('«&nbsp;<strong>' . $email . '</strong>&nbsp;» n\'est pas une adresse email valide.');
-                return;
-            }
-        } else if (Env::v('x') && Env::i('userid')) {
+        // X not registered to main site.
+        if (Env::v('x') && Env::i('userid') && $is_valid_email) {
             $user = User::getSilentWithUID(Env::i('userid'));
             if (!$user) {
                 $page->trigError('Utilisateur invalide.');
@@ -888,40 +878,51 @@ class XnetGrpModule extends PLModule
                 }
             }
         } else {
-            // User is of type xnet. There are 3 possible cases:
-            //  * the email is not known yet: we create a new account and
-            //      propose to send an email to the user so he can activate
-            //      his account,
-            //  * the email is known but the user was not contacted in order to
-            //      activate yet: we propose to send an email to the user so he
-            //      can activate his account,
-            //  * the email is known and the user was already contacted or has
-            //      an active account: nothing to be done.
-            list($mbox, $domain) = explode('@', strtolower($email));
-            $hruid = User::makeHrid($mbox, $domain, 'ext');
-            // User might already have an account (in another group for example).
-            $user = User::getSilent($hruid);
-
-            // If the user has no account yet, creates new account: build names from email address.
-            if (empty($user)) {
-                $parts = explode('.', $mbox);
-                if (count($parts) == 1) {
-                    $lastname = $display_name = $full_name = $directory_name = ucfirst($mbox);
-                    $firstname = '';
-                } else {
-                    $firstname = ucfirst($parts[0]);
-                    $lastname = ucwords(implode(' ', array_slice($parts, 1)));
-                    $display_name = $firstname;
-                    $full_name = "$firstname $lastname";
-                    $directory_name = strtoupper($lastname) . " " . $firstname;
-                }
-                XDB::execute('INSERT INTO  accounts (hruid, display_name, full_name, directory_name, firstname, lastname, email, type, state)
-                                   VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, \'xnet\', \'disabled\')',
-                             $hruid, $display_name, $full_name, $directory_name, $firstname, $lastname, $email);
-                $user = User::getSilent($hruid);
+            $user = User::getSilent($email);
+
+            // Wrong email and no user: failure.
+            if (is_null($user) && !$is_valid_email) {
+                $page->trigError('«&nbsp;<strong>' . $email . '</strong>&nbsp;» n\'est pas une adresse email valide.');
+                return;
             }
 
-            $suggest_account_activation = $this->suggest($user);
+            // Deals with xnet accounts.
+            if (is_null($user) || $user->type == 'xnet') {
+                // User is of type xnet. There are 3 possible cases:
+                //  * the email is not known yet: we create a new account and
+                //      propose to send an email to the user so he can activate
+                //      his account,
+                //  * the email is known but the user was not contacted in order to
+                //      activate yet: we propose to send an email to the user so he
+                //      can activate his account,
+                //  * the email is known and the user was already contacted or has
+                //      an active account: nothing to be done.
+                list($mbox, $domain) = explode('@', strtolower($email));
+                $hruid = User::makeHrid($mbox, $domain, 'ext');
+                // User might already have an account (in another group for example).
+                $user = User::getSilent($hruid);
+
+                // If the user has no account yet, creates new account: build names from email address.
+                if (empty($user)) {
+                    $parts = explode('.', $mbox);
+                    if (count($parts) == 1) {
+                        $lastname = $display_name = $full_name = $directory_name = ucfirst($mbox);
+                        $firstname = '';
+                    } else {
+                        $firstname = ucfirst($parts[0]);
+                        $lastname = ucwords(implode(' ', array_slice($parts, 1)));
+                        $display_name = $firstname;
+                        $full_name = "$firstname $lastname";
+                        $directory_name = strtoupper($lastname) . " " . $firstname;
+                    }
+                    XDB::execute('INSERT INTO  accounts (hruid, display_name, full_name, directory_name, firstname, lastname, email, type, state)
+                                       VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, \'xnet\', \'disabled\')',
+                                 $hruid, $display_name, $full_name, $directory_name, $firstname, $lastname, $email);
+                    $user = User::getSilent($hruid);
+                }
+
+                $suggest_account_activation = $this->suggest($user);
+            }
         }
 
         if ($user) {
@@ -1204,8 +1205,6 @@ class XnetGrpModule extends PLModule
             }
 
             // Update user info
-            $email_changed = (!$user->profile() && strtolower($user->forlifeEmail()) != strtolower(Post::v('email')));
-            $from_email = $user->forlifeEmail();
             if ($user->type == 'virtual' || ($user->type == 'xnet' && !$user->perms)) {
                 $lastname = Post::s('lastname');
                 if (Post::s('type') != 'virtual') {
@@ -1224,13 +1223,11 @@ class XnetGrpModule extends PLModule
                            $full_name, $directory_name, Post::t('display_name'), $firstname, $lastname,
                            (Post::t('sex') == 'male') ? 'male' : 'female', Post::t('email'),
                            (Post::t('type') == 'xnet') ? 'xnet' : 'virtual', $user->id());
-            } else if (!$user->perms) {
+            } else if (!$user->perms && Post::has('email') && require_email_update($user, Post::t('email'))) {
                 XDB::query('UPDATE  accounts
                                SET  email = {?}
                              WHERE  uid = {?}',
                            Post::t('email'), $user->id());
-            }
-            if (require_email_update($user, Post::t('email'))) {
                 $listClient = new MMList(S::user());
                 $listClient->change_user_email($user->forlifeEmail(), Post::t('email'));
                 update_alias_user($user->forlifeEmail(), Post::t('email'));
@@ -1239,10 +1236,29 @@ class XnetGrpModule extends PLModule
                 $page->trigSuccess('Données de l\'utilisateur mises à jour.');
             }
 
-            if (($user->type == 'xnet' && !$user->perms) && Post::b('suggest')) {
-                $request = new AccountReq(S::user(), $user->hruid, Post::t('email'), $globals->asso('nom'));
-                $request->submit();
-                $page->trigSuccess('Le compte va bientôt être activé.');
+            if (($user->type == 'xnet' && !$user->perms)) {
+                if (Post::b('suggest')) {
+                    $request = new AccountReq(S::user(), $user->hruid, Post::t('email'), $globals->asso('nom'));
+                    $request->submit();
+                    $page->trigSuccess('Le compte va bientôt être activé.');
+                }
+                if (Post::b('again')) {
+                    $data = XDB::fetchOneAssoc('SELECT  hash, group_name, sender_name, email
+                                                  FROM  register_pending_xnet
+                                                 WHERE  uid = {?}',
+                                               $user->id());
+
+                    $mailer = new PlMailer('xnet/account.mail.tpl');
+                    $mailer->addCc('validation+xnet_account@polytechnique.org');
+                    $mailer->setTo($data['email']);
+                    $mailer->assign('hash', $data['hash']);
+                    $mailer->assign('email', $data['email']);
+                    $mailer->assign('group', $data['group_name']);
+                    $mailer->assign('sender_name', $data['sender_name']);
+                    $mailer->assign('again', true);
+                    $mailer->send();
+                    $page->trigSuccess('Relance effectuée avec succès.');
+                }
             }
 
             // Update group params for user
@@ -1275,10 +1291,6 @@ class XnetGrpModule extends PLModule
             foreach (Env::v('ml1', array()) as $ml => $state) {
                 $ask = empty($_REQUEST['ml2'][$ml]) ? 0 : 2;
                 if ($ask == $state) {
-                    if ($state && $email_changed) {
-                        $mmlist->replace_email($ml, $from_email, $user->forlifeEmail());
-                        $page->trigSuccess("L'abonnement de {$user->fullName()} à $ml@ a été mis à jour.");
-                    }
                     continue;
                 }
                 if ($state == '1') {
@@ -1289,11 +1301,7 @@ class XnetGrpModule extends PLModule
                     $mmlist->mass_subscribe($ml, Array($user->forlifeEmail()));
                     $page->trigSuccess("{$user->fullName()} a été abonné à $ml@.");
                 } else {
-                    if ($email_changed) {
-                        $mmlist->mass_unsubscribe($ml, Array($from_email));
-                    } else {
-                        $mmlist->mass_unsubscribe($ml, Array($user->forlifeEmail()));
-                    }
+                    $mmlist->mass_unsubscribe($ml, Array($user->forlifeEmail()));
                     $page->trigSuccess("{$user->fullName()} a été désabonné de $ml@.");
                 }
             }
@@ -1303,12 +1311,10 @@ class XnetGrpModule extends PLModule
                 require_once 'emails.inc.php';
                 $ask = !empty($_REQUEST['ml4'][$ml]);
                 list($local_part, ) = explode('@', $ml);
-                if($state == $ask) {
-                    if ($state && $email_changed) {
-                        update_list_alias($user->id(), $from_email, $local_part, $globals->asso('mail_domain'));
-                        $page->trigSuccess("L'abonnement de {$user->fullName()} à $ml a été mis à jour.");
-                    }
-                } else if($ask) {
+                if ($ask == $state) {
+                    continue;
+                }
+                if ($ask) {
                     add_to_list_alias($user->id(), $local_part, $globals->asso('mail_domain'));
                     $page->trigSuccess("{$user->fullName()} a été abonné à $ml.");
                 } else {
@@ -1343,6 +1349,10 @@ class XnetGrpModule extends PLModule
         $page->assign('alias', $user->emailGroupAliases($globals->asso('mail_domain')));
         $page->assign('positions', explode(',', $positions));
         $page->assign('nl_registered', $nl_registered);
+        $page->assign('pending_xnet_account', XDB::fetchOneCell('SELECT  1
+                                                                   FROM  register_pending_xnet
+                                                                  WHERE  uid = {?}',
+                                                                $user->id()));
     }
 
     function handler_rss(PlPage $page, PlUser $user)