Simplifies blacklist verification (Closes #992).
authorStéphane Jacob <sj@m4x.org>
Tue, 3 Nov 2009 23:36:26 +0000 (00:36 +0100)
committerStéphane Jacob <sj@m4x.org>
Sat, 7 Nov 2009 13:05:23 +0000 (14:05 +0100)
ChangeLog
configs/platal.ini
include/newsletter.inc.php
modules/newsletter.php
templates/newsletter/edit.tpl

index 57e13d5..8c8636a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -29,6 +29,7 @@ Bug/Wish:
     * Newsletter:
         - #986: Improves the nl edition page                               -JAC
         - #991: Adds the text of the article in the mail when refused      -JAC
+        - #992: Simplifies blacklist verification                          -JAC
 
     * Profile:
         - #983: Fixes issue with the professional email publicity          -JAC
index c6980ec..946bbf0 100644 (file)
@@ -43,6 +43,9 @@ domain2 = ""
 alias_dom  = ""
 alias_dom2 = ""
 
+blacklist_check_url = ""
+blacklist_host_resolution_limit =
+
 [MailStorage]
 imap_active = 0
 googleapps_active = 0
index c58844e..b03b419 100644 (file)
@@ -26,8 +26,8 @@ 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)
     {
@@ -50,12 +50,12 @@ class NewsLetter extends MassMailer
         }
         $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()) {
@@ -92,19 +92,21 @@ 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;
         }
     }
 
@@ -281,6 +283,61 @@ 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(&$gethostbyname_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 ($gethostbyname_count < $globals->mail->blacklist_host_resolution_limit) {
+                   break;
+                }
+
+                if ($host != $globals->mail->alias_dom && $host != $globals->mail->alias_dom2
+                    && $host != $globals->mail->domain && $host != $globals->mail->domain2) {
+                    $article_ips = array_merge($article_ips, array(gethostbyname($host) => $host));
+                    ++$gethostbyname_count;
+                }
+            }
+        }
+
+        return $article_ips;
+    }
+
+    // }}}
 }
 
 // }}}
index 8d0c4c6..75ab2e9 100644 (file)
@@ -118,10 +118,10 @@ class NewsletterModule extends PLModule
     function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null, $action = 'edit') {
         $page->changeTpl('newsletter/edit.tpl');
         $page->addCssLink('nl.css');
-        $page->setTitle('Administration - Newsletter : Edition');
-        require_once("newsletter.inc.php");
+        $page->setTitle('Administration - Newsletter : Édition');
+        require_once 'newsletter.inc.php';
 
-        $nl  = new NewsLetter($nid);
+        $nl = new NewsLetter($nid);
 
         if($action == 'delete') {
             $nl->delArticle($aid);
@@ -129,38 +129,60 @@ class NewsletterModule extends PLModule
         }
 
         if($aid == 'update') {
-            $nl->_title     = Post::v('title');
-            $nl->_title_mail= Post::v('title_mail');
-            $nl->_date      = Post::v('date');
-            $nl->_head      = Post::v('head');
-            $nl->_shortname = strlen(Post::v('shortname')) ? Post::v('shortname') : null;
+            $nl->_title      = Post::v('title');
+            $nl->_title_mail = Post::v('title_mail');
+            $nl->_date       = Post::v('date');
+            $nl->_head       = Post::v('head');
+            $nl->_shortname  = strlen(Post::v('shortname')) ? Post::v('shortname') : null;
             if (preg_match('/^[-a-z0-9]*$/i', $nl->_shortname) && !is_numeric($nl->_shortname)) {
                 $nl->save();
             } else {
-                $page->trigError('Le nom de la NL n\'est pas valide');
+                $page->trigError("Le nom de la NL n'est pas valide.");
                 pl_redirect('admin/newsletter/edit/' . $nl->_id);
             }
         }
 
         if(Post::v('save')) {
             $art  = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
-                    $aid, Post::v('cid'), Post::v('pos'));
+                                  $aid, Post::v('cid'), Post::v('pos'));
             $nl->saveArticle($art);
             pl_redirect("admin/newsletter/edit/$nid");
         }
 
-        if($action == 'edit' && $aid != 'update') {
+        if ($action == 'edit' && $aid != 'update') {
             $eaid = $aid;
             if(Post::has('title')) {
                 $art  = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
-                        $eaid, Post::v('cid'), Post::v('pos'));
+                                      $eaid, Post::v('cid'), Post::v('pos'));
             } else {
-                  $art = ($eaid == 'new') ? new NLArticle() : $nl->getArt($eaid);
+                $art = ($eaid == 'new') ? new NLArticle() : $nl->getArt($eaid);
             }
             $page->assign('art', $art);
         }
 
-        $page->assign_by_ref('nl',$nl);
+        if ($aid == 'blacklist_check') {
+            $ips_to_check = array();
+            $gethostbyname_count = 0;
+
+            foreach ($nl->_arts as $key => $articles) {
+                foreach ($articles as $article) {
+                    $article_ips = $article->getLinkIps($gethostbyname_count);
+                    if (!empty($article_ips)) {
+                        $ips_to_check[$article->title()] = $article_ips;
+                    }
+                }
+            }
+
+            $page->assign('ips_to_check', $ips_to_check);
+            if ($gethostbyname_count >= $globals->mail->blacklist_host_resolution_limit) {
+                $page-trigError("Toutes les url et adresses emails de la lettre"
+                                . " n'ont pas été prises en compte car la"
+                                . " limite du nombre de résolutions DNS"
+                                . " autorisée a été atteinte.");
+            }
+        }
+
+        $page->assign_by_ref('nl', $nl);
     }
 
     function handler_admin_nl_cat(&$page, $action = 'list', $id = null) {
index 61fc55a..5fd721e 100644 (file)
   {/foreach}
 </table>
 
+<br />
+
+<form action="admin/newsletter/edit/{$nl->id(true)}/blacklist_check" method="post">
+  <table class="bicol" cellpadding="3" cellspacing="0">
+    <tr>
+      <th colspan="2">
+        Vérifier les url et adresses emails sur Spamhaus
+      </th>
+    </tr>
+    {if $ips_to_check|@count > 0}
+    {foreach from=$ips_to_check item=ip_list key=title}
+    {foreach from=$ip_list item=domain key=ip}
+    <tr>
+      <td>{$title}</td>
+      <td><a href="{#globals.mail.blacklist_check_url#}{$ip}">{$domain}</a></td>
+    </tr>
+    {assign var=title value=''}
+    {/foreach}
+    {/foreach}
+    {else}
+    <tr class="center">
+      <td colspan="2">
+        <input type="submit" value="Vérifier" />
+      </td>
+    </tr>
+    {/if}
+  </table>
+</form>
+
 {else}
 
 <p>