2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 require_once("xorg.misc.inc.php");
24 // {{{ class MassMailer
26 abstract class MassMailer
39 function __construct($tpl, $css, $prefix)
43 $this->_prefix
= $prefix;
48 return is_null($this->_shortname
) ?
$this->_id
: $this->_shortname
;
51 public function title($mail = false
)
53 return $mail ?
$this->_title_mail
: $this->_title
;
56 public function head($prenom = null
, $nom = null
, $sexe = null
, $type = 'text')
58 if (is_null($prenom)) {
62 $head = str_replace('<cher>', $sexe ?
'Chère' : 'Cher', $head);
63 $head = str_replace('<prenom>', $prenom, $head);
64 $head = str_replace('<nom>', $nom, $head);
65 return format_text($head, $type, 2, 64);
69 public function css(&$page = null
)
71 if (!is_null($page)) {
72 $page->addCssLink($this->_css
);
75 $css = file_get_contents(dirname(__FILE__
) . '/../htdocs/css/' . $this->_css
);
76 return preg_replace('@/\*.*?\*/@us', '', $css);
80 public function toText(&$page, $prenom, $nom, $sexe)
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);
91 public function toHtml(&$page, $prenom, $nom, $sexe)
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);
103 public function sendTo($prenom, $nom, $login, $sexe, $html, $hash = 0)
106 if (strpos($login, '@') === false
) {
107 $login = "$login@{$globals->mail->domain}";
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
);
117 $mailer->assign('hash', $hash);
118 $mailer->addTo("\"$prenom $nom\" <$login>");
119 $mailer->send($html);
122 protected function getAllRecipients()
125 return "SELECT u.user_id, CONCAT(a.alias, '@{$globals->mail->domain}'),
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
138 public function sendToAll()
141 $query = $this->getAllRecipients() . " LIMIT {?}";
143 $res = XDB
::iterRow($query, $this->_id
, 60);
144 if (!$res->total()) {
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);
152 XDB
::execute("UPDATE {$this->subscriptionTable()}
154 WHERE " . implode(' OR ', $sent), $this->_id
);
160 abstract protected function assignData(&$smarty);
161 abstract protected function setSent();
163 abstract protected function subscriptionTable();
164 abstract protected function subscriptionWhere();
170 function justify($text,$n)
172 $arr = explode("\n",wordwrap($text,$n));
173 $arr = array_map('trim',$arr);
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;
181 if (strlen($line)+
1+
$nxw_len < $n) {
186 if (preg_match('![.:;]$!',$line)) {
191 $tmp = preg_split('! +!',trim($line));
192 $words = count($tmp);
198 $len = array_sum(array_map('strlen',$tmp));
200 $sw = floatval($empty) / floatval($words-1);
204 foreach ($tmp as $word) {
206 $cur +
= $sw +
strlen($word);
207 $l = str_pad($l,intval($cur+
0.5));
209 $res .= trim($l)."\n";
214 function format_text($input, $format, $indent = 0, $width = 68)
216 if ($format == 'text') {
217 return enriched_to_text($input, false
, true
, $indent, $width);
219 return enriched_to_text($input, true
);
222 function enriched_to_text($input,$html=false
,$just=false
,$indent=0,$width=68)
224 $text = trim($input);
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);
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);
238 require_once('url_catcher.inc.php');
239 $text = url_catcher($text);
242 $text = preg_replace('!\[\/?b\]!','*',$text);
243 $text = preg_replace('!\[\/?u\]!','_',$text);
244 $text = preg_replace('!\[\/?i\]!','/',$text);
245 $text = preg_replace('!\[\/?title\]!','***', $text);
246 $text = preg_replace('!\[\/?subtitle\]!','**', $text);
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);
251 $ind = str_pad('',$indent);
252 $text = $ind.str_replace("\n","\n$ind",$text);
260 // vim:set et sw=4 sts=4 sws=4 enc=utf-8: