Use the 'groups' perm instead of 'user' on X.net
[platal.git] / modules / xnetlists.php
index f58955f..c0eaa38 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2009 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -28,32 +28,33 @@ class XnetListsModule extends ListsModule
     function handlers()
     {
         return array(
-            '%grp/lists'           => $this->make_hook('lists',     AUTH_MDP, 'groupmember'),
-            '%grp/lists/create'    => $this->make_hook('create',    AUTH_MDP, 'groupmember'),
+            '%grp/lists'              => $this->make_hook('lists',    AUTH_MDP,    'groupmember'),
+            '%grp/lists/create'       => $this->make_hook('create',   AUTH_MDP,    'groupmember'),
 
-            '%grp/lists/members'   => $this->make_hook('members',   AUTH_COOKIE),
-            '%grp/lists/annu'      => $this->make_hook('annu',      AUTH_COOKIE),
-            '%grp/lists/archives'  => $this->make_hook('archives',  AUTH_COOKIE),
-            '%grp/lists/archives/rss' => $this->make_hook('rss',    AUTH_PUBLIC),
+            '%grp/lists/members'      => $this->make_hook('members',  AUTH_COOKIE, 'groups'),
+            '%grp/lists/csv'          => $this->make_hook('csv',      AUTH_COOKIE, 'groups'),
+            '%grp/lists/annu'         => $this->make_hook('annu',     AUTH_COOKIE, 'groups'),
+            '%grp/lists/archives'     => $this->make_hook('archives', AUTH_COOKIE, 'groups'),
+            '%grp/lists/archives/rss' => $this->make_hook('rss',      AUTH_PUBLIC),
 
-            '%grp/lists/moderate'  => $this->make_hook('moderate',  AUTH_MDP),
-            '%grp/lists/admin'     => $this->make_hook('admin',     AUTH_MDP),
-            '%grp/lists/options'   => $this->make_hook('options',   AUTH_MDP),
-            '%grp/lists/delete'    => $this->make_hook('delete',    AUTH_MDP),
+            '%grp/lists/moderate'     => $this->make_hook('moderate', AUTH_MDP,    'groups'),
+            '%grp/lists/admin'        => $this->make_hook('admin',    AUTH_MDP,    'groups'),
+            '%grp/lists/options'      => $this->make_hook('options',  AUTH_MDP,    'groups'),
+            '%grp/lists/delete'       => $this->make_hook('delete',   AUTH_MDP,    'groups'),
 
-            '%grp/lists/soptions'  => $this->make_hook('soptions',  AUTH_MDP),
-            '%grp/lists/check'     => $this->make_hook('check',     AUTH_MDP),
-            '%grp/lists/sync'      => $this->make_hook('sync',      AUTH_MDP),
+            '%grp/lists/soptions'     => $this->make_hook('soptions', AUTH_MDP,    'groups'),
+            '%grp/lists/check'        => $this->make_hook('check',    AUTH_MDP,    'groups'),
+            '%grp/lists/sync'         => $this->make_hook('sync',     AUTH_MDP,    'groups'),
 
-            '%grp/alias/admin'     => $this->make_hook('aadmin',    AUTH_MDP, 'groupadmin'),
-            '%grp/alias/create'    => $this->make_hook('acreate',   AUTH_MDP, 'groupadmin'),
+            '%grp/alias/admin'        => $this->make_hook('aadmin',   AUTH_MDP,    'groupadmin'),
+            '%grp/alias/create'       => $this->make_hook('acreate',  AUTH_MDP,    'groupadmin'),
 
             /* hack: lists uses that */
-            'profile' => $this->make_hook('profile', AUTH_PUBLIC),
+            'profile'                 => $this->make_hook('profile',  AUTH_PUBLIC),
         );
     }
 
-    function prepare_client(&$page, $user = null)
+    function prepare_client($page, $user = null)
     {
         global $globals;
         Platal::load('lists', 'lists.inc.php');
@@ -69,9 +70,10 @@ class XnetListsModule extends ListsModule
         return $globals->asso('mail_domain');
     }
 
-    function handler_lists(&$page)
+    function handler_lists($page, $order_by = null, $order = null)
     {
         global $globals;
+        require_once 'emails.inc.php';
 
         if (!$globals->asso('mail_domain')) {
             return PL_NOT_FOUND;
@@ -93,32 +95,61 @@ class XnetListsModule extends ListsModule
         if (Post::has('del_alias') && may_update()) {
             S::assert_xsrf_token();
 
-            $alias = Post::v('del_alias');
-            // prevent group admin from erasing aliases from other groups
-            $alias = substr($alias, 0, strpos($alias, '@')).'@'.$globals->asso('mail_domain');
-            XDB::query(
-                    'DELETE FROM  r, v
-                           USING  x4dat.virtual AS v
-                       LEFT JOIN  x4dat.virtual_redirect AS r USING(vid)
-                           WHERE  v.alias={?}', $alias);
-            $page->trigSuccess(Post::v('del_alias')." supprimé !");
+            $alias = Post::t('del_alias');
+            list($local_part, ) = explode('@', $alias);
+            delete_list_alias($local_part, $globals->asso('mail_domain'));
+            $page->trigSuccess($alias . ' supprimé&nbsp;!');
         }
 
         $listes = $this->client->get_lists();
-        $page->assign('listes',$listes);
+        // Default ordering is by ascending names.
+        if (is_null($order_by) || is_null($order)
+            || !in_array($order_by, array('list', 'desc', 'nbsub'))
+            || !in_array($order, array('asc', 'desc'))) {
+            $order_by = 'list';
+            $order = 'asc';
+        }
 
-        $alias  = XDB::iterator(
-                'SELECT  alias,type
-                   FROM  x4dat.virtual
-                  WHERE  alias
-                   LIKE  {?} AND type="user"
-               ORDER BY  alias', '%@'.$globals->asso('mail_domain'));
-        $page->assign('alias', $alias);
+        $compare = function ($a, $b) use ($order_by, $order)
+        {
+            switch ($order_by) {
+              case 'desc':
+                $a[$order_by] = replace_accent($a[$order_by]);
+                $b[$order_by] = replace_accent($b[$order_by]);
+              case 'list':
+                $res = strcasecmp($a[$order_by], $b[$order_by]);
+                break;
+              case 'nbsub':
+                $res = $a[$order_by] - $b[$order_by];
+                break;
+              default:
+                $res = 0;
+            }
 
+            if ($order == 'asc') {
+                return $res;
+            }
+            return $res * -1;
+        };
+        usort($listes, $compare);
+        $page->assign('listes', $listes);
+        $page->assign('order_by', $order_by);
+        $page->assign('order', $order);
+        $page->assign('aliases', iterate_list_alias($globals->asso('mail_domain')));
         $page->assign('may_update', may_update());
+        if (S::suid()) {
+            $page->trigWarning("Attention&nbsp;: l'affichage des listes de diffusion ne tient pas compte de l'option « Voir le site comme&hellip; ».");
+        }
+
+        if (count($listes) > 0 && !$globals->asso('has_ml')) {
+            XDB::execute("UPDATE  groups
+                             SET  flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
+                           WHERE  id = {?}",
+                         $globals->asso('id'));
+        }
     }
 
-    function handler_create(&$page)
+    function handler_create($page)
     {
         global $globals;
 
@@ -134,58 +165,47 @@ class XnetListsModule extends ListsModule
             S::assert_xsrf_token();
         }
 
-        if (!Post::has('liste')) {
-            $page->trigError('champs «adresse souhaitée» vide');
+        if (!Post::has('liste') || !Post::t('liste')) {
+            $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
             return;
         }
 
-        $liste = strtolower(Post::v('liste'));
-
-        if (!preg_match("/^[a-zA-Z0-9\-]*$/", $liste)) {
+        $list = strtolower(Post::t('liste'));
+        if (!preg_match("/^[a-zA-Z0-9\-]*$/", $list)) {
             $page->trigError('le nom de la liste ne doit contenir que des lettres non accentuées, chiffres et tirets');
             return;
         }
 
-        $new = $liste.'@'.$globals->asso('mail_domain');
-        $res = XDB::query('SELECT alias FROM x4dat.virtual WHERE alias={?}', $new);
-
-        if ($res->numRows()) {
-            $page->trigError('cet alias est déjà pris');
+        require_once 'emails.inc.php';
+        if (list_exist($list, $globals->asso('mail_domain'))) {
+            $page->trigError('Cet alias est déjà pris.');
             return;
         }
-        if (!Post::v('desc')) {
-            $page->trigError('le sujet est vide');
+        if (!Post::t('desc')) {
+            $page->trigError('Le sujet est vide.');
             return;
         }
 
-        $ret = $this->client->create_list(
-                    $liste, utf8_decode(Post::v('desc')), Post::v('advertise'),
-                    Post::v('modlevel'), Post::v('inslevel'),
-                    array(S::user()->forlifeEmail()), array(S::user()->forlifeEmail()));
+        $success = $this->client->create_list($list, utf8_decode(Post::t('desc')), Post::t('advertise'),
+                                              Post::t('modlevel'), Post::t('inslevel'),
+                                              array(S::user()->forlifeEmail()), array(S::user()->forlifeEmail()));
 
-        $dom = strtolower($globals->asso("mail_domain"));
-        $red = $dom.'_'.$liste;
-
-        if (!$ret) {
+        if (!$success) {
             $page->kill("Un problème est survenu, contacter "
                         ."<a href='mailto:support@m4x.org'>support@m4x.org</a>");
             return;
         }
-        foreach (array('', 'owner', 'admin', 'bounces', 'unsubscribe') as $app) {
-            $mdir = $app == '' ? '+post' : '+' . $app;
-            if (!empty($app)) {
-                $app  = '-' . $app;
-            }
-            XDB::execute('INSERT INTO x4dat.virtual (alias,type)
-                                    VALUES({?},{?})', $liste. $app . '@'.$dom, 'list');
-            XDB::execute('INSERT INTO x4dat.virtual_redirect (vid,redirect)
-                                    VALUES ({?}, {?})', XDB::insertId(),
-                                   $red . $mdir . '@listes.polytechnique.org');
-        }
-        pl_redirect('lists/admin/'.$liste);
+        create_list($list, $globals->asso('mail_domain'));
+
+        XDB::execute("UPDATE  groups
+                         SET  flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'has_ml')
+                       WHERE  id = {?}",
+                     $globals->asso('id'));
+
+        pl_redirect('lists/admin/' . $list);
     }
 
-    function handler_sync(&$page, $liste = null)
+    function handler_sync($page, $liste = null)
     {
         global $globals;
 
@@ -204,16 +224,13 @@ class XnetListsModule extends ListsModule
         $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
         $subscribers = array_unique($mails);
 
-        $not_in_group_x = array();
-        $not_in_group_ext = array();
-
-        $ann = XDB::iterator('SELECT  uid
-                                FROM  groupex.membres
-                               WHERE  asso_id = {?}', $globals->asso('id'));
+        $ann = XDB::fetchColumn('SELECT  uid
+                                   FROM  group_members
+                                  WHERE  asso_id = {?}', $globals->asso('id'));
+        $users = User::getBulkUsersWithUIDs($ann);
 
         $not_in_list = array();
-        while ($tmp = $ann->next()) {
-            $user = User::getWithUID($tmp['uid']);
+        foreach ($users as $user) {
             if (!in_array(strtolower($user->forlifeEmail()), $subscribers)) {
                 $not_in_list[] = $user;
             }
@@ -222,7 +239,7 @@ class XnetListsModule extends ListsModule
         $page->assign('not_in_list', $not_in_list);
     }
 
-    function handler_aadmin(&$page, $lfull = null)
+    function handler_aadmin($page, $lfull = null)
     {
         global $globals;
 
@@ -231,72 +248,37 @@ class XnetListsModule extends ListsModule
         }
         $page->changeTpl('xnetlists/alias-admin.tpl');
 
+        require_once 'emails.inc.php';
+        list($local_part, $domain) = explode('@', $lfull);
+        if ($globals->asso('mail_domain') != $domain || !preg_match("/^[a-zA-Z0-9\-\.]*$/", $local_part)) {
+            $page->trigErrorRedirect('Le nom de l\'alias est erroné.', $globals->asso('diminutif') . '/lists');
+        }
+
+
         if (Env::has('add_member')) {
             S::assert_xsrf_token();
 
-            $add = Env::v('add_member');
-            if (strstr($add, '@')) {
-                list($mbox,$dom) = explode('@', strtolower($add));
+            if (add_to_list_alias(Env::t('add_member'), $local_part, $domain)) {
+                $page->trigSuccess('Ajout réussit.');
             } else {
-                $mbox = $add;
-                $dom = 'm4x.org';
-            }
-            if ($dom == 'polytechnique.org' || $dom == 'm4x.org') {
-                $res = XDB::query(
-                        "SELECT  a.alias, b.alias
-                           FROM  x4dat.aliases AS a
-                      LEFT JOIN  x4dat.aliases AS b ON (a.id=b.id AND b.type = 'a_vie')
-                          WHERE  a.alias={?} AND a.type!='homonyme'", $mbox);
-                if (list($alias, $blias) = $res->fetchOneRow()) {
-                    $alias = empty($blias) ? $alias : $blias;
-                    XDB::query(
-                        "INSERT INTO  x4dat.virtual_redirect (vid,redirect)
-                              SELECT  vid, {?}
-                                FROM  x4dat.virtual
-                               WHERE  alias={?}", "$alias@m4x.org", $lfull);
-                   $page->trigSuccess("$alias@m4x.org ajouté");
-                } else {
-                    $page->trigError("$mbox@{$globals->mail->domain} n'existe pas.");
-                }
-            } else {
-                XDB::query(
-                        "INSERT INTO  x4dat.virtual_redirect (vid,redirect)
-                              SELECT  vid,{?}
-                                FROM  x4dat.virtual
-                               WHERE  alias={?}", "$mbox@$dom", $lfull);
-                $page->trigSuccess("$mbox@$dom ajouté");
+                $page->trigError('Ajout infructueux.');
             }
         }
 
         if (Env::has('del_member')) {
             S::assert_xsrf_token();
-            XDB::query(
-                    "DELETE FROM  x4dat.virtual_redirect
-                           USING  x4dat.virtual_redirect
-                      INNER JOIN  x4dat.virtual USING(vid)
-                           WHERE  redirect={?} AND alias={?}", Env::v('del_member'), $lfull);
-            pl_redirect('alias/admin/'.$lfull);
-        }
 
-        global $globals;
-        $emails = XDB::fetchColumn('SELECT  redirect
-                                      FROM  virtual_redirect AS vr
-                                INNER JOIN  virtual          AS v  USING(vid)
-                                     WHERE  v.alias = {?}
-                                  ORDER BY  redirect', $lfull);
-        $mem = array();
-        foreach ($emails as $email) {
-            $user = User::getSilent($email);
-            if ($user) {
-                $mem[] = array('user' => $user, 'email' => $email);
+            if (delete_from_list_alias(Env::t('del_member'), $local_part, $domain)) {
+                $page->trigSuccess('Suppression réussie.');
             } else {
-                $mem[] = array('email' => $email);
+                $page->trigError('Suppression infructueuse.');
             }
         }
-        $page->assign('mem', $mem);
+
+        $page->assign('members', list_alias_members($local_part, $domain));
     }
 
-    function handler_acreate(&$page)
+    function handler_acreate($page)
     {
         global $globals;
 
@@ -312,30 +294,27 @@ class XnetListsModule extends ListsModule
         }
 
         if (!Post::has('liste')) {
-            $page->trigError('champs «adresse souhaitée» vide');
+            $page->trigError('Le champs «&nbsp;adresse souhaitée&nbsp;» est vide.');
             return;
         }
-        $liste = Post::v('liste');
-        if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $liste)) {
-            $page->trigError('le nom de l\'alias ne doit contenir que des lettres,'
-                            .' chiffres, tirets et points');
+        $list = Post::v('liste');
+        if (!preg_match("/^[a-zA-Z0-9\-\.]*$/", $list)) {
+            $page->trigError('Le nom de l\'alias ne doit contenir que des lettres,'
+                            .' chiffres, tirets et points.');
             return;
         }
 
-        $new = $liste.'@'.$globals->asso('mail_domain');
-        $res = XDB::query('SELECT COUNT(*) FROM x4dat.virtual WHERE alias={?}', $new);
-        $n   = $res->fetchOneCell();
-        if ($n) {
-            $page->trigError('cet alias est déjà pris');
+        require_once 'emails.inc.php';
+        if (list_exist($list, $globals->asso('mail_domain'))) {
+            $page->trigError('Cet alias est déjà pris.');
             return;
         }
 
-        XDB::query('INSERT INTO x4dat.virtual (alias,type) VALUES({?}, "user")', $new);
-
-        pl_redirect("alias/admin/$new");
+        add_to_list_alias(S::i('uid'), $list, $globals->asso('mail_domain'));
+        pl_redirect('alias/admin/' . $list . '@' . $globals->asso('mail_domain'));
     }
 
-    function handler_profile(&$page, $user = null)
+    function handler_profile($page, $user = null)
     {
         http_redirect('https://www.polytechnique.org/profile/'.$user);
     }