Commit | Line | Data |
---|---|---|
16cd99fb | 1 | <?php |
2 | /*************************************************************************** | |
179afa7f | 3 | * Copyright (C) 2003-2008 Polytechnique.org * |
16cd99fb | 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 | ||
16cd99fb | 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 | ||
fc9d9368 | 37 | protected $_table; |
38 | protected $_subscriptionTable; | |
39 | ||
40 | function __construct($tpl, $css, $prefix, $tbl, $stbl) | |
16cd99fb | 41 | { |
42 | $this->_tpl = $tpl; | |
43 | $this->_css = $css; | |
44 | $this->_prefix = $prefix; | |
fc9d9368 | 45 | $this->_table = $tbl; |
46 | $this->_subscriptionTable = $stbl; | |
16cd99fb | 47 | } |
48 | ||
49 | public function id() | |
50 | { | |
51 | return is_null($this->_shortname) ? $this->_id : $this->_shortname; | |
52 | } | |
53 | ||
fc9d9368 | 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} | |
eaf30d86 | 59 | LIMIT 1"); |
fc9d9368 | 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 | ||
16cd99fb | 101 | public function title($mail = false) |
102 | { | |
103 | return $mail ? $this->_title_mail : $this->_title; | |
104 | } | |
105 | ||
106 | public function head($prenom = null, $nom = null, $sexe = null, $type = 'text') | |
107 | { | |
108 | if (is_null($prenom)) { | |
eaf30d86 | 109 | return $this->_head; |
16cd99fb | 110 | } else { |
111 | $head = $this->_head; | |
a7de4ef7 | 112 | $head = str_replace('<cher>', $sexe ? 'Chère' : 'Cher', $head); |
16cd99fb | 113 | $head = str_replace('<prenom>', $prenom, $head); |
114 | $head = str_replace('<nom>', $nom, $head); | |
8da0d3c1 | 115 | return format_text($head, $type, 2, 64); |
16cd99fb | 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); | |
a14159bf | 126 | return preg_replace('@/\*.*?\*/@us', '', $css); |
16cd99fb | 127 | } |
128 | } | |
129 | ||
130 | public function toText(&$page, $prenom, $nom, $sexe) | |
131 | { | |
132 | $this->css($page); | |
133 | $page->assign('is_mail', false); | |
3ad44e08 | 134 | $page->assign('mail_part', 'text'); |
16cd99fb | 135 | $page->assign('prenom', $prenom); |
136 | $page->assign('nom', $nom); | |
137 | $page->assign('sexe', $sexe); | |
138 | $this->assignData($page); | |
139 | } | |
140 | ||
141 | public function toHtml(&$page, $prenom, $nom, $sexe) | |
142 | { | |
143 | $this->css($page); | |
144 | $page->assign('prefix', $this->_prefix . '/' . $this->id()); | |
145 | $page->assign('is_mail', false); | |
3ad44e08 | 146 | $page->assign('mail_part', 'html'); |
16cd99fb | 147 | $page->assign('prenom', $prenom); |
148 | $page->assign('nom', $nom); | |
149 | $page->assign('sexe', $sexe); | |
150 | $this->assignData($page); | |
151 | } | |
152 | ||
39337e2d FB |
153 | private function createHash($line, $key = null) |
154 | { | |
155 | $hash = implode(time(), $line) . rand(); | |
156 | $hash = md5($hash); | |
157 | return $hash; | |
158 | } | |
159 | ||
a0f05027 | 160 | public function sendTo($prenom, $nom, $login, $sexe, $html, $hash = 0) |
16cd99fb | 161 | { |
162 | global $globals; | |
39337e2d | 163 | $alias = $login; |
a0f05027 | 164 | if (strpos($login, '@') === false) { |
165 | $login = "$login@{$globals->mail->domain}"; | |
166 | } | |
9833e186 FB |
167 | require_once('user.func.inc.php'); |
168 | $forlife = get_user_forlife($login, '_silent_user_callback'); | |
169 | if ($forlife) { | |
170 | $alias = $forlife; | |
171 | } | |
39337e2d FB |
172 | if (strpos($alias, '@') === false && (is_null($hash) || $hash == 0)) { |
173 | ||
174 | $hash = $this->createHash(array($prenom, $nom, $login, $sexe, $html, rand(), "X.org rulez")); | |
175 | XDB::query("UPDATE {$this->_subscriptionTable} as ni | |
176 | INNER JOIN aliases AS a ON (ni.user_id = a.id) | |
177 | SET ni.hash = {?} | |
178 | WHERE ni.user_id != 0 AND a.alias = {?}", | |
179 | $hash, $alias); | |
180 | } | |
16cd99fb | 181 | |
182 | $mailer = new PlMailer($this->_tpl); | |
183 | $this->assignData($mailer); | |
184 | $mailer->assign('is_mail', true); | |
185 | $mailer->assign('prenom', $prenom); | |
186 | $mailer->assign('nom', $nom); | |
187 | $mailer->assign('sexe', $sexe); | |
188 | $mailer->assign('prefix', null); | |
a0f05027 | 189 | $mailer->assign('hash', $hash); |
e0e77c5a FB |
190 | $mailer->assign('email', $login); |
191 | $mailer->assign('alias', $alias); | |
a0f05027 | 192 | $mailer->addTo("\"$prenom $nom\" <$login>"); |
16cd99fb | 193 | $mailer->send($html); |
194 | } | |
195 | ||
a0f05027 | 196 | protected function getAllRecipients() |
197 | { | |
b2192733 | 198 | global $globals; |
199 | return "SELECT u.user_id, CONCAT(a.alias, '@{$globals->mail->domain}'), | |
a0f05027 | 200 | u.prenom, IF(u.nom_usage='', u.nom, u.nom_usage), |
201 | FIND_IN_SET('femme', u.flags), | |
39337e2d | 202 | q.core_mail_fmt AS pref, ni.hash AS hash |
fc9d9368 | 203 | FROM {$this->_subscriptionTable} AS ni |
a0f05027 | 204 | INNER JOIN auth_user_md5 AS u USING(user_id) |
205 | INNER JOIN auth_user_quick AS q ON(q.user_id = u.user_id) | |
206 | INNER JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags)) | |
207 | LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active') | |
c3b45f48 VZ |
208 | WHERE ni.last < {?} AND ({$this->subscriptionWhere()}) AND |
209 | (e.email IS NOT NULL OR FIND_IN_SET('googleapps', u.mail_storage)) | |
a0f05027 | 210 | GROUP BY u.user_id"; |
211 | } | |
212 | ||
16cd99fb | 213 | public function sendToAll() |
214 | { | |
215 | $this->setSent(); | |
a0f05027 | 216 | $query = $this->getAllRecipients() . " LIMIT {?}"; |
16cd99fb | 217 | while (true) { |
a0f05027 | 218 | $res = XDB::iterRow($query, $this->_id, 60); |
16cd99fb | 219 | if (!$res->total()) { |
4e25428d | 220 | return; |
16cd99fb | 221 | } |
222 | $sent = array(); | |
a0f05027 | 223 | while (list($uid, $bestalias, $prenom, $nom, $sexe, $fmt, $hash) = $res->next()) { |
224 | $sent[] = "(user_id='$uid'" . (!$uid ? " AND email='$bestalias')": ')'); | |
225 | $this->sendTo($prenom, $nom, $bestalias, $sexe, $fmt=='html', $hash); | |
16cd99fb | 226 | } |
fc9d9368 | 227 | XDB::execute("UPDATE {$this->_subscriptionTable} |
16cd99fb | 228 | SET last = {?} |
229 | WHERE " . implode(' OR ', $sent), $this->_id); | |
eaf30d86 | 230 | |
16cd99fb | 231 | sleep(60); |
232 | } | |
233 | } | |
234 | ||
235 | abstract protected function assignData(&$smarty); | |
236 | abstract protected function setSent(); | |
237 | ||
16cd99fb | 238 | abstract protected function subscriptionWhere(); |
239 | } | |
240 | ||
241 | // }}} | |
242 | // {{{ Functions | |
243 | ||
8da0d3c1 | 244 | function format_text($input, $format, $indent = 0, $width = 68) |
245 | { | |
246 | if ($format == 'text') { | |
02fdd1c8 | 247 | return MiniWiki::WikiToText($input, true, $indent, $width, "title"); |
8da0d3c1 | 248 | } |
02fdd1c8 | 249 | return MiniWiki::WikiToHTML($input, "title"); |
8da0d3c1 | 250 | } |
251 | ||
02fdd1c8 | 252 | // function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68) |
16cd99fb | 253 | |
254 | // }}} | |
255 | ||
a7de4ef7 | 256 | // vim:set et sw=4 sts=4 sws=4 enc=utf-8: |
16cd99fb | 257 | ?> |