From 6abec99fd39fb1cfebace8afe176bb21ac0e94ae Mon Sep 17 00:00:00 2001 From: dMetzger Date: Mon, 6 Aug 2012 13:32:26 +0000 Subject: [PATCH] [-] BO : moved some code from AdminModulesController to AdminController because is is used on AdminHomeController --- classes/controller/AdminController.php | 88 ++++++++++++++++++ controllers/admin/AdminHomeController.php | 3 + controllers/admin/AdminModulesController.php | 98 +------------------- 3 files changed, 92 insertions(+), 97 deletions(-) diff --git a/classes/controller/AdminController.php b/classes/controller/AdminController.php index 1e3c2c443..7726775a6 100644 --- a/classes/controller/AdminController.php +++ b/classes/controller/AdminController.php @@ -34,6 +34,8 @@ class AdminControllerCore extends Controller public $informations = array(); public $confirmations = array(); public $shopShareDatas = false; + + protected $addons_url = 'api.addons.prestashop.com'; public $_languages = array(); public $default_form_language; @@ -2665,5 +2667,91 @@ class AdminControllerCore extends Controller if ($this->status === '') $this->status = 'error'; } + + public function isFresh($file, $timeout = 604800000) + { + if (file_exists(_PS_ROOT_DIR_.$file)) + { + if (filesize(_PS_ROOT_DIR_.$file) < 1) + return false; + return ((time() - filemtime(_PS_ROOT_DIR_.$file)) < $timeout); + } + else + return false; + } + + public function refresh($file_to_refresh, $external_file) + { + $content = Tools::file_get_contents($external_file); + if ($content) + return file_put_contents(_PS_ROOT_DIR_.$file_to_refresh, $content); + return false; + } + + public function addonsRequest($request, $params = array()) + { + // Config for each request + if ($request == 'native') + { + // Define protocol accepted and post data values for this request + $protocolsList = array('https://' => 443, 'http://' => 80); + $postData = 'version='._PS_VERSION_.'&method=listing&action=native&iso_code='.strtolower(Configuration::get('PS_LOCALE_COUNTRY')).'&iso_lang='.strtolower(Context::getContext()->language->iso_code); + } + if ($request == 'must-have') + { + // Define protocol accepted and post data values for this request + $protocolsList = array('https://' => 443, 'http://' => 80); + $postData = 'version='._PS_VERSION_.'&method=listing&action=must-have&iso_code='.strtolower(Configuration::get('PS_LOCALE_COUNTRY')).'&iso_lang='.strtolower(Context::getContext()->language->iso_code); + } + if ($request == 'customer') + { + // Define protocol accepted and post data values for this request + $protocolsList = array('https://' => 443); + $postData = 'version='._PS_VERSION_.'&method=listing&action=customer&username='.pSQL(trim($this->context->cookie->username_addons)).'&password='.pSQL(trim($this->context->cookie->password_addons)).'&iso_lang='.strtolower(Context::getContext()->language->iso_code); + } + if ($request == 'check_customer') + { + // Define protocol accepted and post data values for this request + $protocolsList = array('https://' => 443); + $postData = 'version='._PS_VERSION_.'&method=check_customer&username='.pSQL($params['username_addons']).'&password='.pSQL($params['password_addons']); + } + if ($request == 'module') + { + // Define protocol accepted and post data values for this request + if (isset($params['username_addons']) && isset($params['password_addons'])) + { + $protocolsList = array('https://' => 443); + $postData = 'version='._PS_VERSION_.'&method=module&id_module='.pSQL($params['id_module']).'&username='.pSQL($params['username_addons']).'&password='.pSQL($params['password_addons']); + } + else + { + $protocolsList = array('https://' => 443, 'http://' => 80); + $postData = 'version='._PS_VERSION_.'&method=module&id_module='.pSQL($params['id_module']); + } + } + + + // Make the request + $opts = array( + 'http'=>array( + 'method'=> 'POST', + 'content' => $postData, + 'header' => 'Content-type: application/x-www-form-urlencoded', + 'timeout' => 5, + ) + ); + $context = stream_context_create($opts); + foreach ($protocolsList as $protocol => $port) + { + $content = file_get_contents($protocol.$this->addons_url, false, $context); + + // If content returned, we cache it + if ($content) + return $content; + } + + // No content, return false + return false; + } } diff --git a/controllers/admin/AdminHomeController.php b/controllers/admin/AdminHomeController.php index b00a0090a..df52aea06 100644 --- a/controllers/admin/AdminHomeController.php +++ b/controllers/admin/AdminHomeController.php @@ -658,6 +658,9 @@ class AdminHomeControllerCore extends AdminController if (Configuration::get('PS_LAST_VERSION_CHECK') < time() - (3600 * Upgrader::DEFAULT_CHECK_VERSION_DELAY_HOURS)) $tpl_vars['refresh_check_version'] = 1; } + + if (!$this->isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, 86400)) + file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, $this->addonsRequest('native')); $tpl_vars['upgrade'] = $upgrade; diff --git a/controllers/admin/AdminModulesController.php b/controllers/admin/AdminModulesController.php index c0a730a45..db9ca1d20 100644 --- a/controllers/admin/AdminModulesController.php +++ b/controllers/admin/AdminModulesController.php @@ -55,7 +55,6 @@ class AdminModulesControllerCore extends AdminController protected $filter_configuration = array(); protected $xml_modules_list = 'api.prestashop.com/xml/modules_list_15.xml'; - protected $addons_url = 'api.addons.prestashop.com'; protected $logged_on_addons = false; /** @@ -129,101 +128,6 @@ class AdminModulesControllerCore extends AdminController $this->logged_on_addons = true; } - /** - * Ajax Request Methods - * - * if modules_list.xml is outdated, - * this function will re-upload it from prestashop.com - * - * @return null - */ - - public function isFresh($file, $timeout = 604800000) - { - if (file_exists(_PS_ROOT_DIR_.$file)) - { - if (filesize(_PS_ROOT_DIR_.$file) < 1) - return false; - return ((time() - filemtime(_PS_ROOT_DIR_.$file)) < $timeout); - } - else - return false; - } - - public function refresh($file_to_refresh, $external_file) - { - $content = Tools::file_get_contents($external_file); - if ($content) - return file_put_contents(_PS_ROOT_DIR_.$file_to_refresh, $content); - return false; - } - - public function addonsRequest($request, $params = array()) - { - // Config for each request - if ($request == 'native') - { - // Define protocol accepted and post data values for this request - $protocolsList = array('https://' => 443, 'http://' => 80); - $postData = 'version='._PS_VERSION_.'&method=listing&action=native&iso_code='.strtolower(Configuration::get('PS_LOCALE_COUNTRY')).'&iso_lang='.strtolower(Context::getContext()->language->iso_code); - } - if ($request == 'must-have') - { - // Define protocol accepted and post data values for this request - $protocolsList = array('https://' => 443, 'http://' => 80); - $postData = 'version='._PS_VERSION_.'&method=listing&action=must-have&iso_code='.strtolower(Configuration::get('PS_LOCALE_COUNTRY')).'&iso_lang='.strtolower(Context::getContext()->language->iso_code); - } - if ($request == 'customer') - { - // Define protocol accepted and post data values for this request - $protocolsList = array('https://' => 443); - $postData = 'version='._PS_VERSION_.'&method=listing&action=customer&username='.pSQL(trim($this->context->cookie->username_addons)).'&password='.pSQL(trim($this->context->cookie->password_addons)).'&iso_lang='.strtolower(Context::getContext()->language->iso_code); - } - if ($request == 'check_customer') - { - // Define protocol accepted and post data values for this request - $protocolsList = array('https://' => 443); - $postData = 'version='._PS_VERSION_.'&method=check_customer&username='.pSQL($params['username_addons']).'&password='.pSQL($params['password_addons']); - } - if ($request == 'module') - { - // Define protocol accepted and post data values for this request - if (isset($params['username_addons']) && isset($params['password_addons'])) - { - $protocolsList = array('https://' => 443); - $postData = 'version='._PS_VERSION_.'&method=module&id_module='.pSQL($params['id_module']).'&username='.pSQL($params['username_addons']).'&password='.pSQL($params['password_addons']); - } - else - { - $protocolsList = array('https://' => 443, 'http://' => 80); - $postData = 'version='._PS_VERSION_.'&method=module&id_module='.pSQL($params['id_module']); - } - } - - - // Make the request - $opts = array( - 'http'=>array( - 'method'=> 'POST', - 'content' => $postData, - 'header' => 'Content-type: application/x-www-form-urlencoded', - 'timeout' => 5, - ) - ); - $context = stream_context_create($opts); - foreach ($protocolsList as $protocol => $port) - { - $content = @file_get_contents($protocol.$this->addons_url, false, $context); - - // If content returned, we cache it - if ($content) - return $content; - } - - // No content, return false - return false; - } - public function ajaxProcessRefreshModuleList() { // Refresh modules_list.xml every week @@ -231,7 +135,7 @@ class AdminModulesControllerCore extends AdminController { if ($this->refresh(Module::CACHE_FILE_MODULES_LIST, 'https://'.$this->xml_modules_list)) $this->status = 'refresh'; - else if ($this->refresh(Module::CACHE_FILE_MODULES_LIST, 'http://'.$this->xml_modules_list)) + elseif ($this->refresh(Module::CACHE_FILE_MODULES_LIST, 'http://'.$this->xml_modules_list)) $this->status = 'refresh'; else $this->status = 'error';