deux titres pour la nl : un pour le mail, un pour le vrai titre
[platal.git] / include / newsletter.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 // {{{ requires + defines
23
24 require_once("xorg.misc.inc.php");
25
26 if (isset($page)) {
27 $page->addCssLink('nl.css');
28 }
29
30 define('FEMME', 1);
31 define('HOMME', 0);
32
33 // }}}
34 // {{{ class NewsLetter
35
36 class NewsLetter
37 {
38 // {{{ properties
39
40 var $_id;
41 var $_shortname;
42 var $_date;
43 var $_title;
44 var $_title_mail;
45 var $_head;
46 var $_cats = Array();
47 var $_arts = Array();
48
49 // }}}
50 // {{{ constructor
51
52 function NewsLetter($id=null)
53 {
54 if (isset($id)) {
55 if ($id == 'last') {
56 $res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
57 $id = $res->fetchOneCell();
58 }
59 $res = XDB::query("SELECT * FROM newsletter WHERE id={?} OR short_name={?} LIMIT 1", $id, $id);
60 } else {
61 $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
62 if (!$res->numRows()) {
63 insert_new_nl();
64 }
65 $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
66 }
67 $nl = $res->fetchOneAssoc();
68
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'];
75
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;
79 }
80
81 $res = XDB::iterRow(
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)
86 WHERE a.id={?}
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);
90 }
91 }
92
93 // }}}
94 // {{{ function setSent()
95
96 function setSent()
97 {
98 XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id);
99 }
100
101 // }}}
102 // {{{ function save()
103
104 function save()
105 {
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);
108 }
109
110 // }}}
111 // {{{ function id()
112
113 function id()
114 {
115 return is_null($this->_shortname) ? $this->_id : $this->_shortname;
116 }
117
118 // }}}
119 // {{{ function title()
120
121 function title($mail = false) {
122 if ($mail) {
123 return $this->_title_mail;
124 }
125 return $this->_title;
126 }
127
128 // }}}
129 // {{{ function head()
130
131 function head()
132 { return $this->_head; }
133
134 // }}}
135 // {{{ function getArt()
136
137 function getArt($aid)
138 {
139 foreach ($this->_arts as $key=>$artlist) {
140 if (isset($artlist["a$aid"])) {
141 return $artlist["a$aid"];
142 }
143 }
144 return null;
145 }
146
147 // }}}
148 // {{{ function saveArticle()
149
150 function saveArticle(&$a)
151 {
152 if ($a->_aid>=0) {
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;
158 } else {
159 XDB::execute(
160 'INSERT INTO newsletter_art
161 SELECT {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?}
162 FROM newsletter_art AS a
163 WHERE a.id={?}',
164 $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id);
165 $this->_arts['a'.$a->_aid] = $a;
166 }
167 }
168
169 // }}}
170 // {{{ function delArticle()
171
172 function delArticle($aid)
173 {
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"]);
177 }
178 }
179
180 // }}}
181 // {{{ function footer
182
183 function footer($html)
184 {
185 global $globals;
186 $url = 'https://www.polytechnique.org';
187
188 if ($html) {
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>&nbsp;|&nbsp;"
192 . "<a href=\"$url/nl/submit\">écrire dans la NL</a>&nbsp;|&nbsp;"
193 . "<a href=\"$url/nl/out\">ne plus recevoir</a>]"
194 . '</div>';
195 } else {
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"
199 . "\n"
200 . "archives : [$url/nl]\n"
201 . "écrire : [$url/nl/submit]\n"
202 . "ne plus recevoir: [$url/nl/out]\n";
203 }
204 }
205
206 // }}}
207 // {{{ function toText()
208
209 function toText($prenom,$nom,$sexe)
210 {
211 $res = "====================================================================\n";
212 $res .= ' '.$this->title()."\n";
213 $res .= "====================================================================\n\n";
214
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);
220
221 if ($head) {
222 $res .= "\n$head\n\n\n";
223 }
224
225 $i = 1;
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";
230 }
231 $i ++;
232 }
233 $res .= "\n\n";
234
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();
241 $res .= "\n\n";
242 }
243 }
244
245 $res .= $this->footer(false);
246
247 return $res;
248 }
249
250 // }}}
251 // {{{ function toHtml()
252
253 function toHtml($prenom, $nom, $sexe, $body=false, $urlprefix = false)
254 {
255 $u = $urlprefix ? 'nl/show/'.$this->id() : '';
256 $res = '<div class="title">'.$this->title().'</div>';
257
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);
263
264 if($head) {
265 $res .= "<div class='intro'>$head</div>";
266 }
267
268 $i = 1;
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}'>&nbsp;&nbsp;- ".htmlentities($art->title())."</a>";
274 }
275 $res .= '</div>';
276 $i ++;
277 }
278
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>";
284 }
285 }
286
287 $res .= $this->footer(true);
288
289 if ($body) {
290 $res = <<<EOF
291 <html>
292 <head>
293 <style type="text/css">
294 <!--
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; }
298
299 a[href] { text-decoration: none; }
300 a[href]:hover { text-decoration: underline; }
301
302 div.lnk { margin: 2ex 0ex 2ex 0ex; padding: 0ex 2ex 0ex 2ex; }
303 div.lnk a { display: block; }
304
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; }
310
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% }
316 -->
317 </style>
318 </head>
319 <body>
320 <div class='nl'>
321 $res
322 </div>
323 </body>
324 </html>
325 EOF;
326 }
327 return $res;
328 }
329
330 // }}}
331 // {{{ function sendTo()
332
333 function sendTo($prenom, $nom, $login, $sex, $html)
334 {
335 global $globals;
336 require_once('diogenes/diogenes.hermes.inc.php');
337
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);
344 }
345 if (!empty($globals->newsletter->retpath)) {
346 $mailer->addHeader('Return-Path',$globals->newsletter->retpath);
347 }
348 $mailer->setTxtBody($this->toText($prenom,$nom,$sex));
349 if ($html) {
350 $mailer->setHTMLBody($this->toHtml($prenom,$nom,$sex,true));
351 }
352 $mailer->send();
353 }
354
355 // }}}
356 }
357
358 // }}}
359 // {{{ class NLArticle
360
361 class NLArticle
362 {
363 // {{{ properties
364
365 var $_aid;
366 var $_cid;
367 var $_pos;
368 var $_title;
369 var $_body;
370 var $_append;
371
372 // }}}
373 // {{{ constructor
374
375 function NLArticle($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
376 {
377 $this->_body = $body;
378 $this->_title = $title;
379 $this->_append = $append;
380 $this->_aid = $aid;
381 $this->_cid = $cid;
382 $this->_pos = $pos;
383 }
384
385 // }}}
386 // {{{ function title()
387
388 function title()
389 { return trim($this->_title); }
390
391 // }}}
392 // {{{ function body()
393
394 function body()
395 { return trim($this->_body); }
396
397 // }}}
398 // {{{ function append()
399
400 function append()
401 { return trim($this->_append); }
402
403 // }}}
404 // {{{ function toText()
405
406 function toText()
407 {
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";
412 }
413
414 // }}}
415 // {{{ function toHtml()
416
417 function toHtml()
418 {
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);
422
423 $art = "$title\n";
424 $art .= "<div class='art'>\n$body\n";
425 if ($app) {
426 $art .= "<div class='app'>$app</div>";
427 }
428 $art .= "</div>\n";
429
430 return $art;
431 }
432
433 // }}}
434 // {{{ function check()
435
436 function check()
437 {
438 $text = enriched_to_text($this->_body);
439 $arr = explode("\n",wordwrap($text,68));
440 $c = 0;
441 foreach ($arr as $line) {
442 if (trim($line)) {
443 $c++;
444 }
445 }
446 return $c<9;
447 }
448
449 // }}}
450 }
451
452 // }}}
453 // {{{ Functions
454
455 function insert_new_nl()
456 {
457 XDB::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
458 }
459
460 function get_nl_slist()
461 {
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();
464 }
465
466 function get_nl_list()
467 {
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();
470 }
471
472 function get_nl_state()
473 {
474 $res = XDB::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
475 return $res->fetchOneCell();
476 }
477
478 function unsubscribe_nl()
479 {
480 XDB::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
481 }
482
483 function subscribe_nl($uid=-1)
484 {
485 $user = ($uid == -1) ? S::v('uid') : $uid;
486 XDB::execute('REPLACE INTO newsletter_ins (user_id,last)
487 VALUES ({?}, 0)', $user);
488 }
489
490 function justify($text,$n)
491 {
492 $arr = explode("\n",wordwrap($text,$n));
493 $arr = array_map('trim',$arr);
494 $res = '';
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;
499 $line = trim($line);
500
501 if (strlen($line)+1+$nxw_len < $n) {
502 $res .= "$line\n";
503 continue;
504 }
505
506 if (preg_match('![.:;]$!',$line)) {
507 $res .= "$line\n";
508 continue;
509 }
510
511 $tmp = preg_split('! +!',trim($line));
512 $words = count($tmp);
513 if ($words <= 1) {
514 $res .= "$line\n";
515 continue;
516 }
517
518 $len = array_sum(array_map('strlen',$tmp));
519 $empty = $n - $len;
520 $sw = floatval($empty) / floatval($words-1);
521
522 $cur = 0;
523 $l = '';
524 foreach ($tmp as $word) {
525 $l .= $word;
526 $cur += $sw + strlen($word);
527 $l = str_pad($l,intval($cur+0.5));
528 }
529 $res .= trim($l)."\n";
530 }
531 return trim($res);
532 }
533
534 function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
535 {
536 $text = trim($input);
537 if ($html) {
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);
547 return nl2br($text);
548 } else {
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);
555 if($indent) {
556 $ind = str_pad('',$indent);
557 $text = $ind.str_replace("\n","\n$ind",$text);
558 }
559 return $text;
560 }
561 }
562
563 // }}}
564
565 // vim:set et sw=4 sts=4 sws=4:
566 ?>