Current File : //etc/zpanel/panel/modules/apache_setting/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 $tryagain;
  static $error;
  static $blank_KeepAlive_setting;
  static $blank_StartServers;
  static $blank_ServerLimit;	
  static $blank_MinSpareServers;
  static $blank_MaxSpareServers;
  static $blank_session_save_path;
  static $blank_MaxRequestsPerChild;
  static $blank_MaxRequestWorkers;
  static $ok;
  
	
	static function getValueBasedOnKey($key)
	{
		global $zdbh;
		global $controller; 
		$currentuser = ctrl_users::GetUserDetail();
		$current_user_id=$currentuser['userid'];
		$sql = "SELECT  * FROM x_apache_setting WHERE x_parameter='$key'";
		$sql = $zdbh->prepare($sql);
		$sql->execute();
		if($sql->rowCount())
		{
			$res = $sql->fetch();
			return $res['x_value'] ;  
		}
		else
		{
			$sql = "SELECT  * FROM x_apache_setting WHERE x_parameter='$key'";
			$sql = $zdbh->prepare($sql);
			$sql->execute();
			$res = $sql->fetch();
			return $res['x_value'] ; 
		}
	}
	
	static function getKeepAlive_setting_On()
	{
		$get_value=self::getValueBasedOnKey('KeepAlive_setting');
		if($get_value=="On")
		return 'Checked="Checked"' ;
		else
		return '';
	}
	
	static function getKeepAlive_setting_Off()
	{
		$get_value=self::getValueBasedOnKey('KeepAlive_setting');
		if($get_value=="Off")
		return 'Checked="Checked"' ;
		else
		return '';
	}
	static function getStartServers()
	{
		return self::getValueBasedOnKey('StartServers');
	}
	static function getServerLimit()
	{
		return self::getValueBasedOnKey('ServerLimit');
	}
	static function getMinSpareServers()
	{
		return self::getValueBasedOnKey('MinSpareServers');
	}
	static function getMaxSpareServers()
	{
		return self::getValueBasedOnKey('MaxSpareServers');
	}
	static function getsession_save_path()
	{
		return self::getValueBasedOnKey('session_save_path');
	}
	static function getMaxRequestsPerChild()
	{
		return self::getValueBasedOnKey('MaxRequestsPerChild');
	}
	static function getMaxRequestWorkers()
	{
		return self::getValueBasedOnKey('MaxRequestWorkers');
	}
	static function doUpdateconfig()
	{
		global $controller; 
		 

		$formvars = $controller->GetAllControllerRequests('FORM');
		
		if(!isset($formvars['KeepAlive_setting']))
		{
			self::$blank_KeepAlive_setting = true;
			return false;
		}
		if(($formvars['StartServers'] == ""))
		{
			self::$blank_StartServers = true;
			return false;
		}
		if($formvars['ServerLimit'] == "")
		{
			self::$blank_ServerLimit = true;
			return false;
		}
		if(($formvars['MinSpareServers'] == ""))
		{
			self::$blank_MinSpareServers = true;
			return false;
		}
		if(($formvars['MaxSpareServers'] == ""))
		{
			self::$blank_MaxSpareServers = true;
			return false;
		}
		if(($formvars['MaxRequestsPerChild'] == ""))
		{
			self::$blank_MaxRequestsPerChild = true;
			return false;
		}
		if(($formvars['MaxRequestWorkers'] == ""))
		{
			self::$blank_MaxRequestWorkers = true;
			return false;
		}
		//print_r($formvars); exit;
		if (self::ExecuteUpdateConfig($formvars['KeepAlive_setting'], $formvars['StartServers'], $formvars['ServerLimit'], $formvars['MinSpareServers'], $formvars['MaxSpareServers'], $formvars['MaxRequestsPerChild'], $formvars['MaxRequestWorkers']))
		return true;
        return false; 
  }

  static function ExecuteUpdateConfig($KeepAlive_setting, $StartServers, $ServerLimit, $MinSpareServers, $MaxSpareServers, $MaxRequestsPerChild, $MaxRequestWorkers)
  {
	 //echo "KeepAlive_setting =>".$KeepAlive_setting."\n StartServers =>".$StartServers."\n MaxRequestWorkers =>".$MaxRequestWorkers."\n MaxRequestsPerChild =>".$MaxRequestsPerChild."\n ServerLimit =>".$ServerLimit."\n MinSpareServers =>".$MinSpareServers."\nMaxSpareServers =>".$MaxSpareServers;
	//exit;
	global $zdbh;
	global $controller; 
	  	  
	$service_port = 4444 ;
	$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;
	}
	  else
		{
		 socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 1, "usec" =>0));
                $in="command Fcgi_Parameter_Change $StartServers $ServerLimit $MinSpareServers $MaxSpareServers $MaxRequestsPerChild $MaxRequestWorkers $KeepAlive_setting";
		       
                socket_write($socket, $in, strlen($in));
                socket_close($socket);
		  	$sql = "Update x_apache_setting set x_value=:x_value WHERE x_parameter='KeepAlive_setting'";
	$sql = $zdbh->prepare($sql);
	$sql->bindParam(':x_value', $KeepAlive_setting);
	
	$sql->execute();
	unset($sql);
	  
	$sql = "Update x_apache_setting set x_value=:x_value WHERE x_parameter='StartServers'";
	$sql = $zdbh->prepare($sql);
	$sql->bindParam(':x_value', $StartServers);
	$sql->execute();
	unset($sql);
	  
	$sql = "Update x_apache_setting set x_value=:x_value WHERE x_parameter='ServerLimit'";
	$sql = $zdbh->prepare($sql);
	$sql->bindParam(':x_value', $ServerLimit);
	$sql->execute();
	unset($sql);
	$sql = "Update x_apache_setting set x_value=:x_value WHERE x_parameter='MinSpareServers'";
	$sql = $zdbh->prepare($sql);
	$sql->bindParam(':x_value', $MinSpareServers);
	$sql->execute();
	unset($sql);
	$sql = "Update x_apache_setting set x_value=:x_value WHERE x_parameter='MaxSpareServers'";
	$sql = $zdbh->prepare($sql);
	$sql->bindParam(':x_value', $MaxSpareServers);
	$sql->execute();
	unset($sql);
	$sql = "Update x_apache_setting set x_value=:x_value WHERE x_parameter='MaxRequestsPerChild'";
	$sql = $zdbh->prepare($sql);
	$sql->bindParam(':x_value', $MaxRequestsPerChild);
	$sql->execute();
	unset($sql);
	$sql = "Update x_apache_setting set x_value=:x_value WHERE x_parameter='MaxRequestWorkers'";
	$sql = $zdbh->prepare($sql);
	$sql->bindParam(':x_value', $MaxRequestWorkers);
	$sql->execute();
	unset($sql);  
		self::$ok=true;
		return true;
	} 
	  
	  
  
	 
	    
	$service_port = 4444 ;
	$address = gethostbyname('localhost');
	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	

	  
	  	  
     
	    
	  
	self::$ok = true;
	  
	return false;
	  
  exit;
	  
	$service_port = 4444 ;
	$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;
	}
        global $zdbh;
        global $controller;
        $currentuser = ctrl_users::GetUserDetail();
        $current_user_id=$currentuser['userid'];
        if($current_user_id == 1)
        {
            $in="command execphpconfig ";
        }
        else
        {
            $in="command phpConfigBasedonUser ";
        }

    	//$memory_limit=="-1" ? $memory_limit=$memory_limit : $memory_limit.="M";
        //$upload_max_filesize.="M";
       // $post_max_size.="M";
        $in .= str_replace(' ', '', $KeepAlive_setting)." ".
        str_replace(' ', '',$StartServers)." ".
		str_replace(' ', '',$ServerLimit)." ".	
        str_replace(' ', '',$MinSpareServers)." ".
        str_replace(' ', '', $MaxSpareServers)." ".
        str_replace(' ', '', $MaxRequestsPerChild)." ".
        str_replace(' ', '', $MaxRequestWorkers);

        if($current_user_id != 1)
        {
            $current_user_name =$currentuser['username'];
            $vhost_path = ctrl_options::GetSystemOption('hosted_dir') . $current_user_name."/";
            $in .= " ".str_replace(' ', '', $current_user_name)." ".  
                    str_replace(' ', '', $vhost_path);
        }
    
        socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 2000, "usec" =>0));
        $out = '';
        socket_write($socket, $in, strlen($in));
	$socket_res = socket_read($socket,1024);
	$socket_res = trim($socket_res);
	socket_close($socket);
	if($socket_res == 'success')
	{
		$php_conf_value_array = array();
		$php_conf_value_array['MaxSpareServers'] = $MaxSpareServers;
		$php_conf_value_array['MaxRequestsPerChild'] = $MaxRequestsPerChild;
		$php_conf_value_array['MaxRequestWorkers'] = $MaxRequestWorkers;
                $php_conf_value_array['MinSpareServers'] = $MinSpareServers;
                $php_conf_value_array['StartServers'] = $StartServers;
		         $php_conf_value_array['ServerLimit'] = $ServerLimit;
                $php_conf_value_array['KeepAlive_setting'] = $KeepAlive_setting;
		foreach($php_conf_value_array as $key => $value)
                {
			$sql = "SELECT * FROM x_apache_setting WHERE x_parameter='$key'";
			$sql = $zdbh->prepare($sql);
			$sql->execute();
			$res = $sql->fetch();
			if($res['x_id'])
			{
				$sql1 = "UPDATE x_apache_setting SET x_value='$value' WHERE x_parameter = '$key'";
			}
			else
			{
				$sql1 = "INSERT INTO x_apache_setting (x_value, x_parameter) VALUES ('$value', '$key')";
			}
			$sql1 = $zdbh->prepare($sql1);
			$sql1->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_varnish set x_isactive=0");
	    $sql->execute();

		self::SetCallDaemon();
		self::$Ok=true;
		return true ; 
	}
	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 getResult()
    {
		if (!fs_director::CheckForEmptyValue(self::$tryagain)) {
		    return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please try again Later. Server service not available."), "Error");
		}
		
		if (!fs_director::CheckForEmptyValue(self::$ok)) {
    		return ui_sysmessage::shout(ui_language::translate("<strong>Success:</strong> Apache configuration updated successfully."), "Success");
		}
		
		if (!fs_director::CheckForEmptyValue(self::$blank_KeepAlive_setting)) {
	    	return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Select any one of the options in KeepAlive_setting ."), "Error");
		}
		
		if (!fs_director::CheckForEmptyValue(self::$blank_StartServers)) {
		    return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the StartServers ."), "Error");
		}
		
		if (!fs_director::CheckForEmptyValue(self::$blank_ServerLimit)) {
		    return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the ServerLimit ."), "Error");
		}
		
        if (!fs_director::CheckForEmptyValue(self::$blank_MinSpareServers)) {
            return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the MinSpareServers ."), "Error");
		}
		
        if (!fs_director::CheckForEmptyValue(self::$blank_MaxSpareServers)) {
	    	return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the MaxSpareServers ."), "Error");
		}
		
        if (!fs_director::CheckForEmptyValue(self::$blank_session_save_path)) {
    		return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the session.save_path ."), "Error");
		}
		
        if (!fs_director::CheckForEmptyValue(self::$blank_MaxRequestsPerChild)) {
		    return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the MaxRequestsPerChild ."), "Error");
		}

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

  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;
    }

}