Implement direct message receive and background services manager, take 1.
[openemr.git] / modules / sms_email_reminder / sms_clickatell.php
blob199be9359f01425d9ca853f0abc939be4c43a1b2
1 <?php
2 /**
3 * CLICKATELL SMS API
5 * This class is meant to send SMS messages (with unicode support) via
6 * the Clickatell gateway and provides support to authenticate to this service,
7 * spending an vouchers and also query for the current account balance. This class
8 * use the fopen or CURL module to communicate with the gateway via HTTP/S.
10 * For more information about CLICKATELL service visit http://www.clickatell.com
12 * @version 1.6
13 * @package sms_api
14 * @author Aleksandar Markovic <mikikg@gmail.com>
15 * @copyright Copyright (c) 2004 - 2007 Aleksandar Markovic
16 * @link http://sourceforge.net/projects/sms-api/ SMS-API Sourceforge project page
17 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
21 /**
22 * Main SMS-API class
24 * Example:
25 * <code>
26 * <?php
27 * require_once ("sms_api.php");
28 * $mysms = new sms();
29 * echo $mysms->session;
30 * echo $mysms->getbalance();
31 * // $mysms->token_pay("1234567890123456"); //spend voucher with SMS credits
32 * $mysms->send ("38160123", "netsector", "TEST MESSAGE");
33 * </code>
34 * @package sms_api
37 class sms
40 /**
41 * Clickatell API-ID
42 * @link http://sourceforge.net/forum/forum.php?thread_id=1005106&forum_id=344522 How to get CLICKATELL API ID?
43 * @var integer
45 var $api_id = "YOUR_CLICKATELL_API_NUMBER";
47 /**
48 * Clickatell username
49 * @var mixed
51 var $user = "YOUR_CLICKATELL_USERNAME";
53 /**
54 * Clickatell password
55 * @var mixed
57 var $password = "YOUR_CLICKATELL_PASSWORD";
59 /**
60 * Use SSL (HTTPS) protocol
61 * @var bool
63 var $use_ssl = false;
65 /**
66 * Define SMS balance limit below class will not work
67 * @var integer
69 var $balace_limit = 0;
71 /**
72 * Gateway command sending method (curl,fopen)
73 * @var mixed
75 var $sending_method = "fopen";
77 /**
78 * Does to use facility for delivering Unicode messages
79 * @var bool
81 var $unicode = false;
83 /**
84 * Optional CURL Proxy
85 * @var bool
87 var $curl_use_proxy = false;
89 /**
90 * Proxy URL and PORT
91 * @var mixed
93 var $curl_proxy = "http://127.0.0.1:8080";
95 /**
96 * Proxy username and password
97 * @var mixed
99 var $curl_proxyuserpwd = "login:secretpass";
102 * Callback
103 * 0 - Off
104 * 1 - Returns only intermediate statuses
105 * 2 - Returns only final statuses
106 * 3 - Returns both intermediate and final statuses
107 * @var integer
109 var $callback = 0;
112 * Session variable
113 * @var mixed
115 var $session;
118 * Class constructor
119 * Create SMS object and authenticate SMS gateway
120 * @return object New SMS object.
121 * @access public
123 function sms () {
124 if ($this->use_ssl) {
125 $this->base = "http://api.clickatell.com/http";
126 $this->base_s = "https://api.clickatell.com/http";
127 } else {
128 $this->base = "http://api.clickatell.com/http";
129 $this->base_s = $this->base;
131 $this->_auth();
135 * Authenticate SMS gateway
136 * @return mixed "OK" or script die
137 * @access private
139 function _auth() {
140 $comm = sprintf ("%s/auth?api_id=%s&user=%s&password=%s", $this->base_s, $this->api_id, $this->user, $this->password);
141 $this->session = $this->_parse_auth ($this->_execgw($comm));
145 * Query SMS credis balance
146 * @return integer number of SMS credits
147 * @access public
149 function getbalance() {
150 $comm = sprintf ("%s/getbalance?session_id=%s", $this->base, $this->session);
151 return $this->_parse_getbalance ($this->_execgw($comm));
155 * Send SMS message
156 * @param to mixed The destination address.
157 * @param from mixed The source/sender address
158 * @param text mixed The text content of the message
159 * @return mixed "OK" or script die
160 * @access public
162 function send($to=null, $from=null, $text=null) {
164 /* Check SMS credits balance */
165 if ($this->getbalance() < $this->balace_limit) {
166 die ("You have reach the SMS credit limit!");
169 /* Check SMS $text length */
170 if ($this->unicode == true) {
171 $this->_chk_mbstring();
172 if (mb_strlen ($text) > 210) {
173 die ("Your unicode message is too long! (Current lenght=".mb_strlen ($text).")");
175 /* Does message need to be concatenate */
176 if (mb_strlen ($text) > 70) {
177 $concat = "&concat=3";
178 } else {
179 $concat = "";
181 } else {
182 if (strlen ($text) > 459) {
183 die ("Your message is too long! (Current lenght=".strlen ($text).")");
185 /* Does message need to be concatenate */
186 if (strlen ($text) > 160) {
187 $concat = "&concat=3";
188 } else {
189 $concat = "";
193 /* Check $to and $from is not empty */
194 if (empty ($to)) {
195 die ("You not specify destination address (TO)!");
197 if (empty ($from)) {
198 die ("You not specify source address (FROM)!");
201 /* Reformat $to number */
202 $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
203 $to = str_replace($cleanup_chr, "", $to);
205 /* Send SMS now */
206 $comm = sprintf ("%s/sendmsg?session_id=%s&to=%s&from=%s&text=%s&callback=%s&unicode=%s%s",
207 $this->base,
208 $this->session,
209 rawurlencode($to),
210 rawurlencode($from),
211 $this->encode_message($text),
212 $this->callback,
213 $this->unicode,
214 $concat
216 return $this->_parse_send ($this->_execgw($comm));
220 * Encode message text according to required standard
221 * @param text mixed Input text of message.
222 * @return mixed Return encoded text of message.
223 * @access public
225 function encode_message ($text) {
226 if ($this->unicode != true) {
227 //standard encoding
228 return rawurlencode($text);
229 } else {
230 //unicode encoding
231 $uni_text_len = mb_strlen ($text, "UTF-8");
232 $out_text = "";
234 //encode each character in text
235 for ($i=0; $i<$uni_text_len; $i++) {
236 $out_text .= $this->uniord(mb_substr ($text, $i, 1, "UTF-8"));
239 return $out_text;
244 * Unicode function replacement for ord()
245 * @param c mixed Unicode character.
246 * @return mixed Return HEX value (with leading zero) of unicode character.
247 * @access public
249 function uniord($c) {
250 $ud = 0;
251 if (ord($c{0})>=0 && ord($c{0})<=127)
252 $ud = ord($c{0});
253 if (ord($c{0})>=192 && ord($c{0})<=223)
254 $ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
255 if (ord($c{0})>=224 && ord($c{0})<=239)
256 $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
257 if (ord($c{0})>=240 && ord($c{0})<=247)
258 $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
259 if (ord($c{0})>=248 && ord($c{0})<=251)
260 $ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
261 if (ord($c{0})>=252 && ord($c{0})<=253)
262 $ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
263 if (ord($c{0})>=254 && ord($c{0})<=255) //error
264 $ud = false;
265 return sprintf("%04x", $ud);
269 * Spend voucher with sms credits
270 * @param token mixed The 16 character voucher number.
271 * @return mixed Status code
272 * @access public
274 function token_pay ($token) {
275 $comm = sprintf ("%s/http/token_pay?session_id=%s&token=%s",
276 $this->base,
277 $this->session,
278 $token);
280 return $this->_execgw($comm);
284 * Execute gateway commands
285 * @access private
287 function _execgw($command) {
288 if ($this->sending_method == "curl")
289 return $this->_curl($command);
290 if ($this->sending_method == "fopen")
291 return $this->_fopen($command);
292 die ("Unsupported sending method!");
296 * CURL sending method
297 * @access private
299 function _curl($command) {
300 $this->_chk_curl();
301 $ch = curl_init ($command);
302 curl_setopt ($ch, CURLOPT_HEADER, 0);
303 curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1);
304 curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,0);
305 if ($this->curl_use_proxy) {
306 curl_setopt ($ch, CURLOPT_PROXY, $this->curl_proxy);
307 curl_setopt ($ch, CURLOPT_PROXYUSERPWD, $this->curl_proxyuserpwd);
309 $result=curl_exec ($ch);
310 curl_close ($ch);
311 return $result;
315 * fopen sending method
316 * @access private
318 function _fopen($command) {
319 $result = '';
320 $handler = @fopen ($command, 'r');
321 if ($handler) {
322 while ($line = @fgets($handler,1024)) {
323 $result .= $line;
325 fclose ($handler);
326 return $result;
327 } else {
328 die ("Error while executing fopen sending method!<br>Please check does PHP have OpenSSL support and check does PHP version is greater than 4.3.0.");
333 * Parse authentication command response text
334 * @access private
336 function _parse_auth ($result) {
337 $session = substr($result, 4);
338 $code = substr($result, 0, 2);
339 if ($code!="OK") {
340 die ("Error in SMS authorization! ($result)");
342 return $session;
346 * Parse send command response text
347 * @access private
349 function _parse_send ($result) {
350 $code = substr($result, 0, 2);
351 if ($code!="ID") {
352 die ("Error sending SMS! ($result)");
353 } else {
354 $code = "OK";
356 return $code;
360 * Parse getbalance command response text
361 * @access private
363 function _parse_getbalance ($result) {
364 $result = substr($result, 8);
365 return (int)$result;
369 * Check for CURL PHP module
370 * @access private
372 function _chk_curl() {
373 if (!extension_loaded('curl')) {
374 die ("This SMS API class can not work without CURL PHP module! Try using fopen sending method.");
379 * Check for Multibyte String Functions PHP module - mbstring
380 * @access private
382 function _chk_mbstring() {
383 if (!extension_loaded('mbstring')) {
384 die ("Error. This SMS API class is setup to use Multibyte String Functions module - mbstring, but module not found. Please try to set unicode=false in class or install mbstring module into PHP.");