diff --git a/classes/Tools.php b/classes/Tools.php index 86bc7c1f8..8212ec4b0 100644 --- a/classes/Tools.php +++ b/classes/Tools.php @@ -3312,6 +3312,20 @@ exit; return preg_replace('/\\\[px]\{[a-z]{1,2}\}|(\/[a-z]*)u([a-z]*)$/i', '$1$2', $pattern); } + /** + * @param string $file + * @param int $timeout + * @return bool + */ + public static function isFresh($file, $timeout = _TIME_1_WEEK_) + { + if (($time = @filemtime(_PS_ROOT_DIR_.$file)) && filesize(_PS_ROOT_DIR_.$file) > 0) { + return ((time() - $time) < $timeout); + } + + return false; + } + protected static $is_addons_up = true; public static function addonsRequest($request, $params = array()) { @@ -3421,6 +3435,20 @@ exit; return false; } + /** + * @param string $file_to_refresh + * @param string $external_file + * @return bool + */ + public static function refresh($file_to_refresh, $external_file) + { + if (self::$is_addons_up && $content = Tools::file_get_contents($external_file)) { + return (bool)file_put_contents(_PS_ROOT_DIR_.$file_to_refresh, $content); + } + self::$is_addons_up = false; + return false; + } + /** * Returns an array containing information about * HTTP file upload variable ($_FILES) diff --git a/classes/controller/AdminController.php b/classes/controller/AdminController.php index f8d893e5c..55b42821b 100644 --- a/classes/controller/AdminController.php +++ b/classes/controller/AdminController.php @@ -2214,36 +2214,7 @@ class AdminControllerCore extends Controller public function ajaxProcessRefreshModuleList($force_reload_cache = false) { - if (!$this->isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) { - if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, Tools::addonsRequest('native'))) { - $this->status = 'refresh'; - } else { - $this->status = 'error'; - } - } else { - $this->status = 'cache'; - } - - if (!$this->isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) { - if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, Tools::addonsRequest('must-have'))) { - $this->status = 'refresh'; - } else { - $this->status = 'error'; - } - } else { - $this->status = 'cache'; - } - - if (!$this->isFresh(Module::CACHE_FILE_TAB_MODULES_LIST, _TIME_1_WEEK_)) { - if ($this->refresh(Module::CACHE_FILE_TAB_MODULES_LIST, _QLO_TAB_MODULE_LIST_URL_)) { - $this->status = 'refresh'; - } else { - $this->status = 'error'; - } - } else { - $this->status = 'cache'; - } - Module::generateTrustedXml(); + $this->status = Module::refreshModuleList($force_reload_cache); } /** @@ -2835,7 +2806,7 @@ class AdminControllerCore extends Controller )); // get upgrade available info - if (!$this->isFresh(Upgrader::CACHE_FILE_UPGRADE_AVAILABE, _TIME_1_DAY_)) { + if (!Tools::isFresh(Upgrader::CACHE_FILE_UPGRADE_AVAILABE, _TIME_1_DAY_)) { file_put_contents(_PS_ROOT_DIR_.Upgrader::CACHE_FILE_UPGRADE_AVAILABE, Tools::addonsRequest('check-version')); } if (file_exists(_PS_ROOT_DIR_.Upgrader::CACHE_FILE_UPGRADE_AVAILABE)) { @@ -4075,28 +4046,24 @@ class AdminControllerCore extends Controller * @param string $file * @param int $timeout * @return bool + * @deprecated Deprecated 1.6.0.0 Use Tools::isFresh instead */ public function isFresh($file, $timeout = _TIME_1_WEEK_) { - if (($time = @filemtime(_PS_ROOT_DIR_.$file)) && filesize(_PS_ROOT_DIR_.$file) > 0) { - return ((time() - $time) < $timeout); - } - - return false; + Tools::displayAsDeprecated(); + return Tools::isFresh($file, $timeout); } /** * @param string $file_to_refresh * @param string $external_file * @return bool + * @deprecated Deprecated 1.6.0.0 Use Tools::refresh instead */ public function refresh($file_to_refresh, $external_file) { - if (self::$is_qloapps_up && $content = Tools::file_get_contents($external_file)) { - return (bool)file_put_contents(_PS_ROOT_DIR_.$file_to_refresh, $content); - } - self::$is_qloapps_up = false; - return false; + Tools::displayAsDeprecated(); + return Tools::refresh($file_to_refresh, $external_file); } /** diff --git a/classes/module/Module.php b/classes/module/Module.php index 9654684ee..ca26836c3 100644 --- a/classes/module/Module.php +++ b/classes/module/Module.php @@ -1606,6 +1606,7 @@ abstract class ModuleCore array('type' => 'addonsMustHave', 'file' => _PS_ROOT_DIR_.Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, 'loggedOnAddons' => 0), array('type' => 'addonsNative', 'file' => _PS_ROOT_DIR_.Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, 'loggedOnAddons' => 0), ); + $modulesOnDisk = Module::getModulesDirOnDisk(); $module_list = array(); foreach ($files_list as $f) { $file = $f['file']; @@ -1642,18 +1643,21 @@ abstract class ModuleCore $item->url = isset($modaddons->url) ? $modaddons->url : null; $item->is_native = false; - if (isset($modaddons->img)) { - if (!file_exists(_PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg')) { - if (!file_put_contents(_PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg', Tools::file_get_contents($modaddons->img))) { - copy(_PS_IMG_DIR_.'404.gif', _PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg'); + if (!in_array($modaddons->name, $modulesOnDisk)) { + if (isset($modaddons->img)) { + if (!file_exists(_PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg')) { + if (!file_put_contents(_PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg', Tools::file_get_contents($modaddons->img))) { + copy(_PS_IMG_DIR_.'404.gif', _PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg'); + } + } + + if (file_exists(_PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg')) { + $item->image = '../img/tmp/'.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg'; } } - - if (file_exists(_PS_TMP_IMG_DIR_.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg')) { - $item->image = '../img/tmp/'.md5((int)$modaddons->id.'-'.$modaddons->name).'.jpg'; - } } + if ($item->type == 'addonsMustHave') { $item->addons_buy_url = strip_tags((string)$modaddons->url); $prices = (array)$modaddons->price; @@ -1759,6 +1763,42 @@ abstract class ModuleCore return Db::getInstance()->executeS($sql); } + public static function refreshModuleList($force_reload_cache = false) + { + $status = false; + if (!Tools::isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) { + if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, Tools::addonsRequest('native'))) { + $status = 'refresh'; + } else { + $status = 'error'; + } + } else { + $status = 'cache'; + } + + if (!Tools::isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) { + if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, Tools::addonsRequest('must-have'))) { + $status = 'refresh'; + } else { + $status = 'error'; + } + } else { + $status = 'cache'; + } + + if (!Tools::isFresh(Module::CACHE_FILE_TAB_MODULES_LIST, _TIME_1_WEEK_)) { + if (Tools::refresh(Module::CACHE_FILE_TAB_MODULES_LIST, _QLO_TAB_MODULE_LIST_URL_)) { + $status = 'refresh'; + } else { + $status = 'error'; + } + } else { + $status = 'cache'; + } + + return $status; + } + /** * Return if the module is provided by addons.prestashop.com or not * @@ -1841,6 +1881,9 @@ abstract class ModuleCore $trusted = array(); $untrusted = array(); + // before generating list first check if file exists and are updated + Module::refreshModuleList(); + $trusted_modules_xml = array( // _PS_ROOT_DIR_.self::CACHE_FILE_ALL_COUNTRY_MODULES_LIST, _PS_ROOT_DIR_.self::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, diff --git a/controllers/admin/AdminDashboardController.php b/controllers/admin/AdminDashboardController.php index 0529bea1e..8f54e0166 100644 --- a/controllers/admin/AdminDashboardController.php +++ b/controllers/admin/AdminDashboardController.php @@ -398,8 +398,8 @@ class AdminDashboardControllerCore extends AdminController public function ajaxProcessGetBlogRss() { $return = array('has_errors' => false, 'rss' => array()); - if (!$this->isFresh('/config/xml/blog-'.$this->context->language->iso_code.'.xml', _TIME_1_DAY_)) { - if (!$this->refresh('/config/xml/blog-'.$this->context->language->iso_code.'.xml', _PS_API_URL_.'/rss/blog/blog-'.$this->context->language->iso_code.'.xml')) { + if (!Tools::isFresh('/config/xml/blog-'.$this->context->language->iso_code.'.xml', _TIME_1_DAY_)) { + if (!Tools::refresh('/config/xml/blog-'.$this->context->language->iso_code.'.xml', _PS_API_URL_.'/rss/blog/blog-'.$this->context->language->iso_code.'.xml')) { $return['has_errors'] = true; } } @@ -506,7 +506,7 @@ class AdminDashboardControllerCore extends AdminController public function getRecommendationContent() { - if (!$this->isFresh(self::DASHBOARD_RECOMMENDATION_CONTENT, _TIME_1_DAY_)) { + if (!Tools::isFresh(self::DASHBOARD_RECOMMENDATION_CONTENT, _TIME_1_DAY_)) { @file_put_contents(_PS_ROOT_DIR_.self::DASHBOARD_RECOMMENDATION_CONTENT, Tools::addonsRequest('dashboard-recommendation')); } if (file_exists(_PS_ROOT_DIR_.self::DASHBOARD_RECOMMENDATION_CONTENT)) { diff --git a/controllers/admin/AdminModulesCatalogController.php b/controllers/admin/AdminModulesCatalogController.php index 071bb0756..9f1abd920 100644 --- a/controllers/admin/AdminModulesCatalogController.php +++ b/controllers/admin/AdminModulesCatalogController.php @@ -242,7 +242,7 @@ class AdminModulesCatalogControllerCore extends AdminController public function getRecommendationContent() { - if (!$this->isFresh(self::CATALOG_RECOMMENDATION_CONTENT, _TIME_1_DAY_)) { + if (!Tools::isFresh(self::CATALOG_RECOMMENDATION_CONTENT, _TIME_1_DAY_)) { @file_put_contents(_PS_ROOT_DIR_.self::CATALOG_RECOMMENDATION_CONTENT, Tools::addonsRequest('catalog-recommendation')); } if (file_exists(_PS_ROOT_DIR_.self::CATALOG_RECOMMENDATION_CONTENT)) { @@ -311,11 +311,11 @@ class AdminModulesCatalogControllerCore extends AdminController protected function initSuggestedModulesList() { - if (!$this->isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, _TIME_1_DAY_)) { + if (!Tools::isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, _TIME_1_DAY_)) { file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, Tools::addonsRequest('native')); } - if (!$this->isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, _TIME_1_DAY_)) { + if (!Tools::isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, _TIME_1_DAY_)) { @file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, Tools::addonsRequest('must-have')); } } diff --git a/controllers/admin/AdminModulesController.php b/controllers/admin/AdminModulesController.php index c6b5f723d..dee7cfa13 100644 --- a/controllers/admin/AdminModulesController.php +++ b/controllers/admin/AdminModulesController.php @@ -172,11 +172,11 @@ class AdminModulesControllerCore extends AdminController public function ajaxProcessRefreshModuleList($force_reload_cache = false) { // Refresh modules_list.xml every week - if (!$this->isFresh(Module::CACHE_FILE_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) { + if (!Tools::isFresh(Module::CACHE_FILE_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) { $xml_modules_list = _QLO_API_DOMAIN_.'/xml/'.str_replace('.', '', _QLOAPPS_VERSION_).'.xml'; - if ($this->refresh(Module::CACHE_FILE_MODULES_LIST, 'https://'.$xml_modules_list)) { + if (Tools::refresh(Module::CACHE_FILE_MODULES_LIST, 'https://'.$xml_modules_list)) { $this->status = 'refresh'; - } elseif ($this->refresh(Module::CACHE_FILE_MODULES_LIST, 'http://'.$xml_modules_list)) { + } elseif (Tools::refresh(Module::CACHE_FILE_MODULES_LIST, 'http://'.$xml_modules_list)) { $this->status = 'refresh'; } else { $this->status = 'error'; @@ -187,7 +187,7 @@ class AdminModulesControllerCore extends AdminController // If logged to Addons Webservices, refresh default country native modules list every day if ($this->status != 'error') { - if (!$this->isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) { + if (!Tools::isFresh(Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) { if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_DEFAULT_COUNTRY_MODULES_LIST, Tools::addonsRequest('native'))) { $this->status = 'refresh'; } else { @@ -197,7 +197,7 @@ class AdminModulesControllerCore extends AdminController $this->status = 'cache'; } - if (!$this->isFresh(Module::CACHE_FILE_ADDONS_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) { + if (!Tools::isFresh(Module::CACHE_FILE_ADDONS_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) { if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_ADDONS_MODULES_LIST, Tools::addonsRequest('addons-modules'))) { $this->status = 'refresh'; } else { @@ -207,7 +207,7 @@ class AdminModulesControllerCore extends AdminController $this->status = 'cache'; } - if (!$this->isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) { + if (!Tools::isFresh(Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, _TIME_1_DAY_) || $force_reload_cache) { if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_MUST_HAVE_MODULES_LIST, Tools::addonsRequest('must-have'))) { $this->status = 'refresh'; } else { @@ -220,7 +220,7 @@ class AdminModulesControllerCore extends AdminController // If logged to Addons Webservices, refresh customer modules list every 5 minutes if ($this->logged_on_addons && $this->status != 'error') { - if (!$this->isFresh(Module::CACHE_FILE_CUSTOMER_MODULES_LIST, 300) || $force_reload_cache) { + if (!Tools::isFresh(Module::CACHE_FILE_CUSTOMER_MODULES_LIST, 300) || $force_reload_cache) { if (file_put_contents(_PS_ROOT_DIR_.Module::CACHE_FILE_CUSTOMER_MODULES_LIST, Tools::addonsRequest('customer'))) { $this->status = 'refresh'; } else { @@ -230,7 +230,6 @@ class AdminModulesControllerCore extends AdminController $this->status = 'cache'; } } - Module::generateTrustedXml(); } public function displayAjaxRefreshModuleList() diff --git a/controllers/admin/AdminThemesController.php b/controllers/admin/AdminThemesController.php index 98619dedd..eaf7f4c8a 100644 --- a/controllers/admin/AdminThemesController.php +++ b/controllers/admin/AdminThemesController.php @@ -506,7 +506,7 @@ class AdminThemesControllerCore extends AdminController ) { return false; } - if (!$this->isFresh(Theme::CACHE_FILE_CUSTOMER_THEMES_LIST, _TIME_1_DAY_)) { + if (!Tools::isFresh(Theme::CACHE_FILE_CUSTOMER_THEMES_LIST, _TIME_1_DAY_)) { file_put_contents(_PS_ROOT_DIR_.Theme::CACHE_FILE_CUSTOMER_THEMES_LIST, Tools::addonsRequest('customer_themes')); }