2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 MailNotFound
24 class MailNotFound
extends Exception
{
29 // {{{ class MassMailer
31 abstract class MassMailer
45 protected $_subscriptionTable;
47 function __construct($tpl, $css, $prefix, $tbl, $stbl)
51 $this->_prefix
= $prefix;
53 $this->_subscriptionTable
= $stbl;
58 return is_null($this->_shortname
) ?
$this->_id
: $this->_shortname
;
61 private function selectId($where)
63 $res = XDB
::query("SELECT IF (n.short_name IS NULL, n.id, n.short_name)
64 FROM {$this->_table} AS n
65 WHERE n.bits != 'new' AND {$where}
67 if ($res->numRows() != 1) {
70 return $res->fetchOneCell();
73 public function prev()
77 $val = $this->selectId("n.id < {$this->_id} ORDER BY n.id DESC");
82 public function next()
86 $val = $this->selectId("n.id > {$this->_id} ORDER BY n.id");
91 public function last()
95 $res = XDB
::query("SELECT MAX(n.id)
96 FROM {$this->_table} AS n
97 WHERE n.bits != 'new' AND n.id > {?}",
99 if ($res->numRows() != 1) {
102 $val = $res->fetchOneCell();
108 public function title($mail = false
)
110 return $mail ?
$this->_title_mail
: $this->_title
;
113 public function head($prenom = null
, $nom = null
, $sexe = null
, $type = 'text')
115 if (is_null($prenom)) {
118 $head = $this->_head
;
119 $head = str_replace('<cher>', $sexe ?
'Chère' : 'Cher', $head);
120 $head = str_replace('<prenom>', $prenom, $head);
121 $head = str_replace('<nom>', $nom, $head);
122 return format_text($head, $type, 2, 64);
126 public function css(&$page = null
)
128 if (!is_null($page)) {
129 $page->addCssLink($this->_css
);
132 $css = file_get_contents(dirname(__FILE__
) . '/../htdocs/css/' . $this->_css
);
133 return preg_replace('@/\*.*?\*/@us', '', $css);
137 public function toText(&$page, $prenom, $nom, $sexe)
140 $page->assign('is_mail', false
);
141 $page->assign('mail_part', 'text');
142 $page->assign('prenom', $prenom);
143 $page->assign('nom', $nom);
144 $page->assign('sexe', $sexe);
145 $this->assignData($page);
148 public function toHtml(&$page, $prenom, $nom, $sexe)
151 $page->assign('prefix', $this->_prefix
. '/' . $this->id());
152 $page->assign('is_mail', false
);
153 $page->assign('mail_part', 'html');
154 $page->assign('prenom', $prenom);
155 $page->assign('nom', $nom);
156 $page->assign('sexe', $sexe);
157 $this->assignData($page);
160 private function createHash($line, $key = null
)
162 $hash = implode(time(), $line) . rand();
167 public function sendTo($hruid, $email, $prenom, $nom, $sexe, $html, $hash = 0)
169 // If $email is not a real email address, tries to compute it up from
170 // the hruid. Otherwise, we suppose that caller will have used a valid
171 // and canonical email address.
172 if (strpos($email, '@') === false
) {
173 if (!($user = User
::getSilent($email))) {
174 Platal
::page()->trigError("'$email' is neither a valid email address nor a valid login; did not send the email.");
176 $email = $user->bestEmail();
179 if ($hruid && (is_null($hash) ||
$hash == 0)) {
180 $hash = $this->createHash(array($email, $prenom, $nom, $sexe, $html, rand(), "X.org rulez"));
181 XDB
::query("UPDATE {$this->_subscriptionTable} as ni
182 INNER JOIN auth_user_md5 AS u USING (user_id)
184 WHERE ni.user_id != 0 AND u.hruid = {?}",
188 $mailer = new PlMailer($this->_tpl
);
189 $this->assignData($mailer);
190 $mailer->assign('is_mail', true
);
191 $mailer->assign('prenom', $prenom);
192 $mailer->assign('nom', $nom);
193 $mailer->assign('sexe', $sexe);
194 $mailer->assign('prefix', null
);
195 $mailer->assign('hash', $hash);
196 $mailer->assign('email', $email);
197 $mailer->assign('alias', $hruid);
198 $mailer->addTo("\"$prenom $nom\" <$email>");
199 $mailer->send($html);
202 protected function getAllRecipients()
205 return "SELECT u.user_id, u.hruid, CONCAT(a.alias, '@{$globals->mail->domain}'),
206 u.prenom, IF(u.nom_usage='', u.nom, u.nom_usage),
207 FIND_IN_SET('femme', u.flags),
208 q.core_mail_fmt AS pref, ni.hash AS hash
209 FROM {$this->_subscriptionTable} AS ni
210 INNER JOIN auth_user_md5 AS u USING(user_id)
211 INNER JOIN auth_user_quick AS q ON(q.user_id = u.user_id)
212 INNER JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
213 LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active')
214 WHERE ni.last < {?} AND ({$this->subscriptionWhere()}) AND
215 (e.email IS NOT NULL OR FIND_IN_SET('googleapps', u.mail_storage))
219 public function sendToAll()
222 $query = $this->getAllRecipients() . " LIMIT {?}";
224 $res = XDB
::iterRow($query, $this->_id
, 60);
225 if (!$res->total()) {
229 while (list($uid, $hruid, $email, $prenom, $nom, $sexe, $fmt, $hash) = $res->next()) {
230 $sent[] = "(user_id='$uid'" . (!$uid ?
" AND email='$email')": ')');
231 $this->sendTo($hruid, $email, $prenom, $nom, $sexe, $fmt=='html', $hash);
233 XDB
::execute("UPDATE {$this->_subscriptionTable}
235 WHERE " . implode(' OR ', $sent), $this->_id
);
241 abstract protected function assignData(&$smarty);
242 abstract protected function setSent();
244 abstract protected function subscriptionWhere();
250 function format_text($input, $format, $indent = 0, $width = 68)
252 if ($format == 'text') {
253 return MiniWiki
::WikiToText($input, true
, $indent, $width, "title");
255 return MiniWiki
::WikiToHTML($input, "title");
258 // function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
262 // vim:set et sw=4 sts=4 sws=4 enc=utf-8: