--- /dev/null
+#!/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";
+}
+
+?>
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
***************************************************************************/
+define('PL_DO_AUTH', 300);
define('PL_FORBIDDEN', 403);
define('PL_NOT_FOUND', 404);
}
}
- 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)
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;
# 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:
$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);
$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 = {?}
{
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),
);
}
- 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';
$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());
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;
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;
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
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;
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)
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()