Current File : //root/panel/modules/addip/code/controller.ext.php_old
<?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
{

	public static $m_error;
	public static $f_status = false;
	//public static $ntw_path = "/etc/sysconfig/network-scripts/ifcfg-eth0";
	public static $ntw_path = "";
	static function n_sanitize($data)
	{
		$data = trim($data);
		$data = stripslashes($data);
		$data = htmlspecialchars($data);
		return $data;
	}
	
	static function getntwpath()
	{
		if(self::$f_status) {}
		else{
			$file_detail = shell_exec("service network status");
			$file_suffix = "";
			if (strlen(self::remove_space($file_detail)) > 0 )
			{
				$ntw_file_array	= array_filter(explode("\n",$file_detail));
				$ntw_file_cnt	= count($ntw_file_array);
				for ($h = 0 ; $h < $ntw_file_cnt; $h++)
				{
					if(self::$f_status) { }
					else{
						
						$ntw_file_str 	= self::trim_space($ntw_file_array[$h]);
						$e 		= explode(" ",$ntw_file_str);
						if($e[0] == "lo")
						{
							if(array_key_exists("1",$e))
							{
								self::$f_status = true;
								self::$ntw_path	= "/etc/sysconfig/network-scripts/ifcfg-".self::remove_space($e[1]);
							}
						}			
					}
				}
			}
		}
		return 	self::$ntw_path;
	}

	static  function getallip()
	{
		global $zdbh;
		if(function_exists('shell_exec')) {
			$ip_detail1 = shell_exec("sh /scripts/mailip.sh getip ");
			$ip_detail2 = trim($ip_detail1);
			if($ip_detail2 !="")
			{
				$pos_detail=strpos($ip_detail2,"@");
				$current_ip="";
				$ip_detail4 = $ip_detail2;
				if($pos_detail)
				{
					$allip=explode("@",$ip_detail2);
					$current_ip = $allip[0];
					$ip_detail4 = $allip[1];
				}
				$ip_detail = array();
				array_push($ip_detail,array("ip_data"=>0,"ip_text"=>"Select private ip"));
				$ip_detail3 = explode(" ",$ip_detail4);
				foreach($ip_detail3 as  $ad)
				{
					$all_ip = array();
					$stmt1 = $zdbh->prepare("select priv_ip from x_serverip where ip_deleted IS NULL");
					$stmt1->execute();
					$row_count1 = $stmt1->rowCount();
					if($row_count1 >0)
					{
						while($rows=$stmt1->fetch())
						{
							array_push($all_ip,$rows['priv_ip']);
						}
					}
					$ad2 = trim(preg_replace('/\s+/',' ', $ad));
					$ad1 = str_replace(" ","",$ad2);
					if (filter_var($ad1, FILTER_VALIDATE_IP)) {
						if(!in_array($ad1,$all_ip))
						{
							array_push($ip_detail,array("ip_data"=>$ad1,"ip_text"=>$ad1));
						}
					}
				}
				//print_r($ip_detail);
				//exit;
				return $ip_detail;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}

	static function call_socket($cmd_detail,$status_code)
	{
                $service_port = 4444 ;
                $address = gethostbyname('localhost');
                $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                if ($socket === false) {
                        return "";
                }
                $result = socket_connect($socket, $address, $service_port);
                if ($result === false) {
                        return "";
                }
                socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 20, "usec" =>0));
                $in="command ".$cmd_detail;
                $out = '';
                if(@socket_write($socket, $in, strlen($in)))
                {
                        sleep(1);
                        $s_data = socket_read($socket,1024);
                        socket_close($socket);
			return $s_data;
                }
                else { return "";  }
        }
	
	static function shell_ex($command)
	{
		return shell_exec($command);
	}
	
	static function remove_space($str)
	{
		$str = trim(preg_replace('/\s+/','', $str));
		return $str;
	}
	
	static function trim_space($str)
        {
                $str = trim(preg_replace('/\s+/',' ', $str));
                return $str;
        }
	
	static function doCreateip()
	{
        	global $controller;
		global $zdbh;
	        runtime_csfr::Protect();
		$formvars = $controller->GetAllControllerRequests('FORM');
		if (array_key_exists("public_ip",$formvars) && array_key_exists("priv_ip",$formvars) && filter_var((isset($formvars['public_ip'])?$formvars['public_ip']:""), FILTER_VALIDATE_IP) && filter_var((isset($formvars['priv_ip'])?$formvars['priv_ip']:""),FILTER_VALIDATE_IP ))
		{
			self::getntwpath();			
			if(self::$f_status)
			{
				$public_ip 	= self::remove_space(self::n_sanitize($formvars['public_ip']));
				$private_ip 	= self::n_sanitize($formvars['priv_ip']);
				$shell_data	= self::call_socket("privip 1 pingCheck ".$private_ip);
				
				echo $shell_data;
				exit;

			
				$sh_d		= self::remove_space($shell_data);
				if(is_numeric($sh_d))
				{
					if($sh_d == 2)
					{
						$original_ip = self::remove_space(ctrl_options::GetSystemOption('server_ip'));
						if($public_ip == $original_ip)
						{
							self::$m_error = "Please use other public ip instead of orginal ip";
						}
						else
						{
							$command_detail	= "privip 2 readip ".self::$ntw_path;
							$ip_data 	= self::call_socket($command_detail);
							$ipdarray	= explode(" ",$ip_data);
							$count_ip	= count($ipdarray);
							$sta_code	= false;
							$check_str	= "IPADDR";
							$str_count	= strlen($check_str);
							$cnt_number	= self::readIpaddrSeq();
							if($cnt_number)
							{
								$stmt5 = $zdbh->prepare("select id from x_serverip where  public_ip =:public_ip_detail and ip_deleted IS NULL ");
								$stmt5->bindParam(":public_ip_detail",$public_ip);
								$stmt5->execute();
								$row_count5 = $stmt5->rowCount();
								if($row_count5 > 0)
								{
									self::$m_error = "Public ip is already added in server, please choose other ip";
								}
								else
								{
									$ip_addr = "IPADDR".$cnt_number."=".$private_ip;		
									$command_detail1 = "privip 3 addip ".self::$ntw_path." ".$ip_addr;
									$ip_data1        = self::call_socket($command_detail1);
									//self::shell_ex("sh /script/addip.sh 
									$stmt1 = $zdbh->prepare("select priv_ip from x_serverip where priv_ip =:ip_detail and ip_deleted IS NULL ");
        	        						$stmt1->bindParam(':ip_detail',$private_ip);
									$stmt1->execute();
									$row_count1 = $stmt1->rowCount();
									if($row_count1 >0)
									{
										$stmt2 = $zdbh->prepare("update x_serverip set public_ip =:public_ip_detail  where priv_ip =: priv_ip_detail and ip_deleted IS NULL ");
										$stmt2->bindParam(":public_ip_detail",$public_ip);
										$stmt2->bindParam(":priv_ip_detail",$priv_ip);
										$stmt2->execute();
									}
									else
									{
										date_default_timezone_set("UTC");
										$current_time = date("Y-m-d H:i:s");
										$current_str_time =  strtotime($current_time);
										$stmt=$zdbh->prepare("insert into x_serverip (priv_ip,public_ip,ip_created) values (:priv_ip,:public_ip,:ip_created) ");
										$stmt->bindParam(':priv_ip',$private_ip);
										$stmt->bindParam(':public_ip',$public_ip);
										$stmt->bindParam(':ip_created',$current_str_time);
										$stmt->execute();
									}
									#header("location: ./?module=" . $controller->GetCurrentModule() );
									self::$m_error = "New ip added successfully";
								}
				        		}
							else { self::$m_error = "Due to some server issue, unable to add private ip"; }
						}
					}
					else
					{
						self::$m_error = "Private ip is already added in server, please choose other ip";
					}
				}
				else
				{
					self::$m_error = "Due to some server issue, unable to add private ip";
				}
			}
			else
			{
				self::$m_error = "Getting some server issue, please contact your system admin";
			}
		}
		else
		{
			self::$m_error = "Please enter all field";
		}
	}
        
	static function getall_ip_detail()
	{
		global $zdbh;
		$stmt3 =$zdbh->prepare("select id,priv_ip,public_ip from x_serverip where ip_deleted IS NULL ");
		$stmt3->execute();
		$row_count3 = $stmt3->rowCount();
		if($row_count3 >0)
		{
			$ip_array 	= array();
			$sl_no 		= 1;
			while($rows=$stmt3->fetch())
			{
				$priv_ip	= $rows['priv_ip'];
				$public_ip	= $rows['public_ip'];
				array_push($ip_array,array('sl_no'=> $sl_no,'priv' => $rows['priv_ip'],'public' => $rows['public_ip'],"a_id"=>$rows['id']));  
				$sl_no++;
                	}
			return $ip_array;
		}
		else
		{
			return false;
		}
        }
    
	static function  doconform_deleteip()
	{
		global $zdbh;
		global $controller;
		runtime_csfr::Protect();
		$formvars = $controller->GetAllControllerRequests('FORM');
		$autoresdata =self::n_sanitize($formvars['autoresdata']);
		$status_data = self::n_sanitize($formvars['st_data']);
		if(array_key_exists("autoresdata",$formvars) && array_key_exists("st_data",$formvars) )
		{
			if ( !fs_director::CheckForEmptyValue($autoresdata) && is_numeric($autoresdata) && !fs_director::CheckForEmptyValue($status_data) && is_numeric($status_data)  )
			{  
				$stmt3 =$zdbh->prepare("select id,public_ip from x_serverip where id =:vhid and ip_deleted IS NULL ");
				$stmt3->bindParam(':vhid',$autoresdata);
				$stmt3->execute();
				$row_count3 = $stmt3->rowCount();
				if($row_count3 >0)
				{
					$rows=$stmt3->fetch();
					if($status_data == 2)
					{
						header("location: ./?module=" . $controller->GetCurrentModule() . "&show=Delete&id=" . $rows['id']."&ip=".$rows['public_ip'] );
					}
					else
					{
						header("location: ./?module=" . $controller->GetCurrentModule() . "&show=Edit&id=" . $rows['id'] );
					}
					exit;
				}
				else
				{
					header("location: ./?module=" . $controller->GetCurrentModule() );
					exit;                
				}
			}
			else
			{
				header("location: ./?module=" . $controller->GetCurrentModule() );
				exit;
			} 
		}
	        else
	        {
			header("location: ./?module=" . $controller->GetCurrentModule() );
			exit;
		}
	}

	static function getisDeleteip()
	{
		global $controller;
		$urlvars = $controller->GetAllControllerRequests('URL');
		return (isset($urlvars['show']) && isset($urlvars['ip'])) && ($urlvars['show'] == "Delete");        
	}

	static function getCurrentip()
	{
		global $controller;
		$auto_res = $controller->GetControllerRequest('URL', 'ip');
		return ($auto_res) ? $auto_res : '';
	}

	static function getid_data()
	{
		global $controller;
		$auto_resid = $controller->GetControllerRequest('URL', 'id');
		return ($auto_resid) ? $auto_resid : 0;
	}

	static function doDeleteip()
	{
		global $zdbh;
		global $controller;
		runtime_csfr::Protect();
		$formvars = $controller->GetAllControllerRequests('FORM');
		if(array_key_exists("inDelete",$formvars))
		{
			$auto_id =self::n_sanitize($formvars['inDelete']);
			$currentuser = ctrl_users::GetUserDetail();
			if ( !fs_director::CheckForEmptyValue($auto_id) && is_numeric($auto_id) && $auto_id > 0)
			{ 
				self::getntwpath();
				if(self::$f_status)
				{
					$stmt3 =$zdbh->prepare("select id,priv_ip from x_serverip  where id =:vhid and ip_deleted IS NULL ");
					$stmt3->bindParam(':vhid',$auto_id);
					$stmt3->execute();
					$row_count3 = $stmt3->rowCount();
					if($row_count3 >0)
					{
						$rows = $stmt3->fetch();
						$auto_ip = $rows['priv_ip'];
						date_default_timezone_set("UTC");
						$current_time = date("Y-m-d H:i:s");
						$current_str_time =  strtotime($current_time);
						$stmt4 =$zdbh->prepare("update x_serverip set ip_deleted =:str_time where id =:vhid");
						$stmt4->bindParam(':str_time',$current_str_time);
						$stmt4->bindParam(':vhid',$auto_id);
						$stmt4->execute();  
						$data 		= shell_exec("eval grep -n '".$auto_ip."' ".self::$ntw_path);
						$ip_array 	= array_filter(explode("\n",$data));
						$ip_array_count	= count($ip_array);
						$ip_line_array 	= array();
						for ($i = 0 ; $i < $ip_array_count; $i++)
						{
							$ip_string 	= self::remove_space($ip_array[$i]);
							if(strlen($ip_string) > 0)
							{
								$ip_line	= explode(":",$ip_string);
								$ip_line_no	= $ip_line[0];
								if(is_numeric($ip_line_no))
								{
									array_push($ip_line_array,$ip_line_no);
								}
							}
						} 	
						if(count($ip_line_array) > 0)
						{
							$shell_data     = self::call_socket("privip 4 ".self::$ntw_path." ".implode(" ",$ip_line_array));
						}
						/*********$affected_rows = $stmt4->rowCount(); *************/
						self::$m_error = "Ip remove successfully";
					}
					else 
					{
						self::$m_error = "Problem in delete public ip,please contact system admin";
					}
				}
				else
				{
					self::$m_error = "Getting some server issue, please contact your system admin";
				}

			}
			else
			{
				self::$m_error = "Problem in delete public ip,please contact system admin";
			}
		}
		else
		{
			self::$m_error = "Problem in delete public ip,please contact system admin ";
		}
	}

	static function getedit_auto()
	{
		global $controller;
		$urlvars = $controller->GetAllControllerRequests('URL');
		return (isset($urlvars['show']) && isset($urlvars['id']) && ($urlvars['show'] == "Edit") && is_numeric($urlvars['id']) ) ;
	}

	static function geteditipdata()
	{
		global $zdbh;
		global $controller;
		$auto_resid = $controller->GetControllerRequest('URL', 'id');
		if ( !fs_director::CheckForEmptyValue($auto_resid) && is_numeric($auto_resid) && $auto_resid > 0)
		{ 
			$stmt3 =$zdbh->prepare("select id,priv_ip,public_ip from x_serverip where id =:vhid  and ip_deleted IS NULL ");
			$stmt3->bindParam(':vhid',$auto_resid);
			$stmt3->execute();
			$row_count3 = $stmt3->rowCount();
			if($row_count3 >0)
			{
				$rows = $stmt3->fetch();
				$ip_array[]=array("id"=>$rows['id'],"public_ip"=>$rows['public_ip'],"private_ip"=>$rows['priv_ip']);
				return $ip_array;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	static function readIpaddrSeq()
        {
		self::getntwpath();
		if(self::$f_status)
		{
			$command_detail = "privip 2 readip ".self::$ntw_path;
	                $ip_data        = self::call_socket($command_detail);
	                $ipdarray       = explode(" ",$ip_data);
			$count_ip       = count($ipdarray);
			$sta_code       = false;
			$check_str      = "IPADDR";
			$str_count      = strlen($check_str);
			$cnt_number     = "";
			$num_array	= array();
			for($i = 0; $i< $count_ip; $i++)
			{
				$ip_str = self::trim_space($ipdarray[$i]);
				if (stripos($ip_str,$check_str) !== false && strtolower($ip_str[0]) =="i")
				{
					$sta_code       = true;
					$ipd            = explode("=",$ip_str);
					$ip_detail1     = $ipd[0];
					$numd           = self::remove_space(substr($ip_detail1,$str_count));
					if(is_numeric($numd))
					{
						array_push($num_array,$numd);
					}
				}
			}
			$sta = 0;
			if($sta_code)
			{
				$count_d = count($num_array);
				if($count_d > 0)
				{
					for($j = 0; $j <= $count_d;$j++)
					{
						$nv = $j+1;
						if(in_array($nv,$num_array))
						{	
						}
						else
						{
							return $nv;
						}	
					}
				}
				else
				{
					return 1;
				}
			}
			else
			{
				return -1;
			}
		}
		else
		{
			return -1;
		}
        }	
	static function getIpaddrSeq()
	{
		self::getntwpath();
		$command_detail = "privip 2 readip ".self::$ntw_path;
		$ip_data        = self::call_socket($command_detail);
		$ipdarray       = explode(" ",$ip_data);
		$count_ip       = count($ipdarray);
		$sta_code       = false;
		$check_str      = "IPADDR";
		$str_count      = strlen($check_str);
		$cnt_number     = "";
		for($i = 0; $i< $count_ip; $i++)
		{
			$ip_str = self::trim_space($ipdarray[$i]);
			if (stripos($ip_str,$check_str) !== false && strtolower($ip_str[0]) =="i")
			{
				$sta_code       = true;
				$ipd            = explode("=",$ip_str);
				$ip_detail1     = $ipd[0];
				$numd           = self::remove_space(substr($ip_detail1,$str_count));
				if(is_numeric($numd))
				{
					if(is_numeric($cnt_number)){
						if($numd > $cnt_number) {
						$cnt_number = $numd;
						}
					}
					else
					{
						$cnt_number = $numd;
					}
				}
			}
		}
		if($sta_code)
		{
			if(is_numeric($cnt_number))
			{
				$cnt_number = $cnt_number+1;
			}
			else
			{
				$cnt_number = 1;
			}
			return  $cnt_number;
		}
		else
		{
			return -1;
		}
	}

	static function doeditIp()
	{
		global $controller;
		global $zdbh;
		runtime_csfr::Protect();
		$formvars = $controller->GetAllControllerRequests('FORM');
		if (array_key_exists("private_ip",$formvars) && array_key_exists("public_ip",$formvars) && array_key_exists("eip_id",$formvars))
		{
			$auto_id 	= self::remove_space(self::n_sanitize($formvars['eip_id']));
			$Priv_Ip 	= self::remove_space(self::n_sanitize($formvars['private_ip']));
			$Public_Ip	= self::remove_space(self::n_sanitize($formvars['public_ip']));
			if (  fs_director::CheckForEmptyValue($Priv_Ip) || fs_director::CheckForEmptyValue($Public_Ip) || fs_director::CheckForEmptyValue($auto_id) || !is_numeric($auto_id) || !filter_var($Priv_Ip,FILTER_VALIDATE_IP ) || !filter_var($Public_Ip,FILTER_VALIDATE_IP ) )
			{
				self::$m_error = "Enter all field.....";
			}
			else
			{
				self::getntwpath();
				if(self::$f_status)
				{
					$original_ip = self::remove_space(ctrl_options::GetSystemOption('server_ip')); 
					if($Public_Ip == $original_ip)
					{
						self::$m_error = "Please use other public ip instead of orginal ip";
					}
					else
					{
						$stmt1 =$zdbh->prepare("select id,public_ip,priv_ip from x_serverip where id =:id AND ip_deleted IS NULL");
						$stmt1->bindParam(':id',$auto_id);
						$stmt1->execute();
						$row_count1 = $stmt1->rowCount();
						if($row_count1 >0)
						{
							$rows1 			= $stmt1->fetch();
							$d_Priv_Ip 		= $rows1['priv_ip'];
							date_default_timezone_set("UTC");
							$current_time 		= date("Y-m-d H:i:s");
							$current_str_time 	= strtotime($current_time);
							$proceed_status		= 1;
							$proceed_msg		= "";
							if($Priv_Ip == $d_Priv_Ip){  }
							else
							{
								$shell_data1     = self::call_socket("privip 1 pingCheck ".$Priv_Ip);
								$sh_d1           = self::remove_space($shell_data1);
					                        if(is_numeric($sh_d1))
								{
									if($sh_d1 == 2) { }
									else
									{
										$proceed_status	= 0;
										$proceed_msg 	= "Private is used, please choose differnt Ip";
									}
								}
								else {
									$proceed_status = 0;
									$proceed_msg	= "Due to some server issue, unable to add private ip";
								}
							}
							$seq	= self::readIpaddrSeq();
							if($seq > 0) { }
							else
							{
								$proceed_status = 0;
								$proceed_msg    = "Due to some server issue, unable to add private ip...";
							}
							if($proceed_status ==1)
							{
								$stmt=$zdbh->prepare("update x_serverip set public_ip=:publicip,priv_ip =:privateip,ip_modified=:str_time where id =:id and ip_deleted IS NULL ");
								$stmt->bindParam(':publicip',$Public_Ip);
								$stmt->bindParam(':privateip',$Priv_Ip);
								$stmt->bindParam(':id',$auto_id);
								$stmt->bindParam(':str_time',$current_str_time);
								$stmt->execute();
								if($Priv_Ip == $d_Priv_Ip)
								{
							
								}
								else
								{
									$data           = shell_exec("eval grep -n '".$d_Priv_Ip."' ".self::$ntw_path);
									$ip_array       = array_filter(explode("\n",$data));
									$ip_array_count = count($ip_array);
									$ip_line_array  = array();
									for ($i = 0 ; $i < $ip_array_count; $i++)
									{
										$ip_string      = self::remove_space($ip_array[$i]);
										if(strlen($ip_string) > 0)
										{
											$ip_line        = explode(":",$ip_string);
											$ip_line_no     = $ip_line[0];
											if(is_numeric($ip_line_no))
											{
												array_push($ip_line_array,$ip_line_no);
											}
										}
									}
									if(count($ip_line_array) > 0)
									{
										$shell_data     = self::call_socket("privip 4 ".self::$ntw_path." ".implode(" ",$ip_line_array));
									}
									$seq1    = self::readIpaddrSeq();
									$ip_addr = "IPADDR".$seq1."=".$Priv_Ip;
									$command_detail1 = "privip 3 addip ".self::$ntw_path." ".$ip_addr;
									$ip_data1        = self::call_socket($command_detail1);
								}
								self::$m_error = "Private ip updated successfully";
							}
							else
							{
								self::$m_error = $proceed_msg;
							}
						}
						else
						{
							self::$m_error = "Public ip is not exist in server.";
						}
					}
				}
				else
				{
					self::$m_error = "Getting some server issue, please contact your system admin";
				}
			}
		}
		else
		{
			self::$m_error = "Enter all field";
		}
	}

    static function dummyfunction()
    {
        global $zdbh;
        global $controller;
        echo "Hello Giri";
    }
    static function getresult()
    {
        if (!fs_director::CheckForEmptyValue(self::$m_error)) {
            return ui_sysmessage::shout(ui_language::translate(self::$m_error), "zannounceerror");
        }
         return;
    }
}