<?php
	define("MESSAGE_HIGH_QUALITY", "GP");
	define("MESSAGE_MEDIUM_QUALITY", "TI");
	define("MESSAGE_LOW_QUALITY", "SI");
		
	class Skebby {
			
		public $username = null;
		public $password = null;
		public $endpoint = null;
		public $messageType = null;
		public $returnCredits = null;
		public $sender = null;
		
		private $auth = null;
		
		function __construct($username, $password, $endpoint, $sender='') {
			$this->endpoint = $endpoint;
			$this->username = $username;
			$this->password = $password;
			
			$this->messageType = MESSAGE_HIGH_QUALITY;
			$this->returnCredits = true;
			$this->sender = $sender;
		}
		
		/**
		 * Authenticates the user given it's username and password.
		 * Returns the pair user_key, Session_key
		 */
		public function login() {
		    $ch = curl_init();
		    curl_setopt($ch, CURLOPT_URL, $this->endpoint .
		                'login?username=' . $this->username .
		                '&password=' . $this->password);
		
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		
		    $response = curl_exec($ch);
		    $info = curl_getinfo($ch);
		    curl_close($ch);
		
		    if ($info['http_code'] != 200) {
		        return null;
		    }
		
		    $auth = explode(";", $response);
		    $this->auth = $auth;
			//file_put_contents('/home/aosga/skebby.log', $this->auth);
		    return $auth;
		}
		
		/**
		 * Sends an SMS message
		 */
		public function sendSMS($message='', $recipients=[], $auth=[]) {
			
			$auth = empty($auth) ? $this->auth : $auth;
			
			if (empty($recipients)) {
				return ['result'=>'ERR', 'httpCode'=>0, 'bodyMsg'=>'No recipients provided'];
			} else {
				$recipients = !is_array($recipients) ? [$recipients] : $recipients;
			}
			
			foreach($recipients as $index => $recipient) {
				$recipients[$index] = $this->prepareNumber($recipient);
			}
						
		    $ch = curl_init();
		    curl_setopt($ch, CURLOPT_URL, $this->endpoint . 'sms');
		    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		        'Content-type: application/json',
		        'user_key: ' . $auth[0],
		        'Session_key: ' . $auth[1]
		    ));
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		    curl_setopt($ch, CURLOPT_POST, 1);
		    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->prepareStructure($message, $recipients)));
		    $response = curl_exec($ch);
		    $info = curl_getinfo($ch);
		    curl_close($ch);
		    
		    if ($info['http_code'] != 201) {
				file_put_contents('/home/aosga/skebby.log', $response);
				return ['result'=>'ERR', 'httpCode'=>$info['http_code'], 'bodyMsg'=>$response];
			} else {
				file_put_contents('/home/aosga/skebby.log', $response);
				return json_decode($response, true);
			}
		}
		
		/**
			* Receive new SMS messages
 		*/
		public function receiveSMS($idSim='', $limit=100, $auth=[]) {
			$auth = empty($auth) ? $this->auth : $auth;
		    $ch = curl_init();
		    curl_setopt($ch, CURLOPT_URL, $this->endpoint . 'newsrsmsmessage/' . $idSim . '?limit=' . $limit);
		    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		        'Content-type: application/json',
		        'user_key: ' . $auth[0],
		        'Session_key: ' . $auth[1]
		    ));
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		    $response = curl_exec($ch);
		    $info = curl_getinfo($ch);
		    curl_close($ch);
		
		    if ($info['http_code'] != 200) {
		        //return null;
		        return ['result'=>'ERR', 'httpCode'=>$info['http_code'], 'bodyMsg'=>$response];
		    }
		
		    return json_decode($response, true);
		}

		
		private function prepareStructure($message='', $recipients=[]) {
			return [
				'message' => $message,
				'message_type' => $this->messageType,
				'returnCredits' => $this->returnCredits,
				'recipient' => $recipients,
				'sender' => $this->sender
			];
		}
		
		private function prepareNumber($number='') {
			$number = trim(preg_replace('/[^0-9]/', '', $number));
			$number = strpos('+', $number) === false ? '+'.$number : $number;
			return $number;
		}
	}