Adds support for the user interface of Google Apps:
[platal.git] / modules / email.php
index a5975cd..f35b354 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -31,7 +31,7 @@ class EmailModule extends PLModule
             'emails/redirect' => $this->make_hook('redirect', AUTH_MDP),
             'emails/send'     => $this->make_hook('send', AUTH_MDP),
             'emails/antispam/submit'  => $this->make_hook('submit', AUTH_COOKIE),
-            'emails/test'     => $this->make_hook('test', AUTH_PUBLIC),
+            'emails/test'     => $this->make_hook('test', AUTH_COOKIE, 'user', NO_AUTH),
 
             'admin/emails/duplicated' => $this->make_hook('duplicated', AUTH_MDP, 'admin'),
             'admin/emails/watch'      => $this->make_hook('duplicated', AUTH_MDP, 'admin'),
@@ -65,14 +65,21 @@ class EmailModule extends PLModule
               ORDER BY  LENGTH(alias)";
         $page->assign('aliases', XDB::iterator($sql, $uid));
 
-               $homonyme = XDB::query("SELECT alias FROM aliases INNER JOIN homonymes ON (id = homonyme_id) WHERE user_id = {?} AND type = 'homonyme'", $uid);
-               $page->assign('homonyme', $homonyme->fetchOneCell());
-               
+    $homonyme = XDB::query("SELECT alias FROM aliases INNER JOIN homonymes ON (id = homonyme_id) WHERE user_id = {?} AND type = 'homonyme'", $uid);
+    $page->assign('homonyme', $homonyme->fetchOneCell());
+
+        // Affichage des redirections de l'utilisateur.
         $sql = "SELECT email
                 FROM emails
                 WHERE uid = {?} AND FIND_IN_SET('active', flags)";
         $page->assign('mails', XDB::iterator($sql, $uid));
 
+        // Affichage des backends actifs de stockage des emails.
+        $sql = "SELECT  mail_storage
+                  FROM  auth_user_md5
+                 WHERE  user_id = {?}";
+        $storages = XDB::query($sql, $uid)->fetchOneCell();
+        $page->assign('storage', explode(',', $storages));
 
         // on regarde si l'utilisateur a un alias et si oui on l'affiche !
         $forlife = S::v('forlife');
@@ -210,20 +217,40 @@ class EmailModule extends PLModule
             $retour = $redirect->delete_email($email);
             $page->assign('retour', $retour);
         }
-               
-               if ($action == 'active' && $email) {
-                       $redirect->modify_one_email($email, true);
-               }
-               
-               if ($action == 'inactive' && $email) {
-                       $redirect->modify_one_email($email, false);
-               }
-               
-               if ($action == 'rewrite' && $email) {
-                       $rewrite = @func_get_arg(3);
-                       $redirect->modify_one_email_redirect($email, $rewrite);
-               }
-               
+
+        if ($action == 'active' && $email) {
+            $redirect->modify_one_email($email, true);
+        }
+
+        if ($action == 'inactive' && $email) {
+            $redirect->modify_one_email($email, false);
+        }
+
+        if ($action == 'rewrite' && $email) {
+            $rewrite = @func_get_arg(3);
+            $redirect->modify_one_email_redirect($email, $rewrite);
+        }
+
+        if ($action == 'storage') {
+            if ($email == 'imap') {
+                $storage = new MailStorageIMAP(S::v('uid'));
+            } else if ($email == 'googleapps') {
+                $storage = new MailStorageGoogleApps(S::v('uid'));
+            } else {
+                $storage = NULL;
+            }
+
+            if ($storage) {
+                $subaction = @func_get_arg(3);
+                if ($subaction == 'active') {
+                    $storage->enable();
+                }
+                if ($subaction == 'inactive') {
+                    $storage->disable();
+                }
+            }
+        }
+
         if (Env::has('emailop')) {
             $actifs = Env::v('emails_actifs', Array());
             print_r(Env::v('emails_rewrite'));
@@ -255,8 +282,21 @@ class EmailModule extends PLModule
                    FROM  aliases
                   WHERE  id={?} AND (type='a_vie' OR type='alias')
                ORDER BY  !FIND_IN_SET('usage',flags), LENGTH(alias)", $uid);
+
         $page->assign('alias', $res->fetchAllAssoc());
         $page->assign('emails',$redirect->emails);
+
+        $res = XDB::query(
+                "SELECT  mail_storage
+                   FROM  auth_user_md5
+                  WHERE  user_id = {?}", $uid);
+        $page->assign('storage', explode(',', $res->fetchOneCell()));
+
+        $res = XDB::query(
+                "SELECT  g_status
+                   FROM  gapps_accounts
+                  WHERE  l_userid = {?}", $uid);
+        $page->assign('googleapps', ($res->numRows() > 0 ? $res->fetchOneCell() : false));
     }
 
     function handler_antispam(&$page, $statut_filtre = null)
@@ -338,52 +378,57 @@ class EmailModule extends PLModule
                 return join(', ', $ret);
             }
 
+            $error = false;
             foreach ($_FILES as &$file) {
                 if ($file['name'] && !PlUpload::get($file, S::v('forlife'), 'emails.send', false)) {
-                    $page->trig("Impossible de télécharger '" . pl_entities($file['name']) . "'");
+                    $page->trig(PlUpload::$lastError);
+                    $error = true;
+                    break;
                 }
             }
 
-            XDB::execute("DELETE FROM  email_send_save
-                                WHERE  uid = {?}", S::i('uid'));
-
-            $to2  = getEmails(Env::v('to_contacts'));
-            $cc2  = getEmails(Env::v('cc_contacts'));
-            $txt  = str_replace('^M', '', Env::v('contenu'));
-            $to   = Env::v('to');
-            $subj = Env::v('sujet');
-            $from = Env::v('from');
-            $cc   = trim(Env::v('cc'));
-            $bcc  = trim(Env::v('bcc'));
-
-            if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) {
-                $page->trig("Indique au moins un destinataire.");
-                $page->assign('uploaded_f', PlUpload::listFilenames(S::v('forlife'), 'emails.send'));
-            } else {
-                $mymail = new PlMailer();
-                $mymail->setFrom($from);
-                $mymail->setSubject($subj);
-                if (!empty($to))  { $mymail->addTo($to); }
-                if (!empty($cc))  { $mymail->addCc($cc); }
-                if (!empty($bcc)) { $mymail->addBcc($bcc); }
-                if (!empty($to2)) { $mymail->addTo($to2); }
-                if (!empty($cc2)) { $mymail->addCc($cc2); }
-                $files =& PlUpload::listFiles(S::v('forlife'), 'emails.send');
-                foreach ($files as $name=>&$upload) {
-                    $mymail->addUploadAttachment($upload, $name);
-                }
-                if (Env::v('nowiki')) {
-                    $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
-                } else {
-                    $mymail->setWikiBody($txt);
-                }
-                if ($mymail->send()) {
-                    $page->trig("Ton mail a bien été envoyé.");
-                    $_REQUEST = array('bcc' => S::v('bestalias').'@'.$globals->mail->domain);
-                    PlUpload::clear(S::v('forlife'), 'emails.send');
-                } else {
-                    $page->trig("Erreur lors de l'envoi du courriel, réessaye.");
+            if (!$error) {
+                XDB::execute("DELETE FROM  email_send_save
+                                    WHERE  uid = {?}", S::i('uid'));
+
+                $to2  = getEmails(Env::v('to_contacts'));
+                $cc2  = getEmails(Env::v('cc_contacts'));
+                $txt  = str_replace('^M', '', Env::v('contenu'));
+                $to   = Env::v('to');
+                $subj = Env::v('sujet');
+                $from = Env::v('from');
+                $cc   = trim(Env::v('cc'));
+                $bcc  = trim(Env::v('bcc'));
+
+                if (empty($to) && empty($cc) && empty($to2) && empty($bcc) && empty($cc2)) {
+                    $page->trig("Indique au moins un destinataire.");
                     $page->assign('uploaded_f', PlUpload::listFilenames(S::v('forlife'), 'emails.send'));
+                } else {
+                    $mymail = new PlMailer();
+                    $mymail->setFrom($from);
+                    $mymail->setSubject($subj);
+                    if (!empty($to))  { $mymail->addTo($to); }
+                    if (!empty($cc))  { $mymail->addCc($cc); }
+                    if (!empty($bcc)) { $mymail->addBcc($bcc); }
+                    if (!empty($to2)) { $mymail->addTo($to2); }
+                    if (!empty($cc2)) { $mymail->addCc($cc2); }
+                    $files =& PlUpload::listFiles(S::v('forlife'), 'emails.send');
+                    foreach ($files as $name=>&$upload) {
+                        $mymail->addUploadAttachment($upload, $name);
+                    }
+                    if (Env::v('nowiki')) {
+                        $mymail->setTxtBody(wordwrap($txt, 78, "\n"));
+                    } else {
+                        $mymail->setWikiBody($txt);
+                    }
+                    if ($mymail->send()) {
+                        $page->trig("Ton mail a bien été envoyé.");
+                        $_REQUEST = array('bcc' => S::v('bestalias').'@'.$globals->mail->domain);
+                        PlUpload::clear(S::v('forlife'), 'emails.send');
+                    } else {
+                        $page->trig("Erreur lors de l'envoi du courriel, réessaye.");
+                        $page->assign('uploaded_f', PlUpload::listFilenames(S::v('forlife'), 'emails.send'));
+                    }
                 }
             }
         } else {
@@ -407,7 +452,7 @@ class EmailModule extends PLModule
                   WHERE  c.uid = {?}
                  ORDER BY u.nom, u.prenom", S::v('uid'));
         $page->assign('contacts', $res->fetchAllAssoc());
-        $page->assign('maxsize', ini_get('post_max_size') . 'o');
+        $page->assign('maxsize', ini_get('upload_max_filesize') . 'o');
     }
 
     function handler_test(&$page, $forlife = null)
@@ -416,7 +461,7 @@ class EmailModule extends PLModule
         if (!S::has_perms() || !$forlife) {
             $forlife = S::v('bestalias');
         }
-        $mailer = new PlMailer('emails/mail.test.tpl');
+        $mailer = new PlMailer('emails/test.mail.tpl');
         $mailer->assign('email', $forlife . '@' . $globals->mail->domain);
         $iterator = XDB::iterator("SELECT  email
                                      FROM  emails AS e
@@ -621,12 +666,12 @@ L'équipe d'administration <support@" . $globals->mail->domain . '>';
         $page->changeTpl('emails/lost.tpl');
 
         $page->assign('lost_emails', XDB::iterator('
-                                       SELECT u.user_id,       a.alias
-                                       FROM auth_user_md5 AS u
-                                               INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = "a_vie")
-                                               LEFT JOIN emails AS e ON (u.user_id=e.uid AND FIND_IN_SET("active",e.flags))
-                                       WHERE e.uid IS NULL AND u.deces = 0
-                                       ORDER BY u.promo DESC, u.nom, u.prenom'));
+          SELECT u.user_id, a.alias
+          FROM auth_user_md5 AS u
+            INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = "a_vie")
+            LEFT JOIN emails AS e ON (u.user_id=e.uid AND FIND_IN_SET("active",e.flags))
+          WHERE e.uid IS NULL AND u.deces = 0
+          ORDER BY u.promo DESC, u.nom, u.prenom'));
     }
 }