Skip to content

Commit

Permalink
Add controller for CRON tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Hlavtox committed Jan 5, 2024
1 parent a25fce1 commit f0d9b84
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 43 deletions.
49 changes: 49 additions & 0 deletions controllers/front/cron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @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);
}
}
34 changes: 34 additions & 0 deletions controllers/front/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @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;
34 changes: 34 additions & 0 deletions controllers/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @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;
8 changes: 6 additions & 2 deletions gsitemap-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
76 changes: 35 additions & 41 deletions gsitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -740,7 +760,6 @@ public function createSitemap($id_shop = 0)
}
}
$this->recursiveSitemapCreator($link_sitemap, $lang['iso_code'], $index);
$page = '';
$index = 0;
}

Expand Down Expand Up @@ -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, '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">' . PHP_EOL);
foreach ($link_sitemap as $key => $file) {
foreach ($link_sitemap as $file) {
fwrite($write_fd, '<url>' . 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);
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit f0d9b84

Please sign in to comment.