269a570f5c2893892008d3b6a88df33ece0307d7
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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($user = null
, $type = 'text')
115 if (is_null($user)) {
118 $head = $this->_head
;
119 $head = str_replace('<cher>', $user->isFemale() ?
'Chère' : 'Cher', $head);
120 $head = str_replace('<prenom>', $user->displayName(), $head);
121 $head = str_replace('<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, $user)
140 $page->assign('is_mail', false
);
141 $page->assign('mail_part', 'text');
142 $page->assign('user', $user);
143 $this->assignData($page);
146 public function toHtml(&$page, $user)
149 $page->assign('prefix', $this->_prefix
. '/' . $this->id());
150 $page->assign('is_mail', false
);
151 $page->assign('mail_part', 'html');
152 $page->assign('user', $user);
153 $this->assignData($page);
156 private function createHash($line, $key = null
)
158 $hash = implode(time(), $line) . rand();
163 public function sendTo($user, $hash = null
)
165 if (is_null($hash)) {
166 $hash = XDB
::fetchOneCell("SELECT hash
167 FROM {$this->_subscriptionTable}
168 WHERE uid = {?}", $user->id());
170 if (is_null($hash)) {
171 $hash = $this->createHash(array($user->displayName(), $user->fullName(),
172 $user->isFemale(), $user->isEmailFormatHtml(),
173 rand(), "X.org rulez"));
174 XDB
::execute("UPDATE {$this->_subscriptionTable} as ni
180 $mailer = new PlMailer($this->_tpl
);
181 $this->assignData($mailer);
182 $mailer->assign('is_mail', true
);
183 $mailer->assign('user', $user);
184 $mailer->assign('prefix', null
);
185 $mailer->assign('hash', $hash);
186 $mailer->addTo('"' . $user->fullName() . '" <' . $user->bestEmail() . '>');
187 $mailer->send($user->isEmailFormatHtml());
190 protected function getAllRecipients()
194 FROM {$this->_subscriptionTable} AS ni
195 INNER JOIN accounts AS a ON (ni.uid = a.uid)
196 LEFT JOIN email_options AS eo ON (eo.uid = a.uid)
197 LEFT JOIN emails AS e ON (e.uid = a.uid AND e.flags='active')
198 WHERE ni.last < {?} AND ({$this->subscriptionWhere()}) AND
199 (e.email IS NOT NULL OR FIND_IN_SET('googleapps', eo.storage))
203 public function sendToAll()
206 $query = XDB
::format($this->getAllRecipients(), $this->_id
) . ' LIMIT 60';
210 $users = User
::getBulkUsersWithUIDs(XDB
::fetchColumn($query));
211 if (count($users) == 0) {
214 foreach ($users as $user) {
215 $sent[] = XDB
::format('uid = {?}', $user->id());
216 $this->sendTo($user, $hash);
219 XDB
::execute("UPDATE {$this->_subscriptionTable}
221 WHERE " . implode(' OR ', $sent), $this->_id
);
228 abstract protected function assignData(&$smarty);
229 abstract protected function setSent();
231 abstract protected function subscriptionWhere();
237 function format_text($input, $format, $indent = 0, $width = 68)
239 if ($format == 'text') {
240 return MiniWiki
::WikiToText($input, true
, $indent, $width, "title");
242 return MiniWiki
::WikiToHTML($input, "title");
245 // function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
249 // vim:set et sw=4 sts=4 sws=4 enc=utf-8: