<?php
/**********************************************************
* Source Copyright 2014 Author                            *
* Unauthorized reproduction is not allowed                *
* Author: JPK Software Solutions Pvt. Ltd.                *
* Email: sales@visionhelpdesk.com                         *
* Owner: www.visionhelpdesk.com                           *
**********************************************************/
if(!defined('_VIS_')) die('Error: Call from undefined function');
class chat {
    var $error;
	
	function livechat_list($operation){
		if(!isset($operation['vis_domain'])){
			$filter='integration_type=19 AND is_enabled=1';
		}else{
			$filter='integration_type=19 AND domain_id='.$operation['vis_domain'];
		}
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		
		$filter1='integration_type=19';
		list($livechat_info, $livechat_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$livechat_details=$livechat_details[0];
		$vis_domain = $livechat_details['domain_id'];
		list($live_info, $livechat_setting)=$object->get_integration_setting($filter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
	    list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
  		try {
			include(VIS_PATH_PLUGINS.'livechatinc/lib/LiveChat_API.php');
			$API = new LiveChat_API($livechat_details['api_key'],$livechat_details['admin_user']);
			if(!isset($operation['vis_page']))
				$page = 1;
			else
				$page = $operation['vis_page'];
					
			if(isset($operation['vis_date_from']) || isset($operation['vis_date_to']) || isset($operation['vis_field_search'])){
            	echo "<script>$('#live_domain').hide();</script>"; 			
				$arr = array('page'=>''.$page.'','date_from'=>''.date('Y-m-d',strtotime($operation['vis_date_from'])).'','date_to'=>''.date('Y-m-d',strtotime($operation['vis_date_to'])).'','query'=>''.$operation['vis_field_search'].'');
			}else{
				$arr = array('page'=>''.$page.'');
			}
  			$chat_object = $API->chats->get($arr);
			$chat_data=$chat_object->chats;
		}
		catch (Exception $e) {
			global $vis_language;
		}
		if(!isset($operation['vis_domain']))
	    	include(VIS_PATH_TEMPLATE.'livechat_visitors.php');
		else
			include(VIS_PATH_TEMPLATE.'ajax_livechatinc.php');			
        return true;
    }
	function chat_detail($operation){
		$spliter=(strstr($operation['vis_action'], '/'))?'/':',';
		$chat_id = $operation['vis_chat_id'];
		$vis_domain = $operation['vis_chk_domain'];
		
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		$filter='integration_type=19 AND domain_id='.$vis_domain;
		list($livechat_info, $livechat_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$livechat_details=$livechat_details[0];
		list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		try {
			include(VIS_PATH_PLUGINS.'livechatinc/lib/LiveChat_API.php');
			$API = new LiveChat_API($livechat_details['api_key'],$livechat_details['admin_user']);
			$chat_object = $API->chats->getSingleChat($chat_id);
		}
		catch (Exception $e) {
			die($e->getCode().' '.$e->getMessage());
		}
	    include(VIS_PATH_TEMPLATE.'chat_details.php');
        return true;
    }
	function livezilla_chat($operation){
		if(!isset($operation['vis_domain'])){
			$filter='integration_type=20 AND is_enabled=1';
		}else{
			$filter='integration_type=20 AND domain_id='.$operation['vis_domain'];
		}
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		 include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $staff_object=new class_staff($database);
		$filter1='integration_type=20';
		list($livechat_info, $livechat_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$livechat_details=$livechat_details[0];
		$vis_domain = $livechat_details['domain_id'];
		list($live_info, $livechat_setting)=$object->get_integration_setting($filter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
	    list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		$dom = (!isset($operation['vis_domain']))?$vis_domain:$operation['vis_domain'];
		$temp_filter='remoteapp_type=20 and (domain_id =0)';
		$remotescreen_details=$staff_object->get_remotescreen_setting($temp_filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		if($remotescreen_details==-1){
			$temp_filter='remoteapp_type=20 and (domain_id ='.$dom.')';
			$remotescreen_details = $staff_object->get_remotescreen_setting($temp_filter, VIS_STAFF_ID,LOCATION_AREA);
		} 
		//print_r($remotescreen_details);
		$user_details = $remotescreen_details[0];
  	  	$livezillaURL = $livechat_details['billing_url'];
		$apiURL = $livezillaURL . "api/v2/api.php";
		// authentication parameters
		$postd["p_user"]=$user_details['user_name'];
		$postd["p_pass"]=md5($user_details['user_password']);
		// function parameter
		
		$postd["p_operators_list"]=1;
		$postd["p_json_pretty"]=1;
		// filter parameters to include yesterday's tickets (100 maximum)
		if(isset($operation['vis_date_from'])){
			echo "<script>$('#live_domain').hide();</script>"; 	
			$yesterdayBegin = strtotime($operation['vis_date_from']);
		}
		else
	    	$yesterdayBegin = strtotime("");
			
		if(isset($operation['vis_date_to'])){
			echo "<script>$('#live_domain').hide();</script>"; 	
			$yesterdayEnd = strtotime($operation['vis_date_to']);
		}
		else
	    	$yesterdayEnd = strtotime("");
			
		if(isset($operation['vis_name'])){
			echo "<script>$('#live_domain').hide();</script>"; 	
			$operator_name = $operation['vis_name'];
		}
		else
	    	$operator_name = '';	
			
		$postd["p_operator"]=$operator_name;
		$postd["p_start_after"]=$yesterdayBegin;
		$postd["p_start_before"]=$yesterdayEnd;
		$postd["p_userid"]=$postd["p_user"];
		$postd["p_limit"]='';
		$postd["p_output"]="Plaintext";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$apiURL);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($postd));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
		$server_output = curl_exec($ch);
		
		if($server_output === false)
		exit(curl_error($ch));
		curl_close($ch);
		$opt_response = json_decode($server_output);
		$groups = $opt_response->Operators[0]->Operator->Groups;
		//print_r($groups);
		$postd["p_operators_list"]=0;
		$postd["p_chats_list"]=1;
        $result=array();	
		for($i=0;$i<count($groups);$i++)
		{
		$postd["p_group"]=$groups[$i];
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$apiURL);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($postd));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
		$server_output = curl_exec($ch);
		
		if($server_output === false)
		exit(curl_error($ch));
		curl_close($ch);
		$response = json_decode($server_output);
		
		$temp = $response->Chats;
		$result = array_merge($result,$temp);
		}
		
		$response = array();
		$response['Chats'] = $result;
		$response = json_decode(json_encode($response), FALSE);
		
		if(!isset($response)){
			global $vis_language;
			/*echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'");</script>';*/				
		}
		if(!isset($operation['vis_domain']))
	    	include(VIS_PATH_TEMPLATE.'livezilla_visitors.php');
		else
			include(VIS_PATH_TEMPLATE.'ajax_livezilla.php');
		
	    return true;
    }
	function livezilla_detail($operation){
		$spliter=(strstr($operation['vis_action'], '/'))?'/':',';
		$action=explode($spliter, $operation['vis_action']);
		$chat_id = $operation['vis_chat_id'];
		$vis_domain = $operation['vis_fz_domain'];
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $staff_object=new class_staff($database);
		$filter='integration_type=20 AND domain_id='.$vis_domain;
		list($livechat_info, $livechat_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$livechat_details=$livechat_details[0];
		list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		$dom = (!isset($operation['vis_domain']))?VIS_DOMAIN:$operation['vis_domain'];
		$temp_filter='remoteapp_type=20 and (domain_id =0)';
		$remotescreen_details=$staff_object->get_remotescreen_setting($temp_filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		if($remotescreen_details==-1){
			$temp_filter='remoteapp_type=20 and (domain_id ='.$dom.')';
			$remotescreen_details = $staff_object->get_remotescreen_setting($temp_filter, VIS_STAFF_ID,LOCATION_AREA);
		}
		//print_r($remotescreen_details);
		$user_details = $remotescreen_details[0];
		$livezillaURL = $livechat_details['billing_url'];
		$apiURL = $livezillaURL . "api/v2/api.php";
		$postd["p_user"]=$user_details['user_name'];
		$postd["p_pass"]=md5($user_details['user_password']);
		$postd["p_chatid"]=''.$chat_id.'';
		// function parameter
		$postd["p_chats_list"]=1;
		$postd["p_json_pretty"]=1;
		// filter parameters to include yesterday's tickets (100 maximum)
		$postd["p_output"]="Plaintext";
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$apiURL);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($postd));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$server_output = curl_exec($ch);
		//print_r($server_output);
		if($server_output === false)
		exit(curl_error($ch));
		curl_close($ch);
		$response = json_decode($server_output);
		$chat_object = $response->Chats[0]->Chat;
	    include(VIS_PATH_TEMPLATE.'chat_details.php');
        return true;
    }

	function userlike_list($operation){
		if(!isset($operation['vis_domain'])){
			$filter='integration_type=22 AND is_enabled=1';
		}else{
			$filter='integration_type=22 AND domain_id='.$operation['vis_domain'];
		}
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		
		list($livechat_info, $livechat_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$userlike_details=$livechat_details[0];
		
		$vis_domain = $userlike_details['domain_id'];
		$filter1='integration_type=22';
		list($live_info, $livechat_setting)=$object->get_integration_setting($filter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
	    list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
  		
		$ch = curl_init();
		if(isset($operation['vis_date_from']) || isset($operation['vis_date_to'])){
			echo "<script>$('#live_domain').hide();</script>"; 
			curl_setopt($ch, CURLOPT_URL, "http://www.userlike.com/api/external/message/chat_meta/?after=".$operation['vis_date_from']."&until=".$operation['vis_date_to']."");
		}else{
			curl_setopt($ch, CURLOPT_URL, "http://www.userlike.com/api/external/message/chat_meta/");
		}
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
			'Content-Type: application/json',
			'Authorization:'.$userlike_details['api_key'].'',                                                                                
			)
		);                                                                                                                   
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
		$result = curl_exec($ch);
		if($result === false)
				exit(curl_error($ch));
		$chat_data =  json_decode($result);
		if(!isset($operation['vis_domain']))
	    	include(VIS_PATH_TEMPLATE.'userlikechat_visitors.php');
		else
			include(VIS_PATH_TEMPLATE.'ajax_userlike.php');			
        return true;
    }
	function get_agent_data($vis_domain)
	{
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		$filter='integration_type=22 AND domain_id='.$vis_domain;
		list($livechat_info, $livechat_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$userlike_details=$livechat_details[0];
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "http://www.userlike.com/api/external/operator/");                                                                     
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
			'Content-Type: application/json',
			'Authorization:'.$userlike_details['api_key'].'',                                                                               
			)
		);                                                                                                                   
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
		$result = curl_exec($ch);
		if($result === false)
				exit(curl_error($ch));
		$operator_data =  json_decode($result);
		return $operator_data;
	}
	function userlike_detail($operation){
		$spliter=(strstr($operation['vis_action'], '/'))?'/':',';
		$chat_id = $operation['vis_chat_id'];
		$visitor_email = $operation['vis_visitor_email'];
		$visitor_name = $operation['vis_visitor_name'];
		$vis_domain = $operation['vis_userlike_domain'];
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		$filter='integration_type=22 AND domain_id='.$vis_domain;
		list($livechat_info, $livechat_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$userlike_details=$livechat_details[0];
		list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "http://www.userlike.com/api/external/message/transcript/".$chat_id."");
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
			'Content-Type: application/json',
			'Authorization:'.$userlike_details['api_key'].'',                                                                                
			)
		);                                                                                                                   
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
		$result = curl_exec($ch);
		if($result === false)
				exit(curl_error($ch));
		$chat_data =  json_decode($result);
	    include(VIS_PATH_TEMPLATE.'chat_details.php');
        return true;
    }
    function comm100_list($operation){
		if(!isset($operation['vis_domain'])){
			$filter='integration_type=21 AND is_enabled=1';
		}else{
			$filter='integration_type=21 AND domain_id='.$operation['vis_domain'];
		}
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		
		list($livechat_info, $livechat_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$comm100_details=$livechat_details[0];
		$vis_domain = $comm100_details['domain_id'];
		$filter1='integration_type=21';
		list($live_info, $livechat_setting)=$object->get_integration_setting($filter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
	    list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		if(isset($operation['vis_field_search']) || isset($operation['vis_date_from']) || isset($operation['vis_date_to'])){
			echo "<script>$('#live_domain').hide();</script>"; 
		}
		if(isset($operation['vis_field_search']) && $operation['vis_field_search']!=''){
			$keyword = $operation['vis_field_search'];
		}else{
			$keyword = '';
		}
  		if(isset($operation['vis_page']) && $operation['vis_page']!='')
				$page = $operation['vis_page'];
			else
				$page = 1;
		if(isset($operation['vis_date_from']) && $operation['vis_date_from']!=''){
			$vis_from_date = $operation['vis_date_from'];
		}else{
			$now = date('Y-m-d');
			$start_date = strtotime($now);
			$end_date = strtotime("-7 day", $start_date);
			$vis_from_date = date('Y-m-d',$end_date);
		}
		if(isset($operation['vis_date_to']) && $operation['vis_date_to']!=''){
			$vis_to_date = $operation['vis_date_to'];
		}else{
			$vis_to_date = $now;
		}
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "https://hosted.comm100.com/livechatapi/chats?date_from=".$vis_from_date."&date_to=".$vis_to_date."&page=".$page."&keyword=".$keyword."");
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_USERPWD, "".$comm100_details['admin_user'].":".$comm100_details['api_key']."");                                                                   
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                     
																		 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
																													   
		$result = curl_exec($ch);
		if($result === false)
				exit(curl_error($ch));
		$chat_object =  json_decode($result);
		$chat_data = $chat_object->chats;
		if(!isset($operation['vis_domain']))
	    	include(VIS_PATH_TEMPLATE.'comm100_visitors.php');
		else
			include(VIS_PATH_TEMPLATE.'ajax_comm100.php');			
        return true;
    }
	function comm100_detail($operation){
		$spliter=(strstr($operation['vis_action'], '/'))?'/':',';
		
		$chat_id = $operation['vis_chat_id'];
		$vis_domain = $operation['vis_chk_domain'];
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		$filter='integration_type=21 AND domain_id='.$vis_domain;
		list($livechat_info, $livechat_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$comm100_details=$livechat_details[0];
		list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		
		$ch = curl_init(); 
		curl_setopt($ch, CURLOPT_URL, "https://hosted.comm100.com/livechatapi/chats/".$chat_id."");
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_USERPWD, "".$comm100_details['admin_user'].":".$comm100_details['api_key']."");                                                        
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                     
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
																													   
		$result = curl_exec($ch);
		if($result === false)
				exit(curl_error($ch));
		$chat_data =  json_decode($result);
		include(VIS_PATH_TEMPLATE.'chat_details.php');
        return true;
    }	
    
    function clickdesk_chat($operation) {
        if(!isset($operation['vis_domain'])){
			$filter='integration_type=30 AND is_enabled=1';
		}else{
			$filter='integration_type=30 AND domain_id='.$operation['vis_domain'];
		}                
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		$filter1='integration_type=30';
		list($clickdesk_info, $clickdesk_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$clickdesk_details=$clickdesk_details[0];
		$vis_domain = $clickdesk_details['domain_id'];
		list($click_desk_info, $clickdesk_setting)=$object->get_integration_setting($filter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
                
                list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);             
                $url = "https://my.clickdesk.com/rest/dev/api/getchats/".$operation['vis_email'];
                ob_start();
		
                $curl_request = curl_init();          
                curl_setopt($curl_request, CURLOPT_URL, $url);
                curl_setopt($curl_request, CURLOPT_POST, 0);
                curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
          
                curl_setopt($curl_request, CURLOPT_HTTPHEADER, array("Accept: application/json"));              
                curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);

                curl_setopt($curl_request, CURLOPT_USERPWD, $clickdesk_details['user_name'].":".$clickdesk_details['api_key']);
                $clickdesk_chat = curl_exec($curl_request);               
      
                if($clickdesk_chat === false)
		exit(curl_error($ch));
		curl_close($ch);
		$response = json_decode($clickdesk_chat, true);                
		if(!isset($response)){
			global $vis_language;			
                }
		if(!isset($operation['vis_domain']))
	    	include(VIS_PATH_TEMPLATE.'clickdesk_chat.php');
		else
			include(VIS_PATH_TEMPLATE.'ajax_clickdesk.php');		    
         return true;
    }
    
    function clickdesk_detail($operation){
       
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);			
		$vis_domain = $operation['vis_fz_domain'];
                $filter='integration_type=30 AND domain_id='.$vis_domain;
		list($clickdesk_info, $clickdesk_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
                $clickdesk_details=$clickdesk_details[0];
                list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
                
                $url = "https://my.clickdesk.com/rest/dev/api/getchats/".$operation['vis_email'];
                ob_start();
		
                $curl_request = curl_init();          
                curl_setopt($curl_request, CURLOPT_URL, $url);
                curl_setopt($curl_request, CURLOPT_POST, 0);
                curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
          
                curl_setopt($curl_request, CURLOPT_HTTPHEADER, array("Accept: application/json"));              
                curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);

                curl_setopt($curl_request, CURLOPT_USERPWD, $clickdesk_details['user_name'].":".$clickdesk_details['api_key']);
                $clickdesk_chat = curl_exec($curl_request);               
      
                if($clickdesk_chat === false)
		exit(curl_error($ch));
		curl_close($ch);
		$response = json_decode($clickdesk_chat, true); 
                include(VIS_PATH_TEMPLATE.'clickdesk_chat_details.php');
        return true;
    }   
   
    function zopim_chat($operation){
                global $vis_language;
		if(!isset($operation['vis_domain'])){
			$filter='integration_type=32 AND is_enabled=1';
		}else{
			$filter='integration_type=32 AND domain_id='.$operation['vis_domain'];
		}
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		$filter1='integration_type=32';
		list($zopim_info, $zopim_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$zopim_details=$zopim_details[0];
		$vis_domain = $zopim_details['domain_id'];
		list($live_info, $zopim_setting)=$object->get_integration_setting($filter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
                list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
                if(isset($operation['vis_date_from']) && $operation['vis_date_from']!='' && isset($operation['vis_date_to']) && $operation['vis_date_to']=='' ) {                    
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['TXTSELECTDATE'].'"); </script>';
                    return false;
                }
                else if(isset($operation['vis_date_from']) && $operation['vis_date_from']=='' && isset($operation['vis_date_to']) && $operation['vis_date_to']!='' ) {                    
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['TXTSELECTDATE'].'"); </script>';
                    return false;
                }
                if(isset($operation['vis_visitor_email']) && $operation['vis_visitor_email']!='') {
                    $url = "https://www.zopim.com/api/v2/chats/search?q=visitor_email:".$operation['vis_visitor_email'];
                }
                elseif(isset($operation['vis_visitor_name']) && $operation['vis_visitor_name']!='') {
                    $url = "https://www.zopim.com/api/v2/chats/search?q=visitor_name:".$operation['vis_visitor_name'];
                }
                elseif(isset($operation['vis_date_from']) && $operation['vis_date_from']!='' && isset($operation['vis_date_to']) && $operation['vis_date_to']!='' ) {                    
                     $vis_date_from = date('Y-m-d',strtotime($operation['vis_date_from']));
                     $vis_date_to = date('Y-m-d',strtotime($operation['vis_date_to']));
                     $url = "https://www.zopim.com/api/v2/chats/search?q=timestamp:[".$vis_date_from." TO ".$vis_date_to."]";
                }              
                else  {
                    $url = "https://www.zopim.com/api/v2/chats";	
                }
                ob_start();
                $curl_request = curl_init();               
                curl_setopt($curl_request, CURLOPT_URL, $url);
                curl_setopt($curl_request, CURLOPT_POST, 0);
                curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);               
                curl_setopt($curl_request, CURLOPT_HTTPHEADER, array("Accept: application/json"));              
                curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);

                curl_setopt($curl_request, CURLOPT_USERPWD, $zopim_details['user_name'].":".$zopim_details['api_key']);
                $server_output = curl_exec($curl_request);
                curl_close($curl_request);
		
		if($server_output === false)
		exit(curl_error($curl_request));
		curl_close($curl_request);
                 if((isset($operation['vis_visitor_email']) && trim($operation['vis_visitor_email'])) || (isset($operation['vis_visitor_name']) && trim($operation['vis_visitor_name'])) || (isset($operation['vis_date_from']) && trim($operation['vis_date_from'])) || (isset($operation['vis_date_to']) && trim($operation['vis_date_to']))) {
                    $response = json_decode($server_output, true);             
                    for($i=0;$i< count($response['results']);$i++) {
                        $curl_request1 = curl_init();
                        $search_url = "https://www.zopim.com/api/v2/chats/".$response['results'][$i]['id'];
                        curl_setopt($curl_request1, CURLOPT_URL, $search_url);
                        curl_setopt($curl_request1, CURLOPT_POST, 0);
                        curl_setopt($curl_request1, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);         
                        curl_setopt($curl_request1, CURLOPT_HTTPHEADER, array("Accept: application/json"));         
                        curl_setopt($curl_request1, CURLOPT_RETURNTRANSFER, 1);
                        curl_setopt($curl_request1, CURLOPT_USERPWD, $zopim_details['user_name'].":".$zopim_details['api_key']);
                        $server_output1 = curl_exec($curl_request1);
                        curl_close($curl_request1);
                        $result = json_decode($server_output1, true);
                        $response[] = $result;
                        $count = $response['count'];
                     }
                }
                else {
                    if($operation['vis_filter_enable'] == 1){
                        $response = json_decode($server_output,true); 
                        $response = $response['chats'];
                        $count = count($response);
                    }
                    else $response = json_decode($server_output);                   
                }
		if(!isset($response)){
			global $vis_language;
			/*echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'");</script>';*/				
		}
		if(!isset($operation['vis_domain']))
                    include(VIS_PATH_TEMPLATE.'zopim_chat.php');
		else
                      if(isset($operation['vis_visitor_email']) || isset($operation['vis_visitor_name']))                      
			include(VIS_PATH_TEMPLATE.'ajax_zopim_search.php');                   
                     else
                        include(VIS_PATH_TEMPLATE.'ajax_zopim.php');		
	    return true;
    }
    function zopim_detail($operation){
		$spliter=(strstr($operation['vis_action'], '/'))?'/':',';
		$chat_id = $operation['vis_chat_id'];
		$visitor_email = $operation['vis_visitor_email'];
		$visitor_name = $operation['vis_visitor_name'];
		$vis_domain = $operation['vis_domain'];
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		$filter='integration_type=32 AND domain_id='.$vis_domain;
		list($zopim_info, $zopim_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$zopim_details=$zopim_details[0];
		list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		 $url = "https://www.zopim.com/api/v2/chats/".$chat_id;
		$curl_request = curl_init();               
                curl_setopt($curl_request, CURLOPT_URL, $url);
                curl_setopt($curl_request, CURLOPT_POST, 0);
                curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);               
                curl_setopt($curl_request, CURLOPT_HTTPHEADER, array("Accept: application/json"));              
                curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);

                curl_setopt($curl_request, CURLOPT_USERPWD, $zopim_details['user_name'].":".$zopim_details['api_key']);
                $server_output = curl_exec($curl_request);
                curl_close($curl_request); 
		
		if($server_output === false)
                    exit(curl_error($curl_request));
		$chat_data =  json_decode($server_output);
	    include(VIS_PATH_TEMPLATE.'zopim_chat_details.php');
        return true;
    }
	function crisp_chat($operation){
		 
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		$filter='integration_type=46';
		list($crisp_info, $crisp_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);  
		$crisp_details=$crisp_details[0];  
		$vis_domain = $crisp_details['domain_id']; 
	    list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);  
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL,'https://api.crisp.im/v1/user/session/login');
		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
		curl_setopt($curl, CURLOPT_USERPWD, $crisp_details['admin_user']. ':'.$crisp_details['api_key']);
		$data = array('email'=>$crisp_details['admin_user'],'password'=>$crisp_details['api_key']);
		$data_json = json_encode($data);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json); 
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_POST, true);
		$crisp = curl_exec($curl);  
		$crisp=json_decode($crisp,true);  
		 
		$update=$object->update_crisp($crisp, $userid, $user); 
		$filter=' integration_type=46';
		 $crisp_data=$object->intergration_crisp($data, $userid, $user,$filter);  
		 $crisp_data=$crisp_data[0];  
		 $curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, 'https://api.crisp.im/v1/user/account/websites');
		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json','Authorization: Basic '. base64_encode($crisp_data['user_name'].':'.$crisp_data['admin_password'] )));
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		$website = curl_exec($curl);
		$website=json_decode($website,true);  
		$operation['vis_website_id']=$website['data'][0]['id'];
	     $curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, 'https://api.crisp.im/v1/website/'.$operation['vis_website_id'].'/conversations');
		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json','Authorization: Basic '. base64_encode($crisp_data['user_name'].':'.$crisp_data['admin_password'] )));
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		$conversations = curl_exec($curl);
		$conversations=json_decode($conversations,true); 
		include(VIS_PATH_TEMPLATE.'crisp_chatdetail.php');
		 			
        return true;
	}
	function crisp_chatlist1($operation){
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		$filter=' integration_type=46';
		$crisp_list=$object->intergration_crisp($data, $userid, $user,$filter);  
		$crisp_list=$crisp_list[0]; 
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, 'https://api.crisp.im/v1/website/'.$operation['vis_website_id'].'/conversations');
		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json','Authorization: Basic '. base64_encode($crisp_list['user_name'].':'.$crisp_list['admin_password'] )));
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		$conversations = curl_exec($curl);
		$conversations=json_decode($conversations,true); 
		include(VIS_PATH_TEMPLATE.'crisp_chatdetailtable.php');
		
		
	}
		 
	function crisp_chatdetail($operation){
		$spliter=(strstr($operation['vis_action'], '/'))?'/':',';
		$action=explode($spliter, $operation['vis_action']); 
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		$filter=' integration_type=46';
		$crisp_detail=$object->intergration_crisp($data, $userid, $user,$filter);  
		$crisp_detail=$crisp_detail[0];
		$vis_domain="";
		 list($dpinf, $department)=$object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA); 
		 
		$curl = curl_init();  
		curl_setopt($curl, CURLOPT_URL, 'https://api.crisp.im/v1/website/'.$action[3].'/conversation/'.$action[4].'');
		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json','Authorization: Basic '. base64_encode($crisp_detail['user_name'].':'.$crisp_detail['admin_password'] )));
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		$allchat = curl_exec($curl);
		$allchat=json_decode($allchat,true); 
		$curl = curl_init();
		for($i=0;$i<=count($allchat['data']['messages']);$i++){
		curl_setopt($curl, CURLOPT_URL, 'https://api.crisp.im/v1/website/'.$action[3].'/operator/'.$allchat['data']['messages'][$i]['user']['user_id'] .'');
		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json','Authorization: Basic '. base64_encode($crisp_detail['user_name'].':'.$crisp_detail['admin_password'] )));
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		 
		$opertor_id = curl_exec($curl);
		$opertor_id=json_decode($opertor_id,true); 
		 }
		include(VIS_PATH_TEMPLATE.'crispchat_details.php');
		
		}
	
}