124 lines
3.7 KiB
PHP
124 lines
3.7 KiB
PHP
<?php
|
|
ob_start(); // Start output buffering
|
|
|
|
// Include PrestaShop configuration
|
|
require_once dirname(__FILE__) . "/../../config/config.inc.php";
|
|
require_once dirname(__FILE__) . "/../../init.php";
|
|
|
|
// Autoloader for module classes
|
|
spl_autoload_register(function ($className) {
|
|
$classMap = [
|
|
'EcomZoneException' => 'classes/EcomZoneException.php',
|
|
'EcomZoneClient' => 'classes/EcomZoneClient.php',
|
|
'EcomZoneLogger' => 'classes/EcomZoneLogger.php',
|
|
'EcomZoneProductSync' => 'classes/EcomZoneProductSync.php',
|
|
'EcomZoneProductImport' => 'classes/EcomZoneProductImport.php',
|
|
'EcomZoneCategoryHandler' => 'classes/EcomZoneCategoryHandler.php',
|
|
'IProductSync' => 'classes/interfaces/IProductSync.php',
|
|
'ICategory' => 'classes/interfaces/ICategory.php'
|
|
];
|
|
|
|
if (isset($classMap[$className])) {
|
|
require_once dirname(__FILE__) . '/' . $classMap[$className];
|
|
}
|
|
});
|
|
|
|
// Function to send JSON response
|
|
function sendJsonResponse($data, $statusCode = 200)
|
|
{
|
|
if (php_sapi_name() === 'cli') {
|
|
echo json_encode($data, JSON_PRETTY_PRINT) . "\n";
|
|
} else {
|
|
if (!headers_sent()) {
|
|
http_response_code($statusCode);
|
|
header("Content-Type: application/json");
|
|
}
|
|
echo json_encode($data);
|
|
}
|
|
exit($statusCode === 200 ? 0 : 1);
|
|
}
|
|
|
|
// Get token from either GET parameter or command line argument
|
|
$token = '';
|
|
if (php_sapi_name() === 'cli') {
|
|
global $argv;
|
|
foreach ($argv as $arg) {
|
|
if (strpos($arg, 'token=') === 0) {
|
|
$token = substr($arg, 6);
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
$token = Tools::getValue('token');
|
|
}
|
|
|
|
// Security check
|
|
$configToken = Configuration::get("ECOMZONE_CRON_TOKEN");
|
|
|
|
if (empty($token) || $token !== $configToken) {
|
|
sendJsonResponse([
|
|
'success' => false,
|
|
'error' => 'Invalid token',
|
|
'provided_token' => $token,
|
|
'expected_token' => $configToken
|
|
], 403);
|
|
}
|
|
|
|
try {
|
|
// Initialize logger
|
|
EcomZoneLogger::log("Starting cron execution", "INFO", [
|
|
"time" => date("Y-m-d H:i:s"),
|
|
]);
|
|
|
|
// Check last run time to prevent too frequent execution
|
|
$lastRun = Configuration::get("ECOMZONE_LAST_CRON_RUN");
|
|
$minInterval = 360;
|
|
|
|
if (!empty($lastRun) && strtotime($lastRun) + $minInterval > time()) {
|
|
EcomZoneLogger::log("Skipping cron - too soon since last run", "INFO", [
|
|
"last_run" => $lastRun,
|
|
"next_run" => date("Y-m-d H:i:s", strtotime($lastRun) + $minInterval),
|
|
]);
|
|
|
|
sendJsonResponse([
|
|
"success" => false,
|
|
"message" => "Too soon since last run",
|
|
"last_run" => $lastRun,
|
|
"next_run" => date("Y-m-d H:i:s", strtotime($lastRun) + $minInterval),
|
|
]);
|
|
}
|
|
|
|
// Initialize Product Sync
|
|
$productSync = new EcomZoneProductSync();
|
|
|
|
// Start the import process
|
|
$result = $productSync->importProducts(100); // Import 100 products per page
|
|
|
|
// Update last run time
|
|
Configuration::updateValue("ECOMZONE_LAST_CRON_RUN", date("Y-m-d H:i:s"));
|
|
|
|
// Log completion and send response
|
|
EcomZoneLogger::log("Cron execution completed", "INFO", [
|
|
"imported" => $result["imported"],
|
|
"total" => $result["total"],
|
|
"errors" => count($result["errors"]),
|
|
]);
|
|
|
|
sendJsonResponse([
|
|
"success" => true,
|
|
"result" => $result,
|
|
"timestamp" => date("Y-m-d H:i:s"),
|
|
]);
|
|
} catch (Exception $e) {
|
|
EcomZoneLogger::log("Cron execution failed", "ERROR", [
|
|
"error" => $e->getMessage(),
|
|
"trace" => $e->getTraceAsString()
|
|
]);
|
|
|
|
sendJsonResponse([
|
|
"success" => false,
|
|
"error" => $e->getMessage()
|
|
], 500);
|
|
}
|
|
|