Merge branch 'master' of /home/git/platal into profile_edit
[platal.git] / modules / xnetgrp / mail.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 Polytechnique.org *
0337d704 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
0337d704 22// {{{ get_all_redirects
23
892083ef 24function get_all_redirects($membres, $mls, &$client)
0337d704 25{
26 global $globals;
787bb3d7 27
0337d704 28 $tos = array();
787bb3d7 29
06db561e 30 if (!empty($membres)) {
31 $membres = array_map(create_function('$str', 'return "\"$str\"";'), $membres);
32 $membres = join(',', $membres);
08cce2ff 33 $res = XDB::query(
0337d704 34 'SELECT IF(u.nom <> "", u.nom, m.nom) AS nom,
35 IF(u.prenom <> "", u.prenom, m.prenom) AS prenom,
36 IF(m.email <> "", m.email, CONCAT(a.alias, "@polytechnique.org")) as email,
0cc4c07d 37 IF(m.sexe IS NULL, FIND_IN_SET("femme", u.flags), m.sexe) AS sexe
0337d704 38 FROM groupex.membres AS m
39 LEFT JOIN auth_user_md5 AS u ON (m.uid=u.user_id AND m.uid<50000)
40 LEFT JOIN aliases AS a ON (a.id=u.user_id and a.type="a_vie")
88198bd8 41 WHERE asso_id = {?}
06db561e 42 AND m.origine IN (' . $membres . ')
88198bd8 43 AND (m.email <> "" OR u.perms <> "pending")', $globals->asso('id'));
0337d704 44 $tos = $res->fetchAllAssoc();
45 }
46
47 foreach ($mls as $ml) {
48 if (list(,$members) = $client->get_members($ml)) {
49 foreach ($members as $mem) {
575dd9be 50 list($m, $dom) = explode('@',$mem[1]);
0337d704 51 if ($dom == $globals->mail->domain || $dom == $globals->mail->domain2) {
08cce2ff 52 $res = XDB::query('SELECT prenom, nom, FIND_IN_SET("femme", u.flags) AS sexe
8cc026af 53 FROM auth_user_md5 AS u
54 INNER JOIN aliases AS a ON u.user_id = a.id
55 WHERE a.alias = {?}', $m);
0337d704 56 if ($person = $res->fetchOneAssoc()) {
57 $person['email'] = $mem[1];
58 $tos[] = $person;
59 }
60 } else {
0cc4c07d 61 $res = XDB::query('SELECT prenom, nom, sexe FROM groupex.membres WHERE email={?}', $mem[1]);
0337d704 62 if ($person = $res->fetchOneAssoc()) {
63 $person['email'] = $mem[1];
64 $tos[] = $person;
65 } else {
66 $tos[] = array('email' => $mem[1]);
67 }
68 }
69 }
70 }
71 }
72
73 return $tos;
74}
75
76// }}}
77// {{{ _send_xnet_mail
78
5b21237d 79function _send_xnet_mail($user, $body, $wiki, $mailer, $replyto = null)
0337d704 80{
a7de4ef7 81 $cher = isset($user['sexe']) ? ($user['sexe'] ? 'Chère' : 'Cher') : 'Cher(e)';
0337d704 82 $nom = isset($user['nom']) ? $user['nom'] : "";
a14159bf 83 $pnom = isset($user['prenom']) ? $user['prenom'] : preg_replace('!@.*!u', '', $user['email']);
0337d704 84 $to = isset($user['prenom']) ? "\"{$user['prenom']} {$user['nom']}\" <{$user['email']}>" : $user['email'];
787bb3d7 85
0337d704 86 $text = $body;
87 $text = preg_replace('!<cher>!i', $cher, $text);
88 $text = preg_replace('!<nom>!i', $nom, $text);
89 $text = preg_replace('!<prenom>!i', $pnom, $text);
90
91 $mailer->addHeader('To', $to);
9898b36e 92 if ($replyto) {
93 $mailer->addHeader('Reply-To', $replyto);
94 }
5b21237d 95 if ($wiki) {
96 $mailer->setWikiBody($text);
97 } else {
98 $mailer->setTxtBody(wordwrap($text, 72));
99 }
91a14f73 100 $mailer->send();
0337d704 101}
102
103// }}}
104// {{{ send_xnet_mails
105
5b21237d 106function send_xnet_mails($from, $sujet, $body, $wiki, $tos, $replyto = null, $upload = null, $name = null)
0337d704 107{
bdccfd95 108 global $globals;
0337d704 109 $sent = array();
110
1e33266a 111 $mailer = new PlMailer();
0337d704 112 $mailer->setSubject($sujet);
113 $mailer->setFrom($from);
13627bef 114 if ($upload && $upload->exists()) {
115 $mailer->addUploadAttachment($upload, $name);
91a14f73 116 }
0337d704 117
118 foreach ($tos as $user) {
119 if ($sent[$user['email']]) continue;
5b21237d 120 _send_xnet_mail($user, $body, $wiki, $mailer, $replyto);
0337d704 121 $sent[$user['email']] = true;
122 }
123}
124
125// }}}
126
a7de4ef7 127// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 128?>