New PlMailer based on Hermes code:
[platal.git] / include / newsletter.inc.php
index 6528a60..426bc27 100644 (file)
 // {{{ requires + defines
 
 require_once("xorg.misc.inc.php");
-require_once("diogenes/diogenes.misc.inc.php");
 
 if (isset($page)) {
-    $page->addCssLink('css/nl.css');
+    $page->addCssLink('nl.css');
 }
 
 define('FEMME', 1);
@@ -39,8 +38,10 @@ class NewsLetter
     // {{{ properties
     
     var $_id;
+    var $_shortname;
     var $_date;
     var $_title;
+    var $_title_mail;
     var $_head;
     var $_cats = Array();
     var $_arts = Array();
@@ -50,39 +51,43 @@ class NewsLetter
     
     function NewsLetter($id=null)
     {
-       global $globals;
-
-       if (isset($id)) {
-           if ($id == 'last') {
-               $res = $globals->xdb->query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
+        if (isset($id)) {
+            if ($id == 'last') {
+                $res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
                 $id  = $res->fetchOneCell();
-           }
-           $res = $globals->xdb->query("SELECT * FROM newsletter WHERE id={?}", $id);
-       } else {
-           $res = $globals->xdb->query("SELECT * FROM newsletter WHERE bits='new'");
-       }
-       $nl = $res->fetchOneAssoc();
-
-       $this->_id    = $nl['id'];
-       $this->_date  = $nl['date'];
-       $this->_title = $nl['titre'];
-       $this->_head  = $nl['head'];
-
-       $res = $globals->xdb->iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
-       while (list($cid, $title) = $res->next()) {
-           $this->_cats[$cid] = $title;
-       }
-       
-       $res = $globals->xdb->iterRow(
+            }
+            $res = XDB::query("SELECT * FROM newsletter WHERE id={?} OR short_name={?} LIMIT 1", $id, $id);
+        } else {
+            $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
+            if (!$res->numRows()) {
+                insert_new_nl();
+            }
+            $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
+        }
+        $nl = $res->fetchOneAssoc();
+
+        $this->_id        = $nl['id'];
+        $this->_shortname = $nl['short_name'];
+        $this->_date      = $nl['date'];
+        $this->_title     = $nl['titre'];
+        $this->_title_mail = $nl['titre_mail'];
+        $this->_head      = $nl['head'];
+
+        $res = XDB::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
+        while (list($cid, $title) = $res->next()) {
+            $this->_cats[$cid] = $title;
+        }
+    
+        $res = XDB::iterRow(
                 "SELECT  a.title,a.body,a.append,a.aid,a.cid,a.pos
                    FROM  newsletter_art AS a
              INNER JOIN  newsletter     AS n USING(id)
              LEFT  JOIN  newsletter_cat AS c ON(a.cid=c.cid)
                   WHERE  a.id={?}
                ORDER BY  c.pos,a.pos", $this->_id);
-       while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) {
-           $this->_arts[$cid]["a$aid"] = new NLArticle($title, $body, $append, $aid, $cid, $pos);
-       }
+        while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) {
+            $this->_arts[$cid]["a$aid"] = new NLArticle($title, $body, $append, $aid, $cid, $pos);
+        }
     }
 
     // }}}
@@ -90,8 +95,7 @@ class NewsLetter
 
     function setSent()
     {
-       global $globals;
-       $globals->xdb->execute("UPDATE  newsletter SET bits='sent' WHERE id={?}", $this->_id);
+        XDB::execute("UPDATE  newsletter SET bits='sent' WHERE id={?}", $this->_id);
     }
 
     // }}}
@@ -99,16 +103,27 @@ class NewsLetter
 
     function save()
     {
-       global $globals;
-       $globals->xdb->execute('UPDATE newsletter SET date={?},titre={?},head={?} WHERE id={?}',
-                $this->_date, $this->_title, $this->_head, $this->_id);
+        XDB::execute('UPDATE newsletter SET date={?},titre={?},titre_mail={?},head={?},short_name={?} WHERE id={?}',
+                     $this->_date, $this->_title, $this->_title_mail, $this->_head, $this->_shortname,$this->_id);
+    }
+
+    // }}}
+    // {{{ function id()
+
+    function id()
+    {
+        return is_null($this->_shortname) ? $this->_id : $this->_shortname;
     }
 
     // }}}
     // {{{ function title()
 
-    function title()
-    { return $this->_title; }
+    function title($mail = false) {
+       if ($mail) {
+               return $this->_title_mail;
+       }
+               return $this->_title;
+       }
 
     // }}}
     // {{{ function head()
@@ -121,12 +136,12 @@ class NewsLetter
     
     function getArt($aid)
     {
-       foreach ($this->_arts as $key=>$artlist) {
-           if (isset($artlist["a$aid"])) {
+    foreach ($this->_arts as $key=>$artlist) {
+        if (isset($artlist["a$aid"])) {
                 return $artlist["a$aid"];
             }
-       }
-       return null;
+    }
+    return null;
     }
 
     // }}}
@@ -134,22 +149,21 @@ class NewsLetter
 
     function saveArticle(&$a)
     {
-       global $globals;
-       if ($a->_aid>=0) {
-           $globals->xdb->execute('REPLACE INTO  newsletter_art (id,aid,cid,pos,title,body,append)
+    if ($a->_aid>=0) {
+        XDB::execute('REPLACE INTO  newsletter_art (id,aid,cid,pos,title,body,append)
                                           VALUES  ({?},{?},{?},{?},{?},{?},{?})',
                                           $this->_id, $a->_aid, $a->_cid, $a->_pos,
                                           $a->_title, $a->_body, $a->_append);
-           $this->_arts['a'.$a->_aid] = $a;
-       } else {
-           $globals->xdb->execute(
-               'INSERT INTO  newsletter_art
-                     SELECT  {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?}
-                       FROM  newsletter_art AS a
-                      WHERE  a.id={?}',
+        $this->_arts['a'.$a->_aid] = $a;
+    } else {
+        XDB::execute(
+        'INSERT INTO  newsletter_art
+              SELECT  {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?}
+            FROM  newsletter_art AS a
+               WHERE  a.id={?}',
                        $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id);
-           $this->_arts['a'.$a->_aid] = $a;
-       }
+        $this->_arts['a'.$a->_aid] = $a;
+    }
     }
 
     // }}}
@@ -157,11 +171,10 @@ class NewsLetter
     
     function delArticle($aid)
     {
-       global $globals;
-       $globals->xdb->execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid);
-       foreach ($this->_arts as $key=>$art) {
-           unset($this->_arts[$key]["a$aid"]);
-       }
+    XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid);
+    foreach ($this->_arts as $key=>$art) {
+        unset($this->_arts[$key]["a$aid"]);
+    }
     }
 
     // }}}
@@ -170,24 +183,24 @@ class NewsLetter
     function footer($html)
     {
         global $globals;
-        $url = $globals->baseurl;
-
-       if ($html) {
-           return '<div class="foot">Cette lettre est envoyée à tous les Polytechniciens sur Internet par l\'intermédiaire de Polytechnique.org.</div>'
-           .  '<div class="foot">'
-           .  "[<a href=\"$url/newsletter/\">archives</a>&nbsp;|&nbsp;"
-           .  "<a href=\"$url/newsletter/submit.php\">écrire dans la NL</a>&nbsp;|&nbsp;"
-           .  "<a href=\"$url/newsletter/?out=1\">ne plus recevoir</a>]"
-           .  '</div>';
-       } else {
-           return "\n\n--------------------------------------------------------------------\n"
-                . "Cette lettre est envoyée à tous les Polytechniciens sur Internet par\n"
-                . "l'intermédiaire de Polytechnique.org.\n"
-                . "\n"
-                . "archives : [$url/newsletter/]\n"
-                . "écrire   : [$url/newsletter/submit.php]\n"
-                . "ne plus recevoir: [$url/newsletter/?out=1]\n";
-       }
+        $url = 'https://www.polytechnique.org';
+
+    if ($html) {
+        return '<div class="foot1">Cette lettre est envoyée à tous les Polytechniciens sur Internet par l\'intermédiaire de Polytechnique.org.</div>'
+        .  '<div class="foot2">'
+        .  "[<a href=\"$url/nl\">archives</a>&nbsp;|&nbsp;"
+        .  "<a href=\"$url/nl/submit\">écrire dans la NL</a>&nbsp;|&nbsp;"
+        .  "<a href=\"$url/nl/out\">ne plus recevoir</a>]"
+        .  '</div>';
+    } else {
+        return "\n\n--------------------------------------------------------------------\n"
+             . "Cette lettre est envoyée à tous les Polytechniciens sur Internet par\n"
+             . "l'intermédiaire de Polytechnique.org.\n"
+         . "\n"
+         . "archives : [$url/nl]\n"
+         . "écrire   : [$url/nl/submit]\n"
+         . "ne plus recevoir: [$url/nl/out]\n";
+    }
     }
 
     // }}}
@@ -195,110 +208,127 @@ class NewsLetter
 
     function toText($prenom,$nom,$sexe)
     {
-       $res  = "====================================================================\n";
-       $res .= ' '.$this->title()."\n";
-       $res .= "====================================================================\n\n";
+    $res  = "====================================================================\n";
+    $res .= ' '.$this->title()."\n";
+    $res .= "====================================================================\n\n";
 
-       $head = $this->head();
-       $head = str_replace('<cher>',   $sexe ? 'Chère' : 'Cher', $head);
-       $head = str_replace('<prenom>', $prenom, $head);
-       $head = str_replace('<nom>',    $nom,    $head);
-       $head = enriched_to_text($head,false,true,2,64);
+    $head = $this->head();
+    $head = str_replace('<cher>',   $sexe ? 'Chère' : 'Cher', $head);
+    $head = str_replace('<prenom>', $prenom, $head);
+    $head = str_replace('<nom>',    $nom,    $head);
+    $head = enriched_to_text($head,false,true,2,64);
 
-       if ($head) {
+    if ($head) {
             $res .= "\n$head\n\n\n";
         }
 
-       $i = 1;
-       foreach ($this->_arts as $cid=>$arts) {
-           $res .= "\n$i *{$this->_cats[$cid]}*\n";
-           foreach ($arts as $art) {
-               $res .= '- '.$art->title()."\n";
-           }
-           $i ++;
-       }
-       $res .= "\n\n";
-           
-       foreach ($this->_arts as $cid=>$arts) {
-           $res .= "--------------------------------------------------------------------\n";
-           $res .= "*{$this->_cats[$cid]}*\n";
-           $res .= "--------------------------------------------------------------------\n\n";
-           foreach ($arts as $art) {
-               $res .= $art->toText();
-               $res .= "\n\n";
-           }
-       }
-       
-       $res .= $this->footer(false);
-       
-       return $res;
+    $i = 1;
+    foreach ($this->_arts as $cid=>$arts) {
+        $res .= "\n$i *{$this->_cats[$cid]}*\n";
+        foreach ($arts as $art) {
+        $res .= '- '.$art->title()."\n";
+        }
+        $i ++;
+    }
+    $res .= "\n\n";
+        
+    foreach ($this->_arts as $cid=>$arts) {
+        $res .= "--------------------------------------------------------------------\n";
+        $res .= "*{$this->_cats[$cid]}*\n";
+        $res .= "--------------------------------------------------------------------\n\n";
+        foreach ($arts as $art) {
+        $res .= $art->toText();
+        $res .= "\n\n";
+        }
+    }
+    
+    $res .= $this->footer(false);
+    
+    return $res;
     }
 
     // }}}
     // {{{ function toHtml()
     
-    function toHtml($prenom,$nom,$sexe,$body=false)
+    function toHtml($prenom, $nom, $sexe, $body=false, $urlprefix = false)
     {
-       $res  = '<div class="title">'.$this->title().'</div>';
-       
-       $head = $this->head();
-       $head = str_replace('<cher>',   $sexe ? 'Chère' : 'Cher', $head);
-       $head = str_replace('<prenom>', $prenom, $head);
-       $head = str_replace('<nom>',    $nom,    $head);
-       $head = enriched_to_text($head,true);
-
-       if($head) {
+        $u    = $urlprefix ? 'nl/show/'.$this->id() : '';
+    $res  = '<div class="title">'.$this->title().'</div>';
+    
+    $head = $this->head();
+    $head = str_replace('<cher>',   $sexe ? 'Chère' : 'Cher', $head);
+    $head = str_replace('<prenom>', $prenom, $head);
+    $head = str_replace('<nom>',    $nom,    $head);
+    $head = enriched_to_text($head, true);
+
+    if($head) {
             $res .= "<div class='intro'>$head</div>";
         }
 
-       $i = 1;
-       $res .= "<a id='top_lnk'></a>";
-       foreach ($this->_arts as $cid=>$arts) {
-           $res .= "<div class='lnk'><a href='#cat$cid'><strong>$i. {$this->_cats[$cid]}</strong></a>";
-           foreach ($arts as $art) {
-               $res .= "<a href='#art{$art->_aid}'>&nbsp;&nbsp;- ".htmlentities($art->title())."</a>";
-           }
-           $res .= '</div>';
-           $i ++;
-       }
+    $i = 1;
+    $res .= "<a id='top_lnk'></a>";
+    foreach ($this->_arts as $cid=>$arts) {
+        $res .= "<div class='lnk'><a href='$u#cat$cid'><strong>$i. {$this->_cats[$cid]}</strong></a>";
+        foreach ($arts as $art) {
+        $res .= "<a href='$u#art{$art->_aid}'>&nbsp;&nbsp;- ".htmlentities($art->title())."</a>";
+        }
+        $res .= '</div>';
+        $i ++;
+    }
 
-       foreach ($this->_arts as $cid=>$arts) {
-           $res .= "<h1><a id='cat$cid'></a><span>".$this->_cats[$cid].'</span></h1>';
-           foreach($arts as $art) {
-               $res .= $art->toHtml();
-               $res .= "<p><a href='#top_lnk'>Revenir au sommaire</a></p>";
-           }
-       }
+    foreach ($this->_arts as $cid=>$arts) {
+        $res .= "<h1 class='xorg_nl'><a id='cat$cid'></a>".$this->_cats[$cid].'</h1>';
+        foreach($arts as $art) {
+            $res .= $art->toHtml();
+            $res .= "<div class='top_lnk'><a href='$u#top_lnk'>Revenir au sommaire</a></div>";
+        }
+    }
 
-       $res .= $this->footer(true);
+    $res .= $this->footer(true);
 
-       if ($body) {
-           $res = <<<EOF
-<html>
+    if ($body) {
+        $res = <<<EOF
+<?xml version="1.0" encoding="iso-8859-15"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
+    <title></title>
     <style type="text/css">
     <!--
-      div.nl    { margin: auto; font-family: "Georgia","times new roman",serif; width: 60ex; text-align: justify; font-size: 10pt; }
-      div.title { margin: 2ex 0ex 2ex 0ex; padding: 1ex; width: 100%; font-size: 140%; text-align: center;
-                 font-weight: bold; border-bottom: 3px red solid; border-top: 3px red solid; }
-      
-      a[href]       { text-decoration: none; }
-      a[href]:hover { text-decoration: underline; }
-      
-      div.lnk   { margin: 2ex 0ex 2ex 0ex; padding: 0ex 2ex 0ex 2ex; }
-      div.lnk a { display: block; }
-      
-      h1 { margin: 6ex 0ex 4ex 0ex; padding: 2px 4ex 2px 0ex; width: 60ex; font-size: 100%;
-           border-bottom: 3px red solid; border-top: 3px red solid; }
-      h2 { width: 100%; margin: 0ex 1ex 0ex 1ex; padding: 2px 0px 2px 0px; font-weight: bold; font-style: italic; font-size: 95%; }
-      h1 span { font-size: 140%; padding: 2px 1ex 2px 1ex; border-bottom: 3px red solid; }
-      h2 span { padding: 2px 4px 2px 4px; border-bottom: 2px yellow solid; }
-      
-      div.art   { padding: 2ex; margin: 0ex 1ex 2ex 1ex; width: 58ex; border-top: 2px yellow solid; }
-      div.app   { padding: 2ex 3ex 0ex 3ex; width: 100%; margin: 0ex; text-align: left; font-size: 95%; }
-      div.intro { padding: 2ex; }
-      div.foot  { border-top: 1px #808080 dashed; font-size: 95%; padding: 1ex; color: #808080; background: inherit;
-                 text-align: center; width: 100% }
+      body      { background-color: #ddd; color: #000; }
+      div.nl    { margin: auto; width: 72ex;
+          font-family: "Georgia", "Times New Roman", serif; font-size: 11pt;
+          text-align: justify;
+          background-color: #fff; color: #000; }
+      a[href]       { text-decoration: none;
+                      background-color: #fff; color: #36c; }
+      a[href]:hover { background-color: #fff; color: #6c0; }
+
+      div.title { margin: 0ex 0ex 3ex 0ex; padding: 5ex 1ex 1ex 15ex;
+          font-size: 130%; font-weight: bold; text-align: right; 
+          background-color: #369; color: #fff;
+          background-image: url(http://dev.m4x.org/images/logo_xorg_nl.png);
+          background-repeat: no-repeat; background-position: 0.5ex 0.5ex; }
+      div.intro { margin: 4ex 3ex; }
+      div.lnk   { margin: 2ex 6ex;}
+      div.lnk a { display: block; font-size: 95%; }
+      div.top_lnk   { margin: 2ex; padding: 0ex; font-size: 85%; text-align: right; }
+      h1.xorg_nl    { margin: 3ex 0ex 2ex 0ex; padding: 1.5ex 2ex 0.5ex 1ex;
+          font-size: 120%; font-weight: bold; text-align: right; 
+          background-color: #369; color: #fff; }
+      h2.xorg_nl    { margin: 2ex 0ex 0ex 0ex; padding: 0.4ex 2ex;
+          font-size: 100%; font-weight: bold; font-style: italic;
+          background-color: #fff; color: #369;
+          border-width: thin 0; border-style: solid; border-color: #369; }
+      div.art   { margin: 2ex 3ex; }
+      div.app   { margin: 2ex 6ex 0ex 3ex; font-size: 95%; text-align: left; }
+      div.foot1 { margin: 8ex 0ex 0ex 0ex; padding: 0.5ex 2ex;
+          font-size: 90%; background-color: #fff; color: #999;
+          border-width: thin 0; border-style: solid; border-color: #ddd;
+          text-align: center; }
+      div.foot2 { padding: 1ex 0ex;
+          font-size: 90%; background-color: #fff; color: #999;
+          text-align: center; }
     -->
     </style>
   </head>
@@ -309,8 +339,8 @@ class NewsLetter
   </body>
 </html>
 EOF;
-       }
-       return $res;
+    }
+    return $res;
     }
 
     // }}}
@@ -319,23 +349,22 @@ EOF;
     function sendTo($prenom, $nom, $login, $sex, $html)
     {
         global $globals;
-       require_once('diogenes/diogenes.hermes.inc.php');
 
-       $mailer = new HermesMailer();
-       $mailer->setFrom($globals->newsletter->from);
-       $mailer->setSubject($this->title());
-       $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
+        $mailer = new PlMailer();
+        $mailer->setFrom($globals->newsletter->from);
+        $mailer->setSubject($this->title(true));
+        $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
         if (!empty($globals->newsletter->replyto)) {
             $mailer->addHeader('Reply-To',$globals->newsletter->replyto);
         }
         if (!empty($globals->newsletter->retpath)) {
             $mailer->addHeader('Return-Path',$globals->newsletter->retpath);
         }
-       $mailer->setTxtBody($this->toText($prenom,$nom,$sex));
-       if ($html) {
-           $mailer->setHTMLBody($this->toHtml($prenom,$nom,$sex,true));
-       }
-       $mailer->send();
+        $mailer->setTxtBody($this->toText($prenom,$nom,$sex));
+        if ($html) {
+            $mailer->setHTMLBody($this->toHtml($prenom,$nom,$sex,true));
+        }
+        $mailer->send();
     }
 
     // }}}
@@ -360,12 +389,12 @@ class NLArticle
     
     function NLArticle($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
     {
-       $this->_body   = $body;
-       $this->_title  = $title;
-       $this->_append = $append;
-       $this->_aid    = $aid;
-       $this->_cid    = $cid;
-       $this->_pos    = $pos;
+    $this->_body   = $body;
+    $this->_title  = $title;
+    $this->_append = $append;
+    $this->_aid    = $aid;
+    $this->_cid    = $cid;
+    $this->_pos    = $pos;
     }
 
     // }}}
@@ -391,10 +420,10 @@ class NLArticle
 
     function toText()
     {
-       $title = '*'.$this->title().'*';
-       $body  = enriched_to_text($this->_body,false,true);
-       $app   = enriched_to_text($this->_append,false,false,4);
-       return trim("$title\n\n$body\n\n$app")."\n";
+    $title = '*'.$this->title().'*';
+    $body  = enriched_to_text($this->_body,false,true);
+    $app   = enriched_to_text($this->_append,false,false,4);
+    return trim("$title\n\n$body\n\n$app")."\n";
     }
 
     // }}}
@@ -402,18 +431,18 @@ class NLArticle
 
     function toHtml()
     {
-       $title = "<h2><a id='art{$this->_aid}'></a><span>".htmlentities($this->title()).'</span></h2>';
-       $body  = enriched_to_text($this->_body,true);
-       $app   = enriched_to_text($this->_append,true);
-       
-       $art   = "$title\n";
-       $art  .= "<div class='art'>\n$body\n";
-       if ($app) {
+    $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".htmlentities($this->title()).'</h2>';
+    $body  = enriched_to_text($this->_body,true);
+    $app   = enriched_to_text($this->_append,true);
+    
+    $art   = "$title\n";
+    $art  .= "<div class='art'>\n$body\n";
+    if ($app) {
             $art .= "<div class='app'>$app</div>";
         }
-       $art  .= "</div>\n";
-       
-       return $art;
+    $art  .= "</div>\n";
+    
+    return $art;
     }
 
     // }}}
@@ -421,15 +450,15 @@ class NLArticle
 
     function check()
     {
-       $text = enriched_to_text($this->_body);
-       $arr  = explode("\n",wordwrap($text,68));
-       $c    = 0;
-       foreach ($arr as $line) {
+    $text = enriched_to_text($this->_body);
+    $arr  = explode("\n",wordwrap($text,68));
+    $c    = 0;
+    foreach ($arr as $line) {
             if (trim($line)) {
                 $c++;
             }
         }
-       return $c<9;
+    return $c<9;
     }
 
     // }}}
@@ -440,43 +469,37 @@ class NLArticle
 
 function insert_new_nl()
 {
-    global $globals;
-    $globals->xdb->execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued'");
+    XDB::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
 }
 
 function get_nl_slist()
 {
-    global $globals;
-    $res = $globals->xdb->query("SELECT id,date,titre FROM newsletter ORDER BY date DESC");
+    $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");
     return $res->fetchAllAssoc();
 }
 
 function get_nl_list()
 {
-    global $globals;
-    $res = $globals->xdb->query("SELECT id,date,titre FROM newsletter WHERE bits!='new' ORDER BY date DESC");
+    $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");
     return $res->fetchAllAssoc();
 }
 
 function get_nl_state()
 {
-    global $globals;
-    $res = $globals->xdb->query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', Session::getInt('uid'));
+    $res = XDB::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
     return $res->fetchOneCell();
 }
  
 function unsubscribe_nl()
 {
-    global $globals;
-    $globals->xdb->execute('DELETE FROM newsletter_ins WHERE user_id={?}', Session::getInt('uid'));
+    XDB::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
 }
  
 function subscribe_nl($uid=-1)
 {
-    global $globals;
-    $user = ($uid == -1) ? Session::getInt('uid') : $uid;
-    $globals->xdb->execute('REPLACE INTO  newsletter_ins (user_id,last)
-                                 VALUES  ({?}, 0)', $user);
+    $user = ($uid == -1) ? S::v('uid') : $uid;
+    XDB::execute('REPLACE INTO  newsletter_ins (user_id,last)
+                        VALUES  ({?}, 0)', $user);
 }
  
 function justify($text,$n)
@@ -485,40 +508,40 @@ function justify($text,$n)
     $arr = array_map('trim',$arr);
     $res = '';
     foreach ($arr as $key => $line) {
-       $nxl       = isset($arr[$key+1]) ? trim($arr[$key+1]) : '';
-       $nxl_split = preg_split('! +!',$nxl);
-       $nxw_len   = count($nxl_split) ? strlen($nxl_split[0]) : 0;
-       $line      = trim($line);
-
-       if (strlen($line)+1+$nxw_len < $n) {
-           $res .= "$line\n";
-           continue;
-       }
-       
-       if (preg_match('![.:;]$!',$line)) {
-           $res .= "$line\n";
-           continue;
-       }
+    $nxl       = isset($arr[$key+1]) ? trim($arr[$key+1]) : '';
+    $nxl_split = preg_split('! +!',$nxl);
+    $nxw_len   = count($nxl_split) ? strlen($nxl_split[0]) : 0;
+    $line      = trim($line);
+
+    if (strlen($line)+1+$nxw_len < $n) {
+        $res .= "$line\n";
+        continue;
+    }
+    
+    if (preg_match('![.:;]$!',$line)) {
+        $res .= "$line\n";
+        continue;
+    }
 
-       $tmp   = preg_split('! +!',trim($line));
-       $words = count($tmp);
-       if ($words <= 1) {
-           $res .= "$line\n";
-           continue;
-       }
+    $tmp   = preg_split('! +!',trim($line));
+    $words = count($tmp);
+    if ($words <= 1) {
+        $res .= "$line\n";
+        continue;
+    }
 
-       $len   = array_sum(array_map('strlen',$tmp));
-       $empty = $n - $len;
-       $sw    = floatval($empty) / floatval($words-1);
-       
-       $cur = 0;
-       $l   = '';
-       foreach ($tmp as $word) {
-           $l   .= $word;
-           $cur += $sw + strlen($word);
-           $l    = str_pad($l,intval($cur+0.5));
-       }
-       $res .= trim($l)."\n";
+    $len   = array_sum(array_map('strlen',$tmp));
+    $empty = $n - $len;
+    $sw    = floatval($empty) / floatval($words-1);
+    
+    $cur = 0;
+    $l   = '';
+    foreach ($tmp as $word) {
+        $l   .= $word;
+        $cur += $sw + strlen($word);
+        $l    = str_pad($l,intval($cur+0.5));
+    }
+    $res .= trim($l)."\n";
     }
     return trim($res);
 }
@@ -527,28 +550,28 @@ function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
 {
     $text = trim($input);
     if ($html) {
-       $text = htmlspecialchars($text);
-       $text = str_replace('[b]','<strong>', $text);
-       $text = str_replace('[/b]','</strong>', $text);
-       $text = str_replace('[i]','<em>', $text);
-       $text = str_replace('[/i]','</em>', $text);
-       $text = str_replace('[u]','<span style="text-decoration: underline">', $text);
-       $text = str_replace('[/u]','</span>', $text);
-       $text = preg_replace('!((https?|ftp)://[^\r\n\t ]*)!','<a href="\1">\1</a>', $text);
-       $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','<a href="mailto:\1">\2</a>', $text);
-       return nl2br($text);
+        $text = htmlspecialchars($text);
+        $text = str_replace('[b]','<strong>', $text);
+        $text = str_replace('[/b]','</strong>', $text);
+        $text = str_replace('[i]','<em>', $text);
+        $text = str_replace('[/i]','</em>', $text);
+        $text = str_replace('[u]','<span style="text-decoration: underline">', $text);
+        $text = str_replace('[/u]','</span>', $text);
+        require_once('url_catcher.inc.php');
+        $text = url_catcher($text);
+        return nl2br($text);
     } else {
-       $text = preg_replace('!\[\/?b\]!','*',$text);
-       $text = preg_replace('!\[\/?u\]!','_',$text);
-       $text = preg_replace('!\[\/?i\]!','/',$text);
-       $text = preg_replace('!((https?|ftp)://[^\r\n\t ]*)!','[\1]', $text);
-       $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text);
-       $text = $just ? justify($text,$width-$indent) : wordwrap($text,$width-$indent);
-       if($indent) {
-           $ind = str_pad('',$indent);
-           $text = $ind.str_replace("\n","\n$ind",$text);
-       }
-       return $text;
+        $text = preg_replace('!\[\/?b\]!','*',$text);
+        $text = preg_replace('!\[\/?u\]!','_',$text);
+        $text = preg_replace('!\[\/?i\]!','/',$text);
+        $text = preg_replace('!(((https?|ftp)://|www\.)[^\r\n\t ]*)!','[\1]', $text);
+        $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text);
+        $text = $just ? justify($text,$width-$indent) : wordwrap($text,$width-$indent);
+        if($indent) {
+            $ind = str_pad('',$indent);
+            $text = $ind.str_replace("\n","\n$ind",$text);
+        }
+        return $text;
     }
 }