HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux vmi3050980 6.8.0-90-generic #91-Ubuntu SMP PREEMPT_DYNAMIC Tue Nov 18 14:14:30 UTC 2025 x86_64
User: gurumedia_st_usr (1012)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/www/gurumedia_st_usr/data/www/gurumedia.store/frdsp/dp/page/frd-p-SoftUp/frdapi-SoftUp7.php
<?php
// =====================================================
// 🔥 Spider eCommerce
// 🔥 Soft Updater Receiver API v7
// 🔥 Code Version: 2
// 🔥 Author: Fazle Rabbi / ChatGPT
// =====================================================

/*

=====================================================
📌 DEVELOPER NOTE
=====================================================

এই ফাইলের কাজ:

✔ Remote update request receive করা
✔ API key validate করা
✔ ZIP update download করা
✔ ZIP extract করা
✔ Auto version update করা
✔ Alter table run করা
✔ Pure JSON response return করা
✔ Shared hosting compatible update system provide করা

-----------------------------------------------------
📌 IMPORTANT ARCHITECTURE NOTES
-----------------------------------------------------

এই API system shared hosting / cPanel compatible
ভাবে optimize করা হয়েছে।

এই file এর সবচেয়ে important বিষয়:

🔥 JSON purity maintain করা

কারণ:

- warning
- notice
- whitespace
- BOM
- accidental echo
- fatal error output

এইগুলা JSON response break করতে পারে।

তাই এখানে:

✔ output buffering
✔ shutdown protection
✔ clean JSON response
✔ fatal error handling
✔ output cleaning system

ব্যবহার করা হয়েছে।

-----------------------------------------------------
📌 MAIN FEATURES
-----------------------------------------------------

✔ Shared hosting optimized
✔ cPanel compatible
✔ Secure API key validation
✔ Pure JSON response system
✔ Output contamination protection
✔ Fatal error protection
✔ ZIP security validation
✔ Zip Slip protection
✔ Maintenance mode system
✔ Auto alter table execution
✔ Safe response helper system
✔ Buffered output protection
✔ Auto cleanup system

-----------------------------------------------------
📌 VERSION CHANGELOG
-----------------------------------------------------

Version 2:
- Added output buffering protection
- Added shutdown fatal error catcher
- Added clean JSON response system
- Added shared hosting optimization
- Added cPanel compatibility improvements
- Added output contamination protection
- Added response cleaning system
- Added safe exit flow
- Improved JSON stability

Version 1:
- Initial updater receiver version

=====================================================

*/



// =====================================================
// 🔹 OUTPUT BUFFER START
// =====================================================

ob_start();



// =====================================================
// 🔹 ERROR CONFIG
// =====================================================

ini_set('display_errors', 0);

error_reporting(E_ALL);



// =====================================================
// 🔹 BASE PATH
// =====================================================

$FR_PATH_HD = "../../../../";



// =====================================================
// 🔹 REQUIRED FILES
// =====================================================

require_once(
    $FR_PATH_HD .
    "frd-src/abc/frd-config.php"
);

require_once(
    $FR_PATH_HD .
    "frd-src/abc/frd-spider-function.php"
);

require_once(
    $FR_PATH_HD .
    "frd-src/abc/frd-this-function.php"
);



// =====================================================
// 🔹 HEADERS
// =====================================================

header("Access-Control-Allow-Origin: *");

header(
    "Access-Control-Allow-Methods: GET, POST, OPTIONS"
);

header(
    "Access-Control-Allow-Headers: Content-Type"
);

header('Content-Type: application/json');



// =====================================================
// 🔹 OPTIONS REQUEST
// =====================================================

if(
    $_SERVER['REQUEST_METHOD']
    ===
    'OPTIONS'
)
{
    exit;
}




// =====================================================
// 🔹 CLEAN OUTPUT FUNCTION
// =====================================================

function FR_ClearOutputBuffer(): void
{
    while(ob_get_level())
    {
        ob_end_clean();
    }
}



// =====================================================
// 🔹 JSON RESPONSE FUNCTION
// =====================================================

function FR_Response(
    bool $status,
    string $message,
    array $extra = []
): void
{

    FR_ClearOutputBuffer();

    echo json_encode(
        array_merge([
            'status' => $status,
            'message' => $message
        ], $extra),
        JSON_UNESCAPED_UNICODE
    );

    exit;

}



// =====================================================
// 🔹 FATAL ERROR HANDLER
// =====================================================

register_shutdown_function(function(){

    $error = error_get_last();

    if(
        $error
        &&
        in_array(
            $error['type'],
            [
                E_ERROR,
                E_PARSE,
                E_CORE_ERROR,
                E_COMPILE_ERROR
            ]
        )
    )
    {

        FR_Response(
            false,
            'Fatal Error: ' .
            $error['message']
        );

    }

});



// =====================================================
// 🔹 INPUT SYSTEM
// =====================================================

$input = [];



$raw = file_get_contents("php://input");

if($raw)
{

    $json = json_decode($raw, true);

    if(
        json_last_error()
        ===
        JSON_ERROR_NONE
    )
    {

        $input = $json;

    }

}



$input = array_merge(
    $input,
    $_POST,
    $_GET
);




// =====================================================
// 🔹 API KEY CHECK
// =====================================================

$API_KEY_SECRET = '123456789';



if(
    !isset($input['API_KEY'])
    ||
    !hash_equals(
        $API_KEY_SECRET,
        $input['API_KEY']
    )
)
{

    http_response_code(403);

    FR_Response(
        false,
        'Unauthorized API key'
    );

}




// =====================================================
// 🔹 VERSION SETUP
// =====================================================

$currentVersion =
    (int)$FR_SOFT_VERSION;

$clientVersion =
    (int)(
        $input['FRpost_LATEST_R_VERSION']
        ??
        0
    );

$nextVersion =
    $currentVersion + 1;




// =====================================================
// 🔹 ZIP CHECK FUNCTION
// =====================================================

function FR_CheckZip(
    string $url
): bool
{

    $ch = curl_init($url);

    curl_setopt_array($ch, [

        CURLOPT_NOBODY => true,

        CURLOPT_RETURNTRANSFER => true,

        CURLOPT_TIMEOUT => 15,

        CURLOPT_CONNECTTIMEOUT => 8,

        CURLOPT_FOLLOWLOCATION => true,

        CURLOPT_SSL_VERIFYPEER => false,

        CURLOPT_USERAGENT => 'SpiderSoftUpdater/7'

    ]);



    curl_exec($ch);



    if(curl_errno($ch))
    {

        curl_close($ch);

        return false;

    }



    $httpCode =
        curl_getinfo(
            $ch,
            CURLINFO_HTTP_CODE
        );

    $fileSize =
        curl_getinfo(
            $ch,
            CURLINFO_CONTENT_LENGTH_DOWNLOAD
        );

    $contentType =
        curl_getinfo(
            $ch,
            CURLINFO_CONTENT_TYPE
        );

    curl_close($ch);



    return (

        $httpCode === 200

        &&

        $fileSize > 50000

        &&

        strpos(
            strtolower($contentType),
            'zip'
        ) !== false

    );

}




// =====================================================
// 🔹 REMOTE ZIP URL
// =====================================================

$remoteZip =
    "https://spiderecommerce.com/ahost/released-version/V-3-$nextVersion-SPIDER_ECOMMERCE.zip";




// =====================================================
// 🔹 CHECK UPDATE EXIST
// =====================================================

if(
    !FR_CheckZip($remoteZip)
)
{

    FR_Response(
        false,
        "No update available. Latest version {$currentVersion}"
    );

}




// =====================================================
// 🔹 MAINTENANCE MODE ON
// =====================================================

$maintFlag =
    $FR_PATH_HD .
    'maintenance.flag';

file_put_contents(
    $maintFlag,
    'updating'
);




// =====================================================
// 🔹 TEMP ZIP PATH
// =====================================================

$tmpZip =
    $FR_PATH_HD .
    "update_{$nextVersion}.zip";




// =====================================================
// 🔹 DOWNLOAD ZIP
// =====================================================

$fp = fopen($tmpZip, 'w');



$ch = curl_init($remoteZip);

curl_setopt_array($ch, [

    CURLOPT_FILE => $fp,

    CURLOPT_FOLLOWLOCATION => true,

    CURLOPT_TIMEOUT => 180,

    CURLOPT_CONNECTTIMEOUT => 15,

    CURLOPT_SSL_VERIFYPEER => false,

    CURLOPT_USERAGENT => 'SpiderSoftUpdater/7'

]);



curl_exec($ch);




if(curl_errno($ch))
{

    fclose($fp);

    if(file_exists($tmpZip))
    {
        unlink($tmpZip);
    }

    if(file_exists($maintFlag))
    {
        unlink($maintFlag);
    }

    FR_Response(
        false,
        'Download failed: ' .
        curl_error($ch)
    );

}



curl_close($ch);

fclose($fp);




// =====================================================
// 🔹 ZIP VALIDATION
// =====================================================

if(
    !file_exists($tmpZip)
    ||
    filesize($tmpZip) < 1000
)
{

    if(file_exists($tmpZip))
    {
        unlink($tmpZip);
    }

    if(file_exists($maintFlag))
    {
        unlink($maintFlag);
    }

    FR_Response(
        false,
        'ZIP download failed'
    );

}




// =====================================================
// 🔹 ZIP OPEN
// =====================================================

$zip = new ZipArchive();



if(
    $zip->open($tmpZip)
    !==
    true
)
{

    unlink($tmpZip);

    if(file_exists($maintFlag))
    {
        unlink($maintFlag);
    }

    FR_Response(
        false,
        'ZIP open failed'
    );

}




// =====================================================
// 🔹 ZIP SLIP PROTECTION
// =====================================================

for(
    $i = 0;
    $i < $zip->numFiles;
    $i++
)
{

    $fileName =
        $zip->getNameIndex($i);



    if(
        strpos(
            $fileName,
            '../'
        ) !== false
    )
    {

        $zip->close();

        unlink($tmpZip);

        if(file_exists($maintFlag))
        {
            unlink($maintFlag);
        }

        FR_Response(
            false,
            'Unsafe ZIP detected'
        );

    }

}




// =====================================================
// 🔹 ZIP EXTRACT
// =====================================================

$zip->extractTo($FR_PATH_HD);

$zip->close();

unlink($tmpZip);




// =====================================================
// 🔹 ALTER TABLE
// =====================================================

$tableUpdated = false;

$alterFile =
    $FR_PATH_HD .
    'frd-src/frd-alter-table.php';



if(file_exists($alterFile))
{

    require_once $alterFile;

    $tableUpdated = true;

    unlink($alterFile);

}




// =====================================================
// 🔹 MAINTENANCE OFF
// =====================================================

if(file_exists($maintFlag))
{
    unlink($maintFlag);
}




// =====================================================
// 🔹 SUCCESS RESPONSE
// =====================================================

FR_Response(

    true,

    "Updated to version {$nextVersion}",

    [

        'runningVersion' =>
            $nextVersion,

        'tableUpdated' =>
            $tableUpdated

    ]

);