File: /var/www/gurumedia_st_usr/data/www/gurumedia.store/frd-api/frd-mixd/frdapi-SoftUp2.php
<?php
/*
Soft Up
Version: 3
*/
// Always JSON response
header('Content-Type: application/json; charset=utf-8');
// ---- Allow only POST ----
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') {
http_response_code(405);
die(json_encode([
'status' => 0,
'message' => 'Only POST allowed'
]));
}
// ---- API KEY check ----
$API_KEY_SECRET = "123456789"; // Change this (must match client)
if (!isset($_POST['API_KEY']) || $_POST['API_KEY'] !== $API_KEY_SECRET) {
http_response_code(403);
die(json_encode([
'status' => 0,
'message' => 'Unauthorized: Invalid API Key'
]));
}
// ---- Input ----
$FRd_LATEST_R_VERSION = isset($_POST['FRd_LATEST_R_VERSION']) ? (int)$_POST['FRd_LATEST_R_VERSION'] : null;
// ---- Environment defaults ----
$FR_SOFT_VERSION = $FR_SOFT_VERSION ?? 0;
$FR_PATH_HD = rtrim($FR_PATH_HD ?? __DIR__.'/../../', '/').'/';
$FRD_HURL = $FRD_HURL ?? ($_SERVER['HTTP_HOST'] ?? '');
// ---- Already latest? ----
if ($FRd_LATEST_R_VERSION !== null && $FRd_LATEST_R_VERSION == $FR_SOFT_VERSION) {
die(json_encode([
'status' => 0,
'message' => "LATEST VERSION $FR_SOFT_VERSION already running on $FRD_HURL"
]));
}
// ---- Next version target ----
$nextVersion = $FR_SOFT_VERSION + 1;
$remoteZipFile = "https://spiderecommerce.com/ahost/released-version/V-3-$nextVersion-SPIDER_ECOMMERCE.zip";
$localZipFile = $FR_PATH_HD."V-3-$nextVersion-SPIDER_ECOMMERCE.zip";
// ---- Step 1: Remote file check ----
$ch = curl_init($remoteZipFile);
curl_setopt_array($ch, [
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER=> true,
CURLOPT_TIMEOUT => 10,
]);
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if (($info['http_code'] ?? 0) !== 200) {
die(json_encode([
'status' => 0,
'message' => "Remote ZIP not found (HTTP ".($info['http_code'] ?? 'NA').")"
]));
}
// ---- Step 2: Download file ----
$zipContents = @file_get_contents($remoteZipFile);
if ($zipContents === false || @file_put_contents($localZipFile, $zipContents) === false) {
die(json_encode([
'status' => 0,
'message' => "Failed to download/save ZIP file"
]));
}
// ---- Step 3: Validate filename version ----
$fileName = basename($localZipFile);
$ARR = explode("-", $fileName);
$fileVer = $ARR[2] ?? '';
if ((string)$fileVer !== (string)$nextVersion) {
die(json_encode([
'status' => 0,
'message' => "Version mismatch in ZIP filename ($fileVer vs $nextVersion)"
]));
}
// ---- Step 4: Extract ZIP ----
$unzip = new ZipArchive();
if ($unzip->open($localZipFile) !== true) {
die(json_encode([
'status' => 0,
'message' => "Failed to open ZIP file"
]));
}
if (!$unzip->extractTo($FR_PATH_HD)) {
$unzip->close();
die(json_encode([
'status' => 0,
'message' => "Failed to extract ZIP file"
]));
}
$unzip->close();
// ---- Step 5: Table update ----
$alterFile = $FR_PATH_HD."frd-src/frd-alter-table.php";
$tableUpdated = false;
if (file_exists($alterFile)) {
require_once $alterFile;
$tableUpdated = true;
}
// ---- Cleanup ----
@unlink($FR_PATH_HD."frd-src/frd-alter-table.php");
@unlink($localZipFile);
// ===============================
// FINAL SUCCESS RESPONSE
// ===============================
echo json_encode([
"status" => 1,
"message" => "Update Completed to Version $nextVersion!",
"runningVersion" => $nextVersion,
"tableUpdated" => $tableUpdated,
"debug" => [
"stepCheck" => true,
"stepDownload" => true,
"stepVersion" => true,
"stepExtract" => true,
"stepTableUpdate" => $tableUpdated
],
"backData" => $_POST
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);