PageRenderTime 55ms CodeModel.GetById 20ms app.highlight 16ms RepoModel.GetById 15ms app.codeStats 0ms

/modules/sms_email_reminder/sms_clickatell.php

https://bitbucket.org/DenizYldrm/openemr
PHP | 390 lines | 181 code | 39 blank | 170 comment | 46 complexity | 32dc77228f15aac654e116128bcbd103 MD5 | raw file
  1<?php
  2/**
  3 * CLICKATELL SMS API
  4 *
  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.
  9 *
 10 * For more information about CLICKATELL service visit http://www.clickatell.com
 11 *
 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
 18 *
 19 */
 20
 21/**
 22 * Main SMS-API class
 23 *
 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
 35 */
 36
 37class sms 
 38{
 39
 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
 44    */
 45    var $api_id = "YOUR_CLICKATELL_API_NUMBER";
 46
 47    /**
 48    * Clickatell username
 49    * @var mixed
 50    */
 51    var $user = "YOUR_CLICKATELL_USERNAME";
 52
 53    /**
 54    * Clickatell password
 55    * @var mixed
 56    */
 57    var $password = "YOUR_CLICKATELL_PASSWORD";
 58
 59    /**
 60    * Use SSL (HTTPS) protocol
 61    * @var bool
 62    */
 63    var $use_ssl = false;
 64
 65    /**
 66    * Define SMS balance limit below class will not work
 67    * @var integer
 68    */
 69    var $balace_limit = 0;
 70
 71    /**
 72    * Gateway command sending method (curl,fopen)
 73    * @var mixed
 74    */
 75    var $sending_method = "fopen";
 76
 77    /**
 78    * Does to use facility for delivering Unicode messages
 79    * @var bool
 80    */
 81    var $unicode = false;
 82
 83    /**
 84    * Optional CURL Proxy
 85    * @var bool
 86    */
 87    var $curl_use_proxy = false;
 88
 89    /**
 90    * Proxy URL and PORT
 91    * @var mixed
 92    */
 93    var $curl_proxy = "http://127.0.0.1:8080";
 94
 95    /**
 96    * Proxy username and password
 97    * @var mixed
 98    */
 99    var $curl_proxyuserpwd = "login:secretpass";
100
101    /**
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
108    */
109    var $callback = 0;
110
111    /**
112    * Session variable
113    * @var mixed
114    */
115    var $session;
116
117    /**
118    * Class constructor
119    * Create SMS object and authenticate SMS gateway
120    * @return object New SMS object.
121    * @access public
122    */
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;
130        }
131        $this->_auth();
132    }
133
134    /**
135    * Authenticate SMS gateway
136    * @return mixed  "OK" or script die
137    * @access private
138    */
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));
142    }
143
144    /**
145    * Query SMS credis balance
146    * @return integer  number of SMS credits
147    * @access public
148    */
149    function getbalance() {
150    	$comm = sprintf ("%s/getbalance?session_id=%s", $this->base, $this->session);
151        return $this->_parse_getbalance ($this->_execgw($comm));
152    }
153
154    /**
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
161    */
162    function send($to=null, $from=null, $text=null) {
163
164    	/* Check SMS credits balance */
165    	if ($this->getbalance() < $this->balace_limit) {
166    	    die ("You have reach the SMS credit limit!");
167    	};
168
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).")");
174        	}
175        	/* Does message need to be concatenate */
176            if (mb_strlen ($text) > 70) {
177                $concat = "&concat=3";
178        	} else {
179                $concat = "";
180            }
181        } else {
182            if (strlen ($text) > 459) {
183    	        die ("Your message is too long! (Current lenght=".strlen ($text).")");
184    	    }
185        	/* Does message need to be concatenate */
186            if (strlen ($text) > 160) {
187                $concat = "&concat=3";
188        	} else {
189                $concat = "";
190            }
191        }
192
193    	/* Check $to and $from is not empty */
194        if (empty ($to)) {
195    	    die ("You not specify destination address (TO)!");
196    	}
197        if (empty ($from)) {
198    	    die ("You not specify source address (FROM)!");
199    	}
200
201    	/* Reformat $to number */
202        $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
203        $to = str_replace($cleanup_chr, "", $to);
204
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
215        );
216        return $this->_parse_send ($this->_execgw($comm));
217    }
218
219    /**
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
224    */
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 = "";
233
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"));
237            }
238
239            return $out_text;
240        }
241    }
242
243    /**
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
248    */
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);
266    }
267
268    /**
269    * Spend voucher with sms credits
270    * @param token mixed  The 16 character voucher number.
271    * @return mixed  Status code
272    * @access public
273    */
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);
279
280        return $this->_execgw($comm);
281    }
282
283    /**
284    * Execute gateway commands
285    * @access private
286    */
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!");
293    }
294
295    /**
296    * CURL sending method
297    * @access private
298    */
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);
308        }
309        $result=curl_exec ($ch);
310        curl_close ($ch);
311        return $result;
312    }
313
314    /**
315    * fopen sending method
316    * @access private
317    */
318    function _fopen($command) {
319        $result = '';
320        $handler = @fopen ($command, 'r');
321        if ($handler) {
322            while ($line = @fgets($handler,1024)) {
323                $result .= $line;
324            }
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.");
329        }
330    }
331
332    /**
333    * Parse authentication command response text
334    * @access private
335    */
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)");
341        }
342        return $session;
343    }
344
345    /**
346    * Parse send command response text
347    * @access private
348    */
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";
355    	}
356        return $code;
357    }
358
359    /**
360    * Parse getbalance command response text
361    * @access private
362    */
363    function _parse_getbalance ($result) {
364    	$result = substr($result, 8);
365        return (int)$result;
366    }
367
368    /**
369    * Check for CURL PHP module
370    * @access private
371    */
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.");
375        }
376    }
377
378    /**
379    * Check for Multibyte String Functions PHP module - mbstring
380    * @access private
381    */
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.");
385        }
386    }
387
388}
389
390?>