Current File : //root/panel/modules/varnish/code/controller.ext.php
<?php

/**
 * @copyright 2014-2015 Sentora Project (http://www.sentora.org/) 
 * Sentora is a GPL fork of the ZPanel Project whose original header follows:
 *
 * ZPanel - A Cross-Platform Open-Source Web Hosting Control panel.
 *
 * @package ZPanel
 * @version $Id$
 * @author Bobby Allen - ballen@bobbyallen.me
 * @copyright (c) 2008-2014 ZPanel Group - http://www.zpanelcp.com/
 * @license http://opensource.org/licenses/gpl-3.0.html GNU Public License v3
 *
 * This program (ZPanel) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
class module_controller extends ctrl_module
{

    static $error;
    static $alreadyexists;
    static $badname;
    static $v_blank;
    static $ok;
    static $edit;
    static $samepackage;
	static $v_interval;
	static $v_threshold;
	static $v_window;
	static $v_timeout;
	static $okupdate;
	static $v_thresholdwindow;
	  
	  static function SetCallDaemon()
    {

        $service_port = 4445 ;
    $address = gethostbyname('localhost');
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                if ($socket === false)
                {
                /* 
                echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
                self::$tryagain=true;
                return false;
                */
                }
       //  echo "Attempting to connect to '$address' on port '$service_port'...";
        $result = socket_connect($socket, $address, $service_port);
        if ($result === false)
                {
        /* 
                echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
                self::$tryagain=true;
                return false;
                */
                }
                socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 2000, "usec" =>0));
                $in="command DaemonCall ";
                socket_write($socket, $in, strlen($in));
                socket_close($socket);

    }

  static function gethelpicon()
    {
        global $zdbh;
        global $controller;
        $temp=$controller->GetControllerRequest('URL','module') ;
        $val = '/assets/one.txt';
        $val1 = '/modules/';
        $name=file_get_contents("modules/$temp/assets/helpicon.txt");
        return $name;
    }
	
	static function getThreshold()
	{
		global $zdbh;
        global $controller;
		$currentuser = ctrl_users::GetUserDetail();
		$sql = "SELECT  * FROM x_varnish WHERE x_user_id=:userid";
        //$numrows = $zdbh->query($sql);
        $numrows = $zdbh->prepare($sql);
        $numrows->bindParam(':userid', $currentuser['userid']);
        $numrows->execute();
		
        if ($numrows->fetchColumn() <> 0) {
			$sql = $zdbh->prepare($sql);
			$sql->bindParam(':userid', $currentuser['userid']);
			$sql->execute();
			$res = $sql->fetch();
			return $res['x_threshold'] ;
		
		}else
		{
		return "3";	
		}
	}
	static function getWindow()
	{
		global $zdbh;
        global $controller;
		$currentuser = ctrl_users::GetUserDetail();
		$sql = "SELECT  * FROM x_varnish WHERE x_user_id=:userid";
        //$numrows = $zdbh->query($sql);
        $numrows = $zdbh->prepare($sql);
        $numrows->bindParam(':userid', $currentuser['userid']);
        $numrows->execute();
		
        if ($numrows->fetchColumn() <> 0) {
			$sql = $zdbh->prepare($sql);
			$sql->bindParam(':userid', $currentuser['userid']);
			$sql->execute();
			$res = $sql->fetch();
			return $res['x_window'] ;
		
		}else
		{
		return "5";	
		}
	}
	
	static function getTimeOut()
	{
		global $zdbh;
        global $controller;
		$currentuser = ctrl_users::GetUserDetail();
		$sql = "SELECT  * FROM x_varnish WHERE x_user_id=:userid";
        //$numrows = $zdbh->query($sql);
        $numrows = $zdbh->prepare($sql);
        $numrows->bindParam(':userid', $currentuser['userid']);
        $numrows->execute();
		
        if ($numrows->fetchColumn() <> 0) {
			$sql = $zdbh->prepare($sql);
			$sql->bindParam(':userid', $currentuser['userid']);
			$sql->execute();
			$res = $sql->fetch();
			return $res['x_timeout'] ;
		
		}else
		{
		return "1";	
		}
	}
	
	static function getInterval()
	{
		global $zdbh;
        global $controller;
		$currentuser = ctrl_users::GetUserDetail();
		$sql = "SELECT  * FROM x_varnish WHERE x_user_id=:userid";
        //$numrows = $zdbh->query($sql);
        $numrows = $zdbh->prepare($sql);
        $numrows->bindParam(':userid', $currentuser['userid']);
        $numrows->execute();
		
        if ($numrows->fetchColumn() <> 0) {
			$sql = $zdbh->prepare($sql);
			$sql->bindParam(':userid', $currentuser['userid']);
			$sql->execute();
			$res = $sql->fetch();
			return $res['x_interval'] ;
		
		}else
		{
		return "5";	
		}
	}
	
	static function getcheckedon()
	{
		global $zdbh;
        global $controller;
		$currentuser = ctrl_users::GetUserDetail();
		$sql = "SELECT  * FROM x_varnish WHERE x_user_id=:userid";
        //$numrows = $zdbh->query($sql);
        $numrows = $zdbh->prepare($sql);
        $numrows->bindParam(':userid', $currentuser['userid']);
        $numrows->execute();
		
        if ($numrows->fetchColumn() <> 0) {
			$sql = $zdbh->prepare($sql);
			$sql->bindParam(':userid', $currentuser['userid']);
			$sql->execute();
			$res = $sql->fetch();
			if($res['x_varnish']=="On")
			return 'checked' ;
			else
			return '';
		}else
		{
		return "";	
		}
	}
	
	static function getcheckedff()
	{
		global $zdbh;
        global $controller;
		$currentuser = ctrl_users::GetUserDetail();
		$sql = "SELECT  * FROM x_varnish WHERE x_user_id=:userid";
        //$numrows = $zdbh->query($sql);
        $numrows = $zdbh->prepare($sql);
        $numrows->bindParam(':userid', $currentuser['userid']);
        $numrows->execute();
		
        if ($numrows->fetchColumn() <> 0) {
			$sql = $zdbh->prepare($sql);
			$sql->bindParam(':userid', $currentuser['userid']);
			$sql->execute();
			$res = $sql->fetch();
			if($res['x_varnish']=="On")
			return '' ;
			else 
			return 'Checked';
		}else
		{
		return "checked";	
		}
	}
	
  static function dooperation()
    {
	
		global $zdbh;
        global $controller;
        $currentuser = ctrl_users::GetUserDetail();
		
        if(!isset($_POST['varnishtype']))
		{
		self::$v_blank = true;
		return false;
		}
       if(trim($_POST['txtinterval'])=="")
	   {
		self::$v_interval = true;
		return false; 
	   }
       if(trim($_POST['txttimeout'])=="")
	   {
		self::$v_timeout = true;
		return false;
	   }
		
       if(trim($_POST['txtwindow'])=="")
	   {
		self::$v_window = true;
		return false; 
	   }
       if(trim($_POST['txtthreshold'])=="")
	   {
		self::$v_threshold = true;
		return false;
	   }
	     if(trim($_POST['txtthreshold'])>=trim($_POST['txtwindow']))
	   {
		self::$v_thresholdwindow = true;
		return false;
	   }
	   
		
		$sql = "SELECT  COUNT(*) FROM x_varnish WHERE x_user_id=:userid";
        //$numrows = $zdbh->query($sql);
        $numrows = $zdbh->prepare($sql);
        $numrows->bindParam(':userid', $currentuser['userid']);
        $numrows->execute();
		
        if ($numrows->fetchColumn() <> 0) {
			// update the column 
		$sql = "Update x_varnish SET x_varnish=:x_varnish,
						    		 x_interval=:x_interval,
									 x_timeout=:x_timeout,
									 x_window=:x_window,
									 x_threshold=:x_threshold,
									 x_lastupdate=:time,
									 x_isactive=:x_isactive									 
									 WHERE	x_user_id=:userid";
        $time = time();
		$x_isactive=0;
        $sql = $zdbh->prepare($sql);
		$sql->bindParam(':userid', $currentuser['userid']);
		$sql->bindParam(':x_varnish', trim($_POST['varnishtype']));
		$sql->bindParam(':x_interval', trim($_POST['txtinterval']));
		$sql->bindParam(':x_timeout', trim($_POST['txttimeout']));
		$sql->bindParam(':x_window', trim($_POST['txtwindow']));
		$sql->bindParam(':x_isactive', $x_isactive);
        $sql->bindParam(':x_threshold', trim($_POST['txtthreshold']));
		$sql->bindParam(':time', $time);
		$sql->execute();
		$customport=8080;
		if(trim($_POST['varnishtype'])=="Off")
		{
		   $customport=80;
		}
	   $sql = $zdbh->prepare("Update x_vhosts SET vh_custom_port_in='".$customport."'");
	   $sql->execute();
		
		
				$gsql=$zdbh->prepare("select vh_id_pk from x_vhosts where   vh_deleted_ts is NULL");	
				//$gsql->bindParam(':userid', $userid);
				$gsql->execute();
				$records_list="";
				$iter=0;
							while($res=$gsql->fetch())
							{
							if($iter==0)
							{
								$records_list=$res['vh_id_pk'];
								$iter=1;
							}	
							else
							{
								
								$records_list=$res['vh_id_pk'];
							}	
							}
			$sql = "UPDATE x_settings SET so_value_tx=:newlist WHERE so_name_vc='dns_hasupdates'";
			$sql = $zdbh->prepare($sql);
			$sql->bindParam(':newlist', $records_list);
			$sql->execute();
			
			$sql = $zdbh->prepare("UPDATE x_settings
			SET so_value_tx='true'
			WHERE so_name_vc='apache_changed'");
			$sql->execute();
			$sql = $zdbh->prepare("UPDATE x_ssl SET ssl_status='0' WHERE ssl_status='1' and ssl_delete is NULL;");
			$sql->execute();
			// by nandhini
			//shell_exec('sh /etc/sentora/panel/cronshell.sh');					
			self::SetCallDaemon();
        self::$okupdate = true;
        return true;
		
		}else
		{
			
			// insert the column 
			$sql = "INSERT INTO x_varnish ( x_user_id, 
											  x_varnish,
											  x_interval,
											  x_timeout,
											  x_window,
											  x_threshold,
											  x_lastupdate,x_isactive) VALUES (
											 :userid,
											 :x_varnish,
											 :x_interval,
											 :x_timeout,
											 :x_window,
											 :x_threshold,
											 :time,
											 :x_isactive)";
        $time = time();
		$x_isactive=0;
        $sql = $zdbh->prepare($sql);
		$sql->bindParam(':userid', $currentuser['userid']);
		$sql->bindParam(':x_varnish', trim($_POST['varnishtype']));
		$sql->bindParam(':x_interval', trim($_POST['txtinterval']));
		$sql->bindParam(':x_timeout', trim($_POST['txttimeout']));
		$sql->bindParam(':x_window', trim($_POST['txtwindow']));
		$sql->bindParam(':x_isactive', $x_isactive);
        $sql->bindParam(':x_threshold', trim($_POST['txtthreshold']));
		$sql->bindParam(':time', $time);
        $sql->execute();
		$customport=8080;
		if(trim($_POST['varnishtype'])=="Off")
		{
		   $customport=80;
		}
	   $sql = $zdbh->prepare("Update x_vhosts SET vh_custom_port_in='".$customport."'");
	   $sql->execute();
	   
	   	$gsql=$zdbh->prepare("select vh_id_pk from x_vhosts where   vh_deleted_ts is NULL");	
				// $gsql->bindParam(':userid', $userid);
				$gsql->execute();
				$records_list="";
				$iter=0;
							while($res=$gsql->fetch())
							{
							if($iter==0)
							{
								$records_list=$res['vh_id_pk'];
								$iter=1;
							}	
							else
							{
								
								$records_list=$res['vh_id_pk'];
							}	
							}
			$sql = "UPDATE x_settings SET so_value_tx=:newlist WHERE so_name_vc='dns_hasupdates'";
			$sql = $zdbh->prepare($sql);
			$sql->bindParam(':newlist', $records_list);
			$sql->execute();
			
			$sql = $zdbh->prepare("UPDATE x_settings
			SET so_value_tx='true'
			WHERE so_name_vc='apache_changed'");
			$sql->execute();
			
			$sql = $zdbh->prepare("UPDATE x_ssl SET ssl_status='0' WHERE ssl_status='1' and ssl_delete is NULL;");
			$sql->execute();
			//by nandhini
			//shell_exec('sh /etc/sentora/panel/cronshell.sh');	
			self::SetCallDaemon();
        self::$ok = true;
        return true;
			
		}
	
	}
	
    static function getResult()
    {
      // 

		if (!fs_director::CheckForEmptyValue(self::$v_blank)) {
		return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please select enabled varnish cache."), "Error");

		}
		if (!fs_director::CheckForEmptyValue(self::$v_interval)) {
		return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the interval."), "Error");

		}

		if (!fs_director::CheckForEmptyValue(self::$v_timeout)) {
		return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the timeout."), "Error");

		}
		if (!fs_director::CheckForEmptyValue(self::$v_window)) {
		return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the window."), "Error");

		}
		if (!fs_director::CheckForEmptyValue(self::$v_threshold)) {
		return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the threshold."), "Error");

		}
		
		if (!fs_director::CheckForEmptyValue(self::$v_thresholdwindow)) {
		return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Threshould value must be less than the window value."), "Error");

		}
	  
        if (!fs_director::CheckForEmptyValue(self::$error)) {
            return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> There was an error for restart the server."), "Error");
        }
        if (!fs_director::CheckForEmptyValue(self::$ok)) {
            return ui_sysmessage::shout(ui_language::translate("<strong>Success:</strong> varnish cache settings updtaed successfully."), "Ok");
        }
		if (!fs_director::CheckForEmptyValue(self::$okupdate)) {
            return ui_sysmessage::shout(ui_language::translate("<strong>Success:</strong> varnish cache settings updtaed successfully."), "Ok");
        }
		
        return;
    }

    /**
     * Webinterface sudo methods.
     */
}