bug fix (#7559)
[openemr.git] / modules / sms_email_reminder / sms_tmb4.php
blob6dbccb51e2d398f9be3b34af46c45426b5fc353a
1 <?php
3 ////////////////////////////////////////////////////////////////////
4 // Class: TM4B SMS Api
5 // Usage:
6 // <code>
7 // require_once("sms_tm4b.php");
8 // $sms = new sms( "user", "pass" );
9 // $sms->send("123456789","sender","message");
10 // </code>
12 // Package: sms_tm4b
13 // Created by: Avasiloaei Dorin
14 // Modified by: Larry Lart
15 ////////////////////////////////////////////////////////////////////
16 class sms
18 // init vars
19 var $username = "";
20 var $password = "";
22 function __construct($strUser, $strPass)
24 $this->username = $strUser;
25 $this->password = $strPass;
28 /**
29 * Send sms method
30 * @access public
31 * @return string response
34 function send($phoneNo, $sender, $message)
36 /* Prepare the server request */
37 $request = "";
38 $request .= "username=" . urlencode($this->username);
39 $request .= "&password=" . urlencode($this->password);
40 $request .= "&revision=2.0";
41 $request .= "&type=broadcast";
42 $request .= "&msg=" . urlencode($message);
43 $request .= "&to=" . urlencode($phoneNo);
45 // larry :: default if not defined - TODO replace
46 if (!$sender) {
47 $request .= "&from=BosmanGGZ";
48 } else {
49 $request .= "&from=" . urlencode($sender);
52 $request .= "&route=GD02";
54 /**
55 * Send the request to the server
56 * @TODO make sure the request was sent
59 $response = $this->_send($request);
60 // larry :: debug
61 echo "DEBUG :SMS ENGINE: sms sent with code =" . text($response) . " for req= " . text($request) . "\n";
63 /**
64 * Return the server response
65 * @TODO parse the server response
68 return $response;
71 /**
72 * Send sms method
73 * @access private
74 * @return string response
76 function _send($request)
78 if (extension_loaded('curl')) {
79 /**
80 * cURL extension is installed
81 * call the method that sends the sms through cURL
84 $response = $this->_send_curl($request);
85 } elseif (!extension_loaded('sockets')) {
86 /**
87 * Sockets extension is installed
88 * call the method that sends the sms through sockets
91 $response = $this->_send_sock($request);
92 } else {
93 /**
94 * The required extensions are not installed
95 * call the method that sends the sms using file_get_contents
98 $response = file_get_contents("https://www.tm4b.com/client/api/http.php?" . $request);
101 /* Return the server response */
102 return $response;
106 * Send SMS through cURL
107 * @access private
108 * @return string response
111 function _send_curl($request)
113 /* Initiate a cURL session */
114 $ch = curl_init();
116 /* Set cURL variables */
117 curl_setopt($ch, CURLOPT_URL, "https://www.tm4b.com/client/api/http.php");
118 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
119 curl_setopt($ch, CURLOPT_POST, 1);
120 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
121 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
123 /* Send the request through cURL */
124 $response = curl_exec($ch);
126 /* End the cURL session */
127 curl_close($ch);
129 /* Return the server response */
130 return $response;
134 * Send SMS using the sockets extension
135 * @access private
136 * @return string response
138 function _send_sock($request)
140 /* Prepare the HTTP headers */
141 $http_header = "POST /client/api/http.php HTTP/1.1\r\n";
142 $http_header .= "Host: tm4b.com\r\n";
143 $http_header .= "User-Agent: HTTP/1.1\r\n";
144 $http_header .= "Content-Type: application/x-www-form-urlencoded\r\n";
145 $http_header .= "Content-Length: " . strlen($request) . "\r\n";
146 $http_header .= "Connection: close\r\n\r\n";
147 $http_header .= $request . "\r\n";
149 /* Set the host that we are connecting to and the port number */
150 $host = "ssl://tm4b.com";
151 $port = 443;
153 /* Connect to the TM4B server */
154 $out = @fsockopen($host, $port, $errno, $errstr);
156 /* Make sure that the connection succeded */
157 if ($out) {
158 /* Send the request */
159 fputs($out, $http_header);
161 /* Get the response */
162 while (!feof($out)) {
163 $result[] = fgets($out);
166 /* Terminate the connection */
167 fclose($out);
170 /* Get the response from the returned string */
171 $response = $result[9];