From 4121509ceb34161ba0c38db899d6c9a287eede94 Mon Sep 17 00:00:00 2001 From: FreddleSpl0it Date: Thu, 20 Apr 2023 11:28:59 +0200 Subject: [PATCH] [Web] optimizing update_sogo_static_view function --- data/web/inc/functions.inc.php | 101 +++++++++++---------------------- 1 file changed, 34 insertions(+), 67 deletions(-) diff --git a/data/web/inc/functions.inc.php b/data/web/inc/functions.inc.php index 251e93d5..8bd55f29 100644 --- a/data/web/inc/functions.inc.php +++ b/data/web/inc/functions.inc.php @@ -1021,74 +1021,41 @@ function update_sogo_static_view($mailbox = null) { } global $pdo; global $lang; - $stmt = $pdo->query("SELECT 'OK' FROM INFORMATION_SCHEMA.TABLES - WHERE TABLE_NAME = 'sogo_view'"); - $num_results = count($stmt->fetchAll(PDO::FETCH_ASSOC)); - if ($num_results != 0) { - if ($mailbox == null){ - $stmt = $pdo->query("REPLACE INTO _sogo_static_view (`c_uid`, `domain`, `c_name`, `c_password`, `c_cn`, `mail`, `aliases`, `ad_aliases`, `ext_acl`, `kind`, `multiple_bookings`) - SELECT - mailbox.username, - mailbox.domain, - mailbox.username, - IF(JSON_UNQUOTE(JSON_VALUE(attributes, '$.force_pw_update')) = '0', IF(JSON_UNQUOTE(JSON_VALUE(attributes, '$.sogo_access')) = 1, password, '{SSHA256}A123A123A321A321A321B321B321B123B123B321B432F123E321123123321321'), '{SSHA256}A123A123A321A321A321B321B321B123B123B321B432F123E321123123321321'), - mailbox.name, - mailbox.username, - IFNULL(GROUP_CONCAT(ga.aliases ORDER BY ga.aliases SEPARATOR ' '), ''), - IFNULL(gda.ad_alias, ''), - IFNULL(external_acl.send_as_acl, ''), - mailbox.kind, - mailbox.multiple_bookings - FROM - mailbox - LEFT OUTER JOIN - grouped_mail_aliases ga - ON ga.username REGEXP CONCAT('(^|,)', mailbox.username, '($|,)') - LEFT OUTER JOIN - grouped_domain_alias_address gda - ON gda.username = mailbox.username - LEFT OUTER JOIN - grouped_sender_acl_external external_acl - ON external_acl.username = mailbox.username - WHERE - mailbox.active = '1' - GROUP BY - mailbox.username;"); - } else { - $stmt = $pdo->prepare("REPLACE INTO _sogo_static_view (`c_uid`, `domain`, `c_name`, `c_password`, `c_cn`, `mail`, `aliases`, `ad_aliases`, `ext_acl`, `kind`, `multiple_bookings`) - SELECT `c_uid`, `domain`, `c_name`, `c_password`, `c_cn`, `mail`, `aliases`, `ad_aliases`, `ext_acl`, `kind`, `multiple_bookings` from sogo_view where `c_uid` = :mailbox - SELECT - mailbox.username, - mailbox.domain, - mailbox.username, - IF(JSON_UNQUOTE(JSON_VALUE(attributes, '$.force_pw_update')) = '0', IF(JSON_UNQUOTE(JSON_VALUE(attributes, '$.sogo_access')) = 1, password, '{SSHA256}A123A123A321A321A321B321B321B123B123B321B432F123E321123123321321'), '{SSHA256}A123A123A321A321A321B321B321B123B123B321B432F123E321123123321321'), - mailbox.name, - mailbox.username, - IFNULL(GROUP_CONCAT(ga.aliases ORDER BY ga.aliases SEPARATOR ' '), ''), - IFNULL(gda.ad_alias, ''), - IFNULL(external_acl.send_as_acl, ''), - mailbox.kind, - mailbox.multiple_bookings - FROM - mailbox - LEFT OUTER JOIN - grouped_mail_aliases ga - ON ga.username REGEXP CONCAT('(^|,)', mailbox.username, '($|,)') - LEFT OUTER JOIN - grouped_domain_alias_address gda - ON gda.username = mailbox.username - LEFT OUTER JOIN - grouped_sender_acl_external external_acl - ON external_acl.username = mailbox.username - WHERE - mailbox.active = '1' AND - mailbox.username = :mailbox - GROUP BY - mailbox.username;"); - $stmt->execute(array(':mailbox' => $mailbox)); - } - $stmt = $pdo->query("DELETE FROM _sogo_static_view WHERE `c_uid` NOT IN (SELECT `username` FROM `mailbox` WHERE `active` = '1');"); + + $query = "REPLACE INTO _sogo_static_view (`c_uid`, `domain`, `c_name`, `c_password`, `c_cn`, `mail`, `aliases`, `ad_aliases`, `ext_acl`, `kind`, `multiple_bookings`) + SELECT + mailbox.username, + mailbox.domain, + mailbox.username, + IF(JSON_UNQUOTE(JSON_VALUE(attributes, '$.force_pw_update')) = '0', + IF(JSON_UNQUOTE(JSON_VALUE(attributes, '$.sogo_access')) = 1, password, '{SSHA256}A123A123A321A321A321B321B321B123B123B321B432F123E321123123321321'), + '{SSHA256}A123A123A321A321A321B321B321B123B123B321B432F123E321123123321321'), + mailbox.name, + mailbox.username, + IFNULL(GROUP_CONCAT(ga.aliases ORDER BY ga.aliases SEPARATOR ' '), ''), + IFNULL(gda.ad_alias, ''), + IFNULL(external_acl.send_as_acl, ''), + mailbox.kind, + mailbox.multiple_bookings + FROM + mailbox + LEFT OUTER JOIN grouped_mail_aliases ga ON ga.username REGEXP CONCAT('(^|,)', mailbox.username, '($|,)') + LEFT OUTER JOIN grouped_domain_alias_address gda ON gda.username = mailbox.username + LEFT OUTER JOIN grouped_sender_acl_external external_acl ON external_acl.username = mailbox.username + WHERE + mailbox.active = '1'"; + + if ($mailbox !== null) { + $query .= " AND mailbox.username = :mailbox"; + $stmt = $pdo->prepare($query); + $stmt->execute(array(':mailbox' => $mailbox)); + } else { + $query .= " GROUP BY mailbox.username"; + $stmt = $pdo->query($query); } + + $stmt = $pdo->query("DELETE FROM _sogo_static_view WHERE `c_uid` NOT IN (SELECT `username` FROM `mailbox` WHERE `active` = '1');"); + flush_memcached(); } function edit_user_account($_data) {