From 9a12c97b978d267bab8366e7eb51bd1257dd58ab Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Thu, 20 Feb 2025 12:35:42 +0100 Subject: [PATCH 01/11] init external modules class --- htdocs/admin/modules.php | 44 +- .../class/externalModules.class.php | 527 ++++++++++++++++++ htdocs/admin/remotestore/css/store.css | 1 + 3 files changed, 561 insertions(+), 11 deletions(-) create mode 100644 htdocs/admin/remotestore/class/externalModules.class.php diff --git a/htdocs/admin/modules.php b/htdocs/admin/modules.php index 71e27fc5729..779d29dd73a 100644 --- a/htdocs/admin/modules.php +++ b/htdocs/admin/modules.php @@ -42,6 +42,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php'; require_once DOL_DOCUMENT_ROOT.'/admin/remotestore/class/dolistore.class.php'; +require_once DOL_DOCUMENT_ROOT.'/admin/remotestore/class/externalModules.class.php'; ' @phan-var-force string $dolibarr_main_url_root_alt @@ -83,13 +84,12 @@ $search_version = GETPOST('search_version', 'alpha'); // For remotestore search $options = array(); $options['per_page'] = 10; +$options['no_page'] = ((int) GETPOSTINT('no_page') ? GETPOSTINT('no_page') : 1); $options['categorie'] = ((int) (GETPOSTINT('categorie') ? GETPOSTINT('categorie') : 0)); -$options['start'] = ((int) (GETPOSTINT('start') ? GETPOSTINT('start') : 0)); -$options['end'] = ((int) (GETPOSTINT('end') ? GETPOSTINT('end') : 0)); $options['search'] = GETPOST('search_keyword', 'alpha'); -$remotestore = new Dolistore(false); - +//$remotestore = new Dolistore(false); +$remotestore = new ExternalModules(); if (!$user->admin) { accessforbidden(); @@ -1246,7 +1246,7 @@ if ($mode == 'marketplace') { print '
'; - // Marketplace + // Marketplace and community modules print '
'; print ''."\n"; print ''."\n"; @@ -1255,6 +1255,7 @@ if ($mode == 'marketplace') { print ''; print ''; + // Marketplace print ''."\n"; $url = 'https://www.dolistore.com'; print ''; @@ -1262,6 +1263,14 @@ if ($mode == 'marketplace') { print ''; print ''; + // Community + print ''."\n"; + $url = 'https://github.com/Dolibarr/dolibarr-community-modules'; + print ''; + print ''; + print ''; + print ''; + print "
'.$langs->trans("URL").'
'.$url.'
'.$langs->trans("CommunityModulesDesc").''.$url.'
\n"; print '
'; @@ -1278,11 +1287,16 @@ if ($mode == 'marketplace') { $nbmaxtoshow = $options['per_page']; $options['per_page']++; - $remotestore->getRemoteCategories(); - $remotestore->getRemoteProducts($options); + //$remotestore->getRemoteCategories(); + //$remotestore->getRemoteProducts($options); print ''.$langs->trans('DOLISTOREdescriptionLong').'

'; + //$previouslink = $remotestore->get_previous_link(); + //$nextlink = $remotestore->get_next_link(); + + $categories_tree = $remotestore->getCategories(); + $products_list = $remotestore->getProducts($options); $previouslink = $remotestore->get_previous_link(); $nextlink = $remotestore->get_next_link(); @@ -1292,7 +1306,14 @@ if ($mode == 'marketplace') {
-
+ + + +
+
@@ -1311,15 +1332,16 @@ if ($mode == 'marketplace') {
    - get_categories(); // Do not use dol_escape_htmltag here, it is already a structured content?> +
- get_products($nbmaxtoshow); ?> + get_products($nbmaxtoshow); ?> +
diff --git a/htdocs/admin/remotestore/class/externalModules.class.php b/htdocs/admin/remotestore/class/externalModules.class.php new file mode 100644 index 00000000000..ae51aacd0b8 --- /dev/null +++ b/htdocs/admin/remotestore/class/externalModules.class.php @@ -0,0 +1,527 @@ + + * + * This program is free software; you can redistribute it and/or modifyion 2.0 (the "License"); + * it under the terms of the GNU General Public License as published bypliance with the License. + * the Free Software Foundation; either version 3 of the License, or + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * or see https://www.gnu.org/ + */ + +include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; + +/** + * Class ExternalModules + */ +class ExternalModules +{ + /** + * @var int Pagination: current page + */ + public $no_page; + + /** + * @var int Pagination: display per page + */ + public $per_page; + /** + * @var int The current categorie + */ + public $categorie; + + /** + * @var string The search keywords + */ + public $search; + + // setups + /** + * @var string + */ + public $url; // the url of this page + /** + * @var string + */ + public $shop_url; // the url of the shop + /** + * @var int + */ + public $lang; // the integer representing the lang in the store + /** + * @var bool + */ + public $debug_api; // useful if no dialog + /** + * @var string + */ + public $dolistore_api_url; + /** + * @var string + */ + public $dolistore_api_key; + /** + * @var array|null + */ + public $products; + + /** + * Constructor + * + * @param boolean $debug Enable debug of request on screen + */ + public function __construct($debug = false) + { + global $langs; + + $this->dolistore_api_url = getDolGlobalString('MAIN_MODULE_DOLISTORE_API_SRV'); + $this->dolistore_api_key = getDolGlobalString('MAIN_MODULE_DOLISTORE_API_KEY'); + + $this->url = DOL_URL_ROOT.'/admin/modules.php?mode=marketplace'; + $this->shop_url = 'https://www.dolistore.com/product.php?id='; + $this->debug_api = $debug; + + $lang = $langs->defaultlang; + $lang_array = array('en_US', 'fr_FR', 'es_ES', 'it_IT', 'de_DE'); + if (!in_array($lang, $lang_array)) { + $lang = 'en_US'; + } + $this->lang = $lang; + + // TODO check access to all remote sources and define valid one + } + + /** + * Test if we can access to remote Dolistore market place. + * + * @param string $resource Resource name + * @param array|false $options Options for the request + * + * @return array{status_code:int,response:?string,header:string} + */ + public function callApi($resource, $options = false) + { + + // If no dolistore_api_key is set, we can't access the API + if (empty($this->dolistore_api_key) || empty($this->dolistore_api_url)) { + return array('status_code' => 0, 'response' => null, 'header' => ''); + } + + $curl = curl_init(); + $httpheader = ['DOLAPIKEY: '.$this->dolistore_api_key]; + + // Add basic auth if needed + if (!empty(getDolGlobalString('MAIN_MODULE_DOLISTORE_BASIC_LOGIN')) && !empty(getDolGlobalString('MAIN_MODULE_DOLISTORE_BASIC_PASSWORD'))) { + curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + $login = getDolGlobalString('MAIN_MODULE_DOLISTORE_BASIC_LOGIN'); + $password = getDolGlobalString('MAIN_MODULE_DOLISTORE_BASIC_PASSWORD'); + curl_setopt($curl, CURLOPT_USERPWD, $login . ':' . $password); + } + + $url = $this->dolistore_api_url . $resource; + + if ($options) { + $url .= '?' . http_build_query($options); + } + + curl_setopt($curl, CURLOPT_URL, $url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_HTTPHEADER, $httpheader); + curl_setopt($curl, CURLOPT_HEADER, 1); + + $response = curl_exec($curl); + + if ($response === false) { + return array('status_code' => 0, 'response' => 'CURL Error: ' . curl_error($curl), 'header' => ''); + } + + $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); + $header = substr($response, 0, $header_size); + $body = substr($response, $header_size); + + // convert body to array if it is json + if (strpos($header, 'Content-Type: application/json') !== false) { + $body = json_decode($body, true); + } + + $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + curl_close($curl); + + return array('status_code' => $status_code, 'response' => $body, 'header' => $header); + } + + /** + * Generate HTML for categories and their children. + * + * @return string HTML string representing the categories and their children. + */ + public function getCategories() + { + $organized_tree = array(); + $html = ''; + + $data = [ + 'lang' => $this->lang + ]; + + $resCategories = $this->callAPI('categories', $data); + if (isset($resCategories['response']) && is_array($resCategories['response'])) { + $organized_tree = $resCategories['response']; + } else { + return $html ; + } + + // TODO call all sources and merge them + + $html = ''; + foreach ($organized_tree as $key => $value) { + if ($value['label'] != "Versions" && $value['label'] != "Specials") { + $html .= '
  • '; + $html .= '' . $value['label'] . ''; + if (isset($value['children'])) { + $html .= '
      '; + usort($value['children'], $this->buildSorter('position')); + foreach ($value['children'] as $key_children => $value_children) { + $html .= '
    • '; + $html .= '' . $value_children['label'] . ''; + $html .= '
    • '; + } + $html .= '
    '; + } + $html .= '
  • '; + } + } + return $html; + } + + /** + * Generate HTML for products. + * @param array $options Options for the request + * @return string HTML string representing the products. + */ + public function getProducts($options) + { + + global $langs; + + // TODO call all sources and merge them + + $html = ""; + $last_month = dol_now() - (30 * 24 * 60 * 60); + $dolibarrversiontouse = DOL_VERSION; + $urldolibarrmodules = 'https://www.dolistore.com'; + + $this->products = array(); + + $this->categorie = $options['categorie'] ?? 0; + $this->per_page = $options['per_page'] ?? 11; + $this->no_page = $options['no_page'] ?? 1; + $this->search = $options['search'] ?? ''; + + $data = [ + 'categorieid' => $this->categorie, + 'limit' => $this->per_page, + 'page' => $this->no_page, + 'search' => $this->search, + 'lang' => $this->lang + ]; + + $resProducts = $this->callAPI('products', $data); + if (!isset($resProducts['response']) || !is_array($resProducts['response']) || ($resProducts['status_code'] != 200 && $resProducts['status_code'] != 201)) { + $html = $this->checkStatusCode($resProducts); + + return $html; + } + + $this->products = $resProducts['response']; + + $i = 0; + foreach ($this->products as $product) { + $i++; + + // check new product ? + $newapp = ''; + if ($last_month < strtotime($product['datec'])) { + $newapp .= ''.$langs->trans('New').' '; + } + + // check updated ? + if ($last_month < strtotime($product['tms']) && $newapp == '') { + $newapp .= ''.$langs->trans('Updated').' '; + } + + // add image or default ? + if ($product["cover_photo_url"] != '') { + $images = 'trans('Version').' '.$product["module_version"]).'">'; + $images .= ''; + } else { + $images = ''; + } + + // free or pay ? + if ($product["price_ttc"] > 0) { + $price = '

    '.price(price2num($product["price_ttc"], 'MT'), 0, $langs, 1, -1, -1, 'EUR').' '.$langs->trans("TTC").'

    '; + $download_link = ''; + } else { + $price = '

    '.$langs->trans('Free').'

    '; + $download_link = ''; + $download_link .= ''; + } + + // Set and check version + $version = ''; + if ($this->version_compare($product["dolibarr_min"], $dolibarrversiontouse) <= 0) { + if ($this->version_compare($product["dolibarr_max"], $dolibarrversiontouse) >= 0) { + //compatible + $version = ''.$langs->trans( + 'CompatibleUpTo', + $product["dolibarr_max"], + $product["dolibarr_min"], + $product["dolibarr_max"] + ).''; + $compatible = ''; + } else { + //never compatible, module expired + $version = ''.$langs->trans( + 'NotCompatible', + $dolibarrversiontouse, + $product["dolibarr_min"], + $product["dolibarr_max"] + ).''; + $compatible = 'NotCompatible'; + } + } else { + //need update + $version = ''.$langs->trans( + 'CompatibleAfterUpdate', + $dolibarrversiontouse, + $product["dolibarr_min"], + $product["dolibarr_max"] + ).''; + $compatible = 'NotCompatible'; + } + + //output template + $html .= ''; + $html .= '
    '; + $html .= $newapp.$images; // No dol_escape_htmltag, it is already escape html + $html .= '
    '; + $html .= '

    '; + $html .= dol_escape_htmltag(dol_string_nohtmltag($product["label"])); + $html .= '
    '; + $html .= $version; // No dol_escape_htmltag, it is already escape html + $html .= '

    '; + $html .= ' '.dol_print_date(dol_stringtotime($product['tms']), 'dayhour').' - '.$langs->trans('Ref').': '.dol_escape_htmltag($product["ref"]).' - '.dol_escape_htmltag($langs->trans('Id')).': '.((int) $product["id"]).'
    '; + $html .= '
    '.dol_escape_htmltag(dol_string_nohtmltag($product["description"])); + $html.= ''; + // do not load if display none + $html .= ''; + $html .= $price; + $html .= ''; + $html .= ''.$download_link.''; + $html .= ''; + } + + if (empty($this->products)) { + $html .= ''; + $html .= '

    '; + $langs->load("website"); + $html .= $langs->trans("noResultsWereFound").'...'; + $html .= '

    '; + $html .= ''; + } + + if (count($this->products) > $data['limit']) { + $html .= ''; + $html .= '

    '; + $html .= $langs->trans("ThereIsMoreThanXAnswers", $data["limit"]).'...'; + $html .= '

    '; + $html .= ''; + } + + + return $html ; + } + + /** + * Sort an array by a key + * @param string $key Key to sort by + * + * @return Closure + */ + public function buildSorter($key) + { + return function ($a, $b) use ($key) { + return strnatcmp($a[$key], $b[$key]); + }; + } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * version compare + * + * @param string $v1 version 1 + * @param string $v2 version 2 + * @return int result of compare + */ + public function version_compare($v1, $v2) + { + // phpcs:enable + $v1 = explode('.', $v1); + $v2 = explode('.', $v2); + $ret = 0; + $level = 0; + $count1 = count($v1); + $count2 = count($v2); + $maxcount = max($count1, $count2); + while ($level < $maxcount) { + $operande1 = isset($v1[$level]) ? $v1[$level] : 'x'; + $operande2 = isset($v2[$level]) ? $v2[$level] : 'x'; + $level++; + if (strtoupper($operande1) == 'X' || strtoupper($operande2) == 'X' || $operande1 == '*' || $operande2 == '*') { + break; + } + if ($operande1 < $operande2) { + $ret = -$level; + break; + } + if ($operande1 > $operande2) { + $ret = $level; + break; + } + } + //print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret.'
    '."\n"; + return $ret; + } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * get previous link + * + * @param string $text symbol previous + * @return string html previous link + */ + public function get_previous_link($text = '<<') + { + // phpcs:enable + return ''.dol_escape_htmltag($text).''; + } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * get next link + * + * @param string $text symbol next + * @return string html next link + */ + public function get_next_link($text = '>>') + { + // phpcs:enable + return ''.dol_escape_htmltag($text).''; + } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * get previous url + * + * @return string previous url + */ + public function get_previous_url() + { + // phpcs:enable + $param_array = array(); + if ($this->no_page > 1) { + $sub = 1; + } else { + $sub = 0; + } + $param_array['no_page'] = $this->no_page - $sub; + if ($this->categorie != 0) { + $param_array['categorie'] = $this->categorie; + } + $param = http_build_query($param_array); + return $this->url."&".$param; + } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * get next url + * + * @return string next url + */ + public function get_next_url() + { + // phpcs:enable + $param_array = array(); + if ($this->products !== null && count($this->products) < $this->per_page) { + $add = 0; + } else { + $add = 1; + } + $param_array['no_page'] = $this->no_page + $add; + if ($this->categorie != 0) { + $param_array['categorie'] = $this->categorie; + } + $param = http_build_query($param_array); + return $this->url."&".$param; + } + + /** + * Take the status code and throw an exception if the server didn't return 200 or 201 code + *

    Unique parameter must take :

    + * 'status_code' => Status code of an HTTP return
    + * 'response' => CURL response + *

    + * + * @param array{status_code:int,response:?string,header:string} $request Response elements of CURL request + * + * @return void + * @throws Exception if HTTP status code is not 200 or 201 + */ + protected function checkStatusCode($request) + { + switch ($request['status_code']) { + case 200: + case 201: + return; + case 204: + $error_message = 'No content'; + break; + case 400: + $error_message = 'Bad Request'; + break; + case 401: + $error_message = 'Unauthorized'; + break; + case 404: + $error_message = 'Not Found'; + break; + case 405: + $error_message = 'Method Not Allowed'; + break; + case 500: + $error_message = 'Internal Server Error'; + break; + default: + return 'This call to the API returned an unexpected HTTP status of: ' . $request['status_code']; + } + + if (!empty($error_message)) { + $response = $request['response']; + if (isset($response['errors']) && is_array($response['errors'])) { + foreach ($response['errors'] as $error) { + $error_message .= ' - (Code ' . $error['code'] . '): ' . $error['message']; + } + } + $error_label = 'This call to the API failed and returned an HTTP status of %d. That means: %s.'; + return sprintf($error_label, $request['status_code'], $error_message); + } + } +} diff --git a/htdocs/admin/remotestore/css/store.css b/htdocs/admin/remotestore/css/store.css index 8e0a9a87caa..63ae2d46ed4 100644 --- a/htdocs/admin/remotestore/css/store.css +++ b/htdocs/admin/remotestore/css/store.css @@ -37,6 +37,7 @@ div.divsearchfield { margin: 0px 0px 0px 0px; padding:0px; list-style: none; line-height: 2em; font-family: Arial; + margin-left: 15px; } .tree li{ font-size: 16px; From 08f3dab91df0ba996dd5f47ad7cb223bb4909974 Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Thu, 20 Feb 2025 12:36:32 +0100 Subject: [PATCH 02/11] add trans --- htdocs/langs/en_US/admin.lang | 1 + 1 file changed, 1 insertion(+) diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index 731a835efa0..bef02ac81d3 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -234,6 +234,7 @@ Updated=Updated AchatTelechargement=Buy / Download GoModuleSetupArea=To deploy/install a new module, go to the Module setup area: %s. DoliStoreDesc=DoliStore, the official market place for Dolibarr ERP/CRM external modules +CommunityModulesDesc=dolibarr-community-modules, This repository contains all community modules for Dolibarr ERP CRM. DoliPartnersDesc=List of companies providing custom-developed modules or features.
    Note: since Dolibarr is an open source application, anyone experienced in PHP programming should be able to develop a module. WebSiteDesc=External websites for more add-on (non-core) modules... DevelopYourModuleDesc=Some solutions to develop your own module... From e3e70ee35b8ea14ce924fe9925465d7da8415af3 Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Thu, 20 Feb 2025 13:12:22 +0100 Subject: [PATCH 03/11] fix phan --- .../class/externalModules.class.php | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/htdocs/admin/remotestore/class/externalModules.class.php b/htdocs/admin/remotestore/class/externalModules.class.php index ae51aacd0b8..fc449022507 100644 --- a/htdocs/admin/remotestore/class/externalModules.class.php +++ b/htdocs/admin/remotestore/class/externalModules.class.php @@ -67,8 +67,9 @@ class ExternalModules * @var string */ public $dolistore_api_key; + /** - * @var array|null + * @var array|null */ public $products; @@ -101,8 +102,8 @@ class ExternalModules /** * Test if we can access to remote Dolistore market place. * - * @param string $resource Resource name - * @param array|false $options Options for the request + * @param string $resource Resource name + * @param array|false $options Options for the request * * @return array{status_code:int,response:?string,header:string} */ @@ -118,7 +119,10 @@ class ExternalModules $httpheader = ['DOLAPIKEY: '.$this->dolistore_api_key]; // Add basic auth if needed - if (!empty(getDolGlobalString('MAIN_MODULE_DOLISTORE_BASIC_LOGIN')) && !empty(getDolGlobalString('MAIN_MODULE_DOLISTORE_BASIC_PASSWORD'))) { + $basicAuthLogin = getDolGlobalString('MAIN_MODULE_DOLISTORE_BASIC_LOGIN'); + $basicAuthPassword = getDolGlobalString('MAIN_MODULE_DOLISTORE_BASIC_PASSWORD'); + + if (!empty($basicAuthLogin) && !empty($basicAuthPassword)) { curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); $login = getDolGlobalString('MAIN_MODULE_DOLISTORE_BASIC_LOGIN'); $password = getDolGlobalString('MAIN_MODULE_DOLISTORE_BASIC_PASSWORD'); @@ -132,9 +136,9 @@ class ExternalModules } curl_setopt($curl, CURLOPT_URL, $url); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $httpheader); - curl_setopt($curl, CURLOPT_HEADER, 1); + curl_setopt($curl, CURLOPT_HEADER, true); $response = curl_exec($curl); @@ -171,7 +175,7 @@ class ExternalModules 'lang' => $this->lang ]; - $resCategories = $this->callAPI('categories', $data); + $resCategories = $this->callApi('categories', $data); if (isset($resCategories['response']) && is_array($resCategories['response'])) { $organized_tree = $resCategories['response']; } else { @@ -203,8 +207,8 @@ class ExternalModules /** * Generate HTML for products. - * @param array $options Options for the request - * @return string HTML string representing the products. + * @param array $options Options for the request + * @return string|null HTML string representing the products. */ public function getProducts($options) { @@ -233,7 +237,7 @@ class ExternalModules 'lang' => $this->lang ]; - $resProducts = $this->callAPI('products', $data); + $resProducts = $this->callApi('products', $data); if (!isset($resProducts['response']) || !is_array($resProducts['response']) || ($resProducts['status_code'] != 200 && $resProducts['status_code'] != 201)) { $html = $this->checkStatusCode($resProducts); @@ -482,11 +486,11 @@ class ExternalModules * * @param array{status_code:int,response:?string,header:string} $request Response elements of CURL request * - * @return void - * @throws Exception if HTTP status code is not 200 or 201 + * @return string|null */ protected function checkStatusCode($request) { + $error_message = ''; switch ($request['status_code']) { case 200: case 201: From d15e265e56e2d60c5bf2b249a2f0475afde6f64a Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Thu, 20 Feb 2025 14:58:06 +0100 Subject: [PATCH 04/11] fix phan errors --- htdocs/admin/modules.php | 11 ++++++----- .../admin/remotestore/class/externalModules.class.php | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/htdocs/admin/modules.php b/htdocs/admin/modules.php index 779d29dd73a..e6efa6a1d85 100644 --- a/htdocs/admin/modules.php +++ b/htdocs/admin/modules.php @@ -82,11 +82,12 @@ $search_version = GETPOST('search_version', 'alpha'); // For remotestore search -$options = array(); -$options['per_page'] = 10; -$options['no_page'] = ((int) GETPOSTINT('no_page') ? GETPOSTINT('no_page') : 1); -$options['categorie'] = ((int) (GETPOSTINT('categorie') ? GETPOSTINT('categorie') : 0)); -$options['search'] = GETPOST('search_keyword', 'alpha'); +$options = array(); +$options['per_page'] = 10; +$options['no_page'] = ((int) GETPOSTINT('no_page') ? GETPOSTINT('no_page') : 1); +$options['categorie'] = ((int) (GETPOSTINT('categorie') ? GETPOSTINT('categorie') : 0)); +$options['search'] = GETPOST('search_keyword', 'alpha'); +$options['source'] = 'dolistore'; //$remotestore = new Dolistore(false); $remotestore = new ExternalModules(); diff --git a/htdocs/admin/remotestore/class/externalModules.class.php b/htdocs/admin/remotestore/class/externalModules.class.php index fc449022507..8ac467fb5e5 100644 --- a/htdocs/admin/remotestore/class/externalModules.class.php +++ b/htdocs/admin/remotestore/class/externalModules.class.php @@ -52,7 +52,7 @@ class ExternalModules */ public $shop_url; // the url of the shop /** - * @var int + * @var string */ public $lang; // the integer representing the lang in the store /** @@ -362,7 +362,7 @@ class ExternalModules */ public function buildSorter($key) { - return function ($a, $b) use ($key) { + return function (array $a, array $b) use ($key): int { return strnatcmp($a[$key], $b[$key]); }; } @@ -517,7 +517,7 @@ class ExternalModules return 'This call to the API returned an unexpected HTTP status of: ' . $request['status_code']; } - if (!empty($error_message)) { + if (!empty($error_message) && isset($error_message)) { $response = $request['response']; if (isset($response['errors']) && is_array($response['errors'])) { foreach ($response['errors'] as $error) { From 1bd029b5b354d9752a38b0dc4c102594fb54711b Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Thu, 20 Feb 2025 15:16:53 +0100 Subject: [PATCH 05/11] fix Phan --- htdocs/admin/modules.php | 2 +- htdocs/admin/remotestore/class/externalModules.class.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/htdocs/admin/modules.php b/htdocs/admin/modules.php index e6efa6a1d85..574ca3500d0 100644 --- a/htdocs/admin/modules.php +++ b/htdocs/admin/modules.php @@ -87,7 +87,7 @@ $options['per_page'] = 10; $options['no_page'] = ((int) GETPOSTINT('no_page') ? GETPOSTINT('no_page') : 1); $options['categorie'] = ((int) (GETPOSTINT('categorie') ? GETPOSTINT('categorie') : 0)); $options['search'] = GETPOST('search_keyword', 'alpha'); -$options['source'] = 'dolistore'; +$options['source'] = GETPOST('source', 'alpha'); //$remotestore = new Dolistore(false); $remotestore = new ExternalModules(); diff --git a/htdocs/admin/remotestore/class/externalModules.class.php b/htdocs/admin/remotestore/class/externalModules.class.php index 8ac467fb5e5..a1c3e761bef 100644 --- a/htdocs/admin/remotestore/class/externalModules.class.php +++ b/htdocs/admin/remotestore/class/externalModules.class.php @@ -363,7 +363,7 @@ class ExternalModules public function buildSorter($key) { return function (array $a, array $b) use ($key): int { - return strnatcmp($a[$key], $b[$key]); + return strnatcmp((string) $a[$key], (string) $b[$key]); }; } @@ -490,7 +490,7 @@ class ExternalModules */ protected function checkStatusCode($request) { - $error_message = ''; + $error_message = ""; switch ($request['status_code']) { case 200: case 201: @@ -517,7 +517,7 @@ class ExternalModules return 'This call to the API returned an unexpected HTTP status of: ' . $request['status_code']; } - if (!empty($error_message) && isset($error_message)) { + if ($error_message != "") { $response = $request['response']; if (isset($response['errors']) && is_array($response['errors'])) { foreach ($response['errors'] as $error) { From 06e237640e85873a179f25cb64a48410307c115b Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Thu, 20 Feb 2025 15:58:34 +0100 Subject: [PATCH 06/11] FIX CI --- .../remotestore/class/externalModules.class.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/htdocs/admin/remotestore/class/externalModules.class.php b/htdocs/admin/remotestore/class/externalModules.class.php index a1c3e761bef..d275dba9a5d 100644 --- a/htdocs/admin/remotestore/class/externalModules.class.php +++ b/htdocs/admin/remotestore/class/externalModules.class.php @@ -358,9 +358,9 @@ class ExternalModules * Sort an array by a key * @param string $key Key to sort by * - * @return Closure + * @return Closure(array, array): int */ - public function buildSorter($key) + public function buildSorter(string $key): Closure { return function (array $a, array $b) use ($key): int { return strnatcmp((string) $a[$key], (string) $b[$key]); @@ -478,11 +478,7 @@ class ExternalModules } /** - * Take the status code and throw an exception if the server didn't return 200 or 201 code - *

    Unique parameter must take :

    - * 'status_code' => Status code of an HTTP return
    - * 'response' => CURL response - *

    + * Check the status code of the request * * @param array{status_code:int,response:?string,header:string} $request Response elements of CURL request * @@ -494,7 +490,7 @@ class ExternalModules switch ($request['status_code']) { case 200: case 201: - return; + return ''; case 204: $error_message = 'No content'; break; @@ -517,7 +513,7 @@ class ExternalModules return 'This call to the API returned an unexpected HTTP status of: ' . $request['status_code']; } - if ($error_message != "") { + if ($error_message !== "") { $response = $request['response']; if (isset($response['errors']) && is_array($response['errors'])) { foreach ($response['errors'] as $error) { From 7d6ce091464a1a333037745a9990f898a71d2f2e Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Thu, 20 Feb 2025 16:17:33 +0100 Subject: [PATCH 07/11] fix CI --- .../class/externalModules.class.php | 58 ++++++++----------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/htdocs/admin/remotestore/class/externalModules.class.php b/htdocs/admin/remotestore/class/externalModules.class.php index d275dba9a5d..46af97ced63 100644 --- a/htdocs/admin/remotestore/class/externalModules.class.php +++ b/htdocs/admin/remotestore/class/externalModules.class.php @@ -363,6 +363,8 @@ class ExternalModules public function buildSorter(string $key): Closure { return function (array $a, array $b) use ($key): int { + /** @var array $a */ + /** @var array $b */ return strnatcmp((string) $a[$key], (string) $b[$key]); }; } @@ -486,42 +488,32 @@ class ExternalModules */ protected function checkStatusCode($request) { - $error_message = ""; - switch ($request['status_code']) { - case 200: - case 201: - return ''; - case 204: - $error_message = 'No content'; - break; - case 400: - $error_message = 'Bad Request'; - break; - case 401: - $error_message = 'Unauthorized'; - break; - case 404: - $error_message = 'Not Found'; - break; - case 405: - $error_message = 'Method Not Allowed'; - break; - case 500: - $error_message = 'Internal Server Error'; - break; - default: - return 'This call to the API returned an unexpected HTTP status of: ' . $request['status_code']; + // Define error messages + $error_messages = [ + 204 => 'No content', + 400 => 'Bad Request', + 401 => 'Unauthorized', + 404 => 'Not Found', + 405 => 'Method Not Allowed', + 500 => 'Internal Server Error', + ]; + + // If status code is 200 or 201, return an empty string + if ($request['status_code'] === 200 || $request['status_code'] === 201) { + return ''; } - if ($error_message !== "") { - $response = $request['response']; - if (isset($response['errors']) && is_array($response['errors'])) { - foreach ($response['errors'] as $error) { - $error_message .= ' - (Code ' . $error['code'] . '): ' . $error['message']; - } + // Get the predefined error message or use a default one + $error_message = $error_messages[$request['status_code']] ?? 'Unexpected HTTP status: ' . $request['status_code']; + + // Append error details if available + if (!empty($request['response']) && isset($request['response']['errors']) && is_array($request['response']['errors'])) { + foreach ($request['response']['errors'] as $error) { + $error_message .= ' - (Code ' . $error['code'] . '): ' . $error['message']; } - $error_label = 'This call to the API failed and returned an HTTP status of %d. That means: %s.'; - return sprintf($error_label, $request['status_code'], $error_message); } + + // Return the formatted error message + return sprintf('This call to the API failed and returned an HTTP status of %d. That means: %s.', $request['status_code'], $error_message); } } From 7e13d7fe52fb34566d189b7277ac5471bae04f53 Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Thu, 20 Feb 2025 16:33:32 +0100 Subject: [PATCH 08/11] Fix CI --- .../remotestore/class/externalModules.class.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/htdocs/admin/remotestore/class/externalModules.class.php b/htdocs/admin/remotestore/class/externalModules.class.php index 46af97ced63..f2a36cbc3d0 100644 --- a/htdocs/admin/remotestore/class/externalModules.class.php +++ b/htdocs/admin/remotestore/class/externalModules.class.php @@ -363,9 +363,16 @@ class ExternalModules public function buildSorter(string $key): Closure { return function (array $a, array $b) use ($key): int { - /** @var array $a */ - /** @var array $b */ - return strnatcmp((string) $a[$key], (string) $b[$key]); + if (!isset($a[$key]) || !isset($b[$key])) { + return 0; + } + + /** @var string $valA */ + $valA = is_scalar($a[$key]) ? (string) $a[$key] : ''; + /** @var string $valB */ + $valB = is_scalar($b[$key]) ? (string) $b[$key] : ''; + + return strnatcmp($valA, $valB); }; } From ad646e441c61296fe7293b65e69490e29a35ac22 Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Thu, 20 Feb 2025 16:42:17 +0100 Subject: [PATCH 09/11] Fix CI --- htdocs/admin/remotestore/class/externalModules.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/htdocs/admin/remotestore/class/externalModules.class.php b/htdocs/admin/remotestore/class/externalModules.class.php index f2a36cbc3d0..a661a5ace6c 100644 --- a/htdocs/admin/remotestore/class/externalModules.class.php +++ b/htdocs/admin/remotestore/class/externalModules.class.php @@ -359,6 +359,8 @@ class ExternalModules * @param string $key Key to sort by * * @return Closure(array, array): int + * @suppress PhanPluginUnknownArrayClosureParamType + * @suppress PhanTypeMismatchDimFetch */ public function buildSorter(string $key): Closure { From b3e7c0dcd0495a602600385609896c6ff03489f9 Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Thu, 20 Feb 2025 17:14:36 +0100 Subject: [PATCH 10/11] fix CI --- .../remotestore/class/externalModules.class.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/htdocs/admin/remotestore/class/externalModules.class.php b/htdocs/admin/remotestore/class/externalModules.class.php index a661a5ace6c..0a0140c69f8 100644 --- a/htdocs/admin/remotestore/class/externalModules.class.php +++ b/htdocs/admin/remotestore/class/externalModules.class.php @@ -359,20 +359,18 @@ class ExternalModules * @param string $key Key to sort by * * @return Closure(array, array): int - * @suppress PhanPluginUnknownArrayClosureParamType - * @suppress PhanTypeMismatchDimFetch */ public function buildSorter(string $key): Closure { return function (array $a, array $b) use ($key): int { - if (!isset($a[$key]) || !isset($b[$key])) { - return 0; - } + /** @var array $a */ + /** @var array $b */ - /** @var string $valA */ - $valA = is_scalar($a[$key]) ? (string) $a[$key] : ''; - /** @var string $valB */ - $valB = is_scalar($b[$key]) ? (string) $b[$key] : ''; + /** @phan-suppress PhanPluginUnknownArrayClosureParamType */ + /** @phan-suppress PhanTypeMismatchDimFetch */ + + $valA = isset($a[$key]) && is_scalar($a[$key]) ? (string) $a[$key] : ''; + $valB = isset($b[$key]) && is_scalar($b[$key]) ? (string) $b[$key] : ''; return strnatcmp($valA, $valB); }; From 6328f056ae3aef8c3b936591f234cf1db8dd6af4 Mon Sep 17 00:00:00 2001 From: Mohamed DAOUD Date: Thu, 20 Feb 2025 17:46:25 +0100 Subject: [PATCH 11/11] Fix CI --- .../remotestore/class/externalModules.class.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/htdocs/admin/remotestore/class/externalModules.class.php b/htdocs/admin/remotestore/class/externalModules.class.php index 0a0140c69f8..beb31d32c46 100644 --- a/htdocs/admin/remotestore/class/externalModules.class.php +++ b/htdocs/admin/remotestore/class/externalModules.class.php @@ -362,13 +362,13 @@ class ExternalModules */ public function buildSorter(string $key): Closure { - return function (array $a, array $b) use ($key): int { - /** @var array $a */ - /** @var array $b */ - - /** @phan-suppress PhanPluginUnknownArrayClosureParamType */ - /** @phan-suppress PhanTypeMismatchDimFetch */ - + return + /** + * @param array $a + * @param array $b + * @return int + */ + function (array $a, array $b) use ($key) { $valA = isset($a[$key]) && is_scalar($a[$key]) ? (string) $a[$key] : ''; $valB = isset($b[$key]) && is_scalar($b[$key]) ? (string) $b[$key] : ''; @@ -489,7 +489,7 @@ class ExternalModules /** * Check the status code of the request * - * @param array{status_code:int,response:?string,header:string} $request Response elements of CURL request + * @param array{status_code:int,response:null|string|array{errors:array{code:int,message:string}[]},header:string} $request Response elements of CURL request * * @return string|null */