AXSpammer cron
authorx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Fri, 19 Jan 2007 21:31:47 +0000 (21:31 +0000)
committerx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Fri, 19 Jan 2007 21:31:47 +0000 (21:31 +0000)
Everything works well...
Just remain the db administration tools

git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1363 839d8a87-29fc-0310-9880-83ba4fa771e5

bin/cron/axletter.send.php [new file with mode: 0755]
classes/platal.php
classes/xdb.php
configs/platal.cron.in
include/massmailer.inc.php
modules/axletter.php
modules/axletter/axletter.inc.php

diff --git a/bin/cron/axletter.send.php b/bin/cron/axletter.send.php
new file mode 100755 (executable)
index 0000000..6773741
--- /dev/null
@@ -0,0 +1,34 @@
+#!/usr/bin/php5 -q
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+require_once('./connect.db.inc.php');
+require_once("../../modules/axletter/axletter.inc.php");
+
+$al = AXLetter::toSend();
+if ($al) {
+    echo "Envoi de la lettre \"{$al->title()}\"\n\n";
+    echo " " . date("H:i:s") . " -> début de l'envoi\n";
+    $al->sendToAll();
+    echo " " . date("H:i:s") . " -> fin de l'envoi\n";
+}
+
+?>
index 39f5f9d..d1e714f 100644 (file)
@@ -19,6 +19,7 @@
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
+define('PL_DO_AUTH',   300);
 define('PL_FORBIDDEN', 403);
 define('PL_NOT_FOUND', 404);
 
@@ -197,7 +198,15 @@ class Platal
             }
         }
 
-        return call_user_func_array($hook['hook'], $args);
+        $val = call_user_func_array($hook['hook'], $args);
+        if ($val == PL_DO_AUTH) {
+            // The handler need a better auth with the current args
+            if (!call_user_func(array($globals->session, 'doAuth'))) {
+                $this->force_login($page);
+            }
+            $val = call_user_func_array($hook['hook'], $args);
+        }
+        return $val;
     }
 
     function force_login(&$page)
index f85c74e..e207905 100644 (file)
@@ -242,7 +242,7 @@ class XOrgDBIterator
 
     function __construct($query, $mode = MYSQL_ASSOC)
     {
-        $this->_result =& new XOrgDBResult($query);
+        $this->_result = new XOrgDBResult($query);
         $this->_pos    = 0;
         $this->_total  = $this->_result->numRows();
         $this->_mode   = $mode;
index 75b969a..6b91e36 100644 (file)
@@ -18,6 +18,9 @@ WD=/home/web/prod/platal/bin/cron
 # validations
 0 */3 * * *    web     cd $WD; ./cron_validations.php
 
+# AX spammer
+15 * * * *      web     cd $WD; ./axletter.send.php | mail -e -s "envoi d'un mail de l'AX" br@m4x.org
+
 # homonymes
 0 0 4 * * *    web     cd $WD; ./homonymes.php
 # vim:set noet syntax=crontab ts=8 sw=8 sts=8:
index d9ad35e..02a93a8 100644 (file)
@@ -100,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);
@@ -111,34 +114,39 @@ 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()
+    {
+        return  "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, 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;
             }
             $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 = {?}
index 4926ddb..9ad8292 100644 (file)
@@ -25,6 +25,7 @@ class AXLetterModule extends PLModule
     {
         return array(
             'ax'             => $this->make_hook('index',        AUTH_COOKIE),
+            'ax/out'         => $this->make_hook('out',    AUTH_PUBLIC),
             'ax/show'        => $this->make_hook('show',   AUTH_COOKIE),
             'ax/edit'        => $this->make_hook('submit', AUTH_MDP),
             'ax/edit/cancel' => $this->make_hook('cancel', AUTH_MDP),
@@ -33,7 +34,21 @@ class AXLetterModule extends PLModule
         );
     }
 
-    function handler_index(&$page, $action = null, $hash = null)
+    function handler_out(&$page, $hash)
+    {
+        if (!$hash) {
+            if (!S::logged()) {
+                return PL_DO_AUTH;
+            } else {
+                return $this->handler_index($page, 'out');
+            }
+        }
+        require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
+        $page->changeTpl('axletter/unsubscribe.tpl');
+        $page->assign('success', AXLetter::unsubscribe($hash, true));
+    }
+
+    function handler_index(&$page, $action = null)
     {
         require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
 
@@ -41,18 +56,13 @@ class AXLetterModule extends PLModule
         $page->assign('xorg_title','Polytechnique.org - Envois de l\'AX');
 
         switch ($action) {
-          case 'out': AXLetter::unsubscribe($hash); break;
           case 'in':  AXLetter::subscribe(); break;
-          default: ;
+          case 'out': AXLetter::unsubscribe(); break;
         }
 
         $perm = AXLetter::hasPerms();
         if ($perm) {
-            $waiting = AXLetter::awaiting();
-            if ($waiting) {
-                $new = new AXLetter($waiting);
-                $page->assign('new', $new);
-            }
+            $page->assign('new', AXLetter::awaiting());
         }
         $page->assign('axs', AXLetter::subscriptionState());
         $page->assign('ax_list', AXLetter::listSent());
@@ -211,12 +221,11 @@ class AXLetterModule extends PLModule
             return PL_FORBIDDEN;
         }
 
-        $waiting = AXLetter::awaiting();
-        if (!$waiting) {
+        $al = AXLetter::awaiting();
+        if (!$alg) {
             $page->kill("Aucune lettre en attente");
             return;
         }
-        $al = new AXLetter($waiting);
         if (!$al->invalid()) {
             $page->kill("Une erreur est survenue lors de l'annulation de l'envoi");
             return;
@@ -237,12 +246,11 @@ class AXLetterModule extends PLModule
             return PL_FORBIDDEN;
         }
 
-        $waiting = AXLetter::awaiting();
-        if (!$waiting) {
+        $al = AXLetter::awaiting();
+        if (!$al) {
             $page->kill("Aucune lettre en attente");
             return;
         }
-        $al = new AXLetter($waiting);
         if (!$al->valid()) {
             $page->kill("Une erreur est survenue lors de la validation de l'envoi");
             return;
index 178a0d3..9aa8cb6 100644 (file)
@@ -73,38 +73,6 @@ class AXLetter extends MassMailer
         return format_text($this->_signature, $format, 10);
     }
 
-    static public function create($subject, $title, $body, $signature, $promo_min, $promo_max, $date, $shortname = null)
-    {
-        $id = AXLetter::awaiting();
-        if ($id) {
-            return new AXLetter($id);
-        }
-        XDB::execute("INSERT INTO  axletter (shortname, echeance,  promo_min, promo_max,
-                                     subject, title, body, signature,
-                                     subject_ini, title_ini, body_ini, signature_ini)
-                           VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
-                     $shortname, $date, $promo_min, $promo_max,
-                     $subject, $title, $body, $signature, $subject, $title, $body, $signature);
-        return new AXLetter(XDB::insertId());
-    }
-
-    public function update($subject, $title, $body, $signature, $promo_min, $promo_max, $date, $shortname = null)
-    {
-        $this->_shortname  = $shortname;
-        $this->_title      = $title;
-        $this->_title_mail = $subject;
-        $this->_body       = $body;
-        $this->_signature  = $signature;
-        $this->_promo_min  = $promo_min;
-        $this->_promo_max  = $promo_max;
-        $this->_date       = $date;
-        return XDB::execute("UPDATE  axletter (shortname, subject, title, body, signature, promo_min, promo_max, echeance)
-                                SET  shorname={?}, subject={?}, title={?}, body={?}, signature={?},
-                                     promo_min={?}, promo_max={?}, echeance={?}
-                              WHERE  id = {?}",
-                            $shortname, $subject, $title, $body, $signature, $promo_min, $promo_max, $date, $this->_id);
-    }
-
     public function valid()
     {
         return XDB::execute("UPDATE  axletter
@@ -126,6 +94,23 @@ class AXLetter extends MassMailer
                        WHERE  id={?}", $this->_id);
     }
 
+    protected function getAllRecipients()
+    {
+        return "SELECT  ni.user_id, IF(ni.user_id = 0, ni.email, a.alias) AS alias,
+                        IF(ni.user_id = 0, ni.prenom, u.prenom) AS prenom,
+                        IF(ni.user_id = 0, ni.nom, IF(u.nom_usage='', u.nom, u.nom_usage)) AS nom,
+                        FIND_IN_SET('femme', IF(ni.user_id = 0, ni.flag, u.flags)) AS sexe,
+                        IF(ni.user_id = 0, 'html', q.core_mail_fmt) AS pref,
+                        IF(ni.user_id = 0, ni.hash, 0) AS hash
+                  FROM  axletter_ins  AS ni
+             LEFT JOIN  auth_user_md5   AS u  USING(user_id)
+             LEFT JOIN  auth_user_quick AS q  ON(q.user_id = u.user_id)
+             LEFT 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 (e.email IS NOT NULL OR ni.user_id = 0)
+              GROUP BY  u.user_id";
+    }              
+
     static public function subscriptionState($uid = null)
     {
         $user = is_null($uid) ? S::v('uid') : $uid;
@@ -135,11 +120,22 @@ class AXLetter extends MassMailer
         return $res->fetchOneCell();
     }   
     
-    static public function unsubscribe($uid = null)
+    static public function unsubscribe($uid = null, $hash = false)
     {
         $user = is_null($uid) ? S::v('uid') : $uid;
+        $field = !$hash ? 'user_id' : 'hash';
+        if (is_null($uid) && $hash) {
+            return false;
+        }
+        $res = XDB::query("SELECT *
+                             FROM axletter_ins
+                            WHERE $field={?}", $user);
+        if (!$res->numRows()) {
+            return false;
+        }
         XDB::execute("DELETE FROM  axletter_ins
-                            WHERE  user_id={?} OR hash = {?}", $user, $user);
+                            WHERE  $field = {?}", $user);
+        return true;
     }
 
     static public function subscribe($uid = null)
@@ -172,10 +168,24 @@ class AXLetter extends MassMailer
 
     static public function awaiting()
     {
-        $res = XDB::query("SELECT  id
+        $res = XDB::query("SELECT  *
                              FROM  axletter
                             WHERE  FIND_IN_SET('new', bits)");
-        return $res->fetchOneCell();
+        if ($res->numRows()) {
+            return new AXLetter($res->fetchOneRow());
+        }
+        return null;
+    }
+
+    static public function toSend()
+    {
+        $res = XDB::query("SELECT  *
+                             FROM  axletter
+                            WHERE  FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0");
+        if ($res->numRows()) {
+            return new AXLetter($res->fetchOneRow());
+        }
+        return null;
     }
 
     static public function listSent()