<?php
##Author : Vision helpdesk
##Created by :  Amol N
##Created on : 01-April-2016 
##Updation on :
##exotel apis for calling,sms & fetch data

	class class_exotel{
	 		
		function connect_call($data){ //customer_to_agent, customer to flow
			return $this->exotel_curl("Calls/connect",1,$data);			
		}
		
		function send_sms($data){  //send singe/bulk sms			
			return $this->exotel_curl("Sms/send",1,$data);	
		}
		
		function get_call_details($call_sid='',$data){ //call_sid is sid of a call call id ='' for all call details
			return $this->exotel_curl("Calls/".$call_sid,0,$data);	
		}
		function get_sms_details($sms_sid='',$data){ 
			return $this->exotel_curl("SMS/Messages/".$sms_sid,0,$data);	
		}
		//Implementation of a DND scrubber using Exotel's Metadata API. 
	
		function get_number_info($numbers_path,$data){
			$numbers = file($numbers_path);
				$dnd_free_numbers = array();			
				
				foreach($numbers as $number) {

					$number = trim($number); 
					$url = "https://".$data['exotel_sid'].":".$data['exotel_token']."@twilix.exotel.in/v1/Accounts/".$data['exotel_sid']."/Numbers/".$number;
				 
					$ch = curl_init();
					curl_setopt($ch, CURLOPT_VERBOSE, 1);
					curl_setopt($ch, CURLOPT_URL, $url);
					curl_setopt($ch, CURLOPT_POST, 0);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
					curl_setopt($ch, CURLOPT_FAILONERROR, 0);
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
				 
					$http_result = curl_exec($ch);
					$error = curl_error($ch);
					$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);
					curl_close($ch);
					
					if($error) continue; 

					$xml = simplexml_load_string($http_result);
					if(isset($xml->RestException->Status) && $xml->RestException->Status == 401) {
						exit('Authentication failed');
					} 
					if($xml->Numbers->DND != "Yes") {
						$dnd_free_numbers[] = "$number\n";
					}
					$result[] = $this->objectToArray($xml);
					sleep(3); //Just so we don't trigger Exotel server's spam buttons..
			}
			return array("DND_FREE"=>$dnd_free_numbers,"NO_INFO"=>$result);
		}
		function exotel_curl($url_type,$request,$data=''){
			//$request = 1: post, 0:no get/post request
			$url = "https://".$data['exotel_sid'].":".$data['exotel_token']."@twilix.exotel.in/v1/Accounts/".$data['exotel_sid']."/".$url_type;
			$post = ($request==1)?1:0;
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_VERBOSE, 1);
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_POST, $post); 
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_FAILONERROR, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
			if($request==1){ curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data['post_data'])); }		 
			$http_result = curl_exec($ch);
			$error = curl_error($ch);
			$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);		 
			curl_close($ch);	
			
			$xml = simplexml_load_string($http_result);
			if(isset($xml->RestException->Status) && $xml->RestException->Status == 401) {
				//exit('Authentication failed');
				return -1;
			} 
			$result = $this->objectToArray($xml);
			return $result;
		}
		
		function objectToArray($d) {		
		  return  json_decode(json_encode($d),true);
		}
	

	}// class_exotel

   ## Refrence
   /*
	$exotel_sid = "nandhanameducationalinstitutions"; 
	$exotel_token = "12f90773dee920b6a077c904ba712fc20dae45cf"; 
	$data = array(	"exotel_sid"=>$exotel_sid,
					"exotel_token"=>$exotel_token,					
				 );
	//connect cust to agent
	$post_data1 = array(
		'From' => "09404591511", //agent
		'To' => "09579290369", //customer
		'CallerId' => "04439592073",
		'TimeLimit' => "",
		'TimeOut' => "",
		'CallType' => "trans" //Can be "trans" for transactional and "promo" for promotional content
	);

	//connect to customer flow
	$post_data2 = array(
		'From' => "09579290369", //customer
		'To' => "09404591511", //agent
		'CallerId' => "04439592073",
		'Url' => "http://my.exotel.in/exoml/start/68774",
		'TimeLimit' => "", //This is optional
		'TimeOut' => "", //This is also optional
		'CallType' => "trans",
		'StatusCallback' => "http://iamdemo.com/vision4/amoln/v4/", //This is also also optional
	);
	//send sms
	$post_data3 = array(
		// 'From' doesn't matter; For transactional, this will be replaced with your SenderId;
		// For promotional, this will be ignored by the SMS gateway
		'From'   => '07350079098',
		'To'    => '09404591511',
		'Body'  => 'vision test Reply 1/2 to 8088919888 to choose an appointment with Dr. Rajasekar', //Incase you are wondering who Dr. Rajasekhar is http://en.wikipedia.org/wiki/Dr._Rajasekhar_(actor)
	);

	//send bulk sms
	$post_data4 = array(
		// 'From' doesn't matter; For transactional, this will be replaced with your SenderId;
		// For promotional, this will be ignored by the SMS gateway
		'From'   => '04439592073',
		'To'    => array('09404591511', '08275569009'),
		'Body'  => 'vision bulk test'
	);

	//get_call_details
	$call_sid = "43282f93c9466a425402def1c86c8013"; //sid of a call 
    //85e002ceb582445fdef7148fea24d26d    //43282f93c9466a425402def1c86c8013 //09cead6dc06cdf3ee256f95aa72532f7
	
	$data["post_data"] = $post_data2;	

	$obj_exo = new class_exotel();
	//sent sms
	//$ret = $obj_exo->send_sms($data);
	
	//make call
	$ret = $obj_exo->connect_call($data);
	//$ret = $obj_exo->get_call_details($call_sid,$data);
	
	//get dnd/num info
	//$ret = $obj_exo->get_number_info("http://iamdemo.com/vision4/amoln/v4/plugins/exotel/my/numbers.txt",$data);
	echo '<pre>';
	print_r($ret);
	echo '</pre>';
 */
?>