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="foot1">Cette lettre est envoyée à tous les Polytechniciens sur Internet par l\'intermédiaire de Polytechnique.org.</div>'
190 . '<div class="foot2">'
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>".$this->_cats
[$cid].'</h1>';
281 foreach($arts as $art) {
282 $res .= $art->toHtml();
283 $res .= "<div class='top_lnk'><a href='$u#top_lnk'>Revenir au sommaire</a></div>";
287 $res .= $this->footer(true
);
291 <?xml version="1.0" encoding="iso-8859-15"?>
292 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
293 <html xmlns="http://www.w3.org/1999/xhtml">
296 <style type="text/css">
298 body { background-color: #ddd; color: #000; }
299 div.nl { margin: auto; width: 72ex;
300 font-family: "Georgia", "Times New Roman", serif; font-size: 11pt;
302 background-color: #fff; color: #000; }
303 a[href] { text-decoration: none;
304 background-color: #fff; color: #36c; }
305 a[href]:hover { background-color: #fff; color: #6c0; }
307 div.title { margin: 0ex 0ex 3ex 0ex; padding: 5ex 1ex 1ex 15ex;
308 font-size: 130%; font-weight: bold; text-align: right;
309 background-color: #369; color: #fff;
310 background-image: url(http://carva.org/aymeric.augustin/images/poly_org_nl.png);
311 background-repeat: no-repeat; background-position: 0.5ex 0.5ex; }
312 div.intro { margin: 4ex 3ex; }
313 div.lnk { margin: 2ex 6ex;}
314 div.lnk a { display: block; font-size: 95%; }
315 div.top_lnk { margin: 2ex; padding: 0ex; font-size: 85%; text-align: right; }
316 h1.xorg_nl { margin: 3ex 0ex 2ex 0ex; padding: 1.5ex 2ex 0.5ex 1ex;
317 font-size: 120%; font-weight: bold; text-align: right;
318 background-color: #369; color: #fff; }
319 h2.xorg_nl { margin: 2ex 0ex 0ex 0ex; padding: 0.4ex 2ex;
320 font-size: 100%; font-weight: bold; font-style: italic;
321 background-color: #fff; color: #369;
322 border-width: thin 0; border-style: solid; border-color: #369; }
323 div.art { margin: 2ex 3ex; }
324 div.app { margin: 2ex 6ex 0ex 3ex; font-size: 95%; text-align: left; }
325 div.foot1 { margin: 8ex 0ex 0ex 0ex; padding: 0.5ex 2ex;
326 font-size: 90%; background-color: #fff; color: #999;
327 border-width: thin 0; border-style: solid; border-color: #ddd;
328 text-align: center; }
329 div.foot2 { padding: 1ex 0ex;
330 font-size: 90%; background-color: #fff; color: #999;
331 text-align: center; }
347 // {{{ function sendTo()
349 function sendTo($prenom, $nom, $login, $sex, $html)
352 require_once('diogenes/diogenes.hermes.inc.php');
354 $mailer = new HermesMailer();
355 $mailer->setFrom($globals->newsletter
->from
);
356 $mailer->setSubject($this->title(true
));
357 $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
358 if (!empty($globals->newsletter
->replyto
)) {
359 $mailer->addHeader('Reply-To',$globals->newsletter
->replyto
);
361 if (!empty($globals->newsletter
->retpath
)) {
362 $mailer->addHeader('Return-Path',$globals->newsletter
->retpath
);
364 $mailer->setTxtBody($this->toText($prenom,$nom,$sex));
366 $mailer->setHTMLBody($this->toHtml($prenom,$nom,$sex,true
));
375 // {{{ class NLArticle
391 function NLArticle($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
393 $this->_body
= $body;
394 $this->_title
= $title;
395 $this->_append
= $append;
402 // {{{ function title()
405 { return trim($this->_title
); }
408 // {{{ function body()
411 { return trim($this->_body
); }
414 // {{{ function append()
417 { return trim($this->_append
); }
420 // {{{ function toText()
424 $title = '*'.$this->title().'*';
425 $body = enriched_to_text($this->_body
,false
,true
);
426 $app = enriched_to_text($this->_append
,false
,false
,4);
427 return trim("$title\n\n$body\n\n$app")."\n";
431 // {{{ function toHtml()
435 $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".htmlentities($this->title()).'</h2>';
436 $body = enriched_to_text($this->_body
,true
);
437 $app = enriched_to_text($this->_append
,true
);
440 $art .= "<div class='art'>\n$body\n";
442 $art .= "<div class='app'>$app</div>";
450 // {{{ function check()
454 $text = enriched_to_text($this->_body
);
455 $arr = explode("\n",wordwrap($text,68));
457 foreach ($arr as $line) {
471 function insert_new_nl()
473 XDB
::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
476 function get_nl_slist()
478 $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");
479 return $res->fetchAllAssoc();
482 function get_nl_list()
484 $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");
485 return $res->fetchAllAssoc();
488 function get_nl_state()
490 $res = XDB
::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S
::v('uid'));
491 return $res->fetchOneCell();
494 function unsubscribe_nl()
496 XDB
::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S
::v('uid'));
499 function subscribe_nl($uid=-1)
501 $user = ($uid == -1) ? S
::v('uid') : $uid;
502 XDB
::execute('REPLACE INTO newsletter_ins (user_id,last)
503 VALUES ({?}, 0)', $user);
506 function justify($text,$n)
508 $arr = explode("\n",wordwrap($text,$n));
509 $arr = array_map('trim',$arr);
511 foreach ($arr as $key => $line) {
512 $nxl = isset($arr[$key+
1]) ?
trim($arr[$key+
1]) : '';
513 $nxl_split = preg_split('! +!',$nxl);
514 $nxw_len = count($nxl_split) ?
strlen($nxl_split[0]) : 0;
517 if (strlen($line)+
1+
$nxw_len < $n) {
522 if (preg_match('![.:;]$!',$line)) {
527 $tmp = preg_split('! +!',trim($line));
528 $words = count($tmp);
534 $len = array_sum(array_map('strlen',$tmp));
536 $sw = floatval($empty) / floatval($words-1);
540 foreach ($tmp as $word) {
542 $cur +
= $sw +
strlen($word);
543 $l = str_pad($l,intval($cur+
0.5));
545 $res .= trim($l)."\n";
550 function enriched_to_text($input,$html=false
,$just=false
,$indent=0,$width=68)
552 $text = trim($input);
554 $text = htmlspecialchars($text);
555 $text = str_replace('[b]','<strong>', $text);
556 $text = str_replace('[/b]','</strong>', $text);
557 $text = str_replace('[i]','<em>', $text);
558 $text = str_replace('[/i]','</em>', $text);
559 $text = str_replace('[u]','<span style="text-decoration: underline">', $text);
560 $text = str_replace('[/u]','</span>', $text);
561 require_once('url_catcher.inc.php');
562 $text = url_catcher($text);
565 $text = preg_replace('!\[\/?b\]!','*',$text);
566 $text = preg_replace('!\[\/?u\]!','_',$text);
567 $text = preg_replace('!\[\/?i\]!','/',$text);
568 $text = preg_replace('!(((https?|ftp)://|www\.)[^\r\n\t ]*)!','[\1]', $text);
569 $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text);
570 $text = $just ?
justify($text,$width-$indent) : wordwrap($text,$width-$indent);
572 $ind = str_pad('',$indent);
573 $text = $ind.str_replace("\n","\n$ind",$text);
581 // vim:set et sw=4 sts=4 sws=4: