Current File : //etc/sentora/panel/modules/apachetotomcat/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 $complete;
    static $error;
    static $writeerror;
    static $nosub;
    static $alreadyexists;
    static $badname;
    static $blank;
    static $ok;
	static $del;
	static $validemail;
    static $badpointing;    
	static function get_client_ip() {
    $ipaddress = '';
				if (isset($_SERVER['HTTP_CLIENT_IP']))
						$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
				else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
						$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
				else if(isset($_SERVER['HTTP_X_FORWARDED']))
						$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
				else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
						$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
				else if(isset($_SERVER['HTTP_FORWARDED']))
						$ipaddress = $_SERVER['HTTP_FORWARDED'];
				else if(isset($_SERVER['REMOTE_ADDR']))
						$ipaddress = $_SERVER['REMOTE_ADDR'];
				else  if (getenv('HTTP_CLIENT_IP'))
						$ipaddress = getenv('HTTP_CLIENT_IP');
				else if(getenv('HTTP_X_FORWARDED_FOR'))
						$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
				else if(getenv('HTTP_X_FORWARDED'))
						$ipaddress = getenv('HTTP_X_FORWARDED');
				else if(getenv('HTTP_FORWARDED_FOR'))
						$ipaddress = getenv('HTTP_FORWARDED_FOR');
				else if(getenv('HTTP_FORWARDED'))
						$ipaddress = getenv('HTTP_FORWARDED');
				else if(getenv('REMOTE_ADDR'))
						$ipaddress = getenv('REMOTE_ADDR');
				else 
						$ipaddress = 'UNKNOWN';
    return $ipaddress;
}
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 doapachetotomcatdelete()
	{
		 global $zdbh;
		 $id=$_REQUEST["inDelete"];
		 
        $time = time();
		$x_deleted_ip=self::get_client_ip();
        $sql = $zdbh->prepare("UPDATE x_apache_tomcat  SET x_delete=:time,x_deleted_ip=:x_deleted_ip WHERE x_id=:id");
		$sql->bindParam(':time', $time);
		$sql->bindParam(':x_deleted_ip',$x_deleted_ip);
        $sql->bindParam(':id', $id);
        $sql->execute();
		self::SetCallDaemon();
  	      $retval = TRUE;
        return $retval;
	}
	static function doapachetotomcat()
	{
			global $zdbh;
			global $controller;
			$domain_name=trim($_POST['inDomain']);
			$retval = FALSE;
			if(empty($domain_name))
			{
						self::$blank = TRUE;
			}
			else 
			{
						$currentuser = ctrl_users::GetUserDetail();   
						$time = time();
						$uid = $currentuser['userid'];
						$get_ip=self::get_client_ip();
						$sql = $zdbh->prepare("INSERT INTO x_apache_tomcat (`userid`, `x_domain_id`,`x_created_ip`,`x_status`) VALUES ('".$uid."', '".$domain_name."','".$get_ip."','0');");
						$sql->execute();
						self::SetCallDaemon();
						self::$ok = TRUE;
						$retval = TRUE;
			}
			return $retval;
	}
	
	static function getCreatedDomain()
    {
        $currentuser = ctrl_users::GetUserDetail();
        return self::ListDomain($currentuser['userid']);
    }
	
	static public function getshowformcontent()
    {
        global $zdbh;
        global $controller;

        $currentuser = ctrl_users::GetUserDetail();
		$sql = "SELECT a.x_id,a.x_domain_id,b.vh_name_vc FROM x_apache_tomcat a,x_vhosts b  WHERE  a.x_delete IS NULL AND a.userid=:userid AND a.x_domain_id=b.vh_id_pk";
		$numrows = $zdbh->prepare($sql);
        $numrows->bindParam(':userid', $currentuser['userid']);
        $numrows->execute();
		
		if ($numrows->fetchColumn() > 0) {
		
            $sql = $zdbh->prepare($sql);
            $sql->bindParam(':userid', $currentuser['userid']);
            $res = array();
            $sql->execute();
			
			$line .= "<h2>" . ui_language::translate("Tomcat domain List") . "</h2>";
			$line .='<form action="./?module=apachetotomcat&action=ConfirmDeleteSsl" method="post">';
			$line .= "<table>";
			$line .= '<tr><th>Domain</th></tr>';
			while ($rowdomain = $sql->fetch()) {
                $listdomain = $rowdomain['vh_name_vc'];
				$listid = $rowdomain['x_id'];		
				$line .= '<tr><td>'.$listdomain.'</td><td><button class="button-loader delete btn btn-danger" type="submit" id="button" name="inDelete_'.$listid.'" id="inDelete_'.$listid.'" value="inDelete_'.$listid.'" >Delete</button></td>';
		
					$line .='</tr>';
				$line .="";
				$op = '';
			}
			$line .= '</table>';
			$line .='</form>';
			return $line;
        } else {
	return  "Your all domains running under Apache Only.";
        }
    }
	
	
	static function ListDomain($uid)
    {
        global $zdbh;
        global $controller;
			
        // $currentuser = ctrl_users::GetUserDetail($uid);
		$sql="SELECT a.x_id,a.x_domain_id,b.vh_name_vc FROM x_apache_tomcat a,x_vhosts b  WHERE  a.x_delete IS NULL AND a.userid=:userid AND a.x_domain_id=b.vh_id_pk";
		//$sql = "SELECT * FROM x_apache_tomcat WHERE x_delete IS NULL AND userid=:userid";
        $numrows = $zdbh->prepare($sql);
        $numrows->bindParam(':userid', $uid);
        $numrows->execute();
		
        if ($numrows->fetchColumn() > 0) {
            $sql = $zdbh->prepare($sql);
            $sql->bindParam(':userid', $uid);
            $res = array();
            $sql->execute();
			
            while ($rowdomain = $sql->fetch()) {
                $res[] = array('x_domain_id' => $rowdomain['vh_name_vc'], 
							   'x_id' => $rowdomain['x_id']);		
            }
            return $res;
        } else {
            return false;
        }
    }
	
	static function doConfirmDeleteSsl()
    {
		       global $zdbh;
        global $controller;
       
        $currentuser = ctrl_users::GetUserDetail();
        $formvars = $controller->GetAllControllerRequests('FORM');
		$get_all_list=self::ListDomain($currentuser['userid']);
		foreach (self::ListDomain($currentuser['userid']) as $row) 
		{
			if (isset($formvars['inDelete_' . $row['x_id'] . ''])) 
			{
			header('location: ./?module=' . $controller->GetCurrentModule() . '&show=Delete&id=' . $row['x_id'] . '&domain=' . $row['x_domain_id']);
			exit;
			} 
		}
		exit;
        return false;
    }
		static function commandcreation($in)
	{
	$service_port = 4444;
	$address = gethostbyname('localhost');
	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	if ($socket === false) {
		self::$error= TRUE;
		return FALSE;
	} 
        $result = socket_connect($socket, $address, $service_port);
        if ($result === false) {
			self::$error= TRUE;
			return FALSE;
        } 
				socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 2000, "usec" =>0));
                $out = '';
                socket_write($socket, $in, strlen($in));
				socket_close($socket);
				sleep(1);
	}
	
	static function changepermissionstart($java_file_path){
	self::commandcreation("command permissionstarttomcat $java_file_path");
	return;
	}
	static function changepermissionend($java_file_path){
	 self::commandcreation("command permissionendtomcat $java_file_path");
	
	return;
	}
	static function deploy($java_file_path){
	self::commandcreation("command deploytomcat $java_file_path");
	return;
	}
	static function tomcatrestart()
	{
	self::commandcreation("command restarttomcat");
	return;
	}
	
	static function getisDeleteDomain()
    {
        global $controller;
        $urlvars = $controller->GetAllControllerRequests('URL');
        return (isset($urlvars['show'])) && ($urlvars['show'] == 'Delete');
    }
	
	static function getCurrentID()
    {
        global $controller;
        $id = $controller->GetControllerRequest('URL', 'id');
        return ($id) ? $id : '';
    }

    static function getCurrentDomain()
    {
        global $controller;
        $domain = $controller->GetControllerRequest('URL', 'domain');
        return ($domain) ? $domain : '';
    }
	
	static function doDeleteSslDomain()
    {
		print_r($_POST);
		echo $_POST['inDelete'];
        global $controller;
        if (isset($_POST['inDelete'])) {
            if (self::ExecuteDeleteSslDomain($_POST['inDelete'])) {
                self::$del = TRUE;
                return true;
            }
        }
        return false;
    }
	

    static function ListDomains($uid)
    {
        global $zdbh;
        $currentuser = ctrl_users::GetUserDetail($uid);
        $sql = "SELECT vh_id_pk,vh_name_vc FROM x_vhosts WHERE vh_acc_fk=:userid AND vh_enabled_in=1 AND vh_deleted_ts IS NULL AND vh_id_pk NOT IN (select x_domain_id from x_apache_tomcat where x_delete IS NULL And userid=:userid) ORDER BY vh_name_vc ASC";
        $numrows = $zdbh->prepare($sql);
        $numrows->bindParam(':userid', $currentuser['userid']);
        $numrows->execute();
		$domaincount = $numrows->fetchColumn();
        if ($domaincount > 0) {
            $sql = $zdbh->prepare($sql);
            $sql->bindParam(':userid', $currentuser['userid']);
            $res = array();
            $sql->execute();
            while ($rowdomains = $sql->fetch()) {
                $res[] = array('domain' => ui_language::translate($rowdomains['vh_name_vc']),'domain_id' => ui_language::translate($rowdomains['vh_id_pk']));
            }
            return $res;
        } else {
            return false;
        }
    }
	
	
    static function getDomainList()
    {
        $currentuser = ctrl_users::GetUserDetail();
        return self::ListDomains($currentuser['userid']);
    }
        
	static function getResult()
    {
        if (!fs_director::CheckForEmptyValue(self::$blank)) {
            return ui_sysmessage::shout(ui_language::translate("Your Domain can not be empty. Please select a valid Domain Name and try again."), "Error");
        }
        if (!fs_director::CheckForEmptyValue(self::$badname)) {
            return ui_sysmessage::shout(ui_language::translate("Your Domain name is not valid. Please enter a valid Domain Name: i.e. 'domain.com'"), "zannounceerror");
        }
        if (!fs_director::CheckForEmptyValue(self::$badpointing)) {
            return ui_sysmessage::shout(ui_language::translate("Your Domain name is not pointing to the IP"), "zannounceerror");
        }
        if (!fs_director::CheckForEmptyValue(self::$alreadyexists)) {
            return ui_sysmessage::shout(ui_language::translate("Your Domain name is not registred on this server."), "zannounceerror");
        }
        if (!fs_director::CheckForEmptyValue(self::$error)) {
            return ui_sysmessage::shout(ui_language::translate("Please remove 'www'. The 'www' will automatically work with all Domains / Subdomains."), "zannounceerror");
        }
        if (!fs_director::CheckForEmptyValue(self::$writeerror)) {
            return ui_sysmessage::shout(ui_language::translate("There was a problem writting to the virtual host container file. Please contact your administrator and report this error. Your domain will not function until this error is corrected."), "zannounceerror");
        }
        if (!fs_director::CheckForEmptyValue(self::$ok)) {
            return ui_sysmessage::shout(ui_language::translate("Successfully migrated from Apache to Tomcat"), "OK");
        }
		if (!fs_director::CheckForEmptyValue(self::$del)) {
            return ui_sysmessage::shout(ui_language::translate("SSL Certificate deleted successfully."), "zannounceok");
        }
		if (!fs_director::CheckForEmptyValue(self::$validemail)) {
            return ui_sysmessage::shout(ui_language::translate("Your email address is not valid."), "zannounceerror");
        }
        return "";
    }
 	
}