16cd99fb |
1 | <?php |
2 | /*************************************************************************** |
3 | * Copyright (C) 2003-2007 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 | function __construct($tpl, $css, $prefix) |
40 | { |
41 | $this->_tpl = $tpl; |
42 | $this->_css = $css; |
43 | $this->_prefix = $prefix; |
44 | } |
45 | |
46 | public function id() |
47 | { |
48 | return is_null($this->_shortname) ? $this->_id : $this->_shortname; |
49 | } |
50 | |
51 | public function title($mail = false) |
52 | { |
53 | return $mail ? $this->_title_mail : $this->_title; |
54 | } |
55 | |
56 | public function head($prenom = null, $nom = null, $sexe = null, $type = 'text') |
57 | { |
58 | if (is_null($prenom)) { |
59 | return $this->_head; |
60 | } else { |
61 | $head = $this->_head; |
a7de4ef7 |
62 | $head = str_replace('<cher>', $sexe ? 'Chère' : 'Cher', $head); |
16cd99fb |
63 | $head = str_replace('<prenom>', $prenom, $head); |
64 | $head = str_replace('<nom>', $nom, $head); |
8da0d3c1 |
65 | return format_text($head, $type, 2, 64); |
16cd99fb |
66 | } |
67 | } |
68 | |
69 | public function css(&$page = null) |
70 | { |
71 | if (!is_null($page)) { |
72 | $page->addCssLink($this->_css); |
73 | return true; |
74 | } else { |
75 | $css = file_get_contents(dirname(__FILE__) . '/../htdocs/css/' . $this->_css); |
a14159bf |
76 | return preg_replace('@/\*.*?\*/@us', '', $css); |
16cd99fb |
77 | } |
78 | } |
79 | |
80 | public function toText(&$page, $prenom, $nom, $sexe) |
81 | { |
82 | $this->css($page); |
83 | $page->assign('is_mail', false); |
84 | $page->assign('html_version', false); |
85 | $page->assign('prenom', $prenom); |
86 | $page->assign('nom', $nom); |
87 | $page->assign('sexe', $sexe); |
88 | $this->assignData($page); |
89 | } |
90 | |
91 | public function toHtml(&$page, $prenom, $nom, $sexe) |
92 | { |
93 | $this->css($page); |
94 | $page->assign('prefix', $this->_prefix . '/' . $this->id()); |
95 | $page->assign('is_mail', false); |
96 | $page->assign('html_version', true); |
97 | $page->assign('prenom', $prenom); |
98 | $page->assign('nom', $nom); |
99 | $page->assign('sexe', $sexe); |
100 | $this->assignData($page); |
101 | } |
102 | |
a0f05027 |
103 | public function sendTo($prenom, $nom, $login, $sexe, $html, $hash = 0) |
16cd99fb |
104 | { |
105 | global $globals; |
a0f05027 |
106 | if (strpos($login, '@') === false) { |
107 | $login = "$login@{$globals->mail->domain}"; |
108 | } |
16cd99fb |
109 | |
110 | $mailer = new PlMailer($this->_tpl); |
111 | $this->assignData($mailer); |
112 | $mailer->assign('is_mail', true); |
113 | $mailer->assign('prenom', $prenom); |
114 | $mailer->assign('nom', $nom); |
115 | $mailer->assign('sexe', $sexe); |
116 | $mailer->assign('prefix', null); |
a0f05027 |
117 | $mailer->assign('hash', $hash); |
118 | $mailer->addTo("\"$prenom $nom\" <$login>"); |
16cd99fb |
119 | $mailer->send($html); |
120 | } |
121 | |
a0f05027 |
122 | protected function getAllRecipients() |
123 | { |
b2192733 |
124 | global $globals; |
125 | return "SELECT u.user_id, CONCAT(a.alias, '@{$globals->mail->domain}'), |
a0f05027 |
126 | u.prenom, IF(u.nom_usage='', u.nom, u.nom_usage), |
127 | FIND_IN_SET('femme', u.flags), |
128 | q.core_mail_fmt AS pref, 0 AS hash |
129 | FROM {$this->subscriptionTable()} AS ni |
130 | INNER JOIN auth_user_md5 AS u USING(user_id) |
131 | INNER JOIN auth_user_quick AS q ON(q.user_id = u.user_id) |
132 | INNER JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags)) |
133 | LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active') |
134 | WHERE ni.last < {?} AND ({$this->subscriptionWhere()}) AND e.email IS NOT NULL |
135 | GROUP BY u.user_id"; |
136 | } |
137 | |
16cd99fb |
138 | public function sendToAll() |
139 | { |
140 | $this->setSent(); |
a0f05027 |
141 | $query = $this->getAllRecipients() . " LIMIT {?}"; |
16cd99fb |
142 | while (true) { |
a0f05027 |
143 | $res = XDB::iterRow($query, $this->_id, 60); |
16cd99fb |
144 | if (!$res->total()) { |
4e25428d |
145 | return; |
16cd99fb |
146 | } |
147 | $sent = array(); |
a0f05027 |
148 | while (list($uid, $bestalias, $prenom, $nom, $sexe, $fmt, $hash) = $res->next()) { |
149 | $sent[] = "(user_id='$uid'" . (!$uid ? " AND email='$bestalias')": ')'); |
150 | $this->sendTo($prenom, $nom, $bestalias, $sexe, $fmt=='html', $hash); |
16cd99fb |
151 | } |
152 | XDB::execute("UPDATE {$this->subscriptionTable()} |
153 | SET last = {?} |
154 | WHERE " . implode(' OR ', $sent), $this->_id); |
b2192733 |
155 | |
16cd99fb |
156 | sleep(60); |
157 | } |
158 | } |
159 | |
160 | abstract protected function assignData(&$smarty); |
161 | abstract protected function setSent(); |
162 | |
163 | abstract protected function subscriptionTable(); |
164 | abstract protected function subscriptionWhere(); |
165 | } |
166 | |
167 | // }}} |
168 | // {{{ Functions |
169 | |
170 | function justify($text,$n) |
171 | { |
172 | $arr = explode("\n",wordwrap($text,$n)); |
173 | $arr = array_map('trim',$arr); |
174 | $res = ''; |
175 | foreach ($arr as $key => $line) { |
176 | $nxl = isset($arr[$key+1]) ? trim($arr[$key+1]) : ''; |
177 | $nxl_split = preg_split('! +!',$nxl); |
178 | $nxw_len = count($nxl_split) ? strlen($nxl_split[0]) : 0; |
179 | $line = trim($line); |
180 | |
181 | if (strlen($line)+1+$nxw_len < $n) { |
182 | $res .= "$line\n"; |
183 | continue; |
184 | } |
185 | |
186 | if (preg_match('![.:;]$!',$line)) { |
187 | $res .= "$line\n"; |
188 | continue; |
189 | } |
190 | |
191 | $tmp = preg_split('! +!',trim($line)); |
192 | $words = count($tmp); |
193 | if ($words <= 1) { |
194 | $res .= "$line\n"; |
195 | continue; |
196 | } |
197 | |
198 | $len = array_sum(array_map('strlen',$tmp)); |
199 | $empty = $n - $len; |
200 | $sw = floatval($empty) / floatval($words-1); |
201 | |
202 | $cur = 0; |
203 | $l = ''; |
204 | foreach ($tmp as $word) { |
205 | $l .= $word; |
206 | $cur += $sw + strlen($word); |
207 | $l = str_pad($l,intval($cur+0.5)); |
208 | } |
209 | $res .= trim($l)."\n"; |
210 | } |
211 | return trim($res); |
212 | } |
213 | |
8da0d3c1 |
214 | function format_text($input, $format, $indent = 0, $width = 68) |
215 | { |
216 | if ($format == 'text') { |
217 | return enriched_to_text($input, false, true, $indent, $width); |
218 | } |
219 | return enriched_to_text($input, true); |
220 | } |
221 | |
16cd99fb |
222 | function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68) |
223 | { |
224 | $text = trim($input); |
225 | if ($html) { |
226 | $text = htmlspecialchars($text); |
227 | $text = str_replace('[b]','<strong>', $text); |
228 | $text = str_replace('[/b]','</strong>', $text); |
229 | $text = str_replace('[i]','<em>', $text); |
230 | $text = str_replace('[/i]','</em>', $text); |
231 | $text = str_replace('[u]','<span style="text-decoration: underline">', $text); |
232 | $text = str_replace('[/u]','</span>', $text); |
8da0d3c1 |
233 | $text = preg_replace("!(\\s*\n)*\[title\]!",'<h1>',$text); |
234 | $text = preg_replace("!\[\/title\](\\s*\n)*!", '</h1>',$text); |
235 | $text = preg_replace("!(\\s*\n)*\[subtitle\]!",'<h2>',$text); |
236 | $text = preg_replace("!\[\/subtitle\](\\s*\n)*!",'</h2>',$text); |
237 | |
16cd99fb |
238 | require_once('url_catcher.inc.php'); |
239 | $text = url_catcher($text); |
240 | return nl2br($text); |
241 | } else { |
242 | $text = preg_replace('!\[\/?b\]!','*',$text); |
243 | $text = preg_replace('!\[\/?u\]!','_',$text); |
244 | $text = preg_replace('!\[\/?i\]!','/',$text); |
8da0d3c1 |
245 | $text = preg_replace('!\[\/?title\]!','***', $text); |
246 | $text = preg_replace('!\[\/?subtitle\]!','**', $text); |
16cd99fb |
247 | $text = preg_replace('!(((https?|ftp)://|www\.)[^\r\n\t ]*)!','[\1]', $text); |
248 | $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text); |
249 | $text = $just ? justify($text,$width-$indent) : wordwrap($text,$width-$indent); |
250 | if($indent) { |
251 | $ind = str_pad('',$indent); |
252 | $text = $ind.str_replace("\n","\n$ind",$text); |
253 | } |
254 | return $text; |
255 | } |
256 | } |
257 | |
258 | // }}} |
259 | |
a7de4ef7 |
260 | // vim:set et sw=4 sts=4 sws=4 enc=utf-8: |
16cd99fb |
261 | ?> |