Merge branch 'xorg/maint' into xorg/master
[platal.git] / modules / xnetgrp / mail.inc.php
index 3a51f5e..8af7b3d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
 
 // {{{ get_all_redirects
 
-function get_all_redirects($membres, $exclude, $mls, &$client)
+function get_all_redirects($membres, $mls, $client)
 {
     global $globals;
-    
+
     $tos = array();
-    
+
+    // TODO: add more filters to choose users
     if (!empty($membres)) {
-        $membres = array_map(create_function('$str', 'return "\"$str\"";'), $membres);
-        $membres = join(',', $membres);
-        $res = XDB::query(
-                    'SELECT  IF(u.nom <> "", u.nom, m.nom) AS nom,
-                             IF(u.prenom <> "", u.prenom, m.prenom) AS prenom,
-                             IF(m.email <> "", m.email, CONCAT(a.alias, "@polytechnique.org")) as email,
-                             IF(m.sexe IS NULL, FIND_IN_SET("femme", u.flags), m.sexe) AS sexe
-                       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.origine IN (' . $membres . ')
-                             AND (m.email <> "" OR u.perms <> "pending")', $globals->asso('id'));
-        $tos = $res->fetchAllAssoc();
+        $uf = new UserFilter(new UFC_Group($globals->asso('id')));
+        $tos = $uf->getUsers();
     }
 
     foreach ($mls as $ml) {
-        if (list(,$members) = $client->get_members($ml)) {
-            foreach ($members as $mem) {
-                list($m, $dom) = explode('@',$mem[1]);
-                if ($dom == $globals->mail->domain || $dom == $globals->mail->domain2) {
-                    $res = XDB::query('SELECT  prenom, nom, FIND_IN_SET("femme", u.flags) AS sexe
-                                         FROM  auth_user_md5 AS u
-                                   INNER JOIN  aliases       AS a ON u.user_id = a.id
-                                        WHERE  a.alias = {?}', $m);
-                    if ($person = $res->fetchOneAssoc()) {
-                        $person['email'] = $mem[1];
-                        $tos[] = $person;
-                    }
+        // $list_members is a (list_details, members, list_owners) array, where
+        // members is an array of (0 => name, 1 => email) arrays.
+        $list_members = $client->get_members($ml);
+        if ($list_members) {
+            foreach ($list_members[1] as $mem) {
+                $uf = new UserFilter(new UFC_Email($mem[1]));
+                $user = $uf->getUser();
+                if ($user) {
+                    $tos[] = $user;
                 } else {
-                    $res = XDB::query('SELECT prenom, nom, sexe FROM groupex.membres WHERE email={?}', $mem[1]);
-                    if ($person = $res->fetchOneAssoc()) {
-                        $person['email'] = $mem[1];
-                        $tos[] = $person;
-                    } else {
-                        $tos[] = array('email' => $mem[1]);
-                    }
+                    $tos[] = $mem[1];
                 }
             }
         }
@@ -76,30 +56,36 @@ function get_all_redirects($membres, $exclude, $mls, &$client)
 // }}}
 // {{{ _send_xnet_mail
 
-function _send_xnet_mail($user, $body, $mailer, $replyto = null)
+function _send_xnet_mail($user, $body, $wiki, $mailer, $replyto = null)
 {
-    $cher = isset($user['sexe']) ? ($user['sexe'] ? 'Chère' : 'Cher') : 'Cher(e)';
-    $nom  = isset($user['nom']) ? $user['nom'] : "";
-    $pnom = isset($user['prenom']) ? $user['prenom'] : preg_replace('!@.*!u', '', $user['email']);
-    $to   = isset($user['prenom']) ? "\"{$user['prenom']} {$user['nom']}\" <{$user['email']}>" : $user['email'];
-    
-    $text = $body;
-    $text = preg_replace('!<cher>!i',   $cher, $text);
-    $text = preg_replace('!<nom>!i',    $nom,  $text);
-    $text = preg_replace('!<prenom>!i', $pnom, $text);
-
-    $mailer->addHeader('To', $to);
+    if ($user instanceof PlUser) {
+        $dear      = $user->isFemale() ? 'Chère' : 'Cher';
+        $lastname  = $user->lastName();
+        $firstname = $user->firstName();
+    } else {
+        $dear      = 'Cher(e)';
+        $lastname  = '';
+        $firstname = $user;
+    }
+
+    $text = str_ireplace(array('<cher>', '<nom>', '<prenom>'),
+                         array($dear, $lastname, $firstname), $body);
+    $mailer->setTo($user);
     if ($replyto) {
         $mailer->addHeader('Reply-To', $replyto);
     }
-    $mailer->setTxtBody(wordwrap($text, 72));
+    if ($wiki) {
+        $mailer->setWikiBody($text);
+    } else {
+        $mailer->setTxtBody(wordwrap($text, 72));
+    }
     $mailer->send();
 }
 
 // }}}
 // {{{ send_xnet_mails
 
-function send_xnet_mails($from, $sujet, $body, $tos, $replyto = null, $attach = null)
+function send_xnet_mails($from, $sujet, $body, $wiki, $tos, $replyto = null, $upload = null, $name = null)
 {
     global $globals;
     $sent = array();
@@ -107,16 +93,21 @@ function send_xnet_mails($from, $sujet, $body, $tos, $replyto = null, $attach =
     $mailer = new PlMailer();
     $mailer->setSubject($sujet);
     $mailer->setFrom($from);
-    if (is_uploaded_file($attach['tmp_name'])) {
-        $mailer->addAttachment($attach['tmp_name'],
-                               $attach['type'],
-                               $attach['name']);
+    if ($upload && $upload->exists()) {
+        $mailer->addUploadAttachment($upload, $name);
     }
 
     foreach ($tos as $user) {
-        if ($sent[$user['email']]) continue;
-        _send_xnet_mail($user, $body, $mailer, $replyto);
-        $sent[$user['email']] = true;
+        if ($user instanceof User) {
+            $email = $user->bestEmail();
+        } else {
+            $email = $user;
+        }
+
+        if (!isset($sent[$email])) {
+            _send_xnet_mail($user, $body, $wiki, $mailer, $replyto);
+            $sent[$email] = true;
+        }
     }
 }