New PlMailer based on Hermes code:
[platal.git] / include / newsletter.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
50a40a33 3 * Copyright (C) 2003-2006 Polytechnique.org *
0337d704 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
24require_once("xorg.misc.inc.php");
0337d704 25
26if (isset($page)) {
c99ef281 27 $page->addCssLink('nl.css');
0337d704 28}
29
30define('FEMME', 1);
31define('HOMME', 0);
32
33// }}}
34// {{{ class NewsLetter
35
36class NewsLetter
37{
38 // {{{ properties
39
40 var $_id;
ed76a506 41 var $_shortname;
0337d704 42 var $_date;
43 var $_title;
8269d2d6 44 var $_title_mail;
0337d704 45 var $_head;
46 var $_cats = Array();
47 var $_arts = Array();
48
49 // }}}
50 // {{{ constructor
51
52 function NewsLetter($id=null)
53 {
ed76a506 54 if (isset($id)) {
55 if ($id == 'last') {
56 $res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
0337d704 57 $id = $res->fetchOneCell();
ed76a506 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'");
ad6ca77d 66 }
ed76a506 67 $nl = $res->fetchOneAssoc();
0337d704 68
ed76a506 69 $this->_id = $nl['id'];
70 $this->_shortname = $nl['short_name'];
71 $this->_date = $nl['date'];
72 $this->_title = $nl['titre'];
8269d2d6 73 $this->_title_mail = $nl['titre_mail'];
ed76a506 74 $this->_head = $nl['head'];
0337d704 75
ed76a506 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 }
ad6ca77d 80
ed76a506 81 $res = XDB::iterRow(
0337d704 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);
ed76a506 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 }
0337d704 91 }
92
93 // }}}
94 // {{{ function setSent()
95
96 function setSent()
97 {
ad6ca77d 98 XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id);
0337d704 99 }
100
101 // }}}
102 // {{{ function save()
103
104 function save()
105 {
8269d2d6 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);
ed76a506 108 }
109
110 // }}}
111 // {{{ function id()
112
113 function id()
114 {
115 return is_null($this->_shortname) ? $this->_id : $this->_shortname;
0337d704 116 }
117
118 // }}}
119 // {{{ function title()
120
8269d2d6 121 function title($mail = false) {
122 if ($mail) {
123 return $this->_title_mail;
124 }
125 return $this->_title;
126 }
0337d704 127
128 // }}}
129 // {{{ function head()
130
131 function head()
132 { return $this->_head; }
133
134 // }}}
135 // {{{ function getArt()
136
137 function getArt($aid)
138 {
ad6ca77d 139 foreach ($this->_arts as $key=>$artlist) {
140 if (isset($artlist["a$aid"])) {
0337d704 141 return $artlist["a$aid"];
142 }
ad6ca77d 143 }
144 return null;
0337d704 145 }
146
147 // }}}
148 // {{{ function saveArticle()
149
150 function saveArticle(&$a)
151 {
ad6ca77d 152 if ($a->_aid>=0) {
153 XDB::execute('REPLACE INTO newsletter_art (id,aid,cid,pos,title,body,append)
0337d704 154 VALUES ({?},{?},{?},{?},{?},{?},{?})',
155 $this->_id, $a->_aid, $a->_cid, $a->_pos,
156 $a->_title, $a->_body, $a->_append);
ad6ca77d 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={?}',
0337d704 164 $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id);
ad6ca77d 165 $this->_arts['a'.$a->_aid] = $a;
166 }
0337d704 167 }
168
169 // }}}
170 // {{{ function delArticle()
171
172 function delArticle($aid)
173 {
ad6ca77d 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 }
0337d704 178 }
179
180 // }}}
181 // {{{ function footer
182
183 function footer($html)
184 {
185 global $globals;
736dcfda 186 $url = 'https://www.polytechnique.org';
0337d704 187
ad6ca77d 188 if ($html) {
377108d3 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">'
ad6ca77d 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 }
0337d704 204 }
205
206 // }}}
207 // {{{ function toText()
208
209 function toText($prenom,$nom,$sexe)
210 {
ad6ca77d 211 $res = "====================================================================\n";
212 $res .= ' '.$this->title()."\n";
213 $res .= "====================================================================\n\n";
0337d704 214
ad6ca77d 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);
0337d704 220
ad6ca77d 221 if ($head) {
0337d704 222 $res .= "\n$head\n\n\n";
223 }
224
ad6ca77d 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;
0337d704 248 }
249
250 // }}}
251 // {{{ function toHtml()
252
6795ed86 253 function toHtml($prenom, $nom, $sexe, $body=false, $urlprefix = false)
0337d704 254 {
ed76a506 255 $u = $urlprefix ? 'nl/show/'.$this->id() : '';
ad6ca77d 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) {
0337d704 265 $res .= "<div class='intro'>$head</div>";
266 }
267
ad6ca77d 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) {
377108d3 280 $res .= "<h1 class='xorg_nl'><a id='cat$cid'></a>".$this->_cats[$cid].'</h1>';
ad6ca77d 281 foreach($arts as $art) {
282 $res .= $art->toHtml();
377108d3 283 $res .= "<div class='top_lnk'><a href='$u#top_lnk'>Revenir au sommaire</a></div>";
ad6ca77d 284 }
285 }
286
287 $res .= $this->footer(true);
288
289 if ($body) {
290 $res = <<<EOF
377108d3 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">
0337d704 294 <head>
377108d3 295 <title></title>
0337d704 296 <style type="text/css">
297 <!--
377108d3 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;
301 text-align: justify;
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; }
306
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;
34cfa5f4 310 background-image: url(http://dev.m4x.org/images/logo_xorg_nl.png);
377108d3 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; }
0337d704 332 -->
333 </style>
334 </head>
335 <body>
336 <div class='nl'>
337 $res
338 </div>
339 </body>
340</html>
341EOF;
ad6ca77d 342 }
343 return $res;
0337d704 344 }
345
346 // }}}
347 // {{{ function sendTo()
348
349 function sendTo($prenom, $nom, $login, $sex, $html)
350 {
351 global $globals;
0337d704 352
1e33266a 353 $mailer = new PlMailer();
354 $mailer->setFrom($globals->newsletter->from);
355 $mailer->setSubject($this->title(true));
356 $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
0337d704 357 if (!empty($globals->newsletter->replyto)) {
358 $mailer->addHeader('Reply-To',$globals->newsletter->replyto);
359 }
360 if (!empty($globals->newsletter->retpath)) {
361 $mailer->addHeader('Return-Path',$globals->newsletter->retpath);
362 }
1e33266a 363 $mailer->setTxtBody($this->toText($prenom,$nom,$sex));
364 if ($html) {
365 $mailer->setHTMLBody($this->toHtml($prenom,$nom,$sex,true));
366 }
367 $mailer->send();
0337d704 368 }
369
370 // }}}
371}
372
373// }}}
374// {{{ class NLArticle
375
376class NLArticle
377{
378 // {{{ properties
379
380 var $_aid;
381 var $_cid;
382 var $_pos;
383 var $_title;
384 var $_body;
385 var $_append;
386
387 // }}}
388 // {{{ constructor
389
390 function NLArticle($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
391 {
ad6ca77d 392 $this->_body = $body;
393 $this->_title = $title;
394 $this->_append = $append;
395 $this->_aid = $aid;
396 $this->_cid = $cid;
397 $this->_pos = $pos;
0337d704 398 }
399
400 // }}}
401 // {{{ function title()
402
403 function title()
404 { return trim($this->_title); }
405
406 // }}}
407 // {{{ function body()
408
409 function body()
410 { return trim($this->_body); }
411
412 // }}}
413 // {{{ function append()
414
415 function append()
416 { return trim($this->_append); }
417
418 // }}}
419 // {{{ function toText()
420
421 function toText()
422 {
ad6ca77d 423 $title = '*'.$this->title().'*';
424 $body = enriched_to_text($this->_body,false,true);
425 $app = enriched_to_text($this->_append,false,false,4);
426 return trim("$title\n\n$body\n\n$app")."\n";
0337d704 427 }
428
429 // }}}
430 // {{{ function toHtml()
431
432 function toHtml()
433 {
377108d3 434 $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".htmlentities($this->title()).'</h2>';
ad6ca77d 435 $body = enriched_to_text($this->_body,true);
436 $app = enriched_to_text($this->_append,true);
437
438 $art = "$title\n";
439 $art .= "<div class='art'>\n$body\n";
440 if ($app) {
0337d704 441 $art .= "<div class='app'>$app</div>";
442 }
ad6ca77d 443 $art .= "</div>\n";
444
445 return $art;
0337d704 446 }
447
448 // }}}
449 // {{{ function check()
450
451 function check()
452 {
ad6ca77d 453 $text = enriched_to_text($this->_body);
454 $arr = explode("\n",wordwrap($text,68));
455 $c = 0;
456 foreach ($arr as $line) {
0337d704 457 if (trim($line)) {
458 $c++;
459 }
460 }
ad6ca77d 461 return $c<9;
0337d704 462 }
463
464 // }}}
465}
466
467// }}}
468// {{{ Functions
469
470function insert_new_nl()
471{
8269d2d6 472 XDB::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
0337d704 473}
474
475function get_nl_slist()
476{
8269d2d6 477 $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");
0337d704 478 return $res->fetchAllAssoc();
479}
480
481function get_nl_list()
482{
8269d2d6 483 $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");
0337d704 484 return $res->fetchAllAssoc();
485}
486
487function get_nl_state()
488{
cab08090 489 $res = XDB::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
0337d704 490 return $res->fetchOneCell();
491}
492
493function unsubscribe_nl()
494{
cab08090 495 XDB::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
0337d704 496}
497
498function subscribe_nl($uid=-1)
499{
cab08090 500 $user = ($uid == -1) ? S::v('uid') : $uid;
08cce2ff 501 XDB::execute('REPLACE INTO newsletter_ins (user_id,last)
a3a049fc 502 VALUES ({?}, 0)', $user);
0337d704 503}
504
505function justify($text,$n)
506{
575dd9be 507 $arr = explode("\n",wordwrap($text,$n));
0337d704 508 $arr = array_map('trim',$arr);
509 $res = '';
510 foreach ($arr as $key => $line) {
ad6ca77d 511 $nxl = isset($arr[$key+1]) ? trim($arr[$key+1]) : '';
512 $nxl_split = preg_split('! +!',$nxl);
513 $nxw_len = count($nxl_split) ? strlen($nxl_split[0]) : 0;
514 $line = trim($line);
515
516 if (strlen($line)+1+$nxw_len < $n) {
517 $res .= "$line\n";
518 continue;
519 }
520
521 if (preg_match('![.:;]$!',$line)) {
522 $res .= "$line\n";
523 continue;
524 }
525
526 $tmp = preg_split('! +!',trim($line));
527 $words = count($tmp);
528 if ($words <= 1) {
529 $res .= "$line\n";
530 continue;
531 }
532
533 $len = array_sum(array_map('strlen',$tmp));
534 $empty = $n - $len;
535 $sw = floatval($empty) / floatval($words-1);
536
537 $cur = 0;
538 $l = '';
539 foreach ($tmp as $word) {
540 $l .= $word;
541 $cur += $sw + strlen($word);
542 $l = str_pad($l,intval($cur+0.5));
543 }
544 $res .= trim($l)."\n";
0337d704 545 }
546 return trim($res);
547}
548
549function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
550{
551 $text = trim($input);
552 if ($html) {
ad6ca77d 553 $text = htmlspecialchars($text);
554 $text = str_replace('[b]','<strong>', $text);
555 $text = str_replace('[/b]','</strong>', $text);
556 $text = str_replace('[i]','<em>', $text);
557 $text = str_replace('[/i]','</em>', $text);
558 $text = str_replace('[u]','<span style="text-decoration: underline">', $text);
559 $text = str_replace('[/u]','</span>', $text);
560 require_once('url_catcher.inc.php');
561 $text = url_catcher($text);
562 return nl2br($text);
0337d704 563 } else {
ad6ca77d 564 $text = preg_replace('!\[\/?b\]!','*',$text);
565 $text = preg_replace('!\[\/?u\]!','_',$text);
566 $text = preg_replace('!\[\/?i\]!','/',$text);
567 $text = preg_replace('!(((https?|ftp)://|www\.)[^\r\n\t ]*)!','[\1]', $text);
568 $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text);
569 $text = $just ? justify($text,$width-$indent) : wordwrap($text,$width-$indent);
570 if($indent) {
571 $ind = str_pad('',$indent);
572 $text = $ind.str_replace("\n","\n$ind",$text);
573 }
574 return $text;
0337d704 575 }
576}
577
578// }}}
579
580// vim:set et sw=4 sts=4 sws=4:
581?>