Current File : //root/panel/modules/autoresponder/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
{

    public static $m_error;
    static function n_sanitize($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    static function getListDomaindata()
    {
        global $zdbh;
        $currentuser = ctrl_users::GetUserDetail();
        $stmt = $zdbh->prepare("select vh_id_pk,vh_name_vc  from  x_vhosts where  vh_acc_fk =:uid  AND vh_deleted_ts  IS NULL  AND  vh_type_in in (1,2) ");
        $stmt->bindParam(':uid',$currentuser['userid']);
        $stmt->execute();
        $row_count = $stmt->rowCount();
        if($row_count >0)
        {
            $domainarray = array();
            while($rows = $stmt->fetch())
            {
               array_push($domainarray,array('d_id' => $rows['vh_id_pk'],'d_name' => $rows['vh_name_vc']) ) ;   
            }
            return $domainarray;
        }
        else
        {
        return false;
        } 
    }
 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 doCreateResponder()
    {
        global $controller;
		global $zdbh;
        runtime_csfr::Protect();
	
		$formvars = $controller->GetAllControllerRequests('FORM');
		if (array_key_exists("auto_email",$formvars) && array_key_exists("auto_subject",$formvars) &&  array_key_exists("auto_from",$formvars) && array_key_exists("auto_body",$formvars) )
		{
            $auto_email =self::n_sanitize($formvars['auto_email']);
            $auto_subject =self::n_sanitize($formvars['auto_subject']);
            $auto_from =self::n_sanitize($formvars['auto_from']);
            $auto_body1 =$formvars['auto_body'];
            $auto_body =self::n_sanitize($auto_body1);
            
            $auto_listdomaindetail = self::n_sanitize($formvars['listdomaindetail']);
            $auto_html = 0;
            $p_status = 0;
            
           
            if(array_key_exists("auto_html",$formvars)){ $auto_html = 1; }
            if ( fs_director::CheckForEmptyValue($auto_email) || fs_director::CheckForEmptyValue($auto_subject) || fs_director::CheckForEmptyValue($auto_from) || fs_director::CheckForEmptyValue($auto_body) )
            {
                
            self::$m_error = "Enter all field";

            }
            else
            {
                

                $currentuser = ctrl_users::GetUserDetail();
                $suid = $currentuser['userid'];

                $stmt1 = $zdbh->prepare("select vh_id_pk,vh_name_vc  from  x_vhosts where  vh_acc_fk =:uid AND vh_id_pk =:did AND vh_deleted_ts  IS NULL AND  vh_type_in in (1,2)");
                $stmt1->bindParam(':uid',$suid);
                $stmt1->bindParam(':did',$auto_listdomaindetail);
                $stmt1->execute();
                $row_count1 = $stmt1->rowCount();
                if($row_count1 >0)
                {



                    $rows1 = $stmt1->fetch();
                    $dom_data = $rows1['vh_name_vc'];
                    $auto_email1 = $auto_email."@".$dom_data;
                    $stmt3 =$zdbh->prepare("select  mb_id_pk  from x_mailboxes where mb_address_vc =:mail_id and mb_deleted_ts IS NULL ");
                    $stmt3->bindParam(':mail_id',$auto_email1);
                    $stmt3->execute();
                    $row_count3 = $stmt3->rowCount();
                    if($row_count3 > 0 )
                    {
                        $stmt2 = $zdbh->prepare("select vh_id  from x_autorespond where vh_mail =:mail_id  and vh_acc_fk =:nuid and vh_did =:ndid  and vh_deleted IS NULL ");
                        $stmt2->bindParam(':nuid',$suid);
                        $stmt2->bindParam(':ndid',$auto_listdomaindetail);
                        $stmt2->bindParam(':mail_id',$auto_email1);
                        $stmt2->execute();
                        $row_count2 = $stmt2->rowCount();
                        if($row_count2 > 0 )
                        {
                            self::$m_error = "Autoresponder already exist with this email id";
                            
                        }
                        else
                        {

                            $auto_file_path = "/var/spool/autoresponse/responses/".$auto_email1;
                            $autoresp_file = fopen("/var/spool/autoresponse/responses/".$auto_email1, "w+") ;
                            if ( !$autoresp_file ) {
                                self::$m_error = "Auto responder  has not been created .Please contact system admin.";
                            }
                            else
                            { 
                                if($auto_html == 1)
                                {
                                    fwrite( $autoresp_file,"Content-type: text/html;\nFrom: ".$auto_from."<".$auto_email1.">\nTo: Replace part\nSubject: ".$auto_subject."\n".$auto_body1);
                                }
                                else
                                {
                                    fwrite( $autoresp_file,"From: ".$auto_from."<".$auto_email1.">\nTo: Replace part\nSubject: ".$auto_subject."\n".$auto_body1);
                                }
                                fclose($autoresp_file);
                                if (file_exists($auto_file_path)) 
                                {
                                    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_autorespond (vh_mail,vh_sub,vh_from,vh_html_status,vh_body,vh_acc_fk,vh_did,vh_created ) values(:auto_email,:auto_subject,:auto_from,:auto_html,:auto_body,:domain_id,:e_id,:str_time) ");
                                    $stmt->bindParam(':auto_email',$auto_email1);
                                    $stmt->bindParam(':auto_subject',$auto_subject);
                                    $stmt->bindParam(':auto_from',$auto_from);
                                    $stmt->bindParam(':auto_html',$auto_html);
                                    $stmt->bindParam(':auto_body',$auto_body);
                                    $stmt->bindParam(':domain_id',$suid);
                                    $stmt->bindParam(':e_id',$auto_listdomaindetail);
                                    $stmt->bindParam(':str_time',$current_str_time);
                                    $stmt->execute();
                                    self::$m_error = "Auto responder created successfully";
                                }
                                else
                                {
                                    self::$m_error = "Auto responder  has not been created .Please contact system admin.";
                                }
                            }
                        }
                    }
                    else
                    {
                        self::$m_error = "Mailid not exist";                
                    }
     
                }
                else
                {
                self::$m_error = "Domain not exist";
                }

            }
        }
        else
        {
            self::$m_error = "Enter all field";
        }
        
    }
        
     static function getmail_detail()
    {
        return self::responderdetail(0);

    }    
    static function responderdetail($uid=1)
    {

        if($uid == 0)
        {
	    global $zdbh;
            $currentuser = ctrl_users::GetUserDetail();
            $suid = $currentuser['userid'];
            $stmt3 =$zdbh->prepare("select vh_id,vh_mail,vh_sub,vh_from,vh_body from x_autorespond where vh_acc_fk =:sid and  vh_deleted IS NULL ");
	    $stmt3->bindParam(':sid',$suid);
            $stmt3->execute();
            $row_count3 = $stmt3->rowCount();
            if($row_count3 >0)
            {
                $auto_array = array();
                while($rows=$stmt3->fetch())
                {
                    $vhbody=$rows['vh_body'];
                    $vhbody1 = $vhbody;
                    if(strlen($vhbody) > 13)
                    {
                    $vhbody = substr($vhbody, 0, 13)."....."; 
                    } 
                     array_push($auto_array,array('a_id' => $rows['vh_id'],'a_mail' => $rows['vh_mail'],'a_sub' => $rows['vh_sub'],'a_from' => $rows['vh_from'],'a_body' => $vhbody,'a_obody'=>$vhbody1) ) ;   
                }
                return $auto_array;
            }
            else
            {
                return false;
            }
            
        }
        else {  }
    }
    
    static function  doconform_autorespond()
    {
       
        global $zdbh;
        global $controller;
        $currentuser = ctrl_users::GetUserDetail();
        $suid = $currentuser['userid'];
        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 vh_id,vh_mail  from x_autorespond   where vh_id =:vhid  and vh_acc_fk =:sid  and vh_deleted IS NULL ");
                $stmt3->bindParam(':sid',$suid);
                $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['vh_id'] . "&email=" . $rows['vh_mail'] . " ");
                    }
                    else
                    {
                    header("location: ./?module=" . $controller->GetCurrentModule() . "&show=Edit&id=" . $rows['vh_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 getisDeleteautorespond()
    {
        global $controller;
        $urlvars = $controller->GetAllControllerRequests('URL');
        return (isset($urlvars['show'])) && ($urlvars['show'] == "Delete");        
    }

    static function getCurrentautorespond()
    {
        global $controller;
        $auto_res = $controller->GetControllerRequest('URL', 'email');
        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 doDeleteAutoresponder()
    {
        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();
            $suid = $currentuser['userid'];
            if ( !fs_director::CheckForEmptyValue($auto_id) && is_numeric($auto_id) && $auto_id > 0)
            { 
                $stmt3 =$zdbh->prepare("select vh_id,vh_mail  from x_autorespond   where vh_id =:vhid  and vh_acc_fk =:sid  and vh_deleted IS NULL ");
                $stmt3->bindParam(':sid',$suid);
                $stmt3->bindParam(':vhid',$auto_id);
                $stmt3->execute();
                $row_count3 = $stmt3->rowCount();
                if($row_count3 >0)
                {

                    $rows = $stmt3->fetch();
                    $auto_email = $rows['vh_mail'];
                    if($auto_email != "")
                    {
                        $auto_file_path = "/var/spool/autoresponse/responses/".$auto_email;
                        if (file_exists($auto_file_path)) 
                        {

                            if(unlink($auto_file_path))
                            {
                                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_autorespond set vh_deleted =:str_time  where  vh_id =:vhid  and vh_acc_fk =:sid ");
                                $stmt4->bindParam(':str_time',$current_str_time);
                                $stmt4->bindParam(':sid',$suid);
                                $stmt4->bindParam(':vhid',$auto_id);
                                $stmt4->execute();   
                                /*********$affected_rows = $stmt4->rowCount(); *************/
                                    self::$m_error = "Autoresponder deleted successfully";
                            }
                            else 
                            {
                                self::$m_error = "Problem in delete autoresponder,please  contact system admin";
                            }
                        }
                        else
                        {
                            self::$m_error = "Problem in delete autoresponder,please  contact system admin";
                        }
                    }
                    else
                    {
                        self::$m_error = "Problem in delete autoresponder,please  contact system admin";
                    }

                }
                else
                {
                    self::$m_error = "Problem in delete autoresponder,please  contact system admin";
                }
            }
            else
            {
                self::$m_error = "Autoresponder is not exist ";
                
            }
        }
        else
        {
            self::$m_error = "Autoresponder is not exist ";
        
        }

    }
    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 geteditautodata()
    {
        global $zdbh;
        global $controller;
        $auto_resid = $controller->GetControllerRequest('URL', 'id');
        $currentuser = ctrl_users::GetUserDetail();
        $suid = $currentuser['userid'];
        if ( !fs_director::CheckForEmptyValue($auto_resid) && is_numeric($auto_resid) && $auto_resid > 0)
        { 
            $stmt3 =$zdbh->prepare("select vh_id,vh_mail,vh_sub,vh_from,vh_html_status,vh_body,vh_did  from x_autorespond   where vh_id =:vhid  and vh_acc_fk =:sid  and vh_deleted IS NULL ");
            $stmt3->bindParam(':sid',$suid);
            $stmt3->bindParam(':vhid',$auto_resid);
            $stmt3->execute();
            $row_count3 = $stmt3->rowCount();
            if($row_count3 >0)
            {
                $rows = $stmt3->fetch();
                if($rows['vh_html_status'] == 1)
                {
                    $st_code="<input type=\"checkbox\" name=\"eauto_html\" checked >";
                }
                else
                {
                    $st_code="<input type=\"checkbox\" name=\"eauto_html\"  >";
                }
                    
                    
               $auto_array[]=array("a_id"=>$rows['vh_id'],"a_mail"=>$rows['vh_mail'],"a_sub"=>$rows['vh_sub'],"a_from"=>$rows['vh_from'],"a_status"=>$st_code,"a_body"=>$rows['vh_body'],"a_did"=>$rows['vh_did']);
               return $auto_array;
            
            }
            else
            {
            return false;
            
            }
                
        }
        else
        {
        return false;
        
        }
        
    
    }
    
    
    static function doeditResponder()
    {
        global $controller;
		global $zdbh;
        runtime_csfr::Protect();
	
		$formvars = $controller->GetAllControllerRequests('FORM');
		
		if (array_key_exists("eauto_subject",$formvars) && array_key_exists("eauto_from",$formvars) && array_key_exists("eauto_id",$formvars) && array_key_exists("eauto_did",$formvars) && array_key_exists("eauto_body",$formvars)  )
		{
            $auto_subject =self::n_sanitize($formvars['eauto_subject']);
            $auto_from =self::n_sanitize($formvars['eauto_from']);
            $auto_id =self::n_sanitize($formvars['eauto_id']);
            $auto_did =self::n_sanitize($formvars['eauto_did']);
            $auto_body1 =$formvars['eauto_body'];
            $auto_body =self::n_sanitize($auto_body1);
            
            $auto_html = 0;
            $p_status = 0;
            
            
            //print_r($formvars);
            //exit;
           
            if(array_key_exists("eauto_html",$formvars)){ $auto_html = 1; }
            if (  fs_director::CheckForEmptyValue($auto_subject) || fs_director::CheckForEmptyValue($auto_from) || fs_director::CheckForEmptyValue($auto_body) || fs_director::CheckForEmptyValue($auto_id) || fs_director::CheckForEmptyValue($auto_did) || !is_numeric($auto_id) || !is_numeric($auto_did) )
            {
                
            self::$m_error = "Enter all field";

            }
            else
            {
                

                $currentuser = ctrl_users::GetUserDetail();
                $suid = $currentuser['userid'];

                $stmt1 = $zdbh->prepare("select vh_id_pk,vh_name_vc  from  x_vhosts where  vh_acc_fk =:uid AND vh_id_pk =:did AND vh_deleted_ts  IS NULL AND vh_type_in in (1,2)");
                $stmt1->bindParam(':uid',$suid);
                $stmt1->bindParam(':did',$auto_did);
                $stmt1->execute();
                $row_count1 = $stmt1->rowCount();
                if($row_count1 >0)
                {

                $rows1 = $stmt1->fetch();
                $dom_data = $rows1['vh_name_vc'];
                    
                    $stmt2 = $zdbh->prepare("select vh_id,vh_mail  from x_autorespond where vh_id =:au_id  and vh_acc_fk =:nuid and vh_did =:ndid  and vh_deleted IS NULL ");
                    $stmt2->bindParam(':nuid',$suid);
                    $stmt2->bindParam(':ndid',$auto_did);
                    $stmt2->bindParam(':au_id',$auto_id);
                    $stmt2->execute();
                    $row_count2 = $stmt2->rowCount();
                    if($row_count2 > 0 )
                    {
                        $row123 = $stmt2->fetch();
                        $auto_email1 = $row123['vh_mail'];
                        $auto_file_path = "/var/spool/autoresponse/responses/".$auto_email1;
                        $autoresp_file = fopen("/var/spool/autoresponse/responses/".$auto_email1, "w+") ;
                        if ( !$autoresp_file ) {
                            self::$m_error = "Auto responder  has not been created .Please contact system admin.";
                        }
                        else
                        { 
                            if($auto_html == 1)
                            {
                                fwrite( $autoresp_file,"Content-type: text/html;\nFrom: ".$auto_from."<".$auto_email1.">\nTo: Replace part\nSubject: ".$auto_subject."\n".$auto_body1);
                            }
                            else
                            {
                                fwrite( $autoresp_file,"From: ".$auto_from."<".$auto_email1.">\nTo: Replace part\nSubject: ".$auto_subject."\n".$auto_body1);
                            }
                            fclose($autoresp_file);
                            if (file_exists($auto_file_path)) 
                            {
                                date_default_timezone_set("UTC");
                                $current_time = date("Y-m-d H:i:s");
                                $current_str_time =  strtotime($current_time);
                                /*$stmt = $zdbh->exec("update x_autorespond set vh_sub='$auto_subject',vh_from='$auto_from',vh_html_status='$auto_html',vh_body='$auto_body',vh_updated='$current_str_time' where vh_id='".$auto_id."' ");*/
                                
                                
         $stmt=$zdbh->prepare("update  x_autorespond set vh_sub=:auto_subject,vh_from=:auto_from,vh_html_status=:auto_html,
          vh_body=:auto_body,vh_updated=:str_time where vh_id =:au_id  and vh_acc_fk =:nuid and vh_did =:ndid and vh_deleted IS NULL ");
                                $stmt->bindParam(':auto_subject',$auto_subject);
                                $stmt->bindParam(':auto_from',$auto_from);
                                $stmt->bindParam(':auto_html',$auto_html);
                                $stmt->bindParam(':auto_body',$auto_body);
                                $stmt->bindParam(':nuid',$suid);
                                $stmt->bindParam(':ndid',$auto_did);
                                $stmt->bindParam(':au_id',$auto_id);
                                $stmt->bindParam(':str_time',$current_str_time);
                                $stmt->execute();
                                
                             
                                self::$m_error = "Auto responder updated successfully";
                            }
                            else
                            {
                                self::$m_error = "Auto responder  has not been update .Please contact system admin.";
                            }
                        }
                        
                        
                        
                        
                    }
                    else
                    {

                       self::$m_error = "Autoresponder not  exist with this email id";
                    }
     
                }
                else
                {
                self::$m_error = "Domain not exist";
                }

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

}