PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Clickatell.php

https://github.com/surflightroy/Ushahidi_Web
PHP | 389 lines | 179 code | 39 blank | 171 comment | 45 complexity | a58a255efff1317f4b394f59c65a61ad 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. * Main SMS-API class
  22. *
  23. * Example:
  24. * <code>
  25. * <?php
  26. * require_once ("sms_api.php");
  27. * $mysms = new sms();
  28. * echo $mysms->session;
  29. * echo $mysms->getbalance();
  30. * // $mysms->token_pay("1234567890123456"); //spend voucher with SMS credits
  31. * $mysms->send ("38160123", "netsector", "TEST MESSAGE");
  32. * ?>
  33. * </code>
  34. * @package sms_api
  35. */
  36. class Clickatell_Core {
  37. /**
  38. * Clickatell API-ID
  39. * @link http://sourceforge.net/forum/forum.php?thread_id=1005106&forum_id=344522 How to get CLICKATELL API ID?
  40. * @var integer
  41. */
  42. var $api_id = "YOUR_CLICKATELL_API_NUMBER";
  43. /**
  44. * Clickatell username
  45. * @var mixed
  46. */
  47. var $user = "YOUR_CLICKATELL_USERNAME";
  48. /**
  49. * Clickatell password
  50. * @var mixed
  51. */
  52. var $password = "YOUR_CLICKATELL_PASSWORD";
  53. /**
  54. * Use SSL (HTTPS) protocol
  55. * @var bool
  56. */
  57. var $use_ssl = false;
  58. /**
  59. * Define SMS balance limit below class will not work
  60. * @var integer
  61. */
  62. var $balace_limit = 0;
  63. /**
  64. * Gateway command sending method (curl,fopen)
  65. * @var mixed
  66. */
  67. var $sending_method = "fopen";
  68. /**
  69. * Does to use facility for delivering Unicode messages
  70. * @var bool
  71. */
  72. var $unicode = false;
  73. /**
  74. * Optional CURL Proxy
  75. * @var bool
  76. */
  77. var $curl_use_proxy = false;
  78. /**
  79. * Proxy URL and PORT
  80. * @var mixed
  81. */
  82. var $curl_proxy = "http://127.0.0.1:8080";
  83. /**
  84. * Proxy username and password
  85. * @var mixed
  86. */
  87. var $curl_proxyuserpwd = "login:secretpass";
  88. /**
  89. * Callback
  90. * 0 - Off
  91. * 1 - Returns only intermediate statuses
  92. * 2 - Returns only final statuses
  93. * 3 - Returns both intermediate and final statuses
  94. * @var integer
  95. */
  96. var $callback = 0;
  97. /**
  98. * Session variable
  99. * @var mixed
  100. */
  101. var $session;
  102. /**
  103. * Class constructor
  104. * Create SMS object and authenticate SMS gateway
  105. * @return object New SMS object.
  106. * @access public
  107. */
  108. function sms () {
  109. if ($this->use_ssl) {
  110. $this->base = "http://api.clickatell.com/http";
  111. $this->base_s = "https://api.clickatell.com/http";
  112. } else {
  113. $this->base = "http://api.clickatell.com/http";
  114. $this->base_s = $this->base;
  115. }
  116. $this->_auth();
  117. }
  118. /**
  119. * Authenticate SMS gateway
  120. * @return mixed "OK" or script die
  121. * @access private
  122. */
  123. function _auth() {
  124. $comm = sprintf ("%s/auth?api_id=%s&user=%s&password=%s", $this->base_s, $this->api_id, $this->user, $this->password);
  125. $this->session = $this->_parse_auth ($this->_execgw($comm));
  126. }
  127. /**
  128. * Query SMS credis balance
  129. * @return integer number of SMS credits
  130. * @access public
  131. */
  132. function getbalance() {
  133. $comm = sprintf ("%s/getbalance?session_id=%s", $this->base, $this->session);
  134. return $this->_parse_getbalance ($this->_execgw($comm));
  135. }
  136. /**
  137. * Send SMS message
  138. * @param to mixed The destination address.
  139. * @param from mixed The source/sender address
  140. * @param text mixed The text content of the message
  141. * @return mixed "OK" or script die
  142. * @access public
  143. */
  144. function send($to=null, $from=null, $text=null) {
  145. /* Check SMS credits balance */
  146. if ($this->getbalance() < $this->balace_limit) {
  147. return "You have reach the SMS credit limit!";
  148. };
  149. /* Check SMS $text length */
  150. if ($this->unicode == true) {
  151. $this->_chk_mbstring();
  152. if (mb_strlen ($text) > 210) {
  153. return Kohana::lang('libraries.clickatell_unicode_message_too_long').mb_strlen ($text).")";
  154. }
  155. /* Does message need to be concatenate */
  156. if (mb_strlen ($text) > 70) {
  157. $concat = "&concat=3";
  158. } else {
  159. $concat = "";
  160. }
  161. } else {
  162. if (strlen ($text) > 459) {
  163. return Kohana::lang('libraries.clickatell_message_too_long').strlen ($text).")";
  164. }
  165. /* Does message need to be concatenate */
  166. if (strlen ($text) > 160) {
  167. $concat = "&concat=3";
  168. } else {
  169. $concat = "";
  170. }
  171. }
  172. /* Check $to and $from is not empty */
  173. if (empty ($to)) {
  174. return Kohana::lang('libraries.clickatell_no_destination_address');
  175. }
  176. if (empty ($from)) {
  177. return Kohana::lang('libraries.clickatell_no_sender_address');
  178. }
  179. /* Reformat $to number */
  180. $cleanup_chr = array ("+", " ", "(", ")", "\r", "\n", "\r\n");
  181. $to = str_replace($cleanup_chr, "", $to);
  182. /* Send SMS now */
  183. $comm = sprintf ("%s/sendmsg?session_id=%s&to=%s&from=%s&text=%s&callback=%s&unicode=%s%s",
  184. $this->base,
  185. $this->session,
  186. rawurlencode($to),
  187. rawurlencode($from),
  188. $this->encode_message($text),
  189. $this->callback,
  190. $this->unicode,
  191. $concat
  192. );
  193. return $this->_parse_send ($this->_execgw($comm));
  194. }
  195. /**
  196. * Encode message text according to required standard
  197. * @param text mixed Input text of message.
  198. * @return mixed Return encoded text of message.
  199. * @access public
  200. */
  201. function encode_message ($text) {
  202. if ($this->unicode != true) {
  203. //standard encoding
  204. return rawurlencode($text);
  205. } else {
  206. //unicode encoding
  207. $uni_text_len = mb_strlen ($text, "UTF-8");
  208. $out_text = "";
  209. //encode each character in text
  210. for ($i=0; $i<$uni_text_len; $i++) {
  211. $out_text .= $this->uniord(mb_substr ($text, $i, 1, "UTF-8"));
  212. }
  213. return $out_text;
  214. }
  215. }
  216. /**
  217. * Unicode function replacement for ord()
  218. * @param c mixed Unicode character.
  219. * @return mixed Return HEX value (with leading zero) of unicode character.
  220. * @access public
  221. */
  222. function uniord($c) {
  223. $ud = 0;
  224. if (ord($c{0})>=0 && ord($c{0})<=127)
  225. $ud = ord($c{0});
  226. if (ord($c{0})>=192 && ord($c{0})<=223)
  227. $ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
  228. if (ord($c{0})>=224 && ord($c{0})<=239)
  229. $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
  230. if (ord($c{0})>=240 && ord($c{0})<=247)
  231. $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
  232. if (ord($c{0})>=248 && ord($c{0})<=251)
  233. $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);
  234. if (ord($c{0})>=252 && ord($c{0})<=253)
  235. $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);
  236. if (ord($c{0})>=254 && ord($c{0})<=255) //error
  237. $ud = false;
  238. return sprintf("%04x", $ud);
  239. }
  240. /**
  241. * Spend voucher with sms credits
  242. * @param token mixed The 16 character voucher number.
  243. * @return mixed Status code
  244. * @access public
  245. */
  246. function token_pay ($token) {
  247. $comm = sprintf ("%s/http/token_pay?session_id=%s&token=%s",
  248. $this->base,
  249. $this->session,
  250. $token);
  251. return $this->_execgw($comm);
  252. }
  253. /**
  254. * Execute gateway commands
  255. * @access private
  256. */
  257. function _execgw($command) {
  258. if ($this->sending_method == "curl")
  259. return $this->_curl($command);
  260. if ($this->sending_method == "fopen")
  261. return $this->_fopen($command);
  262. return Kohana::lang('libraries.clickatell_unsupported_method');
  263. }
  264. /**
  265. * CURL sending method
  266. * @access private
  267. */
  268. function _curl($command) {
  269. $this->_chk_curl();
  270. $ch = curl_init ($command);
  271. curl_setopt ($ch, CURLOPT_HEADER, 0);
  272. curl_setopt ($ch, CURLOPT_RETURNTRANSFER,1);
  273. curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,0);
  274. if ($this->curl_use_proxy) {
  275. curl_setopt ($ch, CURLOPT_PROXY, $this->curl_proxy);
  276. curl_setopt ($ch, CURLOPT_PROXYUSERPWD, $this->curl_proxyuserpwd);
  277. }
  278. $result=curl_exec ($ch);
  279. curl_close ($ch);
  280. return $result;
  281. }
  282. /**
  283. * fopen sending method
  284. * @access private
  285. */
  286. function _fopen($command) {
  287. $result = '';
  288. $handler = @fopen ($command, 'r');
  289. if ($handler) {
  290. while ($line = @fgets($handler,1024)) {
  291. $result .= $line;
  292. }
  293. fclose ($handler);
  294. return $result;
  295. } else {
  296. return Kohana::lang('libraries.clickatell_fopen_error');
  297. }
  298. }
  299. /**
  300. * Parse authentication command response text
  301. * @access private
  302. */
  303. function _parse_auth ($result) {
  304. $session = substr($result, 4);
  305. $code = substr($result, 0, 2);
  306. if ($code!="OK") {
  307. return "Error in SMS authorization! ($result)";
  308. }
  309. return $session;
  310. }
  311. /**
  312. * Parse send command response text
  313. * @access private
  314. */
  315. function _parse_send ($result) {
  316. $code = substr($result, 0, 2);
  317. if ($code!="ID") {
  318. return "Error sending SMS! ($result)";
  319. } else {
  320. $code = "OK";
  321. }
  322. return $code;
  323. }
  324. /**
  325. * Parse getbalance command response text
  326. * @access private
  327. */
  328. function _parse_getbalance ($result) {
  329. $result = substr($result, 8);
  330. return (int)$result;
  331. }
  332. /**
  333. * Check for CURL PHP module
  334. * @access private
  335. */
  336. function _chk_curl() {
  337. if (!extension_loaded('curl')) {
  338. return "This SMS API class can not work without CURL PHP module! Try using fopen sending method.";
  339. }
  340. }
  341. /**
  342. * Check for Multibyte String Functions PHP module - mbstring
  343. * @access private
  344. */
  345. function _chk_mbstring() {
  346. if (!extension_loaded('mbstring')) {
  347. return "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.";
  348. }
  349. }
  350. }