Merge branch 'xorg/maint' into xorg/master
[platal.git] / modules / xnetgrp.php
index 9206635..7b46cfd 100644 (file)
@@ -29,30 +29,31 @@ class XnetGrpModule extends PLModule
             '%grp/asso.php'        => $this->make_hook('index',                 AUTH_PUBLIC),
             '%grp/logo'            => $this->make_hook('logo',                  AUTH_PUBLIC),
             '%grp/site'            => $this->make_hook('site',                  AUTH_PUBLIC),
-            '%grp/edit'            => $this->make_hook('edit',                  AUTH_MDP,    'groupadmin'),
-            '%grp/mail'            => $this->make_hook('mail',                  AUTH_MDP,    'groupadmin'),
-            '%grp/forum'           => $this->make_hook('forum',                 AUTH_MDP,    'groupmember'),
-            '%grp/annuaire'        => $this->make_hook('annuaire',              AUTH_MDP,    'groupannu'),
-            '%grp/annuaire/vcard'  => $this->make_hook('vcard',                 AUTH_MDP,    'groupmember:groupannu'),
-            '%grp/annuaire/csv'    => $this->make_hook('csv',                   AUTH_MDP,    'groupmember:groupannu'),
-            '%grp/trombi'          => $this->make_hook('trombi',                AUTH_MDP,    'groupannu'),
-            '%grp/geoloc'          => $this->make_hook('geoloc',                AUTH_MDP,    'groupannu'),
+            '%grp/edit'            => $this->make_hook('edit',                  AUTH_MDP, 'groupadmin'),
+            '%grp/mail'            => $this->make_hook('mail',                  AUTH_MDP, 'groupadmin'),
+            '%grp/forum'           => $this->make_hook('forum',                 AUTH_MDP, 'groupmember'),
+            '%grp/annuaire'        => $this->make_hook('annuaire',              AUTH_MDP, 'groupannu'),
+            '%grp/annuaire/vcard'  => $this->make_hook('vcard',                 AUTH_MDP, 'groupmember:groupannu'),
+            '%grp/annuaire/csv'    => $this->make_hook('csv',                   AUTH_MDP, 'groupmember:groupannu'),
+            '%grp/trombi'          => $this->make_hook('trombi',                AUTH_MDP, 'groupannu'),
+            '%grp/geoloc'          => $this->make_hook('geoloc',                AUTH_MDP, 'groupannu'),
             '%grp/subscribe'       => $this->make_hook('subscribe',             AUTH_MDP),
-            '%grp/subscribe/valid' => $this->make_hook('subscribe_valid',       AUTH_MDP,    'groupadmin'),
-            '%grp/unsubscribe'     => $this->make_hook('unsubscribe',           AUTH_MDP,    'groupmember'),
+            '%grp/subscribe/valid' => $this->make_hook('subscribe_valid',       AUTH_MDP, 'groupadmin'),
+            '%grp/unsubscribe'     => $this->make_hook('unsubscribe',           AUTH_MDP, 'groupmember'),
 
             '%grp/change_rights'   => $this->make_hook('change_rights',         AUTH_MDP),
-            '%grp/admin/annuaire'  => $this->make_hook('admin_annuaire',        AUTH_MDP,    'groupadmin'),
-            '%grp/member'          => $this->make_hook('admin_member',          AUTH_MDP,    'groupadmin'),
-            '%grp/member/new'      => $this->make_hook('admin_member_new',      AUTH_MDP,    'groupadmin'),
-            '%grp/member/new/ajax' => $this->make_hook('admin_member_new_ajax', AUTH_MDP,    'user', NO_AUTH),
-            '%grp/member/del'      => $this->make_hook('admin_member_del',      AUTH_MDP,    'groupadmin'),
+            '%grp/admin/annuaire'  => $this->make_hook('admin_annuaire',        AUTH_MDP, 'groupadmin'),
+            '%grp/member'          => $this->make_hook('admin_member',          AUTH_MDP, 'groupadmin'),
+            '%grp/member/new'      => $this->make_hook('admin_member_new',      AUTH_MDP, 'groupadmin'),
+            '%grp/member/new/ajax' => $this->make_hook('admin_member_new_ajax', AUTH_MDP, 'user', NO_AUTH),
+            '%grp/member/del'      => $this->make_hook('admin_member_del',      AUTH_MDP, 'groupadmin'),
+            '%grp/member/suggest'  => $this->make_hook('admin_member_suggest',  AUTH_MDP, 'groupadmin'),
 
             '%grp/rss'             => $this->make_token_hook('rss',             AUTH_PUBLIC),
-            '%grp/announce/new'    => $this->make_hook('edit_announce',         AUTH_MDP,    'groupadmin'),
-            '%grp/announce/edit'   => $this->make_hook('edit_announce',         AUTH_MDP,    'groupadmin'),
+            '%grp/announce/new'    => $this->make_hook('edit_announce',         AUTH_MDP, 'groupadmin'),
+            '%grp/announce/edit'   => $this->make_hook('edit_announce',         AUTH_MDP, 'groupadmin'),
             '%grp/announce/photo'  => $this->make_hook('photo_announce',        AUTH_PUBLIC),
-            '%grp/admin/announces' => $this->make_hook('admin_announce',        AUTH_MDP,    'groupadmin'),
+            '%grp/admin/announces' => $this->make_hook('admin_announce',        AUTH_MDP, 'groupadmin'),
         );
     }
 
@@ -656,6 +657,12 @@ class XnetGrpModule extends PLModule
         }
 
         S::assert_xsrf_token();
+        $suggest_account_activation = false;
+
+        // FS#703 : $_GET is urldecoded twice, hence
+        // + (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);
 
         // Finds or creates account: first cases are for users with an account.
         if (!User::isForeignEmailAddress($email)) {
@@ -694,7 +701,15 @@ class XnetGrpModule extends PLModule
                 }
             }
         } else {
-            // User is of 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).
@@ -712,12 +727,13 @@ class XnetGrpModule extends PLModule
                     $full_name = "$firstname $lastname";
                     $directory_name = strtoupper($lastname) . " " . $firstname;
                 }
-                XDB::execute('INSERT INTO  accounts (hruid, display_name, full_name, directory_name,
-                                           email, type)
-                                   VALUES  ({?}, {?}, {?}, {?}, {?}, \'xnet\')',
+                XDB::execute('INSERT INTO  accounts (hruid, display_name, full_name, directory_name, email, type, state)
+                                   VALUES  ({?}, {?}, {?}, {?}, {?}, \'xnet\', \'disabled\')',
                              $hruid, $display_name, $full_name, $directory_name, $email);
                 $user = User::get($hruid);
             }
+
+            $suggest_account_activation = $this->suggest($user);
         }
 
         if ($user) {
@@ -725,8 +741,56 @@ class XnetGrpModule extends PLModule
                                       VALUES  ({?}, {?})',
                          $user->id(), $globals->asso('id'));
             $this->removeSubscriptionRequest($user->id());
-            pl_redirect('member/' . $user->login());
+            if ($suggest_account_activation) {
+                pl_redirect('member/suggest/' . $user->login() . '/' . $email . '/' . $globals->asso('nom'));
+            } else {
+                pl_redirect('member/' . $user->login());
+            }
+        }
+    }
+
+    // Check if the user has a pending or active account, and thus if we should her account's activation.
+    private function suggest(PlUser $user)
+    {
+        $active = XDB::fetchOneCell('SELECT  state = \'active\'
+                                       FROM  accounts
+                                      WHERE  uid = {?}',
+                                    $user->id());
+        $pending = XDB::fetchOneCell('SELECT  uid
+                                        FROM  register_pending_xnet
+                                       WHERE  uid = {?}',
+                                     $user->id());
+        $requested = AccountReq::isPending($user->id());
+
+        if ($active || $pending || $requested) {
+            return false;
         }
+        return true;
+    }
+
+    function handler_admin_member_suggest($page, $hruid, $email)
+    {
+        $page->changeTpl('xnetgrp/membres-suggest.tpl');
+
+        // FS#703 : $_GET is urldecoded twice, hence
+        // + (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);
+
+        if (Post::has('suggest')) {
+            if (Post::t('suggest') == 'yes') {
+                $user = S::user();
+                $group = Platal::globals()->asso('nom');
+                $request = new AccountReq($user, $hruid, $email, $group);
+                $request->submit();
+                $page->trigSuccessRedirect('Un email va bien être envoyé à ' . $email . ' pour l\'activation de son compte.',
+                                           $group . '/member/' . $hruid);
+            } else {
+                pl_redirect('member/' . $hruid);
+            }
+        }
+        $page->assign('email', $email);
+        $page->assign('hruid', $hruid);
     }
 
     function handler_admin_member_new_ajax($page)
@@ -839,11 +903,10 @@ class XnetGrpModule extends PLModule
         } else {
             $page->trigWarning('Tu as été désinscrit du groupe, mais des erreurs se sont produites lors des désinscriptions des alias et des listes de diffusion.');
         }
-        if ($user->type == 'xnet' && $hasSingleGroup && Post::has('accountDeletion')) {
-            XDB::execute('DELETE FROM  acounts
-                                WHERE  uid = {?}',
-                         $user->id());
-            $page->trigSuccess('Ton compte a bien été supprimé.');
+
+        // If user is of type xnet account and this was her last group, disable the account.
+        if ($user->type == 'xnet' && $hasSingleGroup) {
+            $user->clear(true);
         }
         $page->assign('is_member', is_member(true));
     }
@@ -879,28 +942,13 @@ class XnetGrpModule extends PLModule
             $page->trigWarning("{$user->fullName()} a été désinscrit du groupe, mais des erreurs subsistent !");
         }
 
-        // Either deletes or notifies site administrators if it was the last group
-        // of a xnet account.
+        // If user is of type xnet account and this was her last group, disable the account.
         if ($user->type == 'xnet' && $hasSingleGroup) {
-            if ($user->state == 'pending') {
-                // If the user has never logged in the site, we delete her account.
-                XDB::execute('DELETE FROM  acounts
-                                    WHERE  uid = {?}',
-                             $user->id());
-            } else {
-                // It the user has already logged in the site, we notify site
-                // administrators that there is a new xnet account without any
-                // group.
-                $mailer = new PlMailer('xnetgrp/unsubscription.mail.tpl');
-                $mailer->assign('user', $user);
-                $mailer->assign('groupId', $globals->asso('id'));
-                $mailer->assign('groupName', $globals->asso('nom'));
-                $mailer->send();
-            }
+            $user->clear(true);
         }
     }
 
-    private function changeLogin(PlPage &$page, PlUser &$user, $login)
+    private function changeLogin(PlPage $page, PlUser $user, $login)
     {
         // Search the user's uid.
         $xuser = User::getSilent($login);
@@ -944,6 +992,7 @@ class XnetGrpModule extends PLModule
         $mmlist = new MMList(S::user(), $globals->asso('mail_domain'));
 
         if (Post::has('change')) {
+            require_once 'emails.inc.php';
             S::assert_xsrf_token();
 
             // Convert user status to X
@@ -957,7 +1006,7 @@ 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') {
+            if ($user->type == 'virtual' || ($user->type == 'xnet' && !$user->perms)) {
                 XDB::query('UPDATE  accounts
                                SET  full_name = {?}, directory_name = {?}, display_name = {?},
                                     sex = {?}, email = {?}, type = {?}
@@ -965,23 +1014,27 @@ class XnetGrpModule extends PLModule
                            Post::t('full_name'), Post::t('directory_name'), Post::t('display_name'),
                            (Post::t('sex') == 'male') ? 'male' : 'female', Post::t('email'),
                            (Post::t('type') == 'xnet') ? 'xnet' : 'virtual', $user->id());
-                // If user is of type xnet and new password is given.
-                if (!Post::blank('pwhash') && Post::t('type') == 'xnet') {
-                    XDB::query('UPDATE  accounts
-                                   SET  password = {?}
-                                 WHERE  uid = {?}',
-                               Post::t('pwhash'), $user->id());
-                }
             } else if (!$user->perms) {
                 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'));
+            }
             if (XDB::affectedRows()) {
                 $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é.');
+            }
+
             // Update group params for user
             $perms = Post::v('group_perms');
             $comm  = Post::t('comm');
@@ -1039,16 +1092,17 @@ class XnetGrpModule extends PLModule
             foreach (Env::v('ml3', array()) as $ml => $state) {
                 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, $from_email, $ml, $globals->asso('mail_domain'));
+                        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) {
-                    add_to_list_alias($user, $ml, $globals->asso('mail_domain'));
+                    add_to_list_alias($user->id(), $local_part, $globals->asso('mail_domain'));
                     $page->trigSuccess("{$user->fullName()} a été abonné à $ml.");
                 } else {
-                    delete_from_list_alias($user, $ml, $globals->asso('mail_domain'));
+                    delete_from_list_alias($user->id(), $local_part, $globals->asso('mail_domain'));
                     $page->trigSuccess("{$user->fullName()} a été désabonné de $ml.");
                 }
             }
@@ -1057,9 +1111,8 @@ class XnetGrpModule extends PLModule
         $res = XDB::rawFetchAllAssoc('SHOW COLUMNS FROM group_members LIKE \'position\'');
         $positions = str_replace(array('enum(', ')', '\''), '', $res[0]['Type']);
 
-        $page->addJsLink('password.js');
-        $page->assign('onlyGroup', ($user->groupCount() == 1));
         $page->assign('user', $user);
+        $page->assign('suggest', $this->suggest($user));
         $page->assign('listes', $mmlist->get_lists($user->forlifeEmail()));
         $page->assign('alias', $user->emailGroupAliases($globals->asso('mail_domain')));
         $page->assign('positions', explode(',', $positions));
@@ -1199,13 +1252,13 @@ class XnetGrpModule extends PLModule
                 if (!empty($art['contact_html'])) {
                     $fulltext .= "\n\n'''Contacts :'''\\\\\n" . $art['contact_html'];
                 }
-                $post = null;/*
+                $post = null;
                 if ($globals->asso('forum')) {
                     require_once 'banana/forum.inc.php';
                     $banana = new ForumsBanana(S::user());
                     $post = $banana->post($globals->asso('forum'), null,
                                           $art['titre'], MiniWiki::wikiToText($fulltext, false, 0, 80));
-                }*/
+                }
                 XDB::query('INSERT INTO  group_announces (uid, asso_id, create_date, titre, texte, contacts,
                                                           expiration, promo_min, promo_max, flags, post_id)
                                  VALUES  ({?}, {?}, NOW(), {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',