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