Merge commit 'origin/fusionax' into account
[platal.git] / include / massmailer.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
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
22 // {{{ class MassMailer
23
24 abstract class MassMailer
25 {
26 private $_tpl;
27 private $_css;
28 private $_prefix;
29
30 public $_id;
31 public $_shortname;
32 public $_title;
33 public $_title_mail;
34
35 public $_head;
36
37 protected $_table;
38 protected $_subscriptionTable;
39
40 function __construct($tpl, $css, $prefix, $tbl, $stbl)
41 {
42 $this->_tpl = $tpl;
43 $this->_css = $css;
44 $this->_prefix = $prefix;
45 $this->_table = $tbl;
46 $this->_subscriptionTable = $stbl;
47 }
48
49 public function id()
50 {
51 return is_null($this->_shortname) ? $this->_id : $this->_shortname;
52 }
53
54 private function selectId($where)
55 {
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}
59 LIMIT 1");
60 if ($res->numRows() != 1) {
61 return null;
62 }
63 return $res->fetchOneCell();
64 }
65
66 public function prev()
67 {
68 static $val;
69 if (!isset($val)) {
70 $val = $this->selectId("n.id < {$this->_id} ORDER BY n.id DESC");
71 }
72 return $val;
73 }
74
75 public function next()
76 {
77 static $val;
78 if (!isset($val)) {
79 $val = $this->selectId("n.id > {$this->_id} ORDER BY n.id");
80 }
81 return $val;
82 }
83
84 public function last()
85 {
86 static $val;
87 if (!isset($val)) {
88 $res = XDB::query("SELECT MAX(n.id)
89 FROM {$this->_table} AS n
90 WHERE n.bits != 'new' AND n.id > {?}",
91 $this->_id);
92 if ($res->numRows() != 1) {
93 $val = null;
94 } else {
95 $val = $res->fetchOneCell();
96 }
97 }
98 return $val;
99 }
100
101 public function title($mail = false)
102 {
103 return $mail ? $this->_title_mail : $this->_title;
104 }
105
106 public function head($user = null, $type = 'text')
107 {
108 if (is_null($user)) {
109 return $this->_head;
110 } else {
111 $head = $this->_head;
112 $head = str_replace('<cher>', $user->isFemale() ? 'Chère' : 'Cher', $head);
113 $head = str_replace('<prenom>', $user->displayName(), $head);
114 $head = str_replace('<nom>', '', $head);
115 return format_text($head, $type, 2, 64);
116 }
117 }
118
119 public function css(&$page = null)
120 {
121 if (!is_null($page)) {
122 $page->addCssLink($this->_css);
123 return true;
124 } else {
125 $css = file_get_contents(dirname(__FILE__) . '/../htdocs/css/' . $this->_css);
126 return preg_replace('@/\*.*?\*/@us', '', $css);
127 }
128 }
129
130 public function toText(&$page, $user)
131 {
132 $this->css($page);
133 $page->assign('is_mail', false);
134 $page->assign('mail_part', 'text');
135 $page->assign('user', $user);
136 $this->assignData($page);
137 }
138
139 public function toHtml(&$page, $user)
140 {
141 $this->css($page);
142 $page->assign('prefix', $this->_prefix . '/' . $this->id());
143 $page->assign('is_mail', false);
144 $page->assign('mail_part', 'html');
145 $page->assign('user', $user);
146 $this->assignData($page);
147 }
148
149 private function createHash($line, $key = null)
150 {
151 $hash = implode(time(), $line) . rand();
152 $hash = md5($hash);
153 return $hash;
154 }
155
156 public function sendTo($user, $hash = null)
157 {
158 if (is_null($hash)) {
159 $hash = XDB::fetchOneCell("SELECT hash
160 FROM {$this->_subscriptionTable}
161 WHERE user_id = {?}", $user->id());
162 }
163 if (is_null($hash)) {
164 $hash = $this->createHash(array($user->displayName(), $user->fullName(),
165 $user->isFemale(), $user->isEmailFormatHtml(),
166 rand(), "X.org rulez"));
167 XDB::execute("UPDATE {$this->_subscriptionTable} as ni
168 SET ni.hash = {?}
169 WHERE ni.user_id != {?}",
170 $hash, $user->id());
171 }
172
173 $mailer = new PlMailer($this->_tpl);
174 $this->assignData($mailer);
175 $mailer->assign('is_mail', true);
176 $mailer->assign('user', $user);
177 $mailer->assign('prefix', null);
178 $mailer->assign('hash', $hash);
179 $mailer->addTo('"' . $user->fullName() . '" <' . $user->bestEmail() . '>');
180 $mailer->send($user->isEmailFormatHtml());
181 }
182
183 protected function getAllRecipients()
184 {
185 global $globals;
186 return "SELECT a.uid, a.hruid, a.display_name, a.full_name, a.email_format,
187 ni.hash AS hash
188 FROM {$this->_subscriptionTable} AS ni
189 INNER JOIN accounts AS a ON (ni.user_id = a.uid)
190 LEFT JOIN email_options AS eo ON (eo.uid = a.uid)
191 LEFT JOIN emails AS e ON (e.uid = a.uid AND e.flags='active')
192 WHERE ni.last < {?} AND ({$this->subscriptionWhere()}) AND
193 (e.email IS NOT NULL OR FIND_IN_SET('googleapps', eo.storage))
194 GROUP BY a.uid";
195 }
196
197 public function sendToAll()
198 {
199 $this->setSent();
200 $query = XDB::format($this->getAllRecipients(), $this->id()) . ' LIMIT 60';
201 while (true) {
202 $res = XDB::iterRow($query);
203 if (!$res->total()) {
204 return;
205 }
206 while ($infos = $res->next()) {
207 $user = User::getSilentWithValues(null, $infos);
208 $sent[] = XDB::format('user_id = {?}', $user->id());
209 $this->sendTo($user, $hash);
210 }
211 XDB::execute("UPDATE {$this->_subscriptionTable}
212 SET last = {?}
213 WHERE " . implode(' OR ', $sent), $this->_id);
214
215 sleep(60);
216 }
217 }
218
219 abstract protected function assignData(&$smarty);
220 abstract protected function setSent();
221
222 abstract protected function subscriptionWhere();
223 }
224
225 // }}}
226 // {{{ Functions
227
228 function format_text($input, $format, $indent = 0, $width = 68)
229 {
230 if ($format == 'text') {
231 return MiniWiki::WikiToText($input, true, $indent, $width, "title");
232 }
233 return MiniWiki::WikiToHTML($input, "title");
234 }
235
236 // function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
237
238 // }}}
239
240 // vim:set et sw=4 sts=4 sws=4 enc=utf-8:
241 ?>