From 9811b62a39a2f2add97fa9a8c546be41983318d2 Mon Sep 17 00:00:00 2001 From: Aren Sargsyan Date: Sun, 28 Jun 2026 15:51:42 +0400 Subject: [PATCH] NSD-17630 Blesta module: 'Misuse of API' error on domain availability checks --- namesilo.php | 93 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 78 insertions(+), 15 deletions(-) diff --git a/namesilo.php b/namesilo.php index 24b22ef..ce0d1e8 100644 --- a/namesilo.php +++ b/namesilo.php @@ -3309,35 +3309,98 @@ private function manageSettings( */ public function checkAvailability($domain, $module_row_id = null) { + $result = $this->checkDomainsAvailability([$domain], $module_row_id); + + if ($result['availability'][$domain] ?? false) { + return true; + } + + if ($result['premium'][$domain] ?? false) { + $this->Input->setErrors( + ['availability' => ['premium' => Language::_('Namesilo.!error.premium_domain', true, $domain)]] + ); + } + + return false; + } + + /** + * Checks the availability of multiple domain names at once using a single API call. + * + * @param array $domains A list of domain names to check + * @param int $module_row_id The ID of the module row to fetch for the current module + * @return array An associative array of domain => boolean (true if available) + */ + public function bulkCheckAvailability($domains, $module_row_id = null) + { + $result = $this->checkDomainsAvailability($domains, $module_row_id); + + return $result['availability']; + } + + /** + * Checks domain availability via a single API request. + * + * @param array $domains A list of domain names to check + * @param int $module_row_id The ID of the module row to fetch for the current module + * @return array An array with availability and premium flags keyed by domain + */ + private function checkDomainsAvailability(array $domains, $module_row_id = null) + { + $availability = array_fill_keys($domains, false); + $premium = array_fill_keys($domains, false); + + if (empty($domains)) { + return [ + 'availability' => $availability, + 'premium' => $premium, + ]; + } + $row = $this->getModuleRow($module_row_id); $api = $this->getApi($row->meta->user, $row->meta->key, $row->meta->sandbox == 'true'); - $domains = new NamesiloDomains($api); - $result = $domains->check(['domains' => $domain]); + $domainList = implode(',', array_map('strtolower', $domains)); + $domainsApi = new NamesiloDomains($api); + $result = $domainsApi->check(['domains' => $domainList]); $this->processResponse($api, $result); if ((self::$codes[$result->status()][1] ?? 'fail') == 'fail') { - return false; + return [ + 'availability' => $availability, + 'premium' => $premium, + ]; } $responseXML = $result->responseXML(); - $xpath_result = $responseXML->xpath("//available/domain[text()='" . $domain . "']"); - - if (empty($xpath_result)) { - // The domain was not in the available element, its not available. - return false; + if (!$responseXML) { + return [ + 'availability' => $availability, + 'premium' => $premium, + ]; } - $attributes = $xpath_result[0]->attributes(); - if (isset($attributes->premium) && $attributes->premium == '1') { - $this->Input->setErrors( - ['availability' => ['premium' => Language::_('Namesilo.!error.premium_domain', true, $domain)]] - ); + foreach ($domains as $domain) { + $lookup = strtolower($domain); + $xpath_result = $responseXML->xpath("//available/domain[text()='" . $lookup . "']"); - return false; + if (empty($xpath_result)) { + continue; + } + + $attributes = $xpath_result[0]->attributes(); + if (isset($attributes->premium) && (string) $attributes->premium === '1') { + $premium[$domain] = true; + continue; + } + + $availability[$domain] = true; } - return true; + return [ + 'availability' => $availability, + 'premium' => $premium, + ]; } /**