<?php
class sms {
    var $error;

    function clicktell_api($to, $content, $details) {
        $user=$details['user_name'];
        $password=$details['password'];
        $api_id=$details['api_id'];
        $baseurl=$details['base_url'];

        if($user=='' || $password=='' || $api_id=='' || $baseurl=='') {
            $this->error='Invalid inputs';
            return false;
        }
        $text=urlencode($content);

        // auth call
        $url="$baseurl/http/auth?user=$user&password=$password&api_id=$api_id";
        // do auth call


		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$ret = curl_exec($ch);
		curl_close($ch);


        //$ret=file_get_contents($url);

        // split our response. return string is on first line of the data returned
        $sess = split(":",$ret);
        if($sess[0]=="OK") {
            $sess_id=trim($sess[1]); // remove any whitespace
            $url="$baseurl/http/sendmsg?session_id=$sess_id&to=$to&text=$text";
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$ret = curl_exec($ch);
			curl_close($ch);
            // do sendmsg call
            //$ret=file($url);
            $send=split(":",$ret);
            if($send[0]!="ID") {
                $this->error="send message failed ".$ret[0];
                return false;
            }
            else $this->error="send message successful";
        }
        else {
            $this->error="Authentication failure: ". $ret[0];
            return false;
        }
        return true;
    }

    function two_sms($to, $content, $details) {
        $user=$details['user_name'];
        $password=$details['password'];
        $url=$details['base_url'];

        if($user=='' || $password=='' || $url=='') {
            $this->error='Invalid inputs';
            return false;
        }

        $text=substr($content, 0, 160);
        $mobnum=$to;
        $result='';

        $myOutMsg = '<?xml version="1.0" encoding="UTF-8" ?>';
        $myOutMsg .= '<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
        $myOutMsg .= 'xsi:noNamespaceSchemaLocation="http://schema.2sms.com/1.0/0410_RequestSendMessage.xsd" ';
        $myOutMsg .= 'Version="1.0">';
        $myOutMsg .= '<Identification>';
        $myOutMsg .= '<UserID>' . $user .'</UserID>';
        $myOutMsg .= '<Password>'.$password .'</Password>';
        $myOutMsg .= '</Identification>';
        $myOutMsg .= '<Service>';
        $myOutMsg .= '<ServiceName>SendMessage</ServiceName>';
        $myOutMsg .= '<ServiceDetail>';
        $myOutMsg .= '<SingleMessage>';
        $myOutMsg .= '<Destination>'.$mobnum.'</Destination>';
        $myOutMsg .= '<Text>'.$text.'</Text>';
        $myOutMsg .= '</SingleMessage>';
        $myOutMsg .= '</ServiceDetail>';
        $myOutMsg .= '</Service>';
        $myOutMsg .= '</Request>';

        if (function_exists('curl_init')) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url); //'http://www.2sms.com/xml/xml.jsp'
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $myOutMsg);
            $result = curl_exec($ch);
            curl_close($ch);
            //echo $result;

        } else {
            $this->error="Curl Not Found. Using sockets...\r\n\r\n";
            $postdata = "POST /xml/xml.jsp HTTP/1.0\r\n";
            $postdata .="Host: www.2sms.com\r\n";
            $postdata .="Content-length: " . strlen($myOutMsg) . "\r\n" ;
            $postdata .="Content-Type: text/xml\r\n";
            $postdata .="Connection: Close\r\n\r\n";
            $postdata .="$myOutMsg\r\n";

            $fp = fsockopen('www.2sms.com', 80, $errno, $errstr, 30);
            if (!$fp){
                 $this->error.="ERROR:" . $errno . "-" . $errstr;
                 return false;
            }
            else{
                socket_set_timeout($fp, 30);
                fputs($fp,$postdata);

                while(!feof($fp)) {
                    $result.=fgets($fp, 1024);
                }
                fclose($fp);
             }
        }
        if ($result==1 or strcmp('<ErrorCode>00</ErrorCode>', $result)) {
            $this->error='send sms success';
            return true;
        }
        else {
            $this->error='Failed to send sms';
            return false;
        }
        return true;
    }
	
	function twilio_sms($to, $content, $details)
	{
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'voip.php');
		$voip_object=new class_voip($database);
		$twilio_number_details = $voip_object->get_twilio_number(' is_outbound=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $twilio_number = $twilio_number_details[0]['twilio_number'];
		
		$title = $details['title'];
		$acc_sid = $details['acc_sid'];
		$auth_token = $details['auth_token'];
		$app_sid = $details['app_sid'];
		
		if($title=='' || $acc_sid=='' || $auth_token=='' || $app_sid=='') {
            $this->error='Invalid inputs';
            return false;
        }
		
		$array1=array('To' => $to,
			  'From' => $twilio_number,
			  'Body' => $content);
		
		$fields_string = http_build_query($array1);
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, 'https://api.twilio.com/2010-04-01/Accounts/'.$acc_sid.'/Messages.json');
		curl_setopt($curl, CURLOPT_USERPWD, $acc_sid.":".$auth_token);
		curl_setopt($curl, CURLOPT_POSTFIELDS,$fields_string);
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$msg = curl_exec($curl);
		curl_close($curl);
		return json_decode($msg);
	}
}
?>