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