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 // {{{ requires + defines
24 require_once("xorg.misc.inc.php");
30 // {{{ class MassMailer
32 abstract class MassMailer
45 function __construct($tpl, $css, $prefix)
49 $this->_prefix
= $prefix;
54 return is_null($this->_shortname
) ?
$this->_id
: $this->_shortname
;
57 public function title($mail = false
)
59 return $mail ?
$this->_title_mail
: $this->_title
;
62 public function head($prenom = null
, $nom = null
, $sexe = null
, $type = 'text')
64 if (is_null($prenom)) {
68 $head = str_replace('<cher>', $sexe ?
'Chère' : 'Cher', $head);
69 $head = str_replace('<prenom>', $prenom, $head);
70 $head = str_replace('<nom>', $nom, $head);
71 if ($type == 'text') {
72 $head = enriched_to_text($head, false
, true
, 2, 64);
74 $head = enriched_to_text($head, true
);
80 public function css(&$page = null
)
82 if (!is_null($page)) {
83 $page->addCssLink($this->_css
);
86 $css = file_get_contents(dirname(__FILE__
) . '/../htdocs/css/' . $this->_css
);
87 return preg_replace('@/\*.*?\*/@s', '', $css);
91 public function toText(&$page, $prenom, $nom, $sexe)
94 $page->assign('is_mail', false
);
95 $page->assign('html_version', false
);
96 $page->assign('prenom', $prenom);
97 $page->assign('nom', $nom);
98 $page->assign('sexe', $sexe);
99 $this->assignData($page);
102 public function toHtml(&$page, $prenom, $nom, $sexe)
105 $page->assign('prefix', $this->_prefix
. '/' . $this->id());
106 $page->assign('is_mail', false
);
107 $page->assign('html_version', true
);
108 $page->assign('prenom', $prenom);
109 $page->assign('nom', $nom);
110 $page->assign('sexe', $sexe);
111 $this->assignData($page);
114 public function sendTo($prenom, $nom, $login, $sexe, $html)
118 $mailer = new PlMailer($this->_tpl
);
119 $this->assignData($mailer);
120 $mailer->assign('is_mail', true
);
121 $mailer->assign('prenom', $prenom);
122 $mailer->assign('nom', $nom);
123 $mailer->assign('sexe', $sexe);
124 $mailer->assign('prefix', null
);
125 $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
126 $mailer->send($html);
129 public function sendToAll()
132 $query = "SELECT u.user_id, a.alias,
133 u.prenom, IF(u.nom_usage='', u.nom, u.nom_usage),
134 FIND_IN_SET('femme', u.flags),
135 q.core_mail_fmt AS pref
136 FROM {$this->subscriptionTable()} AS ni
137 INNER JOIN auth_user_md5 AS u USING(user_id)
138 INNER JOIN auth_user_quick AS q ON(q.user_id = u.user_id)
139 INNER JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
140 LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active')
141 WHERE ({$this->subscriptionWhere()}) AND e.email IS NOT NULL
145 $res = XDB
::iterRow($query);
146 if (!$res->total()) {
150 while (list($uid, $bestalias, $prenom, $nom, $sexe, $fmt) = $res->next()) {
151 $sent[] = "user_id='$uid'";
152 $this->sendTo($prenom, $nom, $bestalias, $sexe, $fmt=='html');
154 XDB
::execute("UPDATE {$this->subscriptionTable()}
155 SET {$this->subscriptionUpdate()}
156 WHERE " . implode(' OR ', $sent));
161 abstract protected function assignData(&$smarty);
162 abstract protected function setSent();
163 abstract static public function subscribe($uid = -1);
164 abstract static public function unsubscribe($uid = -1);
165 abstract static public function subscriptionState($uid = -1);
167 abstract protected function subscriptionTable();
168 abstract protected function subscriptionWhere();
169 abstract protected function subscriptionUpdate();
173 // {{{ class NewsLetter
175 class NewsLetter
extends MassMailer
178 public $_cats = Array();
179 public $_arts = Array();
181 function __construct($id = null
)
183 parent
::__construct('newsletter/nl.tpl', 'nl.css', 'nl/show');
186 $res = XDB
::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
187 $id = $res->fetchOneCell();
189 $res = XDB
::query("SELECT * FROM newsletter WHERE id={?} OR short_name={?} LIMIT 1", $id, $id);
191 $res = XDB
::query("SELECT * FROM newsletter WHERE bits='new'");
192 if (!$res->numRows()) {
193 Newsletter
::create();
195 $res = XDB
::query("SELECT * FROM newsletter WHERE bits='new'");
197 $nl = $res->fetchOneAssoc();
199 $this->_id
= $nl['id'];
200 $this->_shortname
= $nl['short_name'];
201 $this->_date
= $nl['date'];
202 $this->_title
= $nl['titre'];
203 $this->_title_mail
= $nl['titre_mail'];
204 $this->_head
= $nl['head'];
206 $res = XDB
::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
207 while (list($cid, $title) = $res->next()) {
208 $this->_cats
[$cid] = $title;
212 "SELECT a.title,a.body,a.append,a.aid,a.cid,a.pos
213 FROM newsletter_art AS a
214 INNER JOIN newsletter AS n USING(id)
215 LEFT JOIN newsletter_cat AS c ON(a.cid=c.cid)
217 ORDER BY c.pos,a.pos", $this->_id
);
218 while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) {
219 $this->_arts
[$cid]["a$aid"] = new NLArticle($title, $body, $append, $aid, $cid, $pos);
223 public function save()
225 XDB
::execute('UPDATE newsletter SET date={?},titre={?},titre_mail={?},head={?},short_name={?} WHERE id={?}',
226 $this->_date
, $this->_title
, $this->_title_mail
, $this->_head
, $this->_shortname
,$this->_id
);
229 public function getArt($aid)
231 foreach ($this->_arts
as $key=>$artlist) {
232 if (isset($artlist["a$aid"])) {
233 return $artlist["a$aid"];
239 public function saveArticle(&$a)
242 XDB
::execute('REPLACE INTO newsletter_art (id,aid,cid,pos,title,body,append)
243 VALUES ({?},{?},{?},{?},{?},{?},{?})',
244 $this->_id
, $a->_aid
, $a->_cid
, $a->_pos
,
245 $a->_title
, $a->_body
, $a->_append
);
246 $this->_arts
['a'.$a->_aid
] = $a;
248 XDB
::execute('INSERT INTO newsletter_art
249 SELECT {?},MAX(aid)+1,{?},'.($a->_pos ?
intval($a->_pos
) : 'MAX(pos)+1').',{?},{?},{?}
250 FROM newsletter_art AS a
252 $this->_id
, $a->_cid
, $a->_title
, $a->_body
, $a->_append
, $this->_id
);
253 $this->_arts
['a'.$a->_aid
] = $a;
257 public function delArticle($aid)
259 XDB
::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id
, $aid);
260 foreach ($this->_arts
as $key=>$art) {
261 unset($this->_arts
[$key]["a$aid"]);
265 protected function assignData(&$smarty)
267 $smarty->assign_by_ref('nl', $this);
270 protected function setSent()
272 XDB
::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id
);
275 protected function subscriptionTable()
277 return 'newsletter_ins';
280 protected function subscriptionWhere()
282 return 'ni.last<' . $this->_id
;
285 protected function subscriptionUpdate()
287 return 'last=' . $this->_id
;
290 static public function subscriptionState($uid = -1)
292 $user = ($uid == -1) ? S
::v('uid') : $uid;
293 $res = XDB
::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', $user);
294 return $res->fetchOneCell();
297 static public function unsubscribe($uid = -1)
299 $user = ($uid == -1) ? S
::v('uid') : $uid;
300 XDB
::execute('DELETE FROM newsletter_ins WHERE user_id={?}', $user);
303 static public function subscribe($uid = -1)
305 $user = ($uid == -1) ? S
::v('uid') : $uid;
306 XDB
::execute('REPLACE INTO newsletter_ins (user_id,last) VALUES ({?}, 0)', $user);
309 static public function create()
311 XDB
::execute("INSERT INTO newsletter
312 SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
315 static public function listSent()
317 $res = XDB
::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
320 ORDER BY date DESC");
321 return $res->fetchAllAssoc();
324 static public function listAll()
326 $res = XDB
::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
328 ORDER BY date DESC");
329 return $res->fetchAllAssoc();
334 // {{{ class NLArticle
350 function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
352 $this->_body
= $body;
353 $this->_title
= $title;
354 $this->_append
= $append;
361 // {{{ function title()
363 public function title()
364 { return trim($this->_title
); }
367 // {{{ function body()
369 public function body()
370 { return trim($this->_body
); }
373 // {{{ function append()
375 public function append()
376 { return trim($this->_append
); }
379 // {{{ function toText()
381 public function toText()
383 $title = '*'.$this->title().'*';
384 $body = enriched_to_text($this->_body
,false
,true
);
385 $app = enriched_to_text($this->_append
,false
,false
,4);
386 return trim("$title\n\n$body\n\n$app")."\n";
390 // {{{ function toHtml()
392 public function toHtml()
394 $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".htmlentities($this->title()).'</h2>';
395 $body = enriched_to_text($this->_body
,true
);
396 $app = enriched_to_text($this->_append
,true
);
399 $art .= "<div class='art'>\n$body\n";
401 $art .= "<div class='app'>$app</div>";
409 // {{{ function check()
411 public function check()
413 $text = enriched_to_text($this->_body
);
414 $arr = explode("\n",wordwrap($text,68));
416 foreach ($arr as $line) {
430 function justify($text,$n)
432 $arr = explode("\n",wordwrap($text,$n));
433 $arr = array_map('trim',$arr);
435 foreach ($arr as $key => $line) {
436 $nxl = isset($arr[$key+
1]) ?
trim($arr[$key+
1]) : '';
437 $nxl_split = preg_split('! +!',$nxl);
438 $nxw_len = count($nxl_split) ?
strlen($nxl_split[0]) : 0;
441 if (strlen($line)+
1+
$nxw_len < $n) {
446 if (preg_match('![.:;]$!',$line)) {
451 $tmp = preg_split('! +!',trim($line));
452 $words = count($tmp);
458 $len = array_sum(array_map('strlen',$tmp));
460 $sw = floatval($empty) / floatval($words-1);
464 foreach ($tmp as $word) {
466 $cur +
= $sw +
strlen($word);
467 $l = str_pad($l,intval($cur+
0.5));
469 $res .= trim($l)."\n";
474 function enriched_to_text($input,$html=false
,$just=false
,$indent=0,$width=68)
476 $text = trim($input);
478 $text = htmlspecialchars($text);
479 $text = str_replace('[b]','<strong>', $text);
480 $text = str_replace('[/b]','</strong>', $text);
481 $text = str_replace('[i]','<em>', $text);
482 $text = str_replace('[/i]','</em>', $text);
483 $text = str_replace('[u]','<span style="text-decoration: underline">', $text);
484 $text = str_replace('[/u]','</span>', $text);
485 require_once('url_catcher.inc.php');
486 $text = url_catcher($text);
489 $text = preg_replace('!\[\/?b\]!','*',$text);
490 $text = preg_replace('!\[\/?u\]!','_',$text);
491 $text = preg_replace('!\[\/?i\]!','/',$text);
492 $text = preg_replace('!(((https?|ftp)://|www\.)[^\r\n\t ]*)!','[\1]', $text);
493 $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text);
494 $text = $just ?
justify($text,$width-$indent) : wordwrap($text,$width-$indent);
496 $ind = str_pad('',$indent);
497 $text = $ind.str_replace("\n","\n$ind",$text);
505 // vim:set et sw=4 sts=4 sws=4: