Current File : //root/panel/modules/maillogtrack/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 function n_sanitize($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    
  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 n_valid_date($date_str)
    {
        if(preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $date_str))
        {
            return true;
        }
        else
        {
            return false;
        }
    
    
    }
    static function checkdate_time($dat)
    {
        $datd = explode("-",$dat);
        
        if(checkdate($datd[1],$datd[2],$datd[0]))
        {
            return true;        
        }
        else
        {
            return false;
        
        }
    }

	static function call_socket($cmd_detail)
	{
        	$service_port = 4444 ;
		$address = gethostbyname('localhost');
		$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		if ($socket === false) {
			return "2";
		}
		$result = socket_connect($socket, $address, $service_port);
		if ($result === false) {
			return "2";
		}
		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 "2";  }
	}

    static function domaillog()
    {
    
        global $controller;
        $formvars = $controller->GetAllControllerRequests('FORM');
        
        if(array_key_exists("start_tb",$formvars) && array_key_exists("end_tb",$formvars) && array_key_exists("mail_id",$formvars) && array_key_exists("rcv_stat",$formvars) && array_key_exists("snd_stat",$formvars) && is_numeric($formvars['rcv_stat']) && is_numeric($formvars['snd_stat'])  )
        {
            $start_date = $formvars['start_tb'];
            $end_date = $formvars['end_tb'];
            $mail_id2 = $formvars['mail_id'];
            $rcv_stat = $formvars['rcv_stat'];
            $snd_stat = $formvars['snd_stat'];
            
            
            $sui_array = array(1,2);
            $rs5 = 0;
            
            if(in_array($rcv_stat,$sui_array) && in_array($snd_stat,$sui_array))
            {
            
                if(($rcv_stat ==  1 && $snd_stat  == 1) ||  ($rcv_stat == 2 &&  $snd_stat == 2))
                {
                    $rs5 = 1;
                }
            
            
            $mail_id1 = trim(preg_replace('/\s+/',' ', $mail_id2));
			$mail_id = str_replace(" ","",$mail_id1);
            $st_ch = 0;
            if($mail_id !="")
            {
            $st_ch = 1;
            }
            
         if(self::n_valid_date($start_date) &&  self::n_valid_date($end_date) )
         {
         
         
        $start_date111= explode(" ",$start_date);
        $start_date222 = $start_date111[0];
        $start_date333 = $start_date111[1];
        $end_date111 = explode(" ",$end_date);
        $end_date222 = $end_date111[0];
        $end_date333 = $end_date111[1]; 
        $pattern1 = '/^(0?\d|1\d|2[0-3]):[0-5]\d:[0-5]\d$/';
        
        if(self::checkdate_time($start_date222) && self::checkdate_time($end_date222)  && preg_match($pattern1,$start_date333) && preg_match($pattern1,$end_date333))
        {
        //ini_set("display_errors", true);
        //ini_set("error_reporting", E_ALL);
        //ini_set("memory_limit", "150M");
        //ini_set("max_execution_time", 0);
        $d_path = "/var/log/";
        $m_path = $d_path . "maillog";
        $f_status = array(
	        "sent",
	        "fail",
	        "softbounce",
	        "deferred",
	        "bounced"
        );
        $smatch_param = array(
	        "qmgr",
	        "from=<"
        );
        $tmatch_param = array(
	        "to=<",
	        "status="
        );
        
        
        $s_c = 2;
        $t_c = 2;
        $from_sta = 6;
        $to_sta = 4;
        $end_t = - 2;
        $msg_sta = 1;
        $msg_end = - 2;
        $sta_cod = 7;
        $f_array = array();
        $time_diff = 0;
        $month_array = array(
	        "jan",
	        "feb",
	        "mar",
	        "apr",
	        "may",
	        "jun",
	        "jul",
	        "aug",
	        "sep",
	        "oct",
	        "nov",
	        "dec"
        );
        date_default_timezone_set("UTC");
        $current_time = date("Y-m-d H:i:s");
        $current_year = date("Y");
        $current_month = date("m");
        $current_date = date("d");
        $current_t = date("H:i:s");
        $cur_str = strtotime($current_time);
        $st_ch_d = "status=";
        
        $str_start_date = strtotime($start_date);
        $str_end_date = strtotime($end_date);

        /*if (function_exists('shell_exec'))
	        {
	        $date_data = shell_exec('echo `date +"%Y-%m-%d %T"`');
	        $date_strd = strtotime($date_data);
	        $time_diff = $date_strd - $cur_str;
	        }
        */
        $f_status_c = count($f_status);
        if (is_dir($d_path))
	        {
		$command = "fileperm ".$m_path." 0644";
		$filedst = self::call_socket($command);
	if($filedst == 1)
	{
	        if (file_exists($m_path) && is_readable($m_path))
		        {
		        if ($m_file = fopen($m_path, "r"))
			        {
			        
			        //echo "88888888888888888888";
			        while (!feof($m_file))
				        {
				        //echo "dfasf";
				        $m_line = fgets($m_file);
				        $m_line =trim(preg_replace('/\s+/',' ', $m_line));
				        $n_array = explode(" ", $m_line);
				        $n_count = count($n_array);
				        $dng_m = $current_month;
				        if ($n_count > 3)
					        {
					        
					        $str_month = strtolower($n_array[0]);
					        $key_d = array_search($str_month, $month_array);
					        if ($key_d !== false)
						        {
						        $key_d = $key_d + 1;
						        if ($key_d < 10)
							        {
							        $dng_m = "0" . $key_d;
							        }
						          else
							        {
							        $dng_m = $key_d;
							        }
						        }

                            //print_r($n_array);


					        $dng_d = $n_array[1];
					        $dng_t = $n_array[2];
					        $dng_y = $current_year;
					        //echo $dng_y."............\n";
					        $n_td_a = $dng_y . "-" . $dng_m . "-" . $dng_d . "  " . $dng_t;
					        //echo $n_td_a."\n";
					        $str_ntra = strtotime($n_td_a);
					       //echo $str_ntra."................".$str_start_date."..........".$str_end_date."\n";
					        if ($str_ntra > $str_start_date && $str_ntra < $str_end_date)
						        {
						            //echo "asdfasfasdftttt";
						        $mw = 0;
						        $f_pos = 0;
						        $fstr_dos = "";
						        $low_ds = strtolower($m_line);
						           
						        for (; $mw < $f_status_c; $mw++)
							        {
							        $ms_tgc = $f_status[$mw];
							        if (strpos($low_ds, $ms_tgc) !== false)
								        {
								        $fstr_dos = $ms_tgc;
								        }
							        }

						        $num_d = 0;
						        $mn = 0;
						        for (; $mn < $n_count; $mn++)
							        {
							        $sthaw = strtolower($n_array[$mn]);
							        if (strpos($sthaw, $st_ch_d) !== false)
								        {
								        $f_pos = $mn;
								        $num_d = $mn + 1;
								        }
							        }

						        $i = 0;
						        $j = 0;
						        $st_code = 0;
						        $from_count = 0;
						        $to_count = 0;
						        for (; $i < $s_c; $i++)
							        {
							        $s = 0;
							        $st_c = $smatch_param[$i];
							        for (; $s < $n_count; $s++)
								        {
								        $ins_low = strtolower($n_array[$s]);
								        if (strpos($ins_low, $st_c) !== false)
									        {
									        $from_count++;
									        }
								        }
							        }

						        for (; $j < $t_c; $j++)
							        {
							        $s = 0;
							        $st_c = $tmatch_param[$j];
							        for (; $s < $n_count; $s++)
								        {
								        $ins_low = strtolower($n_array[$s]);
								        if (strpos($ins_low, $st_c) !== false)
									        {
									        $to_count++;
									        }
								        }
							        }


//echo $from_count."..............".$to_count;
						        if ($from_count >= 2 || $to_count >= 2)
							        {
							        $n_id = substr($n_array[5], 0, -1);
							        if ($from_count >= 2)
								        {
								        $mtd_from2 = $n_array[6];
								        $ntd_c = strlen($mtd_from2);
								        $ntd_c1 = $ntd_c - $from_sta;
								        $mtd_from1 = substr($mtd_from2, $from_sta, $ntd_c1);
								        $mtd_from = substr($mtd_from1, 0, $end_t);
								        
								        $mtd_from =  trim(preg_replace('/\s+/',' ', $mtd_from));
			                            $mtd_from = str_replace(" ","",$mtd_from);
			                            
			                            
								        if (array_key_exists($n_id, $f_array))
									        {
									        $f_array[$n_id]['id'] = $n_id;
									        $f_array[$n_id]['from'] = $mtd_from;
									        $f_array[$n_id]['t_time'] =$n_td_a;
									        }
								          else
									        {
									        $md_array = array(
										        "id" => $n_id,
										        "from" => $mtd_from,
										        "t_time" => $n_td_a
									        );
									        $f_array[$n_id] = $md_array;
									        }
								        }
							          else
								        {
								        $mtd_to2 = $n_array[6];
								        $ntd_d = strlen($mtd_to2);
								        $ntd_d1 = $ntd_d - $to_sta;
								        $mtd_to1 = substr($mtd_to2, $to_sta, $ntd_d1);
								        $mtd_to = substr($mtd_to1, 0, $end_t);
								        $mtd_to =  trim(preg_replace('/\s+/',' ', $mtd_to));
			                            $mtd_to = str_replace(" ","",$mtd_to);
			                            
			                            
								        $status_text = "";
								        if ($num_d > 1)
									        {
									        if (array_key_exists($num_d, $n_array))
										        {
										        if ($n_count > $num_d)
											        {
											        $s_d = $n_count - $num_d;
											        }

										        $a_es = array_slice($n_array, $num_d, $s_d);
										        $d_cd = count($a_es);
										        if ($d_cd > 0)
											        {
											        $status_text = implode(" ", $a_es);
											        }
										        }
									        }

								        if ($status_text == "")
									        {
									        }
								          else
									        {
									        $sta_data = $status_text;
									        $etd_d = strlen($sta_data);
									        if ($etd_d >= 2)
										        {
										        $etd_d1 = $etd_d - $msg_sta;
										        $sta_data1 = substr($sta_data, $msg_sta, $etd_d1);
										        $status_text = substr($sta_data1, 0, $msg_end);
										        }
									        }
								        // echo $n_array[12]."\n";

								        $status_detail = "";
								        if ($f_pos > 0)
									        {

									        // print_r($n_array);

									        $status_detail = $n_array[$f_pos];
									        $status_detail1 = strtolower($status_detail);
									        $s_tgc = $fstr_dos;
									        if (strpos($status_detail1, $s_tgc) !== false)
										        {
										        $stacodg = strlen($s_tgc);
										        $status_ct = strlen($status_detail1);
										        $status_detail = substr($status_detail1, $sta_cod, $status_ct);
										        }
									        }

								        if (array_key_exists($n_id, $f_array))
									        {
									            $f_array[$n_id]['id'] = $n_id;
									            if(array_key_exists("to",$f_array[$n_id]))
									            {
									                array_push($f_array[$n_id]['to'],$mtd_to);
									            }
									            else
									            {
									                $f_array[$n_id]['to']=array($mtd_to);
									            }
									        
									            if(array_key_exists("status",$f_array[$n_id]))
									            {
									                array_push($f_array[$n_id]['status'],$status_detail);
									            }
									            else
									            {
									                $f_array[$n_id]['status']=array($status_detail);
									            }
									        
									            if(array_key_exists("msg",$f_array[$n_id]))
									            {
									                array_push($f_array[$n_id]['msg'],$status_text);
									            }
									            else
									            {
									                $f_array[$n_id]['msg'] = array($status_text);
									            }
									        
									            
									        
									            //$f_array[$n_id]['status'] = $status_detail;
									            //$f_array[$n_id]['msg'] = $status_text;
									            $f_array[$n_id]['t_time'] =$n_td_a;
									        }
								          else
									        {
									        $md_array = array(
										        "id" => $n_id,
										        "to" => array($mtd_to),
										        "status" =>array( $status_detail),
										        "msg" => array( $status_text),
										        "t_time" => $n_td_a
									        );
									        $f_array[$n_id] = $md_array;
									        }
								        }
							        }
							        
						        }
						        
						        
					        }
				        }

			        fclose($m_file);
			        
			        //print_r($f_array);
			       // echo "................".$mail_id."_______________";
			       
			       
			       
			       if($st_ch == 1)
			       {
			            $n_f_array = array();
			            //$inter_sec_array = array($mail_id);
			            foreach($f_array as  $keyf => $rowd)
			            {
			                $nstat = 0;
			                $nrtat = 0;
			                    if($snd_stat == 1 || $rs5 == 1  )
			                    {
			                    
		                            if(array_key_exists("from",$rowd))
		                            {
		                                if($rowd['from'] == $mail_id)
	                                    {
	                                        $nstat =1 ;
	                                    }
			                        }
		                    
		                        }
		                        else
		                        {
		                            if($rs5 == 1)
		                            {
		                                $nstat =1 ;
		                            
		                            }
		                        
		                        }
		                        
		                        //echo $nstat."..................................";
		                        
		                        
			                $rtd = array();
			                $rtdn = array();
			                $rt_gs = array();
			                $rt_ms = array();
			                if($nstat == 1 )  {  }
			                else
			                {
			                
			                    if($rs5 ==1  || $rcv_stat == 1)
			                    {
                                    if(array_key_exists("to",$rowd))
                                    {
                                        $rtd =  $rowd['to'];
                                        $rt_gs = $rowd['status'];
                                        $rt_ms = $rowd['msg'];
                                        $c_d_n  = count($rtd);
                                        $y = 0;
                                        for( ;$y < $c_d_n; $y++)
                                        {
                                            if($rtd[$y] == $mail_id)
                                            {}
                                            else
                                            {
                                            
                                                if(array_key_exists($y,$rt_gs)){ unset($rt_gs[$y]); }
                                                if(array_key_exists($y,$rtd)){ unset($rtd[$y]); }
                                                if(array_key_exists($y,$rt_ms)){ unset($rt_ms[$y]); }
                                            
                                            }
                                        }
                                        //$rtdn = array_intersect($rtd,$inter_sec_array);
                                        
                                                               
                                    }
                                }
                            }
                            
                            if(count($rtd) > 0)
                            {
                                $nrtat = 1;
                            
                            }
                            if($nstat == 1 || $nrtat == 1)
                            {
                               $rowd['s'] = $nstat;
                               $rowd['r'] = $nrtat;
                               if($nrtat == 1)
                               {
                                    $rowd['to'] = array_values($rtd);
                                    $rowd['status'] = array_values($rt_gs);
                                    $rowd['msg'] = array_values($rt_ms);
                               }
                               $n_f_array[$keyf] = $rowd; 
                            
                            }
                            
                            			            
			            
			            }
			            $jsarray = array("status"=> 1 ,"res_data" =>$n_f_array,"asd"=>1);
			            echo json_encode($jsarray);
			        }
			        else
			        {
			            $jsarray = array("status"=> 1 ,"res_data" =>$f_array,"asd"=>2);
			            echo json_encode($jsarray);
			        }
			        
			        
			        
			        }
		          else
			        {
			               $jsarray = array("status"=> 0 ,"msg"=>"Unable to open  mail log file");
			               echo json_encode($jsarray);
			        }
		        }
	          else
		        {
                    $jsarray = array("status"=> 0 ,"msg"=>"May be file is not  readable");
                    echo json_encode($jsarray);
		        }
		}
		else
		{
		$jsarray = array("status"=> 0 ,"msg"=>"Getting some server issue, please contact System Admin. ");
                    echo json_encode($jsarray);

		}

	        }
          else
	        {
	            $jsarray = array("status"=> 0 ,"msg"=>"Mail log file is not exist");
                echo json_encode($jsarray);
	        }
            }
            else
            {
                $jsarray = array("status"=> 0 ,"msg"=>"Please send proper date format(Ex: YYYY-MM-DD HH:MM:SS)");
                echo json_encode($jsarray);
            
            } 
	        
	      }
	      else
	      {
	            $jsarray = array("status"=> 0 ,"msg"=>"Please send proper date format(Ex: YYYY-MM-DD HH:MM:SS)");
                echo json_encode($jsarray);
	      
	      }  
	        
	     }
	      else
	      {
	            $jsarray = array("status"=> 0 ,"msg"=>"Please send proper data");
                echo json_encode($jsarray);
	      
	      }   
	        
        }
        else
        {
            $jsarray = array("status"=> 0 ,"msg"=>"Please send proper date format(Ex: YYYY-MM-DD HH:MM:SS)");
                echo json_encode($jsarray);
        
        }
        exit;
    }
    
    

}