<?php
/***************************************************************************
- * Copyright (C) 2003-2007 Polytechnique.org *
+ * Copyright (C) 2003-2008 Polytechnique.org *
* http://opensource.polytechnique.org/ *
* *
* This program is free software; you can redistribute it and/or modify *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
***************************************************************************/
-require_once("xorg.misc.inc.php");
-
// {{{ class MassMailer
abstract class MassMailer
$res = XDB::query("SELECT IF (n.short_name IS NULL, n.id, n.short_name)
FROM {$this->_table} AS n
WHERE n.bits != 'new' AND {$where}
- LIMIT 1");
+ LIMIT 1");
if ($res->numRows() != 1) {
return null;
}
public function head($prenom = null, $nom = null, $sexe = null, $type = 'text')
{
if (is_null($prenom)) {
- return $this->_head;
+ return $this->_head;
} else {
$head = $this->_head;
$head = str_replace('<cher>', $sexe ? 'Chère' : 'Cher', $head);
$this->assignData($page);
}
- public function sendTo($prenom, $nom, $login, $sexe, $html, $hash = 0)
+ private function createHash($line, $key = null)
{
- global $globals;
- if (strpos($login, '@') === false) {
- $login = "$login@{$globals->mail->domain}";
+ $hash = implode(time(), $line) . rand();
+ $hash = md5($hash);
+ return $hash;
+ }
+
+ public function sendTo($hruid, $email, $prenom, $nom, $sexe, $html, $hash = 0)
+ {
+ // If $email is not a real email address, tries to compute it up from
+ // the hruid. Otherwise, we suppose that caller will have used a valid
+ // and canonical email address.
+ if (strpos($email, '@') === false) {
+ if (!($user = User::getSilent($email))) {
+ Platal::page()->trigError("'$email' is neither a valid email address nor a valid login; did not send the email.");
+ }
+ $email = $user->bestEmail();
+ }
+
+ if ($hruid && (is_null($hash) || $hash == 0)) {
+ $hash = $this->createHash(array($email, $prenom, $nom, $sexe, $html, rand(), "X.org rulez"));
+ XDB::query("UPDATE {$this->_subscriptionTable} as ni
+ INNER JOIN auth_user_md5 AS u USING (user_id)
+ SET ni.hash = {?}
+ WHERE ni.user_id != 0 AND u.hruid = {?}",
+ $hash, $hruid);
}
$mailer = new PlMailer($this->_tpl);
$mailer->assign('sexe', $sexe);
$mailer->assign('prefix', null);
$mailer->assign('hash', $hash);
- $mailer->addTo("\"$prenom $nom\" <$login>");
+ $mailer->assign('email', $email);
+ $mailer->assign('alias', $hruid);
+ $mailer->addTo("\"$prenom $nom\" <$email>");
$mailer->send($html);
}
protected function getAllRecipients()
{
global $globals;
- return "SELECT u.user_id, CONCAT(a.alias, '@{$globals->mail->domain}'),
+ return "SELECT u.user_id, u.hruid, 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
+ q.core_mail_fmt AS pref, ni.hash 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
+ WHERE ni.last < {?} AND ({$this->subscriptionWhere()}) AND
+ (e.email IS NOT NULL OR FIND_IN_SET('googleapps', u.mail_storage))
GROUP BY u.user_id";
}
return;
}
$sent = array();
- 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);
+ while (list($uid, $hruid, $email, $prenom, $nom, $sexe, $fmt, $hash) = $res->next()) {
+ $sent[] = "(user_id='$uid'" . (!$uid ? " AND email='$email')": ')');
+ $this->sendTo($hruid, $email, $prenom, $nom, $sexe, $fmt=='html', $hash);
}
XDB::execute("UPDATE {$this->_subscriptionTable}
SET last = {?}
WHERE " . implode(' OR ', $sent), $this->_id);
-
+
sleep(60);
}
}