From f067a3d607ddc43a2c3b903fcf6f0a86249a9d58 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Jacob?= Date: Mon, 7 Mar 2011 17:08:49 +0100 Subject: [PATCH] Finishes best_domain. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Jacob --- include/emails.inc.php | 67 +++++++++++++++++++++++++++----------------------- modules/admin.php | 7 +++--- modules/email.php | 7 +++--- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/include/emails.inc.php b/include/emails.inc.php index 0ff0d33..5f0d72f 100644 --- a/include/emails.inc.php +++ b/include/emails.inc.php @@ -176,30 +176,13 @@ function mark_broken_email($email, $admin = false) // eventually selects a new bestalias when required. function fix_bestalias(User $user) { - // First check if best_domain is properly set. - $count = XDB::fetchOneCell('SELECT COUNT(*) - FROM accounts AS a - INNER JOIN email_virtual_domains AS d ON (d.id = a.best_domain) - INNER JOIN email_virtual_domains AS m ON (d.aliasing = m.id) - WHERE a.uid = {?} AND m.name = {?}', - $user->id(), $user->mainEmailDomain()); - if ($count == 0) { - XDB::execute('UPDATE accounts AS a - INNER JOIN email_virtual_domains AS d ON (d.name = {?}) - SET a.best_domain = d.id - WHERE a.uid = {?}', - $user->mainEmailDomain(), $user->id()); - } - - // Then check the alias. - $count = XDB::fetchOneCell('SELECT COUNT(*) - FROM email_source_account - WHERE uid = {?} AND FIND_IN_SET(\'bestalias\', flags) AND expire IS NULL', - $user->id()); + // First check if the bestalias is properly set. + $alias_count = XDB::fetchOneCell('SELECT COUNT(*) + FROM email_source_account + WHERE uid = {?} AND FIND_IN_SET(\'bestalias\', flags) AND expire IS NULL', + $user->id()); - if ($count == 1) { - return; - } elseif ($count > 1) { + if ($alias_count > 1) { // If too many bestaliases, delete the bestalias flag from all this // user's emails (this should never happen). XDB::execute("UPDATE email_source_account @@ -207,15 +190,37 @@ function fix_bestalias(User $user) WHERE uid = {?}", $user->id()); } + if ($alias_count != 1) { + // If no bestalias is selected, we choose the shortest email which is not + // related to a usage name and contains a '.'. + XDB::execute("UPDATE email_source_account + SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias') + WHERE uid = {?} AND expire IS NULL + ORDER BY NOT FIND_IN_SET('usage', flags), email LIKE '%.%', LENGTH(email) + LIMIT 1", + $user->id()); + } + + // First check if best_domain is properly set. + $domain_count = XDB::fetchOneCell('SELECT COUNT(*) + FROM accounts AS a + INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags)) + INNER JOIN email_virtual_domains AS d ON (d.id = a.best_domain) + INNER JOIN email_virtual_domains AS m ON (d.aliasing = m.id) + INNER JOIN email_virtual_domains AS v ON (v.aliasing = m.id AND v.id = s.domain) + WHERE a.uid = {?} AND (m.name = {?} OR m.name = {?})', + $user->id(), $user->mainEmailDomain(), Platal::globals()->mail->alias_dom); + + if ($domain_count == 0) { + XDB::execute('UPDATE accounts AS a + INNER JOIN email_source_account AS s ON (s.uid = a.uid AND FIND_IN_SET(\'bestalias\', s.flags)) + INNER JOIN email_virtual_domains AS d ON (d.aliasing = s.domain AND (d.name = {?} OR d.name = {?})) + SET a.best_domain = d.id + WHERE a.uid = {?}', + $user->mainEmailDomain(), Platal::globals()->mail->alias_dom, $user->id()); + } + - // If no bestalias is selected, we choose the shortest email which is not - // related to a usage name and contains a '.'. - XDB::execute("UPDATE email_source_account - SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias') - WHERE uid = {?} AND expire IS NULL - ORDER BY NOT FIND_IN_SET('usage', flags), email LIKE '%.%', LENGTH(email) - LIMIT 1", - $user->id()); } // function valide_email() {{{1 diff --git a/modules/admin.php b/modules/admin.php index 9c1e108..2daedea 100644 --- a/modules/admin.php +++ b/modules/admin.php @@ -655,10 +655,9 @@ class AdminModule extends PLModule WHERE uid = {?}", $user->id()); // Then gives the bestalias flag to the given email. list($email, $domain) = explode('@', $best_alias); - XDB::execute("UPDATE email_source_account AS s - INNER JOIN email_virtual_domains AS d ON (s.domain = d.id) - SET s.flags = CONCAT_WS(',', IF(s.flags = '', NULL, s.flags), 'bestalias') - WHERE s.uid = {?} AND s.email = {?} AND d.name = {?}", $user->id(), $email, $domain); + XDB::execute("UPDATE email_source_account + SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias') + WHERE uid = {?} AND email = {?}", $user->id(), $email); // As having a non-null bestalias value is critical in // plat/al's code, we do an a posteriori check on the diff --git a/modules/email.php b/modules/email.php index ef77898..f116ab5 100644 --- a/modules/email.php +++ b/modules/email.php @@ -67,10 +67,9 @@ class EmailModule extends PLModule WHERE uid = {?}", $user->id()); // Then gives the bestalias flag to the given email. list($email, $domain) = explode('@', $email); - XDB::execute("UPDATE email_source_account AS s - INNER JOIN email_virtual_domains AS d ON (s.domain = d.id) - SET s.flags = CONCAT_WS(',', IF(s.flags = '', NULL, s.flags), 'bestalias') - WHERE s.uid = {?} AND s.email = {?} AND d.name = {?}", $user->id(), $email, $domain); + XDB::execute("UPDATE email_source_account + SET flags = CONCAT_WS(',', IF(flags = '', NULL, flags), 'bestalias') + WHERE uid = {?} AND email = {?}", $user->id(), $email); XDB::execute('UPDATE accounts AS a INNER JOIN email_virtual_domains AS d ON (d.name = {?}) SET a.best_domain = d.id -- 2.1.4