PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_mysms/provider/3rdParty/sms_at.inc.php

https://gitlab.com/mattyhead/mysms
PHP | 409 lines | 257 code | 70 blank | 82 comment | 29 complexity | 7a29ef0ae51ae9830eec57c4b75c3b51 MD5 | raw file
  1. <?php
  2. #-----------------------------------------------------------------
  3. # sms.at Gateway XML interface class
  4. #-----------------------------------------------------------------
  5. class sms_at {
  6. var $VERSION;
  7. var $REQUIRED_PHP_VERSION;
  8. var $account;
  9. var $account_type;
  10. var $pass;
  11. var $url;
  12. var $content_type;
  13. var $path;
  14. var $port;
  15. var $scheme;
  16. var $host;
  17. var $verbose;
  18. var $last_transfer_id;
  19. var $last_code;
  20. var $last_code_description;
  21. var $last_response;
  22. var $xml_account_type;
  23. var $xml_account;
  24. var $xml_pass;
  25. function sms_at($account_type, $account, $pass, $url)
  26. {
  27. $this->VERSION = "0.1";
  28. $this->REQUIRED_PHP_VERSION = "4.0";
  29. $parsed_url = parse_url($url);
  30. $host = $parsed_url['host'];
  31. $path = $parsed_url['path'];
  32. $port = (isset($parsed_url['port']))? $parsed_url['port'] : '';
  33. $scheme = $parsed_url['scheme'];
  34. if (!$port && (in_array($scheme,array('','http'))) ) {
  35. $port = 80;
  36. }
  37. if ((strlen($host) < 4) || !$account_type || !$account || !$pass) {
  38. exit("Invalid call of sms.at gateway class. Check arguments.\n");
  39. }
  40. if (!$port) {
  41. exit("Invalid url when calling sms.at gateway class. Mising Port.\n");
  42. }
  43. $this->account = $account;
  44. $this->account_type = ucfirst(strtolower($account_type));
  45. $this->pass = $pass;
  46. $this->url = $url;
  47. $this->content_type = 'text/xml';
  48. $this->host = $host;
  49. $this->path = $path;
  50. $this->port = $port;
  51. $this->scheme = $scheme;
  52. $this->xml_account = $this->str2xml($account);
  53. $this->xml_pass = $this->str2xml($pass);
  54. $this->xml_account_type = $this->str2xml($this->account_type);
  55. }
  56. function set_verbose($value) {
  57. $this->verbose = $value;
  58. }
  59. #--------------------------------------------------------------------------------------------------
  60. # send_simple_text_sms
  61. #
  62. # Parameters:
  63. # - $recipients = Can be string: "recipient_address",
  64. # array of strings: ("recipient_address","recipient_address",..),
  65. # array of arrays: ((recipient_address,recipient_id),recipient_array,..)
  66. # or mixed array: (recipient_array, string,..)
  67. # - $message = String. Raw message text in ISO-8859-1
  68. # - $autosegment = XML string. "none" - dont split SMS into parts when text > 160 chars
  69. # "simple" - split into parts with 160 characters each
  70. # "8bitref" - split into parts using an 8bit reference number
  71. # "16bitref" - split into parts using a 16bit reference number
  72. # - $cod = Optional confirmation of delivery.
  73. # 0..no delivery notification,
  74. # 1..request delivery notification
  75. #---------------------------------------------------------------------------------------------------
  76. function send_simple_text_sms($recipients,$message,$autosegment,$cod) {
  77. $alphabet = ''; # 0..ISO-8859-1 in message (default), 1..8bit binary encoding, 2..16bit UCS2 encoding
  78. $class = ''; # 1..write sms to sim (default), 0..dont write sms to sim (Flash Message)
  79. $id = ''; # optional Id for message. Will be returned with COD report
  80. $sender_address = ''; # optional if different from account's default msisdn.
  81. $sender_type = ''; # International, National, Shortcode, Alphanumeric
  82. $segments = '';
  83. $request = $this->generate_mt_request($alphabet,$class,$id,$sender_address,$sender_type,
  84. $recipients,$segments,$this->str2hex($message),$autosegment,$cod);
  85. $response = $this->send_request($request);
  86. $this->last_response = $response;
  87. list ($resp_header,$resp_content) = preg_split('/\r\n\r\n/', $response);
  88. list ($Code,$CodeDescription,$TransferId) = $this->read_xml_response($resp_content);
  89. $this->last_transfer_id = $TransferId;
  90. $this->last_code = $Code;
  91. $this->last_code_description = $CodeDescription;
  92. return array ($Code,$CodeDescription,$TransferId);
  93. }
  94. #--------------------------------------------------------------------------------------------------
  95. # send_sms
  96. #
  97. # Parameters:
  98. # - $alphabet = ''; # 0..ISO-8859-1 in message (default), 1..8bit binary encoding, 2..16bit UCS2 encoding
  99. # - $class = ''; # 1..write sms to sim (default), 0..dont write sms to sim (Flash Message)
  100. # - $id = ''; # optional Id for message. Will be returned with COD report
  101. # - $sender_address = ''; # optional if different from account's default msisdn.
  102. # - $sender_type = ''; # International, National, Shortcode, Alphanumeric
  103. # - $recipients = Can be string: "recipient_address",
  104. # array of strings: ("recipient_address","recipient_address",..),
  105. # array of arrays: ((recipient_address,recipient_id),recipient_array,..)
  106. # or mixed array: (recipient_array, string,..)
  107. # - $segments = Can be string: "segment_content", (hex encoded! use sms_at->add_segment())
  108. # array of strings: ("segment_content","segment_content",..),
  109. # array of arrays: ((segment_content,segment_header),segment_array,..)
  110. # or mixed array: (segment_array, string,..)
  111. # - $message = String. Raw message text in ISO-8859-1
  112. # - $autosegment = XML string. "none" - dont split SMS into parts when text > 160 chars
  113. # "simple" - split into parts with 160 characters each
  114. # "8bitref" - split into parts using an 8bit reference number
  115. # "16bitref" - split into parts using a 16bit reference number
  116. # - $cod = Optional confirmation of delivery.
  117. # 0..no delivery notification,
  118. # 1..request delivery notification
  119. #---------------------------------------------------------------------------------------------------
  120. function send_sms($alphabet,$class,$id,$sender_address,$sender_type,
  121. $recipients,$segments,$message,$autosegment,$cod) {
  122. $request = $this->generate_mt_request($alphabet,$class,$id,$sender_address,$sender_type,
  123. $recipients,$segments,$this->str2hex($message),$autosegment,$cod);
  124. $response = $this->send_request($request);
  125. $this->last_response = $response;
  126. list ($resp_header,$resp_content) = preg_split('/\r\n\r\n/', $response);
  127. list ($Code,$CodeDescription,$TransferId) = $this->read_xml_response($resp_content);
  128. $this->last_transfer_id = $TransferId;
  129. $this->last_code = $Code;
  130. $this->last_code_description = $CodeDescription;
  131. return array ($Code,$CodeDescription,$TransferId);
  132. }
  133. #------------------------------------------------------
  134. # send_request
  135. # - sends HTTP POST request
  136. # - returns HTTP response
  137. #----------------------------------------------------
  138. function send_request($request) {
  139. $fp = fsockopen($this->host,$this->port);
  140. $header = "POST ".$this->path." HTTP/1.0\r\n";
  141. $header .= "Content-Type: ".$this->content_type."\r\n";
  142. $header .= "Host: ".$this->host."\r\n";
  143. $header .= "Content-Length: ".strlen($request)."\r\n\r\n";
  144. if ($this->verbose) {echo "\n--HTTP Request--\n$header$request\n--\n";}
  145. fputs($fp, "$header");
  146. fputs($fp, "$request");
  147. $response = '';
  148. while(!feof($fp)) {
  149. $response .= fgets($fp, 128);
  150. }
  151. fclose($fp);
  152. if ($this->verbose) {echo "\n--HTTP Response--\n$response\n--\n";}
  153. return $response;
  154. }
  155. #------------------------------------------------------
  156. # read_xml_response
  157. # - dirty but sufficient response XML 'parser'
  158. # returns empty values for invalid/empty response
  159. #------------------------------------------------------
  160. function read_xml_response($content) {
  161. $Code = 0;
  162. $CodeDescription = '';
  163. $TransferId = '';
  164. if (ereg ("<Response>(.*)</Response>", $content, $regs)) {
  165. $Response_content = $regs[1];
  166. if (ereg ("<Code>(.*)</Code>", $Response_content, $regs)) {
  167. $Code = $regs[1];
  168. }
  169. if (ereg ("<CodeDescription>(.*)</CodeDescription>", $Response_content, $regs)) {
  170. $CodeDescription = $regs[1];
  171. }
  172. if (ereg ("<TransferId>(.*)</TransferId>", $Response_content, $regs)) {
  173. $TransferId = $regs[1];
  174. }
  175. }
  176. return array ($Code,$CodeDescription,$TransferId);
  177. }
  178. #--------------------------------------------------
  179. # generate_mt_request
  180. #--------------------------------------------------
  181. function generate_mt_request($alphabet,$class,$message_id,$sender_address,$sender_type,$recipients,$segments,$text,$autosegment,$cod) {
  182. $request = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
  183. $request .= "<Request>\n";
  184. $request .= "<AccountLogin Type=\"".$this->account_type."\">". $this->xml_account ."</AccountLogin>\n";
  185. $request .= "<AccountPass>". $this->xml_pass ."</AccountPass>\n";
  186. $msg_attr_alphabet = ($alphabet != '') ? " Alphabet=\"$alphabet\"" : "";
  187. $msg_attr_class = ($class != '') ? " Class=\"$class\"" : "";
  188. $msg_attr_id = ($message_id != '') ? " Id=\"$message_id\"" : "";
  189. $request .= "<Message Type=\"MTSMS\"".$msg_attr_alphabet.$msg_attr_class.$msg_attr_id.">\n";
  190. if ($sender_address) {
  191. $request .= "<Sender Type=\"$sender_type\">".$sender_address."</Sender>\n";
  192. }
  193. # Recipients
  194. $request .= "<Recipients>\n";
  195. if (is_array($recipients)) {
  196. foreach ($recipients as $recipient) {
  197. if (is_array($recipient)) {
  198. $recipient_address = $recipient[0];
  199. $recipient_id = $recipient[1];
  200. $request .= "<Recipient Type=\"International\" Id=\"$recipient_id\">". $recipient_address ."</Recipient>\n";
  201. } else {
  202. $request .= "<Recipient Type=\"International\">". $recipient ."</Recipient>\n";
  203. }
  204. }
  205. } else {
  206. $request .= "<Recipient Type=\"International\">". $recipients ."</Recipient>\n";
  207. }
  208. $request .= "</Recipients>\n";
  209. if ($segments) {
  210. # Data Element
  211. $request .= "<Data>\n";
  212. if (is_array($segments)) {
  213. foreach ($segments as $segment) {
  214. if (is_array($segment)) {
  215. $segment_content = $segment[0];
  216. $segment_UDH = $segment[1];
  217. $request .= "<Segment UDH=\"$segment_UDH\">". $segment_content ."</Segment>\n";
  218. } else {
  219. $request .= "<Segment>". $segment ."</Segment>\n";
  220. }
  221. }
  222. } else {
  223. $request .= "<Segment>". $segments ."</Segment>\n";
  224. }
  225. $request .= "</Data>\n";
  226. } else {
  227. # Text Element
  228. $request .= "<Text AutoSegment=\"$autosegment\">". $text ."</Text>\n";
  229. }
  230. # COD
  231. $request .= "<Cod>".$cod."</Cod>\n";
  232. $request .= "</Message>\n";
  233. $request .= "</Request>\n";
  234. return $request;
  235. }
  236. #------------------------------------------------------------------------------------
  237. # add_recipient
  238. # - adds recipient to array of recipients.
  239. # pushes recipient into given array for multiple recipients (recipients_array_ref)
  240. #-------------------------------------------------------------------------------------
  241. function add_recipient (&$recipients_array_ref,$recipient_address,$recipient_id) {
  242. $recipient = array(0 => "$recipient_address", 1 => "$recipient_id");
  243. $recipients_array_ref[] = $recipient;
  244. return $recipients_array_ref;
  245. }
  246. #------------------------------------------------------------------------------------
  247. # add_segment
  248. # - adds segment to array of segments.
  249. # pushes segment into given array for multiple segments (segments_array_ref)
  250. #-------------------------------------------------------------------------------------
  251. function add_segment (&$segments_array_ref,$segment_content_hex,$segment_UDH_hex) {
  252. $segment = array(0 => "$segment_content_hex", 1 => "$segment_UDH_hex");
  253. $segments_array_ref[] = $segment;
  254. return $segments_array_ref;
  255. }
  256. #--------------------------------------------------
  257. # str2hex - Encodes String to Hex
  258. #--------------------------------------------------
  259. function str2hex($string) {
  260. $hex = "";
  261. for ($i = 0; $i < strlen($string); $i++) {
  262. $hex .= (strlen(dechex(ord($string[$i]))) < 2) ? "0" . dechex(ord($string[$i])) : dechex(ord($string[$i]));
  263. }
  264. return $hex;
  265. }
  266. #--------------------------------------------------
  267. # hex2str - Decodes Hex(string) to String
  268. #--------------------------------------------------
  269. function hex2str($hex) {
  270. for($i=0;$i<strlen($hex);$i+=2) {
  271. $str.=chr(hexdec(substr($hex,$i,2)));
  272. }
  273. return $str;
  274. }
  275. #--------------------------------------------------
  276. # str2xml - Escapes non xml (simple)
  277. #--------------------------------------------------
  278. function str2xml($string) {
  279. $string = preg_replace('/&/si' , '&amp;', $string);
  280. $string = preg_replace('/\'/si', '&apos;', $string);
  281. $string = preg_replace('/>/si', '&gt;', $string);
  282. $string = preg_replace('/</si', '&lt;', $string);
  283. $string = preg_replace('/\"/si', '&quot;', $string);
  284. return $string;
  285. }
  286. function used_account()
  287. {
  288. return $this->account;
  289. }
  290. function used_account_type()
  291. {
  292. return $this->account_type;
  293. }
  294. function used_url()
  295. {
  296. return $this->url;
  297. }
  298. function used_content_type()
  299. {
  300. return $this->content_type;
  301. }
  302. function used_host()
  303. {
  304. return $this->host;
  305. }
  306. function used_path()
  307. {
  308. return $this->path;
  309. }
  310. function used_port()
  311. {
  312. return $this->port;
  313. }
  314. function used_scheme()
  315. {
  316. return $this->scheme;
  317. }
  318. function VERSION()
  319. {
  320. return $this->VERSION;
  321. }
  322. function last_transfer_id()
  323. {
  324. return $this->last_transfer_id;
  325. }
  326. function last_code()
  327. {
  328. return $this->last_code;
  329. }
  330. function last_code_description()
  331. {
  332. return $this->last_code_description;
  333. }
  334. function last_response()
  335. {
  336. return $this->last_response;
  337. }
  338. } // end of class sms_at
  339. ?>