+ 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());
+ }
+