Current File : //etc/sentora/panel/modules/domain_forwarder/code/controller.ext.php
<?php
/**
 *
 * Domain Forwarder Module for ZPanel 10.1.0
 * Version : 102
 * Author :  Aderemi Adewale (modpluz @ ZPanel Forums)
 * Email : goremmy@gmail.com
 */

class module_controller {

    static $complete;
    static $error;
    static $badname;
    static $badnames;
    static $emptydomain;
    static $domainexists;
    static $emptyforward;
    static $forwardexists;
    static $ok;
    static $editing;
    static $limitreached;
    static $deletedOk;

   /* Load CSS and JS files */
    static function getInit() {
        global $controller;
        $line = '<link rel="stylesheet" type="text/css" href="modules/' . $controller->GetControllerRequest('URL', 'module') . '/assets/domain_forwarder.css">';
        $line .= '<script type="text/javascript" src="modules/' . $controller->GetControllerRequest('URL', 'module') . '/assets/domain_forwarder.js"></script>';
        return $line;
    }

    /**
     * The 'worker' methods.
     */
    static function getForwardList() {
        global $zdbh;
		
		$currentuser = ctrl_users::GetUserDetail();

        $sql = "SELECT x_forwarded_domains.*,x_vhosts.vh_name_vc FROM x_forwarded_domains INNER JOIN 
                    x_vhosts ON x_vhosts.vh_id_pk=x_forwarded_domains.vh_fk_id 
                    WHERE fd_acc_fk=:uid AND vh_acc_fk=:uid 
                    AND fd_deleted_ts IS NULL AND vh_deleted_ts IS NULL 
                    ORDER BY fd_id_pk DESC";
        $bindArray = array(':uid' => $currentuser['userid']);                                        
        $zdbh->bindQuery($sql, $bindArray);
        $rows = $zdbh->returnRows(); 

        if (count($rows) > 0) {
            $res = array();
            foreach($rows as $row_idx=>$row) {
                if($row['fd_type_id'] == 1){
                    $forward_type = 'Permanent(301)';
                } elseif($row['fd_type_id'] == 2){
                    $forward_type = 'Temporary(302)';
                }
                
                if($row['www_yn'] == 1){
                    $www_yn = 'YES';
                } elseif(!$row['www_yn']){
                    $www_yn = 'NO';
                }

                array_push($res, array(
                    'id' => $row['fd_id_pk'],
                    'name' => $row['fd_name'],
                    'forward_type' => $forward_type,
                    'www_yn' => $www_yn,
                    'target_domain' => $row['vh_name_vc'],
                ));
            }
            return $res;
        } else {
            return false;
        }
    }

    static function ListDomains($uid=0) {
        global $zdbh;

		//$currentuser = ctrl_users::GetUserDetail();
        $sql = "SELECT vh_id_pk,vh_name_vc FROM x_vhosts WHERE vh_id_pk NOT IN (SELECT vh_fk_id FROM x_forwarded_domains WHERE  fd_deleted_ts IS NULL) AND vh_acc_fk=:user_id AND vh_deleted_ts IS NULL";
        $bindArray = array(':user_id' => $uid);
        $zdbh->bindQuery($sql, $bindArray);
        $rows = $zdbh->returnRows(); 

        if (count($rows) > 0) {
            $res = array();
            foreach($rows as $row_idx=>$row) {
               array_push($res, array('id' => $row['vh_id_pk'],
				                        'name' => $row['vh_name_vc']));
            }
            return $res;
        } else {
            return false;
        }
    }
    
    static function getisAddDomainForwarder(){
        global $controller;
        $urlvars = $controller->GetAllControllerRequests('URL');
        $formvars = $controller->GetAllControllerRequests('FORM');

		if (isset($urlvars['action']) && (($urlvars['action'] == 'UpdateForward' || $urlvars['action'] == 'DeleteForward' || $urlvars['action']  == 'ConfirmDeleteDomainForwarder') && self::$ok)){
            return true;
		}
		
		if (isset($urlvars['action']) && ($urlvars['action'] != 'ForwardDomain')){
            return false;
		}
        return true;
    }
    
    static function getMaxForwarders(){
        global $zdbh;
		
		$currentuser = ctrl_users::GetUserDetail();
        $sql = "SELECT qt_domain_forwarders_in FROM x_quotas WHERE qt_package_fk=:package_id";
        $bindArray = array(':package_id' => $currentuser['packageid']);
        $zdbh->bindQuery($sql, $bindArray);
        $quota = $zdbh->returnRow();
       if(isset($quota['qt_domain_forwarders_in'])){
            return $quota['qt_domain_forwarders_in'];
        } 
        return 0;
    }

   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 getCanAddForward(){
        /* global $zdbh;
        
		$currentuser = ctrl_users::GetUserDetail();
        // let's check total domain forwards this user already has against max domain forwarders
        $sql = "SELECT COUNT(fd_id_pk) AS total_forwards FROM x_forwarded_domains WHERE fd_acc_fk=:user_id AND fd_deleted_ts IS NULL";
        $bindArray = array(':user_id' => $currentuser['userid']);
        $zdbh->bindQuery($sql, $bindArray);
        $forwarders = $zdbh->returnRow();
        $total_forwarders = $forwarders['total_forwards'];
        
        $max_forwards = self::getMaxForwarders();
        if($total_forwarders >= $max_forwards){
            return false;
        }
        */
        return true;
    }

    static function CheckForwardForErrors($domain_id,$domain) {
        global $zdbh;
        
        // Check for spaces and remove if found...
        $domain = strtolower(str_replace(' ', '', $domain));
        
        // Check to make sure the domain is not blank before we go any further...
        if ($domain == '') {
            self::$emptyforward = true;
            self::$editing = true;
            return false;
        }

        if (!$domain_id) {
            self::$emptydomain = true;
            self::$editing = true;
            return false;
        }

        if(!filter_var($domain, FILTER_VALIDATE_URL)) {
    	self::$badnames = true;
        return false;
	}

        // Check for invalid characters in the domain...
     /*   if (!fs_director::IsValidDomainName($domain)) {
            self::$badname = true;
            self::$editing = true;
            return false;
        }
       */ 
        // Check to make sure the domain is in the correct format before we go any further...
        /*$wwwclean = stristr($domain, 'www.');
        if ($wwwclean == true) {
            self::$badname = true;
            self::$editing = true;
            return false;
        }*/

        // Check to see if the domain already exists in ZPanel somewhere....
        $numrows = $zdbh->prepare("SELECT COUNT(*) FROM x_vhosts WHERE vh_name_vc=:domain AND vh_deleted_ts IS NULL");
        $numrows->bindParam(':domain', $domain);
        if ($numrows->execute()) {
            if ($numrows->fetchColumn() > 0) {
                self::$domainexists = true;
                self::$editing = true;
                return false;
            }
        }

        // Check to see if the forwarded domain already exists in ZPanel somewhere....
        $sql = "SELECT COUNT(*) FROM x_forwarded_domains WHERE fd_name=:domain AND fd_deleted_ts IS NULL";
        if(self::getisEditForward() && self::getForwardDomainID()){
            $sql .= " AND fd_id_pk<>:id";
        }
        /*echo((int) self::getisEditForward().'<br>');
        echo(self::getForwardDomainID().'<br>');
        echo($sql);
        exit;*/
        $numrows = $zdbh->prepare($sql);
        $numrows->bindParam(':domain', $domain);
        if(self::getisEditForward() && self::getForwardDomainID()){
            $numrows->bindParam(':id', self::getForwardDomainID());
        }
        if ($numrows->execute()) {
            if ($numrows->fetchColumn() > 0) {
                self::$forwardexists = true;
                self::$editing = true;
                return false;
            }
        }

        return true;
    }
	
	 static function doEditDomainForwarder()
	    {
		global $controller;
		runtime_csfr::Protect();
		$currentuser = ctrl_users::GetUserDetail();
		$formvars = $controller->GetAllControllerRequests('FORM');
		foreach (self::getForwardList($currentuser['userid']) as $row) {
		    if (isset($formvars['inDelete_' . $row['id']])) {
		        header("location: ./?module=" . $controller->GetCurrentModule() . '&show=Delete&other=' . $row['id']);
		        exit;
		    }
		    if (isset($formvars['inEdit_' . $row['id']])) {
		        header('location: ./?module=' . $controller->GetCurrentModule() . '&show=Edit&other=' . $row['id']);
		        exit;
		    }
				if (isset($formvars['inquotaEdit_' . $row['id']])) {
		        header('location: ./?module=' . $controller->GetCurrentModule() . '&show=quotaEdit&other=' . $row['id']);
		        exit;
		    }
		 }
		return true;
	    }

	    static function getCurrentID()
	    {
		global $controller;
		$id = $controller->GetControllerRequest('URL', 'other');
		return ($id) ? $id : '';
	    }
    /**
     * End 'worker' methods.
     */

    /**
     * Webinterface sudo methods.
     */
    static function getDomainList() {
        global $controller,$zdbh;
        $currentuser = ctrl_users::GetUserDetail();
        $res = array();
        $domains = self::ListDomains($currentuser['userid']);
        if (!fs_director::CheckForEmptyValue($domains)){
           $selected_id = 0;
           /* if(self::getisEditForward()){
              if(self::getForwardDomainID()){
                $domain_id = self::getForwardDomainID();
                
			    $sql = "SELECT * FROM x_forwarded_domains WHERE fd_id_pk=:id AND fd_deleted_ts IS NOT NULL";
                $bindArray = array(':id' => $domain_id);
                $zdbh->bindQuery($sql, $bindArray);
                $row = $zdbh->returnRow(); 
			    $selected_id = $row['vh_fk_id']; 
                $del = $row['fd_deleted_ts'];               
              }
           }
              */              
            foreach ($domains as $row){
                $selected_yn = ($row['id'] == $selected_id) ? ' selected="selected"':'';
                $del_res = ($del != NULL) ? '1':'0';
                array_push($res, array('name' => $row['name'],'id' => $row['id'], 'selected_yn' => $selected_yn, 'delete' => $del_res));
            }
            return $res;
        } else {
            return false;
        }
    }

    static function getCSFR_Tag() {
        return runtime_csfr::Token();
    }

    static function getModuleName() {
        $module_name = ui_module::GetModuleName();
        return $module_name;

    }

  static function getModuleIcon() {
        global $controller;
                // Check if the current userland theme has a module icon override
         if(trim(strtolower(ui_template::GetUserTemplate()))=="zentora-master")
        {
        $mod_dir=$controller->GetControllerRequest('URL', 'module') ;
         if (file_exists('etc/styles/' . ui_template::GetUserTemplate() . '/img/style/' . $mod_dir . '.jpg'))
            return './etc/styles/' . ui_template::GetUserTemplate() . '/img/style/' . $mod_dir . '.jpg';
                else
                          $module_icon = "/modules/" . $controller->GetControllerRequest('URL', 'module') . "/assets/icon.png";
                                return $module_icon;
        }else
                {
        $module_icon = "/modules/" . $controller->GetControllerRequest('URL', 'module') . "/assets/icon.png";
        return $module_icon;
                }
        }


    static function getModuleDesc() {
        $message = ui_language::translate(ui_module::GetModuleDescription());
        return $message;
    }



    static function getisForwardDomain() {
        global $controller;
        $urlvars = $controller->GetAllControllerRequests('URL');
        $formvars = $controller->GetAllControllerRequests('FORM');
		
		if(!$formvars){
			return false;
		}
		
		if (!fs_director::CheckForEmptyValue(self::CheckForwardForErrors($formvars['domain_id'],$formvars['fd_name']))) {
	        if ((isset($urlvars['action'])) && ($urlvars['action'] == "ConfirmDomainForward"))
	            return true;

		}
        return false;
    }
	
    static function getisEditForward() {
        global $controller;
        $urlvars = $controller->GetAllControllerRequests('URL');
        $formvars = $controller->GetAllControllerRequests('FORM');
		
		if(!$formvars){
			return false;
		}
		
		if (isset($formvars['id']) && !fs_director::CheckForEmptyValue($formvars['id'])){
	        if ((isset($urlvars['action'])) && ($urlvars['action'] == "EditForward" || ($urlvars['action'] == "UpdateForward" && !self::$ok)))
	            return true;

		}
        return false;
    }
    
    static function doEditForward(){
        return self::getisEditForward();
    }

	static function getForwardDomainName(){
        global $controller, $zdbh;
		$domain_id = (int) $controller->GetControllerRequest('FORM', 'id');
        if ($domain_id) {
			$sql = "SELECT fd_name FROM x_forwarded_domains WHERE fd_id_pk=:id AND fd_deleted_ts IS NULL";
            $bindArray = array(':id' => $domain_id);                                        
            $zdbh->bindQuery($sql, $bindArray);
            $row = $zdbh->returnRow(); 
			return $row['fd_name'];
        } else {
            return "";
        }
	}

	static function getForwardDomainWWWChecked(){
        global $controller, $zdbh;
		$domain_id = (int) $controller->GetControllerRequest('FORM', 'id');
        if ($domain_id) {
			$sql = "SELECT www_yn FROM x_forwarded_domains WHERE fd_id_pk=:id AND fd_deleted_ts IS NULL";
            $bindArray = array(':id' => $domain_id);                                        
            $zdbh->bindQuery($sql, $bindArray);
            $row = $zdbh->returnRow(); 
			if($row['www_yn'] == 1){
			    return ' checked="checked"';
			} else {
			    return '';
			}
        } else {
            return '';
        }
	}

	    static function getisDeleteDomainForwarder()
	    {
		global $controller;
		$urlvars = $controller->GetAllControllerRequests('URL');
		return (isset($urlvars['show'])) && ($urlvars['show'] == "Delete");
	    }
	    static function doConfirmDeleteDomainForwarder()
	    {
		global $controller;
		runtime_csfr::Protect();
		$formvars = $controller->GetAllControllerRequests('FORM');
		return self::ExecuteDeleteDomainForwarder($formvars['inDelete']);
	    }

	    

	static function getForwardedDomainType(){
        global $controller, $zdbh;
		$domain_id = (int) $controller->GetControllerRequest('FORM', 'id');
        $fd_type_id = 1;
        
        if ($domain_id) {
			$sql = "SELECT fd_type_id FROM x_forwarded_domains WHERE fd_id_pk=:id AND fd_deleted_ts IS NULL";
            $bindArray = array(':id' => $domain_id);                                        
            $zdbh->bindQuery($sql, $bindArray);
            $row = $zdbh->returnRow();
            $fd_type_id = $row['fd_type_id'];
        }

        
        //redirect option
        $ret_html = '<option value="1"';
        
        if($fd_type_id == 1){
            $ret_html .= ' selected="selected"';
        }
        $ret_html .= '>Permanent(301)</option>';

        //forward option
        $ret_html .= '<option value="2"';
        
        if($fd_type_id == 2){
            $ret_html .= ' selected="selected"';
        }
        $ret_html .= '>Temporary(302)</option>';
        
        return $ret_html;
	}

	static function getForwardProtocols(){
        global $controller, $zdbh;
		$domain_id = (int) $controller->GetControllerRequest('FORM', 'id');
        $fd_protocol = 'http';
        
        if ($domain_id) {
			$sql = "SELECT fd_protocol FROM x_forwarded_domains WHERE fd_id_pk=:id AND fd_deleted_ts IS NULL";
            $bindArray = array(':id' => $domain_id);                                        
            $zdbh->bindQuery($sql, $bindArray);
            $row = $zdbh->returnRow();
            $fd_protocol = $row['fd_protocol'];
        }
        
        //protocol options
        $ret_html = '<option value="http"';
        
        if($fd_protocol == 'http'){
            $ret_html .= ' selected="selected"';
        }
        $ret_html .= '>http://</option>';

        $ret_html .= '<option value="https"';
        
        if($fd_protocol == 'https'){
            $ret_html .= ' selected="selected"';
        }
        $ret_html .= '>https://</option>';
        
        return $ret_html;
	}

	static function getForwardDomainID(){
        global $controller;
        if ($controller->GetControllerRequest('FORM', 'id')){
            return $controller->GetControllerRequest('FORM', 'id');
        } else {
            return "";
        }
	}

    static function getisDeleteForward(){
        global $controller;
        $urlvars = $controller->GetAllControllerRequests('URL');
        $formvars = $controller->GetAllControllerRequests('FORM');

        if((isset($urlvars['action']) && $urlvars['action'] == 'ConfirmDeleteForward')){
            return true;
        }
        return false;
    }
    
    static function SetWriteApacheConfigTrue(){
        global $zdbh;
        // update apache_changed
		$sql = $zdbh->prepare("UPDATE x_settings SET so_value_tx='true'	WHERE so_name_vc='apache_changed'");
		$sql->execute();        
    }
    
    /*static function doConfirmDeleteForward(){
        return true;
    }*/
    static function doDeleteForwardedDomain()
    {
        global $controller;
        runtime_csfr::Protect();
//        $currentuser = ctrl_users::GetUserDetail();
        $formvars = $controller->GetAllControllerRequests('FORM');
        if (isset($formvars['inDelete'])) {
            if (self::ExecuteDeleteParkedDomain($formvars['inDelete'])) {
                self::$ok = TRUE;
                return true;
            }
        }
        return false;
    }

    static function doDeleteForward() {
        global $controller;
        runtime_csfr::Protect();
        $formvars = $controller->GetAllControllerRequests('FORM');
        
        if((int) $formvars['fd_id']){
            if (self::ExecuteDeleteForward((int) $formvars['fd_id'])) {
                self::SetWriteApacheConfigTrue();
                self::$ok = true;
                return true;
            }        
        } else {
            self::$error = true;
            return false;
        }        
        return;
    }
    

	static function doForwardDomain(){
        global $controller;
        runtime_csfr::Protect();
        $currentuser = ctrl_users::GetUserDetail();
        $formvars = $controller->GetAllControllerRequests('FORM');

        //validate form submission
		/*if (!fs_director::CheckForEmptyValue(self::CheckForwardForErrors($formvars['domain_id'],$formvars['fd_name']))){
	         return false;
		}*/
		
		if(fs_director::CheckForEmptyValue(self::getCanAddForward())){
            self::$limitreached = true;
            return false;		    
		}
        
        if (self::ExecuteForwardDomain($formvars)){
           self::SetWriteApacheConfigTrue();
           self::$ok = true;
           return true;			
        } else {
           return false;
        }
        return;		
	}

	static function doUpdateForward(){
        global $controller;
        runtime_csfr::Protect();
        $currentuser = ctrl_users::GetUserDetail();
        $formvars = $controller->GetAllControllerRequests('FORM');

        //validate form submission
		/*if (!fs_director::CheckForEmptyValue(self::CheckForwardForErrors($formvars['domain_id'],$formvars['fd_name']))){
	         return false;
		}*/
        
        if (self::ExecuteUpdateForward($formvars)){
           self::SetWriteApacheConfigTrue();
           self::$ok = true;
           self::$editing = false;
           return true;			
        } else {
           return false;
        }
        return;		
	}

    static function ExecuteForwardDomain($data) {
        global $zdbh;
        $retval = false;        
        runtime_hook::Execute('OnBeforeForwardDomain');
		if (!fs_director::CheckForEmptyValue(self::CheckForwardForErrors($data['domain_id'],$data['fd_name']))){
	   $currentuser = ctrl_users::GetUserDetail();
           $data['www_yn'] = isset($data['www_yn']) ? $data['www_yn'] : 0;
           $data['fd_type_id'] = isset($data['fd_type_id']) ? $data['fd_type_id'] : 1;
           $data['fd_protocol'] = isset($data['fd_protocol']) ? $data['fd_protocol'] : '';
           $fd_name = $data['fd_name'];
           $fd_type_id =  $data['fd_type_id'];
           $www_yn = $data['www_yn'];
           if($fd_type_id == 1)
           {
           $r_result = 301;
           }
           else
           {
           $r_result = 302;
           }
           $service_query = $zdbh->prepare("SELECT * FROM x_varnish");
           $service_query->execute();
           $rows = $service_query->fetch();
 
           // If it is not nginx
           if($rows['x_nginx'] == "Off")
           {
           // create the .htaccess file
           $vhost_sql = "SELECT * FROM x_vhosts WHERE vh_id_pk =:id";
           $bindArray = array(':id' => $data['domain_id']);                                        
           $zdbh->bindQuery($vhost_sql, $bindArray);
           $row = $zdbh->returnRow();
           $directory = $row['vh_directory_vc'];
           $path = ctrl_options::GetSystemOption('hosted_dir');
           $create_name = $path.$currentuser['username'].'/public_html'.$directory.'/.htaccess'; 
           //check for file exists or not
           if(file_exists($create_name))
           {
           $create_backup_name = $path.$currentuser['username'].'/public_html'.$directory.'/.htaccess_backup';
           $backup = copy($create_name,$create_backup_name);
           unlink($create_name);
           $create_file = $path.$currentuser['username'].'/public_html'.$directory.'/.htaccess';
           // open the .htaccess file for editing 
           $file_handle = fopen($create_file, 'w') or die("Error: Can't open file");
	   //enter the contents
           $content_string = "RewriteEngine on\n";
           fwrite($file_handle, $content_string);
           $content_string = "RewriteCond %{HTTP_HOST} ^".$row['vh_name_vc']." "."[NC,OR]\n";
           fwrite($file_handle, $content_string);
           $content_string = "RewriteCond %{HTTP_HOST} ^www.".$row['vh_name_vc']." "."[NC]\n";
           fwrite($file_handle, $content_string);
           // change www.website.com for your website
           $content_string = 'RewriteRule ^(.*)$'.' '.$fd_name.' '.'[L,R='. $r_result .',NC]';
           fwrite($file_handle, $content_string);
           fclose($file_handle);
           }
           else
           {
           // open the .htaccess file for editing 
           $file_handle = fopen($create_name, 'w') or die("Error: Can't open file");
	   //enter the contents
           $content_string = "RewriteEngine on\n";
           fwrite($file_handle, $content_string);
           $content_string = "RewriteCond %{HTTP_HOST} ^".$row['vh_name_vc']." "."[NC,OR]\n";
           fwrite($file_handle, $content_string);
           $content_string = "RewriteCond %{HTTP_HOST} ^www.".$row['vh_name_vc']." "."[NC]\n";
           fwrite($file_handle, $content_string);
           // change www.website.com for your website
           $content_string = 'RewriteRule ^(.*)$'.' '.$fd_name.' '.'[L,R='. $r_result .',NC]';
           fwrite($file_handle, $content_string);
           fclose($file_handle);
           }
           }

           // If it is nginx
           else
           if($rows['x_nginx'] == "On")
           {
           $vhost_sql = "SELECT * FROM x_vhosts WHERE vh_id_pk =:id";
           $bindArray = array(':id' => $data['domain_id']);
           $zdbh->bindQuery($vhost_sql, $bindArray);
           $row = $zdbh->returnRow();
           $directory = $row['vh_name_vc'];

            $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;
            }
            socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 30, "usec" =>0));

            str_replace(' ', '', $userid);
            str_replace(' ', '', $lid);
            $in="command nginxredirect $directory $r_result $fd_name".
            $out = '';
            socket_write($socket, $in, strlen($in));
            sleep(3);
            //$s_data = socket_read($socket,1024);
            // $in="command exec /usr/bin/php /etc/sentora/panel/bin/daemon.php";
            //$out = '';
            //socket_write($socket, $in, strlen($in));
            socket_close($socket);

	   
           }
           $sql = $zdbh->prepare("INSERT INTO x_forwarded_domains (vh_fk_id,fd_acc_fk,fd_name,
                                   www_yn,fd_type_id,fd_protocol) VALUES (
                                   :domain_id, :user_id, :name, :www_yn, :type_id, :protocol)");
           $sql->bindParam(':domain_id', $data['domain_id']);
           $sql->bindParam(':name', $data['fd_name']);
           $sql->bindParam(':user_id', $currentuser['userid']);
           $sql->bindParam(':www_yn', $data['www_yn']);
           $sql->bindParam(':type_id', $data['fd_type_id']);
           $sql->bindParam(':protocol', $data['fd_protocol']);
           $sql->execute();
           
		   $retval = true;

		}
        runtime_hook::Execute('OnAfterForwardDomain');
		

        return $retval;
    
    }

    static function ExecuteDeleteDomainForwarder($id)
    {

        global $zdbh;
        $currentuser = ctrl_users::GetUserDetail();

        $sql = $zdbh->prepare("UPDATE x_forwarded_domains
							   SET fd_deleted_ts=:time
							   WHERE fd_id_pk=:id");
        $sql->bindParam(':id', $id);
        $time = time();
        $sql->bindParam(':time', $time);
        $sql->execute();
	self::$deletedOk=TRUE;
        
        self::SetWriteApacheConfigTrue();
   
        $service_query = $zdbh->prepare("SELECT * FROM x_varnish");
        $service_query->execute();
        $datarows = $service_query->fetch();
        

        $df_sql = "SELECT * FROM x_forwarded_domains WHERE fd_id_pk =:id";
        $bindArrays = array(':id' => $id);                                        
        $zdbh->bindQuery($df_sql, $bindArrays);
        $rows = $zdbh->returnRow();
        $fd = $rows['vh_fk_id'];

        $vhost_sql = "SELECT * FROM x_vhosts WHERE vh_id_pk =:id";
        $bindArray = array(':id' => $fd);                                        
        $zdbh->bindQuery($vhost_sql, $bindArray);
        $row = $zdbh->returnRow();
        $directory = $row['vh_directory_vc'];
        $domain_directory = $row['vh_name_vc'];

         // If it is not nginx
        if($datarows['x_nginx'] == "Off")
        {
        $path = ctrl_options::GetSystemOption('hosted_dir');
        $create_name = $path.$currentuser['username'].'/public_html'.$directory.'/.htaccess_backup';
        $create_backup_name = $path.$currentuser['username'].'/public_html'.$directory.'/.htaccess';
        if(file_exists($create_name))
        {
        $backup = copy($create_name,$create_backup_name);
        unlink($create_name);
        }
        else
        {
        unlink($create_backup_name);
        }
        }
        else
        if($datarows['x_nginx'] == "On")
        {
            $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;
            }
            socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 30, "usec" =>0));

            str_replace(' ', '', $userid);
            str_replace(' ', '', $lid);
            $in="command nginxredirectdel $domain_directory".
            $out = '';
            socket_write($socket, $in, strlen($in));
            sleep(3);
            //$s_data = socket_read($socket,1024);
            // $in="command exec /usr/bin/php /etc/sentora/panel/bin/daemon.php";
            //$out = '';
            //socket_write($socket, $in, strlen($in));
            socket_close($socket);


        }
	$retval = TRUE;
        header('location:?module=domain_forwarder');
        return $retval;
       
    }

    static function ExecuteUpdateForward($data){
        global $zdbh;
        $retval = false;
        
        runtime_hook::Execute('OnBeforeUpdateForwardDomain');
		if (!fs_director::CheckForEmptyValue(self::CheckForwardForErrors($data['domain_id'],$data['fd_name']))){
	       $currentuser = ctrl_users::GetUserDetail();
           $data['www_yn'] = isset($data['www_yn']) ? $data['www_yn'] : 0;
           $data['fd_type_id'] = isset($data['fd_type_id']) ? $data['fd_type_id'] : 1;

           $sql = $zdbh->prepare("UPDATE x_forwarded_domains SET vh_fk_id=:domain_id,fd_name=:name,
                                   www_yn=:www_yn,fd_type_id=:type_id,fd_protocol=:protocol 
                                   WHERE fd_id_pk=:id");
           $sql->bindParam(':domain_id', $data['domain_id']);
           $sql->bindParam(':name', $data['fd_name']);
           $sql->bindParam(':id', self::getForwardDomainID());
           $sql->bindParam(':www_yn', $data['www_yn']);
           $sql->bindParam(':type_id', $data['fd_type_id']);
           $sql->bindParam(':protocol', $data['fd_protocol']);
           $sql->execute();
           
		   $retval = true;
		}
        runtime_hook::Execute('OnAfterUpdateForwardDomain');
		

        return $retval;
    }

    static function ExecuteDeleteForward($id){
        global $zdbh,$controller;
    
        $currentuser = ctrl_users::GetUserDetail();

           //delete domain forward
           runtime_hook::Execute('OnBeforeDeleteDomainForward');
           $sql = $zdbh->prepare("UPDATE x_forwarded_domains SET fd_deleted_ts='".time()."' 
                                    WHERE fd_id_pk=:id AND fd_acc_fk=:user_id");
           $sql->bindParam(':user_id', $currentuser['userid']);
           $sql->bindParam(':id', $id);
           $sql->execute();
           header('location:?module=domain_forwarder');
           runtime_hook::Execute('OnAfterDeleteDomainForward');
           
           self::$complete = true;
           return true;            

    }




    static function getResult() {
        if (!fs_director::CheckForEmptyValue(self::$badname)) {
            return ui_sysmessage::shout(ui_language::translate("Please specify a valid forwarded domain and try again."), "zannounceerror");
        }
        if (!fs_director::CheckForEmptyValue(self::$badnames)) {
            return ui_sysmessage::shout(ui_language::translate("Please specify a valid forwarded domain with prefix http:// or https:// and try again."), "zannounceerror");
        }

        if (!fs_director::CheckForEmptyValue(self::$emptyforward)) {
            return ui_sysmessage::shout(ui_language::translate("Forwarded domain cannot be empty."), "zannounceerror");
        }

        if (!fs_director::CheckForEmptyValue(self::$emptydomain)) {
            return ui_sysmessage::shout(ui_language::translate("Please select a domain to forward to."), "zannounceerror");
        }
        if (!fs_director::CheckForEmptyValue(self::$deletedOk)) {
            return ui_sysmessage::shout(ui_language::translate("Domain Forwarder Deleted Successfully!!!"), "zannounceerror");
        }

        if (!fs_director::CheckForEmptyValue(self::$ok)) {
            return ui_sysmessage::shout(ui_language::translate("Process completed successfully."), "zannounceok");
        }
        if (!fs_director::CheckForEmptyValue(self::$domainexists)) {
            return ui_sysmessage::shout(ui_language::translate("That domain is not available."), "zannounceerror");
        }
        if (!fs_director::CheckForEmptyValue(self::$forwardexists)) {
            return ui_sysmessage::shout(ui_language::translate("Forwarded Domain is not available."), "zannounceerror");
        }
        if (!fs_director::CheckForEmptyValue(self::$limitreached)) {
            return ui_sysmessage::shout(ui_language::translate("Cannot add new forward, maximum domain forwarders limit reached."), "zannounceerror");
        }
        if (!fs_director::CheckForEmptyValue(self::$error)) {
            return ui_sysmessage::shout(ui_language::translate("An error has occurred while executing your request, please check your input and try again."), "zannounceerror");
        }

        return;
    }

    /**
     * Webinterface sudo methods.
     */
}

?>