A few fixes on upgrade scripts.
[platal.git] / include / newsletter.inc.php
index eb0a6a5..d15d64e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -26,12 +26,12 @@ require_once("massmailer.inc.php");
 class NewsLetter extends MassMailer
 {
     public $_date;
-    public $_cats = Array();
-    public $_arts = Array();
+    public $_cats = array();
+    public $_arts = array();
 
     function __construct($id = null)
     {
-        parent::__construct('newsletter/nl.tpl', 'nl.css', 'nl/show');
+        parent::__construct('newsletter/nl.mail.tpl', 'nl.css', 'nl/show', 'newsletter', 'newsletter_ins');
         if (isset($id)) {
             if ($id == 'last') {
                 $res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
@@ -41,24 +41,27 @@ class NewsLetter extends MassMailer
         } else {
             $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
             if (!$res->numRows()) {
-                Newsletter::create();
+                NewsLetter::create();
             }
-            $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
+            $res = XDB::query("SELECT * FROM newsletter WHERE bits='new' ORDER BY id DESC LIMIT 1");
+        }
+        if ($res->numRows() != 1) {
+            throw new MailNotFound();
         }
         $nl = $res->fetchOneAssoc();
 
-        $this->_id        = $nl['id'];
-        $this->_shortname = $nl['short_name'];
-        $this->_date      = $nl['date'];
-        $this->_title     = $nl['titre'];
+        $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'];
+        $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
@@ -76,7 +79,7 @@ class NewsLetter extends MassMailer
         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);
     }
-  
+
     public function getArt($aid)
     {
         foreach ($this->_arts as $key=>$artlist) {
@@ -89,22 +92,24 @@ class NewsLetter extends MassMailer
 
     public function saveArticle(&$a)
     {
-        if ($a->_aid>=0) {
-            XDB::execute('REPLACE INTO  newsletter_art (id,aid,cid,pos,title,body,append)
-                                VALUES  ({?},{?},{?},{?},{?},{?},{?})',
+        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;
+                          $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').',{?},{?},{?}
+                               SELECT  {?}, MAX(aid)+1, {?}, '
+                                       . ($a->_pos ? intval($a->_pos) : 'MAX(pos)+1')
+                                       . ', {?}, {?}, {?}
                                  FROM  newsletter_art AS a
-                                WHERE  a.id={?}',
+                                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;
         }
     }
-   
+
     public function delArticle($aid)
     {
         XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid);
@@ -121,34 +126,29 @@ class NewsLetter extends MassMailer
     protected function setSent()
     {
         XDB::execute("UPDATE newsletter  SET bits='sent' WHERE id={?}", $this->_id);
-    }   
+    }
 
     static public function subscriptionState($uid = null)
     {
         $user = is_null($uid) ? S::v('uid') : $uid;
         $res = XDB::query("SELECT  1
                              FROM  newsletter_ins
-                            WHERE  user_id={?}", $user);
+                            WHERE  uid={?}", $user);
         return $res->fetchOneCell();
-    }   
-    
+    }
+
     static public function unsubscribe($uid = null)
     {
         $user = is_null($uid) ? S::v('uid') : $uid;
         XDB::execute("DELETE FROM  newsletter_ins
-                            WHERE  user_id={?}", $user);
+                            WHERE  uid={?}", $user);
     }
 
     static public function subscribe($uid = null)
     {
         $user = is_null($uid) ? S::v('uid') : $uid;
-        XDB::execute("REPLACE INTO  newsletter_ins (user_id,last)
-                            VALUES  ({?}, 0)", $user);
-    }
-
-    protected function subscriptionTable()
-    {
-        return 'newsletter_ins';
+        XDB::execute("REPLACE INTO  newsletter_ins (uid,last)
+                            VALUES  ({?}, NULL)", $user);
     }
 
     protected function subscriptionWhere()
@@ -186,7 +186,7 @@ class NewsLetter extends MassMailer
 class NLArticle
 {
     // {{{ properties
-    
+
     var $_aid;
     var $_cid;
     var $_pos;
@@ -196,7 +196,7 @@ class NLArticle
 
     // }}}
     // {{{ constructor
-    
+
     function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
     {
         $this->_body   = $body;
@@ -215,43 +215,54 @@ class NLArticle
 
     // }}}
     // {{{ function body()
-    
+
     public function body()
     { return trim($this->_body); }
-    
+
     // }}}
     // {{{ function append()
-    
+
     public function append()
     { return trim($this->_append); }
 
     // }}}
     // {{{ function toText()
 
-    public function toText()
+    public function toText($hash = null, $login = null)
     {
         $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";
+        $body  = MiniWiki::WikiToText($this->_body, true);
+        $app   = MiniWiki::WikiToText($this->_append,false,4);
+        $text = trim("$title\n\n$body\n\n$app")."\n";
+        if (!is_null($hash) && !is_null($login)) {
+            $text = str_replace('%HASH%', "$hash/$login", $text);
+        } else {
+            $text = str_replace('%HASH%', '', $text);
+        }
+        return $text;
     }
 
     // }}}
     // {{{ function toHtml()
 
-    public function toHtml()
+    public function toHtml($hash = null, $login = null)
     {
-        $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);
-    
+        $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".pl_entities($this->title()).'</h2>';
+        $body  = MiniWiki::WikiToHTML($this->_body);
+        $app   = MiniWiki::WikiToHTML($this->_append);
+
         $art   = "$title\n";
         $art  .= "<div class='art'>\n$body\n";
         if ($app) {
             $art .= "<div class='app'>$app</div>";
         }
         $art  .= "</div>\n";
-    
+        if (!is_null($hash) && !is_null($login)) {
+            $art = str_replace('%HASH%', "$hash/$login", $art);
+        } else {
+            $art = str_replace('%HASH%', '', $art);
+        }
+
         return $art;
     }
 
@@ -260,7 +271,7 @@ class NLArticle
 
     public function check()
     {
-        $text = enriched_to_text($this->_body);
+        $text = MiniWiki::WikiToText($this->_body);
         $arr  = explode("\n",wordwrap($text,68));
         $c    = 0;
         foreach ($arr as $line) {
@@ -272,9 +283,63 @@ class NLArticle
     }
 
     // }}}
+    // {{{ function parseUrlsFromArticle()
+
+    private function parseUrlsFromArticle()
+    {
+        $email_regex = '([a-z0-9.\-+_\$]+@([\-.+_]?[a-z0-9])+)';
+        $url_regex = '((https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+)';
+        $regex = '{' . $email_regex . '|' . $url_regex . '}i';
+
+        $matches = array();
+        $body_matches = array();
+        if (preg_match_all($regex, $this->body(), $body_matches)) {
+            $matches = array_merge($matches, $body_matches[0]);
+        }
+
+        $append_matches = array();
+        if (preg_match_all($regex, $this->append(), $append_matches)) {
+            $matches = array_merge($matches, $append_matches[0]);
+        }
+
+        return $matches;
+    }
+
+    // }}}
+    // {{{ function getLinkIps()
+
+    public function getLinkIps(&$blacklist_host_resolution_count)
+    {
+        $matches = $this->parseUrlsFromArticle();
+        $article_ips = array();
+
+        if (!empty($matches)) {
+            global $globals;
+
+            foreach ($matches as $match) {
+                $host = parse_url($match, PHP_URL_HOST);
+                if ($host == '') {
+                    list(, $host) = explode('@', $match);
+                }
+
+                if ($blacklist_host_resolution_count >= $globals->mail->blacklist_host_resolution_limit) {
+                   break;
+                }
+
+                if (!preg_match('/^(' . str_replace(' ', '|', $globals->mail->domain_whitelist) . ')$/i', $host)) {
+                    $article_ips = array_merge($article_ips, array(gethostbyname($host) => $host));
+                    ++$blacklist_host_resolution_count;
+                }
+            }
+        }
+
+        return $article_ips;
+    }
+
+    // }}}
 }
 
 // }}}
 
-// vim:set et sw=4 sts=4 sws=4:
+// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
 ?>