Small improvement
[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;
41 var $_date;
42 var $_title;
43 var $_head;
44 var $_cats = Array();
45 var $_arts = Array();
46
47 // }}}
48 // {{{ constructor
49
50 function NewsLetter($id=null)
51 {
0337d704 52 if (isset($id)) {
53 if ($id == 'last') {
08cce2ff 54 $res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
0337d704 55 $id = $res->fetchOneCell();
56 }
08cce2ff 57 $res = XDB::query("SELECT * FROM newsletter WHERE id={?}", $id);
0337d704 58 } else {
08cce2ff 59 $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
3ca8026f 60 if (!$res->numRows()) {
61 insert_new_nl();
62 }
63 $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
64 }
0337d704 65 $nl = $res->fetchOneAssoc();
66
67 $this->_id = $nl['id'];
68 $this->_date = $nl['date'];
69 $this->_title = $nl['titre'];
70 $this->_head = $nl['head'];
71
08cce2ff 72 $res = XDB::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
0337d704 73 while (list($cid, $title) = $res->next()) {
74 $this->_cats[$cid] = $title;
75 }
76
08cce2ff 77 $res = XDB::iterRow(
0337d704 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)
82 WHERE a.id={?}
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);
86 }
87 }
88
89 // }}}
90 // {{{ function setSent()
91
92 function setSent()
93 {
3ca8026f 94 XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id);
0337d704 95 }
96
97 // }}}
98 // {{{ function save()
99
100 function save()
101 {
08cce2ff 102 XDB::execute('UPDATE newsletter SET date={?},titre={?},head={?} WHERE id={?}',
a3a049fc 103 $this->_date, $this->_title, $this->_head, $this->_id);
0337d704 104 }
105
106 // }}}
107 // {{{ function title()
108
109 function title()
110 { return $this->_title; }
111
112 // }}}
113 // {{{ function head()
114
115 function head()
116 { return $this->_head; }
117
118 // }}}
119 // {{{ function getArt()
120
121 function getArt($aid)
122 {
123 foreach ($this->_arts as $key=>$artlist) {
124 if (isset($artlist["a$aid"])) {
125 return $artlist["a$aid"];
126 }
127 }
128 return null;
129 }
130
131 // }}}
132 // {{{ function saveArticle()
133
134 function saveArticle(&$a)
135 {
0337d704 136 if ($a->_aid>=0) {
08cce2ff 137 XDB::execute('REPLACE INTO newsletter_art (id,aid,cid,pos,title,body,append)
0337d704 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;
142 } else {
08cce2ff 143 XDB::execute(
0337d704 144 'INSERT INTO newsletter_art
145 SELECT {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?}
146 FROM newsletter_art AS a
147 WHERE a.id={?}',
148 $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id);
149 $this->_arts['a'.$a->_aid] = $a;
150 }
151 }
152
153 // }}}
154 // {{{ function delArticle()
155
156 function delArticle($aid)
157 {
08cce2ff 158 XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid);
0337d704 159 foreach ($this->_arts as $key=>$art) {
160 unset($this->_arts[$key]["a$aid"]);
161 }
162 }
163
164 // }}}
165 // {{{ function footer
166
167 function footer($html)
168 {
169 global $globals;
170 $url = $globals->baseurl;
171
172 if ($html) {
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">'
164ca57a 175 . "[<a href=\"$url/nl\">archives</a>&nbsp;|&nbsp;"
176 . "<a href=\"$url/nl/submit\">écrire dans la NL</a>&nbsp;|&nbsp;"
177 . "<a href=\"$url/nl/out\">ne plus recevoir</a>]"
0337d704 178 . '</div>';
179 } else {
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"
183 . "\n"
164ca57a 184 . "archives : [$url/nl]\n"
185 . "écrire : [$url/nl/submit]\n"
186 . "ne plus recevoir: [$url/nl/out]\n";
0337d704 187 }
188 }
189
190 // }}}
191 // {{{ function toText()
192
193 function toText($prenom,$nom,$sexe)
194 {
195 $res = "====================================================================\n";
196 $res .= ' '.$this->title()."\n";
197 $res .= "====================================================================\n\n";
198
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);
204
205 if ($head) {
206 $res .= "\n$head\n\n\n";
207 }
208
209 $i = 1;
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";
214 }
215 $i ++;
216 }
217 $res .= "\n\n";
218
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();
225 $res .= "\n\n";
226 }
227 }
228
229 $res .= $this->footer(false);
230
231 return $res;
232 }
233
234 // }}}
235 // {{{ function toHtml()
236
237 function toHtml($prenom,$nom,$sexe,$body=false)
238 {
239 $res = '<div class="title">'.$this->title().'</div>';
240
241 $head = $this->head();
242 $head = str_replace('<cher>', $sexe ? 'Chère' : 'Cher', $head);
243 $head = str_replace('<prenom>', $prenom, $head);
244 $head = str_replace('<nom>', $nom, $head);
245 $head = enriched_to_text($head,true);
246
247 if($head) {
248 $res .= "<div class='intro'>$head</div>";
249 }
250
251 $i = 1;
0983dc41 252 $res .= "<a id='top_lnk'></a>";
0337d704 253 foreach ($this->_arts as $cid=>$arts) {
254 $res .= "<div class='lnk'><a href='#cat$cid'><strong>$i. {$this->_cats[$cid]}</strong></a>";
255 foreach ($arts as $art) {
256 $res .= "<a href='#art{$art->_aid}'>&nbsp;&nbsp;- ".htmlentities($art->title())."</a>";
257 }
258 $res .= '</div>';
259 $i ++;
260 }
261
262 foreach ($this->_arts as $cid=>$arts) {
263 $res .= "<h1><a id='cat$cid'></a><span>".$this->_cats[$cid].'</span></h1>';
264 foreach($arts as $art) {
c6cdf6b8 265 $res .= $art->toHtml();
266 $res .= "<p><a href='#top_lnk'>Revenir au sommaire</a></p>";
0337d704 267 }
268 }
269
270 $res .= $this->footer(true);
271
272 if ($body) {
273 $res = <<<EOF
274<html>
275 <head>
276 <style type="text/css">
277 <!--
278 div.nl { margin: auto; font-family: "Georgia","times new roman",serif; width: 60ex; text-align: justify; font-size: 10pt; }
279 div.title { margin: 2ex 0ex 2ex 0ex; padding: 1ex; width: 100%; font-size: 140%; text-align: center;
280 font-weight: bold; border-bottom: 3px red solid; border-top: 3px red solid; }
281
282 a[href] { text-decoration: none; }
283 a[href]:hover { text-decoration: underline; }
284
285 div.lnk { margin: 2ex 0ex 2ex 0ex; padding: 0ex 2ex 0ex 2ex; }
286 div.lnk a { display: block; }
287
288 h1 { margin: 6ex 0ex 4ex 0ex; padding: 2px 4ex 2px 0ex; width: 60ex; font-size: 100%;
289 border-bottom: 3px red solid; border-top: 3px red solid; }
290 h2 { width: 100%; margin: 0ex 1ex 0ex 1ex; padding: 2px 0px 2px 0px; font-weight: bold; font-style: italic; font-size: 95%; }
291 h1 span { font-size: 140%; padding: 2px 1ex 2px 1ex; border-bottom: 3px red solid; }
292 h2 span { padding: 2px 4px 2px 4px; border-bottom: 2px yellow solid; }
293
294 div.art { padding: 2ex; margin: 0ex 1ex 2ex 1ex; width: 58ex; border-top: 2px yellow solid; }
295 div.app { padding: 2ex 3ex 0ex 3ex; width: 100%; margin: 0ex; text-align: left; font-size: 95%; }
296 div.intro { padding: 2ex; }
297 div.foot { border-top: 1px #808080 dashed; font-size: 95%; padding: 1ex; color: #808080; background: inherit;
298 text-align: center; width: 100% }
299 -->
300 </style>
301 </head>
302 <body>
303 <div class='nl'>
304 $res
305 </div>
306 </body>
307</html>
308EOF;
309 }
310 return $res;
311 }
312
313 // }}}
314 // {{{ function sendTo()
315
316 function sendTo($prenom, $nom, $login, $sex, $html)
317 {
318 global $globals;
319 require_once('diogenes/diogenes.hermes.inc.php');
320
321 $mailer = new HermesMailer();
322 $mailer->setFrom($globals->newsletter->from);
323 $mailer->setSubject($this->title());
324 $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
325 if (!empty($globals->newsletter->replyto)) {
326 $mailer->addHeader('Reply-To',$globals->newsletter->replyto);
327 }
328 if (!empty($globals->newsletter->retpath)) {
329 $mailer->addHeader('Return-Path',$globals->newsletter->retpath);
330 }
331 $mailer->setTxtBody($this->toText($prenom,$nom,$sex));
332 if ($html) {
333 $mailer->setHTMLBody($this->toHtml($prenom,$nom,$sex,true));
334 }
335 $mailer->send();
336 }
337
338 // }}}
339}
340
341// }}}
342// {{{ class NLArticle
343
344class NLArticle
345{
346 // {{{ properties
347
348 var $_aid;
349 var $_cid;
350 var $_pos;
351 var $_title;
352 var $_body;
353 var $_append;
354
355 // }}}
356 // {{{ constructor
357
358 function NLArticle($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
359 {
360 $this->_body = $body;
361 $this->_title = $title;
362 $this->_append = $append;
363 $this->_aid = $aid;
364 $this->_cid = $cid;
365 $this->_pos = $pos;
366 }
367
368 // }}}
369 // {{{ function title()
370
371 function title()
372 { return trim($this->_title); }
373
374 // }}}
375 // {{{ function body()
376
377 function body()
378 { return trim($this->_body); }
379
380 // }}}
381 // {{{ function append()
382
383 function append()
384 { return trim($this->_append); }
385
386 // }}}
387 // {{{ function toText()
388
389 function toText()
390 {
391 $title = '*'.$this->title().'*';
392 $body = enriched_to_text($this->_body,false,true);
393 $app = enriched_to_text($this->_append,false,false,4);
394 return trim("$title\n\n$body\n\n$app")."\n";
395 }
396
397 // }}}
398 // {{{ function toHtml()
399
400 function toHtml()
401 {
402 $title = "<h2><a id='art{$this->_aid}'></a><span>".htmlentities($this->title()).'</span></h2>';
403 $body = enriched_to_text($this->_body,true);
404 $app = enriched_to_text($this->_append,true);
405
406 $art = "$title\n";
407 $art .= "<div class='art'>\n$body\n";
408 if ($app) {
409 $art .= "<div class='app'>$app</div>";
410 }
411 $art .= "</div>\n";
412
413 return $art;
414 }
415
416 // }}}
417 // {{{ function check()
418
419 function check()
420 {
421 $text = enriched_to_text($this->_body);
422 $arr = explode("\n",wordwrap($text,68));
423 $c = 0;
424 foreach ($arr as $line) {
425 if (trim($line)) {
426 $c++;
427 }
428 }
429 return $c<9;
430 }
431
432 // }}}
433}
434
435// }}}
436// {{{ Functions
437
438function insert_new_nl()
439{
08cce2ff 440 XDB::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued'");
0337d704 441}
442
443function get_nl_slist()
444{
08cce2ff 445 $res = XDB::query("SELECT id,date,titre FROM newsletter ORDER BY date DESC");
0337d704 446 return $res->fetchAllAssoc();
447}
448
449function get_nl_list()
450{
08cce2ff 451 $res = XDB::query("SELECT id,date,titre FROM newsletter WHERE bits!='new' ORDER BY date DESC");
0337d704 452 return $res->fetchAllAssoc();
453}
454
455function get_nl_state()
456{
cab08090 457 $res = XDB::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
0337d704 458 return $res->fetchOneCell();
459}
460
461function unsubscribe_nl()
462{
cab08090 463 XDB::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
0337d704 464}
465
466function subscribe_nl($uid=-1)
467{
cab08090 468 $user = ($uid == -1) ? S::v('uid') : $uid;
08cce2ff 469 XDB::execute('REPLACE INTO newsletter_ins (user_id,last)
a3a049fc 470 VALUES ({?}, 0)', $user);
0337d704 471}
472
473function justify($text,$n)
474{
575dd9be 475 $arr = explode("\n",wordwrap($text,$n));
0337d704 476 $arr = array_map('trim',$arr);
477 $res = '';
478 foreach ($arr as $key => $line) {
479 $nxl = isset($arr[$key+1]) ? trim($arr[$key+1]) : '';
480 $nxl_split = preg_split('! +!',$nxl);
481 $nxw_len = count($nxl_split) ? strlen($nxl_split[0]) : 0;
482 $line = trim($line);
483
484 if (strlen($line)+1+$nxw_len < $n) {
485 $res .= "$line\n";
486 continue;
487 }
488
489 if (preg_match('![.:;]$!',$line)) {
490 $res .= "$line\n";
491 continue;
492 }
493
494 $tmp = preg_split('! +!',trim($line));
495 $words = count($tmp);
496 if ($words <= 1) {
497 $res .= "$line\n";
498 continue;
499 }
500
501 $len = array_sum(array_map('strlen',$tmp));
502 $empty = $n - $len;
503 $sw = floatval($empty) / floatval($words-1);
504
505 $cur = 0;
506 $l = '';
507 foreach ($tmp as $word) {
508 $l .= $word;
509 $cur += $sw + strlen($word);
510 $l = str_pad($l,intval($cur+0.5));
511 }
512 $res .= trim($l)."\n";
513 }
514 return trim($res);
515}
516
517function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
518{
519 $text = trim($input);
520 if ($html) {
521 $text = htmlspecialchars($text);
522 $text = str_replace('[b]','<strong>', $text);
523 $text = str_replace('[/b]','</strong>', $text);
524 $text = str_replace('[i]','<em>', $text);
525 $text = str_replace('[/i]','</em>', $text);
526 $text = str_replace('[u]','<span style="text-decoration: underline">', $text);
527 $text = str_replace('[/u]','</span>', $text);
528 $text = preg_replace('!((https?|ftp)://[^\r\n\t ]*)!','<a href="\1">\1</a>', $text);
529 $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','<a href="mailto:\1">\2</a>', $text);
530 return nl2br($text);
531 } else {
532 $text = preg_replace('!\[\/?b\]!','*',$text);
533 $text = preg_replace('!\[\/?u\]!','_',$text);
534 $text = preg_replace('!\[\/?i\]!','/',$text);
535 $text = preg_replace('!((https?|ftp)://[^\r\n\t ]*)!','[\1]', $text);
536 $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text);
537 $text = $just ? justify($text,$width-$indent) : wordwrap($text,$width-$indent);
538 if($indent) {
539 $ind = str_pad('',$indent);
540 $text = $ind.str_replace("\n","\n$ind",$text);
541 }
542 return $text;
543 }
544}
545
546// }}}
547
548// vim:set et sw=4 sts=4 sws=4:
549?>