Close #407: RSS feed for Forums and MLs
[platal.git] / include / massmailer.inc.php
index e326a31..fd5093d 100644 (file)
@@ -59,15 +59,10 @@ abstract class MassMailer
             return $this->_head; 
         } else {
             $head = $this->_head;
-            $head = str_replace('<cher>',   $sexe ? 'Chère' : 'Cher', $head);
+            $head = str_replace('<cher>',   $sexe ? 'Chère' : 'Cher', $head);
             $head = str_replace('<prenom>', $prenom, $head);
             $head = str_replace('<nom>',    $nom,    $head);
-            if ($type == 'text') {
-                $head = enriched_to_text($head, false, true, 2, 64);
-            } else {
-                $head = enriched_to_text($head, true);
-            }
-            return $head;
+            return format_text($head, $type, 2, 64);
         }
     }
 
@@ -78,7 +73,7 @@ abstract class MassMailer
             return true;
         } else {
             $css = file_get_contents(dirname(__FILE__) . '/../htdocs/css/' . $this->_css);
-            return preg_replace('@/\*.*?\*/@s', '', $css);
+            return preg_replace('@/\*.*?\*/@us', '', $css);
         }
     }
 
@@ -105,9 +100,12 @@ abstract class MassMailer
         $this->assignData($page);
     }
 
-    public function sendTo($prenom, $nom, $login, $sexe, $html)
+    public function sendTo($prenom, $nom, $login, $sexe, $html, $hash = 0)
     {
         global $globals;
+        if (strpos($login, '@') === false) {
+            $login = "$login@{$globals->mail->domain}";
+        }
 
         $mailer = new PlMailer($this->_tpl);
         $this->assignData($mailer);
@@ -116,38 +114,45 @@ abstract class MassMailer
         $mailer->assign('nom',     $nom);
         $mailer->assign('sexe',    $sexe);
         $mailer->assign('prefix',  null);
-        $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
+        $mailer->assign('hash',    $hash);
+        $mailer->addTo("\"$prenom $nom\" <$login>");
         $mailer->send($html);
     }
 
+    protected function getAllRecipients()
+    {
+        global $globals;
+        return  "SELECT  u.user_id, CONCAT(a.alias, '@{$globals->mail->domain}'),
+                         u.prenom, IF(u.nom_usage='', u.nom, u.nom_usage),
+                         FIND_IN_SET('femme', u.flags),
+                         q.core_mail_fmt AS pref, 0 AS hash
+                   FROM  {$this->subscriptionTable()}  AS ni
+             INNER JOIN  auth_user_md5   AS u  USING(user_id)
+             INNER JOIN  auth_user_quick AS q  ON(q.user_id = u.user_id)
+             INNER JOIN  aliases         AS a  ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
+              LEFT JOIN  emails          AS e  ON(e.uid=u.user_id AND e.flags='active')
+                  WHERE  ni.last < {?} AND ({$this->subscriptionWhere()}) AND e.email IS NOT NULL
+               GROUP BY  u.user_id";
+    }
+
     public function sendToAll()
     {
         $this->setSent();
-        $query = "SELECT  u.user_id, a.alias,
-                          u.prenom, IF(u.nom_usage='', u.nom, u.nom_usage),
-                          FIND_IN_SET('femme', u.flags),
-                          q.core_mail_fmt AS pref
-                    FROM  {$this->subscriptionTable()}  AS ni
-              INNER JOIN  auth_user_md5   AS u  USING(user_id)
-              INNER JOIN  auth_user_quick AS q  ON(q.user_id = u.user_id)
-              INNER JOIN  aliases         AS a  ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
-               LEFT JOIN  emails          AS e  ON(e.uid=u.user_id AND e.flags='active')
-                   WHERE  ni.last < {?} AND ({$this->subscriptionWhere()}) AND e.email IS NOT NULL
-                GROUP BY  u.user_id
-                   LIMIT  60";
+        $query = $this->getAllRecipients() . " LIMIT {?}";
         while (true) {
-            $res = XDB::iterRow($query, $this->_id);
+            $res = XDB::iterRow($query, $this->_id, 60);
             if (!$res->total()) {
-                exit;
+                return;
             }
             $sent = array();
-            while (list($uid, $bestalias, $prenom, $nom, $sexe, $fmt) = $res->next()) {
-                $sent[] = "user_id='$uid'";
-                $this->sendTo($prenom, $nom, $bestalias, $sexe, $fmt=='html');
+            while (list($uid, $bestalias, $prenom, $nom, $sexe, $fmt, $hash) = $res->next()) {
+                $sent[] = "(user_id='$uid'" . (!$uid ? " AND email='$bestalias')": ')');
+                $this->sendTo($prenom, $nom, $bestalias, $sexe, $fmt=='html', $hash);
             }
             XDB::execute("UPDATE  {$this->subscriptionTable()}
                              SET  last = {?}
                            WHERE " . implode(' OR ', $sent), $this->_id);
+            
             sleep(60);
         }
     }
@@ -206,6 +211,14 @@ function justify($text,$n)
     return trim($res);
 }
 
+function format_text($input, $format, $indent = 0, $width = 68)
+{
+    if ($format == 'text') {
+        return enriched_to_text($input, false, true, $indent, $width);
+    }
+    return enriched_to_text($input, true);
+}
+
 function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
 {
     $text = trim($input);
@@ -217,6 +230,11 @@ function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
         $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("!(\\s*\n)*\[title\]!",'<h1>',$text);
+        $text = preg_replace("!\[\/title\](\\s*\n)*!", '</h1>',$text);
+        $text = preg_replace("!(\\s*\n)*\[subtitle\]!",'<h2>',$text);
+        $text = preg_replace("!\[\/subtitle\](\\s*\n)*!",'</h2>',$text);
+
         require_once('url_catcher.inc.php');
         $text = url_catcher($text);
         return nl2br($text);
@@ -224,6 +242,8 @@ function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
         $text = preg_replace('!\[\/?b\]!','*',$text);
         $text = preg_replace('!\[\/?u\]!','_',$text);
         $text = preg_replace('!\[\/?i\]!','/',$text);
+        $text = preg_replace('!\[\/?title\]!','***', $text);
+        $text = preg_replace('!\[\/?subtitle\]!','**', $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);
@@ -237,5 +257,5 @@ function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
 
 // }}}
 
-// vim:set et sw=4 sts=4 sws=4:
+// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
 ?>