a60ecb39a2da343e608e66b32af4b2444aae36f0
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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");
27 $page->addCssLink('nl.css');
34 // {{{ class NewsLetter
50 function NewsLetter($id=null
)
54 $res = XDB
::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
55 $id = $res->fetchOneCell();
57 $res = XDB
::query("SELECT * FROM newsletter WHERE id={?}", $id);
59 $res = XDB
::query("SELECT * FROM newsletter WHERE bits='new'");
60 if (!$res->numRows()) {
63 $res = XDB
::query("SELECT * FROM newsletter WHERE bits='new'");
65 $nl = $res->fetchOneAssoc();
67 $this->_id
= $nl['id'];
68 $this->_date
= $nl['date'];
69 $this->_title
= $nl['titre'];
70 $this->_head
= $nl['head'];
72 $res = XDB
::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
73 while (list($cid, $title) = $res->next()) {
74 $this->_cats
[$cid] = $title;
78 "SELECT a.title,a.body,a.append,a.aid,a.cid,a.pos
79 FROM newsletter_art AS a
80 INNER JOIN newsletter AS n USING(id)
81 LEFT JOIN newsletter_cat AS c ON(a.cid=c.cid)
83 ORDER BY c.pos,a.pos", $this->_id
);
84 while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) {
85 $this->_arts
[$cid]["a$aid"] = new NLArticle($title, $body, $append, $aid, $cid, $pos);
90 // {{{ function setSent()
94 XDB
::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id
);
98 // {{{ function save()
102 XDB
::execute('UPDATE newsletter SET date={?},titre={?},head={?} WHERE id={?}',
103 $this->_date
, $this->_title
, $this->_head
, $this->_id
);
107 // {{{ function title()
110 { return $this->_title
; }
113 // {{{ function head()
116 { return $this->_head
; }
119 // {{{ function getArt()
121 function getArt($aid)
123 foreach ($this->_arts
as $key=>$artlist) {
124 if (isset($artlist["a$aid"])) {
125 return $artlist["a$aid"];
132 // {{{ function saveArticle()
134 function saveArticle(&$a)
137 XDB
::execute('REPLACE INTO newsletter_art (id,aid,cid,pos,title,body,append)
138 VALUES ({?},{?},{?},{?},{?},{?},{?})',
139 $this->_id
, $a->_aid
, $a->_cid
, $a->_pos
,
140 $a->_title
, $a->_body
, $a->_append
);
141 $this->_arts
['a'.$a->_aid
] = $a;
144 'INSERT INTO newsletter_art
145 SELECT {?},MAX(aid)+1,{?},'.($a->_pos ?
intval($a->_pos
) : 'MAX(pos)+1').',{?},{?},{?}
146 FROM newsletter_art AS a
148 $this->_id
, $a->_cid
, $a->_title
, $a->_body
, $a->_append
, $this->_id
);
149 $this->_arts
['a'.$a->_aid
] = $a;
154 // {{{ function delArticle()
156 function delArticle($aid)
158 XDB
::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id
, $aid);
159 foreach ($this->_arts
as $key=>$art) {
160 unset($this->_arts
[$key]["a$aid"]);
165 // {{{ function footer
167 function footer($html)
170 $url = 'https://www.polytechnique.org';
173 return '<div class="foot">Cette lettre est envoyée à tous les Polytechniciens sur Internet par l\'intermédiaire de Polytechnique.org.</div>'
174 . '<div class="foot">'
175 . "[<a href=\"$url/nl\">archives</a> | "
176 . "<a href=\"$url/nl/submit\">écrire dans la NL</a> | "
177 . "<a href=\"$url/nl/out\">ne plus recevoir</a>]"
180 return "\n\n--------------------------------------------------------------------\n"
181 . "Cette lettre est envoyée à tous les Polytechniciens sur Internet par\n"
182 . "l'intermédiaire de Polytechnique.org.\n"
184 . "archives : [$url/nl]\n"
185 . "écrire : [$url/nl/submit]\n"
186 . "ne plus recevoir: [$url/nl/out]\n";
191 // {{{ function toText()
193 function toText($prenom,$nom,$sexe)
195 $res = "====================================================================\n";
196 $res .= ' '.$this->title()."\n";
197 $res .= "====================================================================\n\n";
199 $head = $this->head();
200 $head = str_replace('<cher>', $sexe ?
'Chère' : 'Cher', $head);
201 $head = str_replace('<prenom>', $prenom, $head);
202 $head = str_replace('<nom>', $nom, $head);
203 $head = enriched_to_text($head,false
,true
,2,64);
206 $res .= "\n$head\n\n\n";
210 foreach ($this->_arts
as $cid=>$arts) {
211 $res .= "\n$i *{$this->_cats[$cid]}*\n";
212 foreach ($arts as $art) {
213 $res .= '- '.$art->title()."\n";
219 foreach ($this->_arts
as $cid=>$arts) {
220 $res .= "--------------------------------------------------------------------\n";
221 $res .= "*{$this->_cats[$cid]}*\n";
222 $res .= "--------------------------------------------------------------------\n\n";
223 foreach ($arts as $art) {
224 $res .= $art->toText();
229 $res .= $this->footer(false
);
235 // {{{ function toHtml()
237 function toHtml($prenom, $nom, $sexe, $body=false
, $urlprefix = false
)
239 $u = $urlprefix ?
'nl/show/'.$this->_id
: '';
240 $res = '<div class="title">'.$this->title().'</div>';
242 $head = $this->head();
243 $head = str_replace('<cher>', $sexe ?
'Chère' : 'Cher', $head);
244 $head = str_replace('<prenom>', $prenom, $head);
245 $head = str_replace('<nom>', $nom, $head);
246 $head = enriched_to_text($head, true
);
249 $res .= "<div class='intro'>$head</div>";
253 $res .= "<a id='top_lnk'></a>";
254 foreach ($this->_arts
as $cid=>$arts) {
255 $res .= "<div class='lnk'><a href='$u#cat$cid'><strong>$i. {$this->_cats[$cid]}</strong></a>";
256 foreach ($arts as $art) {
257 $res .= "<a href='$u#art{$art->_aid}'> - ".htmlentities($art->title())."</a>";
263 foreach ($this->_arts
as $cid=>$arts) {
264 $res .= "<h1 class='xorg_nl'><a id='cat$cid'></a><span>".$this->_cats
[$cid].'</span></h1>';
265 foreach($arts as $art) {
266 $res .= $art->toHtml();
267 $res .= "<p><a href='$u#top_lnk'>Revenir au sommaire</a></p>";
271 $res .= $this->footer(true
);
277 <style type="text/css">
279 div.nl { margin: auto; font-family: "Georgia","times new roman",serif; width: 60ex; text-align: justify; font-size: 10pt; }
280 div.title { margin: 2ex 0ex 2ex 0ex; padding: 1ex; width: 100%; font-size: 140%; text-align: center;
281 font-weight: bold; border-bottom: 3px red solid; border-top: 3px red solid; }
283 a[href] { text-decoration: none; }
284 a[href]:hover { text-decoration: underline; }
286 div.lnk { margin: 2ex 0ex 2ex 0ex; padding: 0ex 2ex 0ex 2ex; }
287 div.lnk a { display: block; }
289 h1.xorg_nl { margin: 6ex 0ex 4ex 0ex; padding: 2px 4ex 2px 0ex; width: 60ex; font-size: 100%;
290 border-bottom: 3px red solid; border-top: 3px red solid; }
291 h2.xorg_nl { width: 100%; margin: 0ex 1ex 0ex 1ex; padding: 2px 0px 2px 0px; font-weight: bold; font-style: italic; font-size: 95%; }
292 h1.xorg_nl span { font-size: 140%; padding: 2px 1ex 2px 1ex; border-bottom: 3px red solid; }
293 h2.xorg_nl span { padding: 2px 4px 2px 4px; border-bottom: 2px yellow solid; }
295 div.art { padding: 2ex; margin: 0ex 1ex 2ex 1ex; width: 58ex; border-top: 2px yellow solid; }
296 div.app { padding: 2ex 3ex 0ex 3ex; width: 100%; margin: 0ex; text-align: left; font-size: 95%; }
297 div.intro { padding: 2ex; }
298 div.foot { border-top: 1px #808080 dashed; font-size: 95%; padding: 1ex; color: #808080; background: inherit;
299 text-align: center; width: 100% }
315 // {{{ function sendTo()
317 function sendTo($prenom, $nom, $login, $sex, $html)
320 require_once('diogenes/diogenes.hermes.inc.php');
322 $mailer = new HermesMailer();
323 $mailer->setFrom($globals->newsletter
->from
);
324 $mailer->setSubject($this->title());
325 $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
326 if (!empty($globals->newsletter
->replyto
)) {
327 $mailer->addHeader('Reply-To',$globals->newsletter
->replyto
);
329 if (!empty($globals->newsletter
->retpath
)) {
330 $mailer->addHeader('Return-Path',$globals->newsletter
->retpath
);
332 $mailer->setTxtBody($this->toText($prenom,$nom,$sex));
334 $mailer->setHTMLBody($this->toHtml($prenom,$nom,$sex,true
));
343 // {{{ class NLArticle
359 function NLArticle($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
361 $this->_body
= $body;
362 $this->_title
= $title;
363 $this->_append
= $append;
370 // {{{ function title()
373 { return trim($this->_title
); }
376 // {{{ function body()
379 { return trim($this->_body
); }
382 // {{{ function append()
385 { return trim($this->_append
); }
388 // {{{ function toText()
392 $title = '*'.$this->title().'*';
393 $body = enriched_to_text($this->_body
,false
,true
);
394 $app = enriched_to_text($this->_append
,false
,false
,4);
395 return trim("$title\n\n$body\n\n$app")."\n";
399 // {{{ function toHtml()
403 $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a><span>".htmlentities($this->title()).'</span></h2>';
404 $body = enriched_to_text($this->_body
,true
);
405 $app = enriched_to_text($this->_append
,true
);
408 $art .= "<div class='art'>\n$body\n";
410 $art .= "<div class='app'>$app</div>";
418 // {{{ function check()
422 $text = enriched_to_text($this->_body
);
423 $arr = explode("\n",wordwrap($text,68));
425 foreach ($arr as $line) {
439 function insert_new_nl()
441 XDB
::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued'");
444 function get_nl_slist()
446 $res = XDB
::query("SELECT id,date,titre FROM newsletter ORDER BY date DESC");
447 return $res->fetchAllAssoc();
450 function get_nl_list()
452 $res = XDB
::query("SELECT id,date,titre FROM newsletter WHERE bits!='new' ORDER BY date DESC");
453 return $res->fetchAllAssoc();
456 function get_nl_state()
458 $res = XDB
::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S
::v('uid'));
459 return $res->fetchOneCell();
462 function unsubscribe_nl()
464 XDB
::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S
::v('uid'));
467 function subscribe_nl($uid=-1)
469 $user = ($uid == -1) ? S
::v('uid') : $uid;
470 XDB
::execute('REPLACE INTO newsletter_ins (user_id,last)
471 VALUES ({?}, 0)', $user);
474 function justify($text,$n)
476 $arr = explode("\n",wordwrap($text,$n));
477 $arr = array_map('trim',$arr);
479 foreach ($arr as $key => $line) {
480 $nxl = isset($arr[$key+
1]) ?
trim($arr[$key+
1]) : '';
481 $nxl_split = preg_split('! +!',$nxl);
482 $nxw_len = count($nxl_split) ?
strlen($nxl_split[0]) : 0;
485 if (strlen($line)+
1+
$nxw_len < $n) {
490 if (preg_match('![.:;]$!',$line)) {
495 $tmp = preg_split('! +!',trim($line));
496 $words = count($tmp);
502 $len = array_sum(array_map('strlen',$tmp));
504 $sw = floatval($empty) / floatval($words-1);
508 foreach ($tmp as $word) {
510 $cur +
= $sw +
strlen($word);
511 $l = str_pad($l,intval($cur+
0.5));
513 $res .= trim($l)."\n";
518 function enriched_to_text($input,$html=false
,$just=false
,$indent=0,$width=68)
520 $text = trim($input);
522 $text = htmlspecialchars($text);
523 $text = str_replace('[b]','<strong>', $text);
524 $text = str_replace('[/b]','</strong>', $text);
525 $text = str_replace('[i]','<em>', $text);
526 $text = str_replace('[/i]','</em>', $text);
527 $text = str_replace('[u]','<span style="text-decoration: underline">', $text);
528 $text = str_replace('[/u]','</span>', $text);
529 require_once('url_catcher.inc.php');
530 $text = url_catcher($text);
533 $text = preg_replace('!\[\/?b\]!','*',$text);
534 $text = preg_replace('!\[\/?u\]!','_',$text);
535 $text = preg_replace('!\[\/?i\]!','/',$text);
536 $text = preg_replace('!(((https?|ftp)://|www\.)[^\r\n\t ]*)!','[\1]', $text);
537 $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text);
538 $text = $just ?
justify($text,$width-$indent) : wordwrap($text,$width-$indent);
540 $ind = str_pad('',$indent);
541 $text = $ind.str_replace("\n","\n$ind",$text);
549 // vim:set et sw=4 sts=4 sws=4: