diff --git a/CHANGELOG.md b/CHANGELOG.md index 046c866..60ae946 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] + +### Fixed + +- Fix error 500 (PHP memory exhausted) on the "Oauth authorization" tab of an application with many authorizations: the list is now paginated +- Speed up the mail collector form: the authorization dropdown now loads and searches authorizations page by page instead of loading them all +- Speed up the "Mail collectors" tab of an application: only the matching mail collectors are read, without one extra query per mail collector +- Remove a database query run by the plugin on every GLPI page and AJAX request + ## [1.5.6] - 2026-11-05 ### Fixed diff --git a/ajax/dropdownAuthorization.php b/ajax/dropdownAuthorization.php index dbab8eb..89bb240 100644 --- a/ajax/dropdownAuthorization.php +++ b/ajax/dropdownAuthorization.php @@ -28,39 +28,91 @@ * ------------------------------------------------------------------------- */ -header('Content-Type: text/html; charset=UTF-8'); -Html::header_nocache(); +use function Safe\json_encode; Session::checkLoginUser(); Session::checkRight("config", UPDATE); +Session::writeClose(); + /** @var DBmysql $DB */ -global $DB; +/** @var array $CFG_GLPI */ +global $DB, $CFG_GLPI; -$iterator = $DB->request( - [ - 'FROM' => PluginOauthimapAuthorization::getTable(), - 'WHERE' => [ - PluginOauthimapApplication::getForeignKeyField() => $_POST['application_id'] ?? null, +$application_fkey = PluginOauthimapApplication::getForeignKeyField(); +$authorization_fkey = PluginOauthimapAuthorization::getForeignKeyField(); +$application_id = (int) ($_POST['application_id'] ?? 0); +$create_label = __('Create authorization for another user', 'oauthimap'); + +Html::header_nocache(); + +if (($_POST['action'] ?? '') === 'get_values') { + header('Content-Type: application/json; charset=UTF-8'); + + $dropdown_max = max(1, (int) $CFG_GLPI['dropdown_max']); + $page = max(1, (int) ($_POST['page'] ?? 1)); + $page_limit = min($dropdown_max, max(1, (int) ($_POST['page_limit'] ?? $dropdown_max))); + $search = trim((string) ($_POST['searchText'] ?? '')); + + $where = [$application_fkey => $application_id]; + // A search reduced to nothing (`$`, `^$`, `null`) does not filter, like an empty search. + $search_value = $search !== '' ? Search::makeTextSearchValue($search) : null; + if (is_string($search_value) && $search_value !== '') { + $where['email'] = ['LIKE', $search_value]; + } + + $results = []; + if ($page === 1 && str_contains(mb_strtolower($create_label), mb_strtolower(trim($search, '^$')))) { + $results[] = ['id' => -1, 'text' => $create_label]; + } + + $iterator = $DB->request( + [ + 'SELECT' => ['id', 'email'], + 'FROM' => PluginOauthimapAuthorization::getTable(), + 'WHERE' => $where, + 'ORDER' => 'email', + 'START' => ($page - 1) * $page_limit, + 'LIMIT' => $page_limit, ], - ], -); -$authorizations = [ - '-1' => __s('Create authorization for another user', 'oauthimap'), -]; -$value = -1; -foreach ($iterator as $row) { - $authorizations[$row['id']] = $row['email']; - if (array_key_exists('selected', $_POST) && $row['email'] == $_POST['selected']) { - $value = $row['id']; + ); + foreach ($iterator as $row) { + $results[] = ['id' => (int) $row['id'], 'text' => $row['email']]; + } + + echo json_encode(['results' => $results, 'count' => count($iterator)]); + + return; +} + +header('Content-Type: text/html; charset=UTF-8'); + +$value = -1; +$valuename = $create_label; +if (($_POST['selected'] ?? '') !== '') { + $selected = $DB->request( + [ + 'SELECT' => ['id', 'email'], + 'FROM' => PluginOauthimapAuthorization::getTable(), + 'WHERE' => [$application_fkey => $application_id, 'email' => $_POST['selected']], + 'LIMIT' => 1, + ], + )->current(); + if ($selected !== null) { + $value = (int) $selected['id']; + $valuename = $selected['email']; } } -Dropdown::showFromArray( - PluginOauthimapAuthorization::getForeignKeyField(), - $authorizations, +echo Html::jsAjaxDropdown( + $authorization_fkey, + 'dropdown_' . $authorization_fkey . mt_rand(), + $CFG_GLPI['root_doc'] . '/plugins/oauthimap/ajax/dropdownAuthorization.php', [ - 'display_emptychoice' => false, - 'value' => $value, + 'value' => $value, + 'valuename' => $valuename, + 'width' => '100%', + 'action' => 'get_values', + 'application_id' => $application_id, ], ); diff --git a/inc/authorization.class.php b/inc/authorization.class.php index 978c24e..91e5034 100644 --- a/inc/authorization.class.php +++ b/inc/authorization.class.php @@ -106,12 +106,25 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $ /** @var array $CFG_GLPI */ global $DB, $CFG_GLPI; + $criteria = [ + PluginOauthimapApplication::getForeignKeyField() => $item->getID(), + ]; + $total = countElementsInTable(self::getTable(), $criteria); + $start = (int) ($_GET['start'] ?? 0); + $limit = max(1, (int) $_SESSION['glpilist_limit']); + if ($start < 0) { + $start = 0; + } elseif ($total > 0 && $start >= $total) { + $start = intdiv($total - 1, $limit) * $limit; + } + $iterator = $DB->request( [ 'FROM' => self::getTable(), - 'WHERE' => [ - PluginOauthimapApplication::getForeignKeyField() => $item->getID(), - ], + 'WHERE' => $criteria, + 'ORDER' => 'id', + 'START' => $start, + 'LIMIT' => $limit, ], ); @@ -133,8 +146,33 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $ echo ''; // #mainformtable echo ''; // [name=asset_form] + if ($total > 0) { + Html::printAjaxPager('', $start, $total); + + $title = json_encode(__s('Connection diagnostic', 'oauthimap')); + // One handler for all rows (rebound on each tab render), the modal is created and removed by core. + echo Html::scriptBlock(<<', + }); + $('#' + id + ' iframe').on('load', function () { + var doc = this.contentWindow.document; + $(this).height(Math.min(doc.documentElement.scrollHeight, doc.body.scrollHeight) || 650); + }); + }); +JAVASCRIPT); + } + + $csrf_token = Session::getNewCSRFToken(); + echo ''; - if ($iterator->count() === 0) { + if ($total === 0) { echo ''; } else { echo ''; @@ -151,16 +189,9 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $ echo ''; echo '
' . __s('No authorizations.', 'oauthimap') . '
' . htmlspecialchars($row['email'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . ''; - $modal_id = 'plugin_oauthimap_authorization_diagnostic_' . mt_rand(); - Ajax::createIframeModalWindow( - $modal_id, - self::getFormURLWithID($row['id']) . '&diagnose', - [ - 'title' => __s('Connection diagnostic', 'oauthimap'), - 'height' => 650, - ], - ); - echo ''; + $diagnostic_url = self::getFormURLWithID($row['id']) . '&diagnose&_in_modal=1'; + echo ''; echo ' ' . __s('Diagnose', 'oauthimap'); echo ''; echo ' '; @@ -169,7 +200,7 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $ echo ''; echo ' '; echo '
'; - echo Html::hidden('_glpi_csrf_token', ['value' => Session::getNewCSRFToken()]); + echo Html::hidden('_glpi_csrf_token', ['value' => $csrf_token]); echo Html::hidden('id', ['value' => $row['id']]); echo '