Skip to content

Commit

Permalink
Merge pull request #43 from matks/clean
Browse files Browse the repository at this point in the history
Comply to validator.prestashop.com rules
  • Loading branch information
PierreRambaud authored Jul 26, 2019
2 parents 48869f7 + ba92a06 commit bd6c064
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 83 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
composer.lock
vendor/
28 changes: 23 additions & 5 deletions classes/CronJobsForms.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* 2007-2016 PrestaShop
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
Expand All @@ -19,7 +19,7 @@
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
Expand Down Expand Up @@ -279,21 +279,39 @@ public static function getTasksListValues()
{
$id_shop = (int)Context::getContext()->shop->id;
$id_shop_group = (int)Context::getContext()->shop->id_shop_group;
$table_name = _DB_PREFIX_.bqSQL(self::$module->name);

$select_query = sprintf(
"SELECT * FROM `%s` WHERE `id_shop` = '%d' AND `id_shop_group` = '%d'",
$table_name,
$id_shop,
$id_shop_group
);

self::$module->addNewModulesTasks();
$crons = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.bqSQL(self::$module->name).'` WHERE `id_shop` = \''.$id_shop.'\' AND `id_shop_group` = \''.$id_shop_group.'\'');
$crons = Db::getInstance()->executeS($select_query);

foreach ($crons as $key => &$cron) {
if (empty($cron['id_module']) == false) {
$module = Module::getInstanceById((int)$cron['id_module']);

$delete_query = sprintf(
"DELETE FROM `%s` WHERE `id_cronjob` = '%s'",
$table_name,
(int)$cron['id_cronjob']
);

if ($module == false) {
Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.bqSQL(self::$module->name).' WHERE `id_cronjob` = \''.(int)$cron['id_cronjob'].'\'');
Db::getInstance()->execute($delete_query);
unset($crons[$key]);
break;
}

$query = 'SELECT `name` FROM `'._DB_PREFIX_.'module` WHERE `id_module` = \''.(int)$cron['id_module'].'\'';
$query = sprintf(
"SELECT `name` FROM `%s` WHERE `id_module` = '%s'",
_DB_PREFIX_.'module',
(int)$cron['id_module']
);
$module_name = Db::getInstance()->getValue($query);

$cron['description'] = Tools::safeOutput(Module::getModuleName($module_name));
Expand Down
4 changes: 2 additions & 2 deletions classes/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* 2007-2016 PrestaShop
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
Expand All @@ -19,7 +19,7 @@
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
Expand Down
23 changes: 18 additions & 5 deletions controllers/admin/AdminCronJobsController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* 2007-2016 PrestaShop
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
Expand All @@ -19,7 +19,7 @@
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
Expand Down Expand Up @@ -55,17 +55,25 @@ protected function runModulesCrons()
{
$query = 'SELECT * FROM '._DB_PREFIX_.bqSQL($this->module->name).' WHERE `active` = 1 AND `id_module` IS NOT NULL';
$crons = Db::getInstance()->executeS($query);
$table_name = _DB_PREFIX_.bqSQL($this->module->name);


if (is_array($crons) && (count($crons) > 0)) {
foreach ($crons as &$cron) {
$module = Module::getInstanceById((int)$cron['id_module']);

$delete_query = sprintf(
"DELETE FROM `%s` WHERE `id_cronjob` = '%s'",
$table_name,
(int)$cron['id_cronjob']
);

if ($module == false) {
Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.bqSQL($this->module->name).' WHERE `id_cronjob` = \''.(int)$cron['id_cronjob'].'\'');
Db::getInstance()->execute($delete_query);
break;
} elseif ($this->shouldBeExecuted($cron) == true) {
Hook::exec('actionCronJob', array(), $cron['id_module']);
$query = 'UPDATE '._DB_PREFIX_.bqSQL($this->module->name).' SET `updated_at` = NOW(), `active` = IF (`one_shot` = TRUE, FALSE, `active`) WHERE `id_cronjob` = \''.(int)$cron['id_cronjob'].'\'';
$query =
Db::getInstance()->execute($query);
}
}
Expand All @@ -81,7 +89,12 @@ protected function runTasksCrons()
foreach ($crons as &$cron) {
if ($this->shouldBeExecuted($cron) == true) {
Tools::file_get_contents(urldecode($cron['task']), false);
$query = 'UPDATE '._DB_PREFIX_.bqSQL($this->module->name).' SET `updated_at` = NOW(), `active` = IF (`one_shot` = TRUE, FALSE, `active`) WHERE `id_cronjob` = \''.(int)$cron['id_cronjob'].'\'';
$query = sprintf(
"UPDATE `%s` SET `updated_at` = NOW(), `active` = IF (`one_shot` = TRUE, FALSE, `active`) WHERE `id_cronjob` = '%s'",
_DB_PREFIX_.bqSQL($this->module->name),
(int)$cron['id_cronjob']
);

Db::getInstance()->execute($query);
}
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/admin/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* 2007-2016 PrestaShop
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
Expand All @@ -19,7 +19,7 @@
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
Expand Down
4 changes: 2 additions & 2 deletions controllers/front/callback.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* 2007-2016 PrestaShop
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
Expand All @@ -19,7 +19,7 @@
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
Expand Down
4 changes: 2 additions & 2 deletions controllers/front/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* 2007-2016 PrestaShop
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
Expand All @@ -19,7 +19,7 @@
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
Expand Down
4 changes: 2 additions & 2 deletions controllers/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* 2007-2016 PrestaShop
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
Expand All @@ -19,7 +19,7 @@
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
Expand Down
69 changes: 58 additions & 11 deletions cronjobs.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* 2007-2016 PrestaShop
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
Expand All @@ -19,7 +19,7 @@
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
Expand Down Expand Up @@ -255,7 +255,15 @@ public function getContent()
$output = $output.$this->renderForm(CronJobsForms::getJobForm(), CronJobsForms::getNewJobFormValues(), 'submitNewCronJob', true, $back_url);
} elseif (Tools::isSubmit('updatecronjobs') && Tools::isSubmit('id_cronjob')) {
$form_structure = CronJobsForms::getJobForm('Update cron task', true);
$form = $this->renderForm($form_structure, CronJobsForms::getUpdateJobFormValues(), 'submitUpdateCronJob', true, $back_url, true);
$form = $this->renderForm(
$form_structure,
CronJobsForms::getUpdateJobFormValues(),
'submitUpdateCronJob',
true,
$back_url,
true
);

$output = $output.$form;
} elseif (Tools::isSubmit('deletecronjobs') && Tools::isSubmit('id_cronjob')) {
$this->postProcessDeleteCronJob((int)Tools::getValue('id_cronjob'));
Expand Down Expand Up @@ -364,13 +372,22 @@ protected function checkLocalEnvironment()
}
}

/**
* @return bool
*/
protected function isLocalEnvironment()
{
if (isset($_SERVER['REMOTE_ADDR']) === false) {
return true;
}

return in_array(Tools::getRemoteAddr(), array('127.0.0.1', '::1')) || preg_match('/^172\.16\.|^192\.168\.|^10\.|^127\.|^localhost|\.local$/', Configuration::get('PS_SHOP_DOMAIN'));
$is_a_local_ip = in_array(Tools::getRemoteAddr(), array('127.0.0.1', '::1'));
$is_a_local_shop_domain = preg_match(
'/^172\.16\.|^192\.168\.|^10\.|^127\.|^localhost|\.local$/',
Configuration::get('PS_SHOP_DOMAIN')
);

return ($is_a_local_ip || $is_a_local_shop_domain);
}

protected function renderForm($form, $form_values, $action, $cancel = false, $back_url = false, $update = false)
Expand Down Expand Up @@ -513,6 +530,7 @@ protected function postProcessUpdateJob()
public function addNewModulesTasks()
{
$crons = Hook::getHookModuleExecList('actionCronJob');
$table_name = _DB_PREFIX_.bqSQL($this->name);

if ($crons == false) {
return false;
Expand All @@ -526,12 +544,20 @@ public function addNewModulesTasks()
$module = Module::getInstanceById((int)$cron['id_module']);

if ($module == false) {
Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.bqSQL($this->name).' WHERE `id_cronjob` = \''.(int)$cron['id_cronjob'].'\'');
Db::getInstance()->execute(sprintf("DELETE FROM '%s' WHERE `id_cronjob` = '%s'", $table_name, (int)$cron['id_cronjob']));
break;
}

$cronjob = (bool)Db::getInstance()->getValue('SELECT `id_cronjob` FROM `'._DB_PREFIX_.bqSQL($this->name).'`
WHERE `id_module` = \''.$id_module.'\' AND `id_shop` = \''.$id_shop.'\' AND `id_shop_group` = \''.$id_shop_group.'\'');
$select_query = sprintf(
"SELECT `id_cronjob` FROM `%s`
WHERE `id_module` = '%s' AND `id_shop` = '%s' AND `id_shop_group` = '%s'",
$table_name,
$id_module,
$id_shop,
$id_shop_group
);

$cronjob = (bool)Db::getInstance()->getValue($select_query);

if ($cronjob == false) {
$this->registerModuleHook($id_module);
Expand Down Expand Up @@ -709,12 +735,27 @@ protected function updateWebservice($use_webservice)
protected function postProcessDeleteCronJob($id_cronjob)
{
$id_cronjob = Tools::getValue('id_cronjob');
$id_module = Db::getInstance()->getValue('SELECT `id_module` FROM '._DB_PREFIX_.bqSQL($this->name).' WHERE `id_cronjob` = \''.(int)$id_cronjob.'\'');
$table_name = _DB_PREFIX_.bqSQL($this->name);

$select_query = sprintf(
"SELECT `id_module` FROM `%s` WHERE `id_cronjob` = '%s'",
$table_name,
(int)$id_cronjob
);
$id_module = Db::getInstance()->getValue($select_query);

if ((bool)$id_module == false) {
Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.bqSQL($this->name).' WHERE `id_cronjob` = \''.(int)$id_cronjob.'\'');
Db::getInstance()->execute(sprintf(
"DELETE FROM `%s` WHERE `id_cronjob` = '%s'",
$table_name,
(int)$id_cronjob
));
} else {
Db::getInstance()->execute('UPDATE '._DB_PREFIX_.bqSQL($this->name).' SET `active` = FALSE WHERE `id_cronjob` = \''.(int)$id_cronjob.'\'');
Db::getInstance()->execute(sprintf(
"UPDATE `%s` SET `active` = FALSE WHERE `id_cronjob` = '%s'",
$table_name,
(int)$id_cronjob
));
}

return Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', false)
Expand Down Expand Up @@ -747,6 +788,12 @@ protected function registerModuleHook($id_module)

protected function unregisterModuleHook($id_module)
{
return Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.bqSQL($this->name).' WHERE `id_module` = \''.(int)$id_module.'\'');
$table_name = _DB_PREFIX_.bqSQL($this->name);

return Db::getInstance()->execute(sprintf(
"DELETE FROM `%s` WHERE `id_module` = '%s'",
$table_name,
(int)$id_module
));
}
}
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* 2007-2016 PrestaShop
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
Expand All @@ -19,7 +19,7 @@
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
Expand Down
4 changes: 2 additions & 2 deletions translations/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* 2007-2016 PrestaShop
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
Expand All @@ -19,7 +19,7 @@
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
Expand Down
4 changes: 2 additions & 2 deletions upgrade/index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* 2007-2016 PrestaShop
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
Expand All @@ -19,7 +19,7 @@
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2016 PrestaShop SA
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
Expand Down
Loading

0 comments on commit bd6c064

Please sign in to comment.