2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // {{{ class MassMailer
24 abstract class MassMailer
38 protected $_subscriptionTable;
40 function __construct($tpl, $css, $prefix, $tbl, $stbl)
44 $this->_prefix
= $prefix;
46 $this->_subscriptionTable
= $stbl;
51 return is_null($this->_shortname
) ?
$this->_id
: $this->_shortname
;
54 private function selectId($where)
56 $res = XDB
::query("SELECT IF (n.short_name IS NULL, n.id, n.short_name)
57 FROM {$this->_table} AS n
58 WHERE n.bits != 'new' AND {$where}
60 if ($res->numRows() != 1) {
63 return $res->fetchOneCell();
66 public function prev()
70 $val = $this->selectId("n.id < {$this->_id} ORDER BY n.id DESC");
75 public function next()
79 $val = $this->selectId("n.id > {$this->_id} ORDER BY n.id");
84 public function last()
88 $res = XDB
::query("SELECT MAX(n.id)
89 FROM {$this->_table} AS n
90 WHERE n.bits != 'new' AND n.id > {?}",
92 if ($res->numRows() != 1) {
95 $val = $res->fetchOneCell();
101 public function title($mail = false
)
103 return $mail ?
$this->_title_mail
: $this->_title
;
106 public function head($prenom = null
, $nom = null
, $sexe = null
, $type = 'text')
108 if (is_null($prenom)) {
111 $head = $this->_head
;
112 $head = str_replace('<cher>', $sexe ?
'Chère' : 'Cher', $head);
113 $head = str_replace('<prenom>', $prenom, $head);
114 $head = str_replace('<nom>', $nom, $head);
115 return format_text($head, $type, 2, 64);
119 public function css(&$page = null
)
121 if (!is_null($page)) {
122 $page->addCssLink($this->_css
);
125 $css = file_get_contents(dirname(__FILE__
) . '/../htdocs/css/' . $this->_css
);
126 return preg_replace('@/\*.*?\*/@us', '', $css);
130 public function toText(&$page, $prenom, $nom, $sexe)
133 $page->assign('is_mail', false
);
134 $page->assign('mail_part', 'text');
135 $page->assign('prenom', $prenom);
136 $page->assign('nom', $nom);
137 $page->assign('sexe', $sexe);
138 $this->assignData($page);
141 public function toHtml(&$page, $prenom, $nom, $sexe)
144 $page->assign('prefix', $this->_prefix
. '/' . $this->id());
145 $page->assign('is_mail', false
);
146 $page->assign('mail_part', 'html');
147 $page->assign('prenom', $prenom);
148 $page->assign('nom', $nom);
149 $page->assign('sexe', $sexe);
150 $this->assignData($page);
153 private function createHash($line, $key = null
)
155 $hash = implode(time(), $line) . rand();
160 public function sendTo($hruid, $email, $prenom, $nom, $sexe, $html, $hash = 0)
162 // If $email is not a real email address, tries to compute it up from
163 // the hruid. Otherwise, we suppose that caller will have used a valid
164 // and canonical email address.
165 if (strpos($email, '@') === false
) {
166 if (!($user = User
::getSilent($email))) {
167 Platal
::page()->trigError("'$email' is neither a valid email address nor a valid login; did not send the email.");
169 $email = $user->bestEmail();
172 if ($hruid && (is_null($hash) ||
$hash == 0)) {
173 $hash = $this->createHash(array($email, $prenom, $nom, $sexe, $html, rand(), "X.org rulez"));
174 XDB
::query("UPDATE {$this->_subscriptionTable} as ni
175 INNER JOIN auth_user_md5 AS u USING (user_id)
177 WHERE ni.user_id != 0 AND u.hruid = {?}",
181 $mailer = new PlMailer($this->_tpl
);
182 $this->assignData($mailer);
183 $mailer->assign('is_mail', true
);
184 $mailer->assign('prenom', $prenom);
185 $mailer->assign('nom', $nom);
186 $mailer->assign('sexe', $sexe);
187 $mailer->assign('prefix', null
);
188 $mailer->assign('hash', $hash);
189 $mailer->assign('email', $email);
190 $mailer->assign('alias', $hruid);
191 $mailer->addTo("\"$prenom $nom\" <$email>");
192 $mailer->send($html);
195 protected function getAllRecipients()
198 return "SELECT u.user_id, u.hruid, CONCAT(a.alias, '@{$globals->mail->domain}'),
199 u.prenom, IF(u.nom_usage='', u.nom, u.nom_usage),
200 FIND_IN_SET('femme', u.flags),
201 q.core_mail_fmt AS pref, ni.hash AS hash
202 FROM {$this->_subscriptionTable} AS ni
203 INNER JOIN auth_user_md5 AS u USING(user_id)
204 INNER JOIN auth_user_quick AS q ON(q.user_id = u.user_id)
205 INNER JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
206 LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active')
207 WHERE ni.last < {?} AND ({$this->subscriptionWhere()}) AND
208 (e.email IS NOT NULL OR FIND_IN_SET('googleapps', u.mail_storage))
212 public function sendToAll()
215 $query = $this->getAllRecipients() . " LIMIT {?}";
217 $res = XDB
::iterRow($query, $this->_id
, 60);
218 if (!$res->total()) {
222 while (list($uid, $hruid, $email, $prenom, $nom, $sexe, $fmt, $hash) = $res->next()) {
223 $sent[] = "(user_id='$uid'" . (!$uid ?
" AND email='$email')": ')');
224 $this->sendTo($hruid, $email, $prenom, $nom, $sexe, $fmt=='html', $hash);
226 XDB
::execute("UPDATE {$this->_subscriptionTable}
228 WHERE " . implode(' OR ', $sent), $this->_id
);
234 abstract protected function assignData(&$smarty);
235 abstract protected function setSent();
237 abstract protected function subscriptionWhere();
243 function format_text($input, $format, $indent = 0, $width = 68)
245 if ($format == 'text') {
246 return MiniWiki
::WikiToText($input, true
, $indent, $width, "title");
248 return MiniWiki
::WikiToHTML($input, "title");
251 // function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
255 // vim:set et sw=4 sts=4 sws=4 enc=utf-8: