Current File : //etc/zpanel/panel/modules/phpconfig/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_file_uploads;
  	static $blank_max_execution_time;
  	static $blank_max_input_time;
  	static $blank_memory_limit;
  	static $blank_session_save_path;
  	static $blank_upload_max_filesize;
  	static $blank_post_max_size;
  	static $Ok;
	
	static function getValueBasedOnKey($key)
	{
		global $zdbh;
		global $controller; 
		$currentuser = ctrl_users::GetUserDetail();
		$current_user_id=$currentuser['userid'];
		$sql = "SELECT  * FROM x_php_config WHERE x_clearname='$key' AND x_old_value='$current_user_id'";
		$sql = $zdbh->prepare($sql);
		$sql->execute();
		if($sql->rowCount())
		{
			$res = $sql->fetch();
			return $res['x_value'] ;  
		}
		else
		{
			$sql = "SELECT  * FROM x_php_config WHERE x_clearname='$key' AND x_old_value='1'";
			$sql = $zdbh->prepare($sql);
			$sql->execute();
			$res = $sql->fetch();
			return $res['x_value'] ; 
		}
	}

	  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 getfile_uploads_On()
	{
		$get_value=self::getValueBasedOnKey('file_uploads');
		if($get_value=="On")
		return 'Checked="Checked"' ;
		else
		return '';
	}
	
	static function getfile_uploads_Off()
	{
		$get_value=self::getValueBasedOnKey('file_uploads');
		if($get_value=="Off")
		return 'Checked="Checked"' ;
		else
		return '';
	}
	static function getmax_execution_time()
	{
		return self::getValueBasedOnKey('max_execution_time');
	}
	static function getmax_input_time()
	{
		return self::getValueBasedOnKey('max_input_time');
	}
	static function getmemory_limit()
	{
		return self::getValueBasedOnKey('memory_limit');
	}
	static function getsession_save_path()
	{
		return self::getValueBasedOnKey('session_save_path');
	}
	static function getupload_max_filesize()
	{
		return self::getValueBasedOnKey('upload_max_filesize');
	}
	static function getpost_max_size()
	{
		return self::getValueBasedOnKey('post_max_size');
	}
	static function doUpdateconfig()
	{
		global $controller; 
	
		$formvars = $controller->GetAllControllerRequests('FORM');
		if(!isset($formvars['file_uploads']))
		{
			self::$blank_file_uploads = true;
			return false;
		}
		if(!isset($formvars['max_execution_time']))
		{
			self::$blank_max_execution_time = true;
			return false;
		}
		if(!isset($formvars['max_input_time']))
		{
			self::$blank_max_input_time = true;
			return false;
		}
		if(!isset($formvars['memory_limit']))
		{
			self::$blank_memory_limit = true;
			return false;
		}
		if(!isset($formvars['upload_max_filesize']))
		{
			self::$blank_upload_max_filesize = true;
			return false;
		}
		if(!isset($formvars['post_max_size']))
		{
			self::$blank_post_max_size = true;
			return false;
		}
		if(!isset($formvars['session_save_path']))
		{
			self::$blank_session_save_path = true;
			return false;
		}
        
		if (self::ExecuteUpdateConfig($formvars['file_uploads'], $formvars['max_execution_time'], $formvars['max_input_time'], $formvars['memory_limit'], $formvars['upload_max_filesize'], $formvars['post_max_size'],$formvars['session_save_path']))
			return true;
        	return false; 
  	}

  	static function ExecuteUpdateConfig($file_uploads, $max_execution_time, $max_input_time, $memory_limit, $upload_max_filesize, $post_max_size,$session_save_path)
  	{
		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;
		}
		$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(' ', '', $file_uploads)." ".
		str_replace(' ', '',$max_execution_time)." ".
		str_replace(' ', '',$max_input_time)." ".
		str_replace(' ', '', $memory_limit)." ".
		str_replace(' ', '', $upload_max_filesize)." ".
		str_replace(' ', '', $post_max_size);

		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['memory_limit'] = rtrim($memory_limit,"M");
			$php_conf_value_array['upload_max_filesize'] = rtrim($upload_max_filesize,"M");
			$php_conf_value_array['post_max_size'] = rtrim($post_max_size,"M");
			$php_conf_value_array['max_input_time'] = $max_input_time;
			$php_conf_value_array['max_execution_time'] = $max_execution_time;
			$php_conf_value_array['file_uploads'] = $file_uploads;
			foreach($php_conf_value_array as $key => $value)
			{
				$sql = "SELECT * FROM x_php_config WHERE x_clearname='$key' AND x_old_value='$current_user_id'";
				$sql = $zdbh->prepare($sql);
				$sql->execute();
				$res = $sql->fetch();
				if($res['x_id'])
				{
					$sql1 = "UPDATE x_php_config SET x_value='$value' WHERE x_old_value='$current_user_id' AND x_clearname = '$key'";
				}
				else
				{
					$sql1 = "INSERT INTO x_php_config (x_value, x_old_value, x_clearname) VALUES ('$value', '$current_user_id', '$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> PHP configuration updated successfully."), "Success");
		}
		
		if (!fs_director::CheckForEmptyValue(self::$blank_file_uploads)) {
	    		return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Select any one of the options in file_uploads ."), "Error");
		}
		
		if (!fs_director::CheckForEmptyValue(self::$blank_max_execution_time)) {
		    	return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the max_execution_time ."), "Error");
		}
		
        	if (!fs_director::CheckForEmptyValue(self::$blank_max_input_time)) {
            		return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the max_input_time ."), "Error");
		}
		
        	if (!fs_director::CheckForEmptyValue(self::$blank_memory_limit)) {
	    		return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the memory_limit ."), "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_upload_max_filesize)) {
		    	return ui_sysmessage::shout(ui_language::translate("<strong>Error:</strong> Please enter the upload_max_filesize ."), "Error");
		}

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