increment database counter for prior commits
[openemr.git] / modules / sms_email_reminder / sms_clickatell.php
blobc3ac2fefa396d18c4c39d57b19d29f14f7e62387
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 __construct()
125 if ($this->use_ssl) {
126 $this->base = "http://api.clickatell.com/http";
127 $this->base_s = "https://api.clickatell.com/http";
128 } else {
129 $this->base = "http://api.clickatell.com/http";
130 $this->base_s = $this->base;
133 $this->_auth();
137 * Authenticate SMS gateway
138 * @return mixed "OK" or script die
139 * @access private
141 function _auth()
143 $comm = sprintf("%s/auth?api_id=%s&user=%s&password=%s", $this->base_s, $this->api_id, $this->user, $this->password);
144 $this->session = $this->_parse_auth($this->_execgw($comm));
148 * Query SMS credis balance
149 * @return integer number of SMS credits
150 * @access public
152 function getbalance()
154 $comm = sprintf("%s/getbalance?session_id=%s", $this->base, $this->session);
155 return $this->_parse_getbalance($this->_execgw($comm));
159 * Send SMS message
160 * @param to mixed The destination address.
161 * @param from mixed The source/sender address
162 * @param text mixed The text content of the message
163 * @return mixed "OK" or script die
164 * @access public
166 function send($to = null, $from = null, $text = null)
169 /* Check SMS credits balance */
170 if ($this->getbalance() < $this->balace_limit) {
171 die("You have reach the SMS credit limit!");
174 /* Check SMS $text length */
175 if ($this->unicode == true) {
176 $this->_chk_mbstring();
177 if (mb_strlen($text) > 210) {
178 die("Your unicode message is too long! (Current lenght=".mb_strlen($text).")");
181 /* Does message need to be concatenate */
182 if (mb_strlen($text) > 70) {
183 $concat = "&concat=3";
184 } else {
185 $concat = "";
187 } else {
188 if (strlen($text) > 459) {
189 die("Your message is too long! (Current lenght=".strlen($text).")");
192 /* Does message need to be concatenate */
193 if (strlen($text) > 160) {
194 $concat = "&concat=3";
195 } else {
196 $concat = "";
200 /* Check $to and $from is not empty */
201 if (empty($to)) {
202 die("You not specify destination address (TO)!");
205 if (empty($from)) {
206 die("You not specify source address (FROM)!");
209 /* Reformat $to number */
210 $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
211 $to = str_replace($cleanup_chr, "", $to);
213 /* Send SMS now */
214 $comm = sprintf(
215 "%s/sendmsg?session_id=%s&to=%s&from=%s&text=%s&callback=%s&unicode=%s%s",
216 $this->base,
217 $this->session,
218 rawurlencode($to),
219 rawurlencode($from),
220 $this->encode_message($text),
221 $this->callback,
222 $this->unicode,
223 $concat
225 return $this->_parse_send($this->_execgw($comm));
229 * Encode message text according to required standard
230 * @param text mixed Input text of message.
231 * @return mixed Return encoded text of message.
232 * @access public
234 function encode_message($text)
236 if ($this->unicode != true) {
237 //standard encoding
238 return rawurlencode($text);
239 } else {
240 //unicode encoding
241 $uni_text_len = mb_strlen($text, "UTF-8");
242 $out_text = "";
244 //encode each character in text
245 for ($i=0; $i<$uni_text_len; $i++) {
246 $out_text .= $this->uniord(mb_substr($text, $i, 1, "UTF-8"));
249 return $out_text;
254 * Unicode function replacement for ord()
255 * @param c mixed Unicode character.
256 * @return mixed Return HEX value (with leading zero) of unicode character.
257 * @access public
259 function uniord($c)
261 $ud = 0;
262 if (ord($c{0})>=0 && ord($c{0})<=127) {
263 $ud = ord($c{0});
266 if (ord($c{0})>=192 && ord($c{0})<=223) {
267 $ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
270 if (ord($c{0})>=224 && ord($c{0})<=239) {
271 $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
274 if (ord($c{0})>=240 && ord($c{0})<=247) {
275 $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
278 if (ord($c{0})>=248 && ord($c{0})<=251) {
279 $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);
282 if (ord($c{0})>=252 && ord($c{0})<=253) {
283 $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);
286 if (ord($c{0})>=254 && ord($c{0})<=255) { //error
287 $ud = false;
290 return sprintf("%04x", $ud);
294 * Spend voucher with sms credits
295 * @param token mixed The 16 character voucher number.
296 * @return mixed Status code
297 * @access public
299 function token_pay($token)
301 $comm = sprintf(
302 "%s/http/token_pay?session_id=%s&token=%s",
303 $this->base,
304 $this->session,
305 $token
308 return $this->_execgw($comm);
312 * Execute gateway commands
313 * @access private
315 function _execgw($command)
317 if ($this->sending_method == "curl") {
318 return $this->_curl($command);
321 if ($this->sending_method == "fopen") {
322 return $this->_fopen($command);
325 die("Unsupported sending method!");
329 * CURL sending method
330 * @access private
332 function _curl($command)
334 $this->_chk_curl();
335 $ch = curl_init($command);
336 curl_setopt($ch, CURLOPT_HEADER, 0);
337 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
338 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
339 if ($this->curl_use_proxy) {
340 curl_setopt($ch, CURLOPT_PROXY, $this->curl_proxy);
341 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->curl_proxyuserpwd);
344 $result=curl_exec($ch);
345 curl_close($ch);
346 return $result;
350 * fopen sending method
351 * @access private
353 function _fopen($command)
355 $result = '';
356 $handler = @fopen($command, 'r');
357 if ($handler) {
358 while ($line = @fgets($handler, 1024)) {
359 $result .= $line;
362 fclose($handler);
363 return $result;
364 } else {
365 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.");
370 * Parse authentication command response text
371 * @access private
373 function _parse_auth($result)
375 $session = substr($result, 4);
376 $code = substr($result, 0, 2);
377 if ($code!="OK") {
378 die("Error in SMS authorization! ($result)");
381 return $session;
385 * Parse send command response text
386 * @access private
388 function _parse_send($result)
390 $code = substr($result, 0, 2);
391 if ($code!="ID") {
392 die("Error sending SMS! ($result)");
393 } else {
394 $code = "OK";
397 return $code;
401 * Parse getbalance command response text
402 * @access private
404 function _parse_getbalance($result)
406 $result = substr($result, 8);
407 return (int)$result;
411 * Check for CURL PHP module
412 * @access private
414 function _chk_curl()
416 if (!extension_loaded('curl')) {
417 die("This SMS API class can not work without CURL PHP module! Try using fopen sending method.");
422 * Check for Multibyte String Functions PHP module - mbstring
423 * @access private
425 function _chk_mbstring()
427 if (!extension_loaded('mbstring')) {
428 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.");