<?php
namespace PEZESHKYAR\SMS\Drivers;

class Ippanel{
    protected $username;
	
	protected $password;

    public function __construct($username,$password)
	{
		$this->username = $username;
		$this->password = $password;
	}

    public function sendSms($to,$from,$text){
        $url = "https://ippanel.com/services.jspd";
		
		$rcpt_nm = array($to);
		$param = array
					(
						'uname'=>$this->username,
						'pass'=>$this->password,
						'from'=>$from,
						'message'=>$text,
						'to'=>json_encode($rcpt_nm),
						'op'=>'send'
					);
					
		$handler = curl_init($url);             
		curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($handler, CURLOPT_POSTFIELDS, $param);                       
		curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
		$response = curl_exec($handler);

        $code         = curl_getinfo($handler, CURLINFO_HTTP_CODE);
		
		$curl_errno   = curl_errno($handler);
		
		$curl_error   = curl_error($handler);
		
		if ($curl_errno) {
			
			throw new \Exception($curl_error);
			
		}

        curl_close($handler);

        return $response;
    }

    public function sendPatterns($to,$from,$pattern_code,$input_data){
        $username = $this->username;
        $password = $this->password;
        $to = array($to);
        $url = "https://ippanel.com/patterns/pattern?username=" . $username . "&password=" . urlencode($password) . "&from=$from&to=" . json_encode($to) . "&input_data=" . urlencode(json_encode($input_data)) . "&pattern_code=$pattern_code";

        $handler = curl_init($url);
        curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($handler, CURLOPT_POSTFIELDS, $input_data);
        curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($handler);

        $code         = curl_getinfo($handler, CURLINFO_HTTP_CODE);
		
		$curl_errno   = curl_errno($handler);
		
		$curl_error   = curl_error($handler);
		
		if ($curl_errno) {
			
			throw new \Exception($curl_error);
			
		}

        curl_close($handler);

        return $response;
    }
}