From d5e609051d83309a9a291432cbac450999661373 Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Mon, 29 Dec 2008 19:05:03 +0100 Subject: [PATCH] Uses User objects in massmailer. WARNING: This deprecates the ability of massmailer to send messages to user with no X.org account. Signed-off-by: Florent Bruneau --- classes/profile.php | 17 +++++-- include/massmailer.inc.php | 91 ++++++++++++++++---------------------- include/vcard.inc.php | 8 ++-- modules/axletter.php | 9 ++-- modules/newsletter.php | 11 ++--- templates/axletter/letter.mail.tpl | 4 +- templates/newsletter/nl.mail.tpl | 8 ++-- 7 files changed, 69 insertions(+), 79 deletions(-) diff --git a/classes/profile.php b/classes/profile.php index 217a3f0..9b30115 100644 --- a/classes/profile.php +++ b/classes/profile.php @@ -23,30 +23,34 @@ class Profile { private $pid; private $hrpid; + private $promo; private function __construct($login) { if ($login instanceof PlUser) { - $res = XDB::query('SELECT p.pid, p.hrpid + $res = XDB::query('SELECT p.pid, p.hrpid, pd.promo_display FROM account_profiles AS ap INNER JOIN profiles AS p ON (p.pid = ap.pid) + INNER JOIN profile_display AS pd ON (pd.uid = p.pid) WHERE ap.uid = {?} AND FIND_IN_SET(\'owner\', ap.perms)', $login->id()); } else if (is_numeric($login)) { - $res = XDB::query('SELECT p.pid, p.hrpid + $res = XDB::query('SELECT p.pid, p.hrpid, pd.promo_display FROM profiles AS p + INNER JOIN profile_display AS pd ON (pd.uid = p.pid) WHERE p.pid = {?}', $login); } else { - $res = XDB::query('SELECT p.pid, p.hrpid + $res = XDB::query('SELECT p.pid, p.hrpid, pd.promo_display FROM profiles AS p + INNER JOIN profile_display AS pd ON (pd.uid = p.pid) WHERE p.hrpid = {?}', $login); } if ($res->numRows() != 1) { throw new UserNotFoundException(); } - list($this->pid, $this->hrpid) = $res->fetchOneRow(); + list($this->pid, $this->hrpid, $this->promo) = $res->fetchOneRow(); } public function id() @@ -59,6 +63,11 @@ class Profile return $this->hrpid; } + public function promo() + { + return $this->promo; + } + public function owner() { return User::getSilent($this); diff --git a/include/massmailer.inc.php b/include/massmailer.inc.php index 261fb8b..db1441e 100644 --- a/include/massmailer.inc.php +++ b/include/massmailer.inc.php @@ -103,15 +103,15 @@ abstract class MassMailer return $mail ? $this->_title_mail : $this->_title; } - public function head($prenom = null, $nom = null, $sexe = null, $type = 'text') + public function head($user = null, $type = 'text') { - if (is_null($prenom)) { + if (is_null($user)) { return $this->_head; } else { $head = $this->_head; - $head = str_replace('', $sexe ? 'Chère' : 'Cher', $head); - $head = str_replace('', $prenom, $head); - $head = str_replace('', $nom, $head); + $head = str_replace('', $user->isFemale() ? 'Chère' : 'Cher', $head); + $head = str_replace('', $user->displayName(), $head); + $head = str_replace('', '', $head); return format_text($head, $type, 2, 64); } } @@ -127,26 +127,22 @@ abstract class MassMailer } } - public function toText(&$page, $prenom, $nom, $sexe) + public function toText(&$page, $user) { $this->css($page); $page->assign('is_mail', false); $page->assign('mail_part', 'text'); - $page->assign('prenom', $prenom); - $page->assign('nom', $nom); - $page->assign('sexe', $sexe); + $page->assign('user', $user); $this->assignData($page); } - public function toHtml(&$page, $prenom, $nom, $sexe) + public function toHtml(&$page, $user) { $this->css($page); $page->assign('prefix', $this->_prefix . '/' . $this->id()); $page->assign('is_mail', false); $page->assign('mail_part', 'html'); - $page->assign('prenom', $prenom); - $page->assign('nom', $nom); - $page->assign('sexe', $sexe); + $page->assign('user', $user); $this->assignData($page); } @@ -157,71 +153,60 @@ abstract class MassMailer return $hash; } - public function sendTo($hruid, $email, $prenom, $nom, $sexe, $html, $hash = 0) + public function sendTo($user, $hash = null) { - // 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 (is_null($hash)) { + $hash = XDB::fetchOneCell("SELECT hash + FROM {$this->_subscriptionTable} + WHERE user_id = {?}", $user->id()); } - - 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); + if (is_null($hash)) { + $hash = $this->createHash(array($user->displayName(), $user->fullName(), + $user->isFemale(), $user->isEmailFormatHtml(), + rand(), "X.org rulez")); + XDB::execute("UPDATE {$this->_subscriptionTable} as ni + SET ni.hash = {?} + WHERE ni.user_id != {?}", + $hash, $user->id()); } $mailer = new PlMailer($this->_tpl); $this->assignData($mailer); $mailer->assign('is_mail', true); - $mailer->assign('prenom', $prenom); - $mailer->assign('nom', $nom); - $mailer->assign('sexe', $sexe); + $mailer->assign('user', $user); $mailer->assign('prefix', null); $mailer->assign('hash', $hash); - $mailer->assign('email', $email); - $mailer->assign('alias', $hruid); - $mailer->addTo("\"$prenom $nom\" <$email>"); - $mailer->send($html); + $mailer->addTo('"' . $user->fullName() . '" <' . $user->bestEmail() . '>'); + $mailer->send($user->isEmailFormatHtml()); } protected function getAllRecipients() { global $globals; - 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, ni.hash AS hash + return "SELECT a.uid, a.hruid, a.display_name, a.full_name, a.email_format, + 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') + INNER JOIN accounts AS a ON (ni.user_id = a.uid) + LEFT JOIN email_options AS eo ON (eo.uid = a.uid) + LEFT JOIN emails AS e ON (e.uid = a.uid AND e.flags='active') 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"; + (e.email IS NOT NULL OR FIND_IN_SET('googleapps', eo.storage)) + GROUP BY a.uid"; } public function sendToAll() { $this->setSent(); - $query = $this->getAllRecipients() . " LIMIT {?}"; + $query = XDB::format($this->getAllRecipients(), $this->id()) . ' LIMIT 60'; while (true) { - $res = XDB::iterRow($query, $this->_id, 60); + $res = XDB::iterRow($query); if (!$res->total()) { return; } - $sent = array(); - 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); + while ($infos = $res->next()) { + $user = User::getSilentWithValues(null, $infos); + $sent[] = XDB::format('user_id = {?}', $user->id()); + $this->sendTo($user, $hash); } XDB::execute("UPDATE {$this->_subscriptionTable} SET last = {?} diff --git a/include/vcard.inc.php b/include/vcard.inc.php index b62d09f..709253e 100644 --- a/include/vcard.inc.php +++ b/include/vcard.inc.php @@ -37,7 +37,7 @@ class VCard extends PlVCard public function addUser($user) { - $user = User::getSilent($user); + $user = Profile::get($user); if ($user) { $this->user_list[] = $user; $this->count++; @@ -59,7 +59,7 @@ class VCard extends PlVCard { global $globals; $login = $entry['value']; - $user = get_user_details($login->login()); + $user = get_user_details($login->hrid()); if (empty($user['nom_usage'])) { $entry = new PlVCardEntry($user['prenom'], $user['nom'], null, null, @$user['nickname']); @@ -135,8 +135,8 @@ class VCard extends PlVCard // Melix $res = XDB::query( "SELECT alias - FROM virtual - INNER JOIN virtual_redirect USING(vid) + FROM virtual AS v + INNER JOIN virtual_redirect AS vr ON (v.vid = vr.vid) INNER JOIN auth_user_quick ON ( user_id = {?} AND emails_alias_pub = 'public' ) WHERE ( redirect={?} OR redirect={?} ) AND alias LIKE '%@{$globals->mail->alias_dom}'", diff --git a/modules/axletter.php b/modules/axletter.php index 9414f8d..0162815 100644 --- a/modules/axletter.php +++ b/modules/axletter.php @@ -269,15 +269,14 @@ class AXLetterModule extends PLModule $page->changeTpl('axletter/show.tpl'); $nl = new AXLetter($nid); + $user =& S::user(); if (Get::has('text')) { - $nl->toText($page, S::v('prenom'), S::v('nom'), S::v('femme')); + $nl->toText($page, $user); } else { - $nl->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme')); + $nl->toHtml($page, $user); } if (Post::has('send')) { - $nl->sendTo(S::user()->login(), S::user()->bestEmail(), - S::v('prenom'), S::v('nom'), - S::v('femme'), S::v('mail_fmt') != 'texte'); + $nl->sendTo($user); } } diff --git a/modules/newsletter.php b/modules/newsletter.php index 8390eed..5928dff 100644 --- a/modules/newsletter.php +++ b/modules/newsletter.php @@ -57,17 +57,14 @@ class NewsletterModule extends PLModule require_once 'newsletter.inc.php'; $nl = new NewsLetter($nid); + $user =& S::user(); if (Get::has('text')) { - $nl->toText($page, S::v('prenom'), S::v('nom'), S::v('femme')); + $nl->toText($page, $user); } else { - $nl->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme')); + $nl->toHtml($page, $user); } if (Post::has('send')) { - $res = XDB::query("SELECT hash FROM newsletter_ins WHERE user_id = {?}", S::i('uid')); - $nl->sendTo(S::user()->login(), S::user()->bestEmail(), - S::v('prenom'), S::v('nom'), - S::v('femme'), S::v('mail_fmt') != 'texte', - $res->fetchOneCell()); + $nl->sendTo($user); } } diff --git a/templates/axletter/letter.mail.tpl b/templates/axletter/letter.mail.tpl index 5ae5706..9e04ec3 100644 --- a/templates/axletter/letter.mail.tpl +++ b/templates/axletter/letter.mail.tpl @@ -34,7 +34,7 @@ {$am->title()} ==================================================================== -{$am->head($prenom, $nom, $sexe, 'text')} +{$am->head($user, 'text')} {$am->body('text')} @@ -75,7 +75,7 @@ ne plus recevoir : <https://www.polytechnique.org/ax/out{if $hash}/{$hash}{/i {/if}
{$am->title()}
-
{$am->head($prenom, $nom, $sexe, 'html')|smarty:nodefaults}
+
{$am->head($user, 'html')|smarty:nodefaults}
{$am->body('html')|smarty:nodefaults}
{$am->signature('html')|smarty:nodefaults}
diff --git a/templates/newsletter/nl.mail.tpl b/templates/newsletter/nl.mail.tpl index cd6ecd3..7ab2b4b 100644 --- a/templates/newsletter/nl.mail.tpl +++ b/templates/newsletter/nl.mail.tpl @@ -34,7 +34,7 @@ {$nl->title()} ==================================================================== -{$nl->head($prenom, $nom, $sexe, 'text')} +{$nl->head($user, 'text')} {foreach from=$nl->_arts key=cid item=arts name=cats} @@ -51,7 +51,7 @@ -------------------------------------------------------------------- {foreach from=$arts item=art} -{$art->toText($hash, $alias)} +{$art->toText($hash, $user->login())} {/foreach} {/foreach} @@ -93,7 +93,7 @@ ne plus recevoir : <https://www.polytechnique.org/nl/out> {/if}
{$nl->title()}
-
{$nl->head($prenom, $nom, $sexe, 'html')|smarty:nodefaults}
+
{$nl->head($user, 'html')|smarty:nodefaults}
{foreach from=$nl->_arts key=cid item=arts name=cats}
@@ -109,7 +109,7 @@ ne plus recevoir : <https://www.polytechnique.org/nl/out> {$nl->_cats[$cid]} {foreach from=$arts item=art} - {$art->toHtml($hash, $alias)|smarty:nodefaults} + {$art->toHtml($hash, $user->login())|smarty:nodefaults} {/foreach} {/foreach} -- 2.1.4