migrate inscrire.php
[platal.git] / modules / xnetgrp.php
index 3b0c455..9e9ddb3 100644 (file)
@@ -24,11 +24,16 @@ class XnetGrpModule extends PLModule
     function handlers()
     {
         return array(
-            'grp'            => $this->make_hook('index',    AUTH_PUBLIC),
-            'grp/asso.php'   => $this->make_hook('index',    AUTH_PUBLIC),
-            'grp/logo'       => $this->make_hook('logo',     AUTH_PUBLIC),
-            'grp/edit'       => $this->make_hook('edit',     AUTH_MDP),
-            'grp/annuaire'   => $this->make_hook('annuaire', AUTH_MDP),
+            'grp'            => $this->make_hook('index',     AUTH_PUBLIC),
+            'grp/asso.php'   => $this->make_hook('index',     AUTH_PUBLIC),
+            'grp/logo'       => $this->make_hook('logo',      AUTH_PUBLIC),
+            'grp/edit'       => $this->make_hook('edit',      AUTH_MDP),
+            'grp/mail'       => $this->make_hook('mail',      AUTH_MDP),
+            'grp/annuaire'   => $this->make_hook('annuaire',  AUTH_MDP),
+            'grp/subscribe'  => $this->make_hook('subscribe', AUTH_MDP),
+
+            'grp/admin/annuaire'
+                 => $this->make_hook('admin_annuaire', AUTH_MDP),
         );
     }
 
@@ -139,6 +144,33 @@ class XnetGrpModule extends PLModule
         }
     }
 
+    function handler_mail(&$page)
+    {
+        global $globals;
+
+        require_once 'lists.inc.php';
+
+        new_groupadmin_page('xnet/groupe/mail.tpl');
+        $client =& lists_xmlrpc(Session::getInt('uid'),
+                                Session::get('password'),
+                                $globals->asso('mail_domain'));
+        $page->assign('listes', $client->get_lists());
+
+        if (Post::has('send')) {
+            $from  = Post::get('from');
+            $sujet = Post::get('sujet');
+            $body  = Post::get('body');
+
+            $mls = array_keys(Env::getMixed('ml', array()));
+
+            require_once 'xnet/mail.inc.php';
+            $tos = get_all_redirects(Post::has('membres'), $mls, $client);
+            send_xnet_mails($from, $sujet, $body, $tos, Post::get('replyto'));
+            $page->kill("Mail envoyé !");
+            $page->assign('sent', true);
+        }
+    }
+
     function handler_annuaire(&$page)
     {
         global $globals;
@@ -240,6 +272,182 @@ class XnetGrpModule extends PLModule
 
         $page->assign('ann', $ann);
     }
+
+    function handler_subscribe(&$page, $u = null)
+    {
+        $page->changeTpl('xnet/groupe/inscrire.tpl');
+
+        $page->useMenu();
+        $page->setType($globals->asso('cat'));
+        $page->assign('asso', $globals->asso());
+        $page->assign('admin', may_update());
+
+        if (!$globals->asso('inscriptible'))
+                $page->kill("Il n'est pas possible de s'inscire en ligne à ce "
+                            ."groupe. Essaie de joindre le contact indiqué "
+                            ."sur la page de présentation.");
+
+        if (!is_null($u) && may_update()) {
+            $page->assign('u', $u);
+            $res = $globals->xdb->query("SELECT nom, prenom, promo, user_id
+                                           FROM auth_user_md5 AS u
+                                     INNER JOIN aliases AS al ON (al.id = u.user_id
+                                                                  AND al.type != 'liste')
+                                          WHERE al.alias = {?}", $u);
+
+            if (list($nom, $prenom, $promo, $uid) = $res->fetchOneRow()) {
+                $res = $globals->xdb->query("SELECT  COUNT(*)
+                                               FROM  groupex.membres AS m
+                                         INNER JOIN  aliases  AS a ON (m.uid = a.id
+                                                                       AND a.type != 'homonyme')
+                                              WHERE  a.alias = {?} AND m.asso_id = {?}",
+                                            $u, $globals->asso('id'));
+                $n   = $res->fetchOneCell();
+                if ($n) {
+                    $page->trig_run("$prenom $nom est déjà membre du groupe !");
+                }
+                elseif (Env::has('accept'))
+                {
+                    $globals->xdb->execute("INSERT INTO groupex.membres
+                                            VALUES ({?}, {?}, 'membre', 'X', NULL, NULL, NULL, NULL)",
+                                            $globals->asso('id'), $uid);
+                    require_once 'diogenes/diogenes.hermes.inc.php';
+                    $mailer = new HermesMailer();
+                    $mailer->addTo("$u@polytechnique.org");
+                    $mailer->setFrom('"'.Session::get('prenom').' '.Session::get('nom')
+                                     .'" <'.Session::get('forlife').'@polytechnique.org>');
+                    $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription');
+                    $message = "Cher Camarade,\n"
+                             . "\n"
+                             . "  Suite à ta demande d'adhésion à ".$globals->asso('nom').",\n"
+                             . "j'ai le plaisir de t'annoncer que ton inscription a été validée !\n"
+                             . "\n"
+                             . "Bien cordialement,\n"
+                             . "{$_SESSION["prenom"]} {$_SESSION["nom"]}.";
+                    $mailer->setTxtBody($message);
+                    $mailer->send();
+                    $page->kill("$prenom $nom a bien été inscrit");
+                }
+                elseif (Env::has('refuse'))
+                {
+                    require_once 'diogenes/diogenes.hermes.inc.php';
+                    $mailer = new HermesMailer();
+                    $mailer->addTo("$u@polytechnique.org");
+                    $mailer->setFrom('"'.Session::get('prenom').' '.Session::get('nom')
+                                     .'" <'.Session::get('forlife').'@polytechnique.org>');
+                    $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription annulée');
+                    $mailer->setTxtBody(Env::get('motif'));
+                    $mailer->send();
+                    $page->kill("la demande $prenom $nom a bien été refusée");
+                } else {
+                    $page->assign('show_form', true);
+                    $page->assign('prenom', $prenom);
+                    $page->assign('nom', $nom);
+                    $page->assign('promo', $promo);
+                    $page->assign('uid', $uid);
+                }
+                return;
+            }
+            return PL_NOT_FOUND;
+        }
+
+        if (is_member()) {
+            $page->kill("tu es déjà membre !");
+            return;
+        }
+
+        if (Post::has('inscrire')) {
+            $res = $globals->xdb->query('SELECT  IF(m.email IS NULL,
+                                                    CONCAT(al.alias,"@polytechnique.org"),
+                                                    m.email)
+                                           FROM  groupex.membres AS m
+                                     INNER JOIN  aliases         AS al ON (al.type = "a_vie"
+                                                                           AND al.id = m.uid)
+                                          WHERE  perms="admin" AND m.asso_id = {?}',
+                                         $globals->asso('id'));
+            $emails = $res->fetchColumn();
+            $to     = implode(',', $emails);
+
+            $append = "\n"
+                    . "-- \n"
+                    . "Ce message a été envoyé suite à la demande d'inscription de\n"
+                    . Session::get('prenom').' '.Session::get('nom').' (X'.Session::get('promo').")\n"
+                    . "Via le site www.polytechnique.net. Tu peux choisir de valider ou\n"
+                    . "de refuser sa demande d'inscription depuis la page :\n"
+                    .
+                    "http://www.polytechnique.net/".$globals->asso("diminutif")."/subscribe/"
+                        .Session::get('forlife')."\n"
+                    . "\n"
+                    . "En cas de problème, contacter l'équipe de Polytechnique.org\n"
+                    . "à l'adresse : support@polytechnique.org\n";
+
+            if (!$to) {
+                $to = $globals->asso("mail").", support@polytechnique.org";
+                $append = "\n-- \nLe groupe ".$globals->asso("nom")
+                        ." n'a pas d'administrateur, l'équipe de"
+                        ." Polytechnique.org a été prévenue et va rapidement"
+                        ." résoudre ce problème.\n";
+            }
+
+            require_once 'diogenes/diogenes.hermes.inc.php';
+            $mailer = new HermesMailer();
+            $mailer->addTo($to);
+            $mailer->setFrom('"'.Session::get('prenom').' '.Session::get('nom')
+                             .'" <'.Session::get('forlife').'@polytechnique.org>');
+            $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription');
+            $mailer->setTxtBody(Post::get('message').$append);
+            $mailer->send();
+        }
+    }
+
+    function handler_admin_annuaire(&$page)
+    {
+        global $globals;
+
+        require_once 'lists.inc.php';
+        require_once 'xnet/mail.inc.php';
+
+        new_groupadmin_page('xnet/groupe/annuaire-admin.tpl');
+        $client =& lists_xmlrpc(Session::getInt('uid'),
+                                Session::get('password'),
+                                $globals->asso('mail_domain'));
+        $lists  = $client->get_lists();
+        if (!$lists) $lists = array();
+        $listes = array_map(create_function('$arr', 'return $arr["list"];'), $lists);
+
+        $subscribers = array();
+
+        foreach ($listes as $list) {
+            list(,$members) = $client->get_members($list);
+            $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
+            $subscribers = array_unique(array_merge($subscribers, $mails));
+        }
+
+        $not_in_group_x = array();
+        $not_in_group_ext = array();
+
+        foreach ($subscribers as $mail) {
+            $res = $globals->xdb->query(
+                       'SELECT  COUNT(*)
+                          FROM  groupex.membres AS m
+                     LEFT JOIN  auth_user_md5   AS u ON (m.uid=u.user_id AND m.uid<50000)
+                     LEFT JOIN  aliases         AS a ON (a.id=u.user_id and a.type="a_vie")
+                         WHERE  asso_id = {?} AND
+                                (m.email = {?} OR CONCAT(a.alias, "@polytechnique.org") = {?})',
+                        $globals->asso('id'), $mail, $mail);
+            if ($res->fetchOneCell() == 0) {
+                if (strstr($mail, '@polytechnique.org') === false) {
+                    $not_in_group_ext[] = $mail;
+                } else {
+                    $not_in_group_x[] = $mail;
+                }
+            }
+        }
+
+        $page->assign('not_in_group_ext', $not_in_group_ext);
+        $page->assign('not_in_group_x', $not_in_group_x);
+        $page->assign('lists', $lists);
+    }
 }
 
 ?>