HOME


Mini Shell 1.0
DIR: /opt/tijeers/cpmalscan/bin/
Upload File :
Current File : //opt/tijeers/cpmalscan/bin/update.php
<?php

$base_url = "http://downloads.cpmalscan.com/v4/";
$force = false;
$stopped = false;

if (!function_exists('curl_version')) {
    echo "ERROR: php curl is not installed.\n";
    exit(1);
}

function curl_get_file_contents($URL)
{
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_URL, $URL);
    $contents = curl_exec($c);
    if (curl_errno($c)) {
        echo "ERROR: " . curl_error($c);
        exit(0);
    }
    curl_close($c);

    if ($contents) {
        return $contents;
    } else {
        echo "ERROR: Empty data fetch on $URL";
        exit(0);
    }
}

function stop_scan_and_services()
{
    global $stopped;

    if ($stopped) {
        return;
    }

    $cmds = [
        "systemctl stop cpmalscan-files-monitoring",
        "systemctl stop cpmalscan-waflog-parser",
        "systemctl disable cpmalscan-files-monitoring",
        "systemctl disable cpmalscan-waflog-parser",
        "killall -9 cpmalscan",
    ];

    foreach ($cmds as $cmd) {
        system($cmd);
    }
    $stopped = true;
}

function start_scan_and_services()
{
    global $stopped;
    if (!$stopped) {
        return;
    }
    $cmds = [
        "systemctl enable cpmalscan-files-monitoring",
        "systemctl enable cpmalscan-waflog-parser",
        "systemctl restart cpmalscan-files-monitoring",
        "systemctl restart cpmalscan-waflog-parser",
    ];

    foreach ($cmds as $cmd) {
        system($cmd);
    }
}
function update()
{
    global $base_url, $force, $stopped;
    $checks = [
        [
            'file' => '/opt/tijeers/cpmalscan/bin/cpmalscan',
            'md5_url' =>  $base_url . 'bin/cpmalscan.md5',
            'url' =>  $base_url . 'bin/cpmalscan',
        ],
        [
            'file' => '/opt/tijeers/cpmalscan/bin/waflogparser',
            'md5_url' =>  $base_url . 'bin/waflogparser.md5',
            'url' =>  $base_url . 'bin/waflogparser',
        ],
        [
            'file' => '/opt/tijeers/cpmalscan/db/malware.db',
            'md5_url' =>  $base_url . 'db/malware.db.md5',
            'url' =>  $base_url . 'db/malware.db',
        ],
    ];

    foreach ($checks as $check) {
        $md5 = md5_file($check['file']);
        $r_md5 = trim(curl_get_file_contents($check['md5_url']));

        if (!$force) {
            if ($md5 == $r_md5) {
                continue;
            }
        }
        stop_scan_and_services();
        echo "{$check['file']}  ::  $md5  ::  $r_md5 \n";
        system("wget -O {$check['file']}   {$check['url']}");
    }

    if ($stopped) {
        system("wget -O  http://downloads.cpmalscan.com/v4/version.txt  /opt/tijeers/cpmalscan/etc/version.txt");
        system("/opt/tijeers/cpmalscan/bin/cpmalscan configure");
        start_scan_and_services();
    } else {
        echo "All up to date.";
    }
}

function check_args()
{
    global $argv, $force;

    foreach ($argv as $arg) {
        if (trim($arg) == "--force") {
            $force = true;
            return;
        }
    }
}
function main()
{
    check_args();
    update();
}
main();