482a61d14acde0b83cd94066b13470a4b1de796f
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
52 function NewsLetter($id=null
)
56 $res = XDB
::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
57 $id = $res->fetchOneCell();
59 $res = XDB
::query("SELECT * FROM newsletter WHERE id={?} OR short_name={?} LIMIT 1", $id, $id);
61 $res = XDB
::query("SELECT * FROM newsletter WHERE bits='new'");
62 if (!$res->numRows()) {
65 $res = XDB
::query("SELECT * FROM newsletter WHERE bits='new'");
67 $nl = $res->fetchOneAssoc();
69 $this->_id
= $nl['id'];
70 $this->_shortname
= $nl['short_name'];
71 $this->_date
= $nl['date'];
72 $this->_title
= $nl['titre'];
73 $this->_title_mail
= $nl['titre_mail'];
74 $this->_head
= $nl['head'];
76 $res = XDB
::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
77 while (list($cid, $title) = $res->next()) {
78 $this->_cats
[$cid] = $title;
82 "SELECT a.title,a.body,a.append,a.aid,a.cid,a.pos
83 FROM newsletter_art AS a
84 INNER JOIN newsletter AS n USING(id)
85 LEFT JOIN newsletter_cat AS c ON(a.cid=c.cid)
87 ORDER BY c.pos,a.pos", $this->_id
);
88 while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) {
89 $this->_arts
[$cid]["a$aid"] = new NLArticle($title, $body, $append, $aid, $cid, $pos);
94 // {{{ function setSent()
98 XDB
::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id
);
102 // {{{ function save()
106 XDB
::execute('UPDATE newsletter SET date={?},titre={?},titre_mail={?},head={?},short_name={?} WHERE id={?}',
107 $this->_date
, $this->_title
, $this->_title_mail
, $this->_head
, $this->_shortname
,$this->_id
);
115 return is_null($this->_shortname
) ?
$this->_id
: $this->_shortname
;
119 // {{{ function title()
121 function title($mail = false
) {
123 return $this->_title_mail
;
125 return $this->_title
;
129 // {{{ function head()
132 { return $this->_head
; }
135 // {{{ function getArt()
137 function getArt($aid)
139 foreach ($this->_arts
as $key=>$artlist) {
140 if (isset($artlist["a$aid"])) {
141 return $artlist["a$aid"];
148 // {{{ function saveArticle()
150 function saveArticle(&$a)
153 XDB
::execute('REPLACE INTO newsletter_art (id,aid,cid,pos,title,body,append)
154 VALUES ({?},{?},{?},{?},{?},{?},{?})',
155 $this->_id
, $a->_aid
, $a->_cid
, $a->_pos
,
156 $a->_title
, $a->_body
, $a->_append
);
157 $this->_arts
['a'.$a->_aid
] = $a;
160 'INSERT INTO newsletter_art
161 SELECT {?},MAX(aid)+1,{?},'.($a->_pos ?
intval($a->_pos
) : 'MAX(pos)+1').',{?},{?},{?}
162 FROM newsletter_art AS a
164 $this->_id
, $a->_cid
, $a->_title
, $a->_body
, $a->_append
, $this->_id
);
165 $this->_arts
['a'.$a->_aid
] = $a;
170 // {{{ function delArticle()
172 function delArticle($aid)
174 XDB
::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id
, $aid);
175 foreach ($this->_arts
as $key=>$art) {
176 unset($this->_arts
[$key]["a$aid"]);
181 // {{{ function footer
183 function footer($html)
186 $url = 'https://www.polytechnique.org';
189 return '<div class="foot">Cette lettre est envoyée à tous les Polytechniciens sur Internet par l\'intermédiaire de Polytechnique.org.</div>'
190 . '<div class="foot">'
191 . "[<a href=\"$url/nl\">archives</a> | "
192 . "<a href=\"$url/nl/submit\">écrire dans la NL</a> | "
193 . "<a href=\"$url/nl/out\">ne plus recevoir</a>]"
196 return "\n\n--------------------------------------------------------------------\n"
197 . "Cette lettre est envoyée à tous les Polytechniciens sur Internet par\n"
198 . "l'intermédiaire de Polytechnique.org.\n"
200 . "archives : [$url/nl]\n"
201 . "écrire : [$url/nl/submit]\n"
202 . "ne plus recevoir: [$url/nl/out]\n";
207 // {{{ function toText()
209 function toText($prenom,$nom,$sexe)
211 $res = "====================================================================\n";
212 $res .= ' '.$this->title()."\n";
213 $res .= "====================================================================\n\n";
215 $head = $this->head();
216 $head = str_replace('<cher>', $sexe ?
'Chère' : 'Cher', $head);
217 $head = str_replace('<prenom>', $prenom, $head);
218 $head = str_replace('<nom>', $nom, $head);
219 $head = enriched_to_text($head,false
,true
,2,64);
222 $res .= "\n$head\n\n\n";
226 foreach ($this->_arts
as $cid=>$arts) {
227 $res .= "\n$i *{$this->_cats[$cid]}*\n";
228 foreach ($arts as $art) {
229 $res .= '- '.$art->title()."\n";
235 foreach ($this->_arts
as $cid=>$arts) {
236 $res .= "--------------------------------------------------------------------\n";
237 $res .= "*{$this->_cats[$cid]}*\n";
238 $res .= "--------------------------------------------------------------------\n\n";
239 foreach ($arts as $art) {
240 $res .= $art->toText();
245 $res .= $this->footer(false
);
251 // {{{ function toHtml()
253 function toHtml($prenom, $nom, $sexe, $body=false
, $urlprefix = false
)
255 $u = $urlprefix ?
'nl/show/'.$this->id() : '';
256 $res = '<div class="title">'.$this->title().'</div>';
258 $head = $this->head();
259 $head = str_replace('<cher>', $sexe ?
'Chère' : 'Cher', $head);
260 $head = str_replace('<prenom>', $prenom, $head);
261 $head = str_replace('<nom>', $nom, $head);
262 $head = enriched_to_text($head, true
);
265 $res .= "<div class='intro'>$head</div>";
269 $res .= "<a id='top_lnk'></a>";
270 foreach ($this->_arts
as $cid=>$arts) {
271 $res .= "<div class='lnk'><a href='$u#cat$cid'><strong>$i. {$this->_cats[$cid]}</strong></a>";
272 foreach ($arts as $art) {
273 $res .= "<a href='$u#art{$art->_aid}'> - ".htmlentities($art->title())."</a>";
279 foreach ($this->_arts
as $cid=>$arts) {
280 $res .= "<h1 class='xorg_nl'><a id='cat$cid'></a><span>".$this->_cats
[$cid].'</span></h1>';
281 foreach($arts as $art) {
282 $res .= $art->toHtml();
283 $res .= "<p><a href='$u#top_lnk'>Revenir au sommaire</a></p>";
287 $res .= $this->footer(true
);
293 <style type="text/css">
295 div.nl { margin: auto; font-family: "Georgia","times new roman",serif; width: 60ex; text-align: justify; font-size: 10pt; }
296 div.title { margin: 2ex 0ex 2ex 0ex; padding: 1ex; width: 100%; font-size: 140%; text-align: center;
297 font-weight: bold; border-bottom: 3px red solid; border-top: 3px red solid; }
299 a[href] { text-decoration: none; }
300 a[href]:hover { text-decoration: underline; }
302 div.lnk { margin: 2ex 0ex 2ex 0ex; padding: 0ex 2ex 0ex 2ex; }
303 div.lnk a { display: block; }
305 h1.xorg_nl { margin: 6ex 0ex 4ex 0ex; padding: 2px 4ex 2px 0ex; width: 60ex; font-size: 100%;
306 border-bottom: 3px red solid; border-top: 3px red solid; }
307 h2.xorg_nl { width: 100%; margin: 0ex 1ex 0ex 1ex; padding: 2px 0px 2px 0px; font-weight: bold; font-style: italic; font-size: 95%; }
308 h1.xorg_nl span { font-size: 140%; padding: 2px 1ex 2px 1ex; border-bottom: 3px red solid; }
309 h2.xorg_nl span { padding: 2px 4px 2px 4px; border-bottom: 2px yellow solid; }
311 div.art { padding: 2ex; margin: 0ex 1ex 2ex 1ex; width: 58ex; border-top: 2px yellow solid; }
312 div.app { padding: 2ex 3ex 0ex 3ex; width: 100%; margin: 0ex; text-align: left; font-size: 95%; }
313 div.intro { padding: 2ex; }
314 div.foot { border-top: 1px #808080 dashed; font-size: 95%; padding: 1ex; color: #808080; background: inherit;
315 text-align: center; width: 100% }
331 // {{{ function sendTo()
333 function sendTo($prenom, $nom, $login, $sex, $html)
336 require_once('diogenes/diogenes.hermes.inc.php');
338 $mailer = new HermesMailer();
339 $mailer->setFrom($globals->newsletter
->from
);
340 $mailer->setSubject($this->title(true
));
341 $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
342 if (!empty($globals->newsletter
->replyto
)) {
343 $mailer->addHeader('Reply-To',$globals->newsletter
->replyto
);
345 if (!empty($globals->newsletter
->retpath
)) {
346 $mailer->addHeader('Return-Path',$globals->newsletter
->retpath
);
348 $mailer->setTxtBody($this->toText($prenom,$nom,$sex));
350 $mailer->setHTMLBody($this->toHtml($prenom,$nom,$sex,true
));
359 // {{{ class NLArticle
375 function NLArticle($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
377 $this->_body
= $body;
378 $this->_title
= $title;
379 $this->_append
= $append;
386 // {{{ function title()
389 { return trim($this->_title
); }
392 // {{{ function body()
395 { return trim($this->_body
); }
398 // {{{ function append()
401 { return trim($this->_append
); }
404 // {{{ function toText()
408 $title = '*'.$this->title().'*';
409 $body = enriched_to_text($this->_body
,false
,true
);
410 $app = enriched_to_text($this->_append
,false
,false
,4);
411 return trim("$title\n\n$body\n\n$app")."\n";
415 // {{{ function toHtml()
419 $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a><span>".htmlentities($this->title()).'</span></h2>';
420 $body = enriched_to_text($this->_body
,true
);
421 $app = enriched_to_text($this->_append
,true
);
424 $art .= "<div class='art'>\n$body\n";
426 $art .= "<div class='app'>$app</div>";
434 // {{{ function check()
438 $text = enriched_to_text($this->_body
);
439 $arr = explode("\n",wordwrap($text,68));
441 foreach ($arr as $line) {
455 function insert_new_nl()
457 XDB
::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
460 function get_nl_slist()
462 $res = XDB
::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre FROM newsletter ORDER BY date DESC");
463 return $res->fetchAllAssoc();
466 function get_nl_list()
468 $res = XDB
::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre FROM newsletter WHERE bits!='new' ORDER BY date DESC");
469 return $res->fetchAllAssoc();
472 function get_nl_state()
474 $res = XDB
::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S
::v('uid'));
475 return $res->fetchOneCell();
478 function unsubscribe_nl()
480 XDB
::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S
::v('uid'));
483 function subscribe_nl($uid=-1)
485 $user = ($uid == -1) ? S
::v('uid') : $uid;
486 XDB
::execute('REPLACE INTO newsletter_ins (user_id,last)
487 VALUES ({?}, 0)', $user);
490 function justify($text,$n)
492 $arr = explode("\n",wordwrap($text,$n));
493 $arr = array_map('trim',$arr);
495 foreach ($arr as $key => $line) {
496 $nxl = isset($arr[$key+
1]) ?
trim($arr[$key+
1]) : '';
497 $nxl_split = preg_split('! +!',$nxl);
498 $nxw_len = count($nxl_split) ?
strlen($nxl_split[0]) : 0;
501 if (strlen($line)+
1+
$nxw_len < $n) {
506 if (preg_match('![.:;]$!',$line)) {
511 $tmp = preg_split('! +!',trim($line));
512 $words = count($tmp);
518 $len = array_sum(array_map('strlen',$tmp));
520 $sw = floatval($empty) / floatval($words-1);
524 foreach ($tmp as $word) {
526 $cur +
= $sw +
strlen($word);
527 $l = str_pad($l,intval($cur+
0.5));
529 $res .= trim($l)."\n";
534 function enriched_to_text($input,$html=false
,$just=false
,$indent=0,$width=68)
536 $text = trim($input);
538 $text = htmlspecialchars($text);
539 $text = str_replace('[b]','<strong>', $text);
540 $text = str_replace('[/b]','</strong>', $text);
541 $text = str_replace('[i]','<em>', $text);
542 $text = str_replace('[/i]','</em>', $text);
543 $text = str_replace('[u]','<span style="text-decoration: underline">', $text);
544 $text = str_replace('[/u]','</span>', $text);
545 require_once('url_catcher.inc.php');
546 $text = url_catcher($text);
549 $text = preg_replace('!\[\/?b\]!','*',$text);
550 $text = preg_replace('!\[\/?u\]!','_',$text);
551 $text = preg_replace('!\[\/?i\]!','/',$text);
552 $text = preg_replace('!(((https?|ftp)://|www\.)[^\r\n\t ]*)!','[\1]', $text);
553 $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text);
554 $text = $just ?
justify($text,$width-$indent) : wordwrap($text,$width-$indent);
556 $ind = str_pad('',$indent);
557 $text = $ind.str_replace("\n","\n$ind",$text);
565 // vim:set et sw=4 sts=4 sws=4: