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