From f0d9b8417911e6aac61ce65b5765ca51a0274c0b Mon Sep 17 00:00:00 2001 From: Hlavtox Date: Fri, 5 Jan 2024 13:09:16 +0100 Subject: [PATCH] Add controller for CRON tasks --- controllers/front/cron.php | 49 ++++++++++++++++++++++++ controllers/front/index.php | 34 +++++++++++++++++ controllers/index.php | 34 +++++++++++++++++ gsitemap-cron.php | 8 +++- gsitemap.php | 76 +++++++++++++++++-------------------- 5 files changed, 158 insertions(+), 43 deletions(-) create mode 100644 controllers/front/cron.php create mode 100644 controllers/front/index.php create mode 100644 controllers/index.php diff --git a/controllers/front/cron.php b/controllers/front/cron.php new file mode 100644 index 0000000..1f7a06c --- /dev/null +++ b/controllers/front/cron.php @@ -0,0 +1,49 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ +class GsitemapCronModuleFrontController extends ModuleFrontController +{ + public function postProcess() + { + // Check correct token when not in CLI context + if (!Tools::isPHPCLI() && Tools::substr(Tools::hash('gsitemap/cron'), 0, 10) != Tools::getValue('token')) { + exit('Bad token'); + } + + // Check if the requested shop exists + $shops = Db::getInstance()->ExecuteS('SELECT id_shop FROM `' . _DB_PREFIX_ . 'shop`'); + $list_id_shop = []; + foreach ($shops as $shop) { + $list_id_shop[] = (int) $shop['id_shop']; + } + + $id_shop = (Tools::getIsset('id_shop') && in_array(Tools::getValue('id_shop'), $list_id_shop)) ? (int) Tools::getValue('id_shop') : (int) Configuration::get('PS_SHOP_DEFAULT'); + + // Mark a flag that we are in cron context + $this->module->cron = true; + + // If this is the first request to generate, we delete all previous sitemaps + if (!Tools::getIsset('continue')) { + $this->module->emptySitemap((int) $id_shop); + } + + // Run generation + $this->module->createSitemap((int) $id_shop); + } +} diff --git a/controllers/front/index.php b/controllers/front/index.php new file mode 100644 index 0000000..45df26c --- /dev/null +++ b/controllers/front/index.php @@ -0,0 +1,34 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + */ +header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); +header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + +header('Cache-Control: no-store, no-cache, must-revalidate'); +header('Cache-Control: post-check=0, pre-check=0', false); +header('Pragma: no-cache'); + +header('Location: ../'); +exit; diff --git a/controllers/index.php b/controllers/index.php new file mode 100644 index 0000000..45df26c --- /dev/null +++ b/controllers/index.php @@ -0,0 +1,34 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + */ +header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); +header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + +header('Cache-Control: no-store, no-cache, must-revalidate'); +header('Cache-Control: post-check=0, pre-check=0', false); +header('Pragma: no-cache'); + +header('Location: ../'); +exit; diff --git a/gsitemap-cron.php b/gsitemap-cron.php index 2185b55..97bfb3d 100644 --- a/gsitemap-cron.php +++ b/gsitemap-cron.php @@ -24,9 +24,13 @@ * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) */ -/* - * This file can be called using a cron to generate Google sitemap files automatically +/** + * This file can be called using a cron to generate Google sitemap files automatically. + * + * @deprecated This file should no longer be used and will be removed in 5.0.0 version of this module. + * Please update your cron tasks to use the new URL available in module settings. */ +trigger_error('This file should no longer be used and will be removed in 5.0.0 version of this module. Please update your cron tasks to use the new URL available in module settings.', E_USER_NOTICE); include dirname(__FILE__) . '/../../config/config.inc.php'; diff --git a/gsitemap.php b/gsitemap.php index 36d22f6..84fa70b 100755 --- a/gsitemap.php +++ b/gsitemap.php @@ -111,6 +111,7 @@ public function install() 'GSITEMAP_PRIORITY_CMS' => 0.7, 'GSITEMAP_FREQUENCY' => 'weekly', 'GSITEMAP_LAST_EXPORT' => false, + 'GSITEMAP_DISABLE_LINKS' => '', ] as $key => $val) { if (!Configuration::updateValue($key, $val)) { return false; @@ -154,15 +155,16 @@ protected function installHook() public function uninstall() { foreach ([ - 'GSITEMAP_PRIORITY_HOME' => '', - 'GSITEMAP_PRIORITY_PRODUCT' => '', - 'GSITEMAP_PRIORITY_CATEGORY' => '', - 'GSITEMAP_PRIORITY_MANUFACTURER' => '', - 'GSITEMAP_PRIORITY_CMS' => '', - 'GSITEMAP_FREQUENCY' => '', - 'GSITEMAP_LAST_EXPORT' => '', - ] as $key => $val) { - if (!Configuration::deleteByName($key)) { + 'GSITEMAP_PRIORITY_HOME', + 'GSITEMAP_PRIORITY_PRODUCT', + 'GSITEMAP_PRIORITY_CATEGORY', + 'GSITEMAP_PRIORITY_MANUFACTURER', + 'GSITEMAP_PRIORITY_CMS', + 'GSITEMAP_FREQUENCY', + 'GSITEMAP_LAST_EXPORT', + 'GSITEMAP_DISABLE_LINKS', + ] as $configurationKey) { + if (!Configuration::deleteByName($configurationKey)) { return false; } } @@ -197,7 +199,6 @@ public function getContent() /* Store the posted parameters and generate a new Google sitemap files for the current Shop */ if (Tools::isSubmit('SubmitGsitemap')) { Configuration::updateValue('GSITEMAP_FREQUENCY', pSQL(Tools::getValue('gsitemap_frequency'))); - Configuration::updateValue('GSITEMAP_INDEX_CHECK', ''); $meta = ''; if (Tools::getValue('gsitemap_meta')) { $meta .= implode(', ', Tools::getValue('gsitemap_meta')); @@ -227,7 +228,14 @@ function ($meta) { $store_url = $this->context->link->getBaseLink(); $this->context->smarty->assign([ 'gsitemap_form' => './index.php?controller=AdminModules&configure=gsitemap&token=' . Tools::getAdminTokenLite('AdminModules') . '&tab_module=' . $this->tab . '&module_name=gsitemap', - 'gsitemap_cron' => $store_url . 'modules/gsitemap/gsitemap-cron.php?token=' . Tools::substr(Tools::hash('gsitemap/cron'), 0, 10) . '&id_shop=' . $this->context->shop->id, + 'gsitemap_cron' => $this->context->link->getModuleLink( + 'gsitemap', + 'cron', + [ + 'token' => Tools::substr(Tools::hash('gsitemap/cron'), 0, 10), + 'id_shop' => $this->context->shop->id, + ] + ), 'gsitemap_feed_exists' => file_exists($this->normalizeDirectory(_PS_ROOT_DIR_) . 'index_sitemap.xml'), 'gsitemap_last_export' => Configuration::get('GSITEMAP_LAST_EXPORT'), 'gsitemap_frequency' => Configuration::get('GSITEMAP_FREQUENCY'), @@ -313,7 +321,19 @@ public function addLinkToSitemap(&$link_sitemap, $new_link, $lang, &$index, &$i, exit(); } else { if ($this->cron) { - Tools::redirect($this->context->link->getBaseLink() . 'modules/gsitemap/gsitemap-cron.php?continue=1&token=' . Tools::substr(Tools::hash('gsitemap/cron'), 0, 10) . '&type=' . $new_link['type'] . '&lang=' . $lang . '&index=' . $index . '&id=' . (int) $id_obj . '&id_shop=' . $this->context->shop->id); + Tools::redirect($this->context->link->getModuleLink( + 'gsitemap', + 'cron', + [ + 'continue' => '1', + 'token' => Tools::substr(Tools::hash('gsitemap/cron'), 0, 10), + 'type' => $new_link['type'], + 'lang' => $lang, + 'index' => $index, + 'id' => (int) $id_obj, + 'id_shop' => $this->context->shop->id, + ] + )); } else { Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true, [], [ 'tab_module' => $this->tab, @@ -740,7 +760,6 @@ public function createSitemap($id_shop = 0) } } $this->recursiveSitemapCreator($link_sitemap, $lang['iso_code'], $index); - $page = ''; $index = 0; } @@ -788,7 +807,7 @@ protected function recursiveSitemapCreator($link_sitemap, $lang, &$index) $write_fd = fopen($this->normalizeDirectory(_PS_ROOT_DIR_) . $sitemap_link, 'wb'); fwrite($write_fd, '' . PHP_EOL . '' . PHP_EOL); - foreach ($link_sitemap as $key => $file) { + foreach ($link_sitemap as $file) { fwrite($write_fd, '' . PHP_EOL); $lastmod = (isset($file['lastmod']) && !empty($file['lastmod'])) ? date('c', strtotime($file['lastmod'])) : null; $this->addSitemapNode($write_fd, htmlspecialchars(strip_tags($file['link'])), $this->getPriorityPage($file['page']), Configuration::get('GSITEMAP_FREQUENCY'), $lastmod); @@ -822,7 +841,8 @@ protected function recursiveSitemapCreator($link_sitemap, $lang, &$index) } /** - * return the priority value set in the configuration parameters + * Returns the priority value set in the configuration parameters. + * Falls back to 0.1 for things that don't have priority. * * @param string $page * @@ -880,32 +900,6 @@ protected function createIndexSitemap() return true; } - protected function tableColumnExists($table_name, $column = null) - { - if (array_key_exists($table_name, $this->sql_checks)) { - if (!empty($column) && array_key_exists($column, $this->sql_checks[$table_name])) { - return $this->sql_checks[$table_name][$column]; - } else { - return $this->sql_checks[$table_name]; - } - } - - $table = Db::getInstance()->ExecuteS('SHOW TABLES LIKE \'' . $table_name . '\''); - if (empty($column)) { - if (count($table) < 1) { - return $this->sql_checks[$table_name] = false; - } else { - $this->sql_checks[$table_name] = true; - } - } else { - $table = Db::getInstance()->ExecuteS('SELECT * FROM `' . $table_name . '` LIMIT 1'); - - return $this->sql_checks[$table_name][$column] = array_key_exists($column, current($table)); - } - - return true; - } - protected function normalizeDirectory($directory) { $last = $directory[Tools::strlen($directory) - 1];