This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
forked from johnulist/shoppingfluxexport
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cron.php
105 lines (90 loc) · 3.88 KB
/
cron.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* 2007-2019 PrestaShop SA and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* 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 http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
include(dirname(__FILE__) . '/../../config/config.inc.php');
include(dirname(__FILE__) . '/../../init.php');
include_once(dirname(__FILE__) . '/sfpayment.php');
ini_set("memory_limit", "2048M");
ini_set('display_errors', 'off');
$id_shop = Context::getContext()->shop->id;
$sf = Module::getInstanceByName('shoppingfluxexport');
if (!$sf || !$sf->active) {
die("<?xml version='1.0' encoding='utf-8'?><error>Module inactive</error>");
}
$token = Tools::getValue('token');
if (version_compare(_PS_VERSION_, '1.5', '>') && Shop::isFeatureActive()) {
$tokenInConfig = $sf->getTokenValue($id_shop);
$allTokens_raw = $sf->getAllTokensOfShop();
$allTokens = array();
foreach ($allTokens_raw as $allTokens_subraw) {
$allTokens[$allTokens_subraw['token']] = $allTokens_subraw['token'];
}
} else {
$tokenInConfig = $sf->getTokenValue();
$allTokens[$tokenInConfig] = $tokenInConfig;
}
if ($token == '' || ($token != $tokenInConfig && ! in_array($token, $allTokens))) {
die("<?xml version='1.0' encoding='utf-8'?><error>Invalid Token</error>");
}
$current = Tools::getValue('current');
$lang = Tools::getValue('lang');
// Do not allow feed generation if less than 2 hours before last generation
$frequency_in_hours = 2;
$today = date('Y-m-d H:i:s');
$keyCronTime = 'SHOPPING_FLUX_CRON_TIME';
if (!empty($lang)) {
// When a lang is provided, we will have a separate frequency
if (!Language::getIdByIso($lang)) {
// The lang is not valid
$logMessage = 'Invalid lang: '.$lang;
SfLogger::getInstance()->log(SF_LOG_CRON, $logMessage);
die("<?xml version='1.0' encoding='utf-8'?><error>Invalid lang</error>");
}
// Separate the configuration key by lang, such as : SHOPPING_FLUX_CRON_TIME_FR
$keyCronTime = $keyCronTime.'_'.Tools::strtoupper($lang);
}
$last_executed = Configuration::get($keyCronTime, null, null, $id_shop);
if (empty($last_executed) || ($last_executed == '0')) {
$last_executed = 0;
}
// Convert to unix timestamp
$timestamp_last_exec = strtotime($last_executed);
$timestamp_today = strtotime($today);
$hours = ($timestamp_today - $timestamp_last_exec) / (60 * 60);
if (empty($current)) {
if ($hours >= $frequency_in_hours) {
SfLogger::getInstance()->log(SF_LOG_CRON, 'CRON CALL - begining of treament');
$sf->initFeed($lang);
} else {
$logMessage = 'CRON CALL - Not initiated SHOULD NOT HAVE BEEN CALLED. ';
$logMessage .= 'Cron call has already been called at ' . $last_executed;
SfLogger::getInstance()->log(SF_LOG_CRON, $logMessage);
$logMessage = 'NEXT CRON CALL - Can be initiated at ';
$logMessage .= date('Y-m-d H:i:s', strtotime($last_executed . ' + ' . $frequency_in_hours . ' hour'));
SfLogger::getInstance()->log(SF_LOG_CRON, $logMessage);
}
} else {
$sf->writeFeed(Tools::getValue('total'), Tools::getValue('current'), $lang);
}