Highway to PSR2
[openemr.git] / modules / sms_email_reminder / sms_tmb4.php
blob5d84b47d3177a3e56de6970fd31d40e5fe7531ff
1 <?php
2 ////////////////////////////////////////////////////////////////////
3 // Class: TM4B SMS Api
4 // Usage:
5 // <code>
6 // require_once("sms_tm4b.php");
7 // $sms = new sms( "user", "pass" );
8 // $sms->send("123456789","sender","message");
9 // </code>
10 //
11 // Package: sms_tm4b
12 // Created by: Avasiloaei Dorin
13 // Modified by: Larry Lart
14 ////////////////////////////////////////////////////////////////////
15 class sms
17 // init vars
18 var $username = "";
19 var $password = "";
21 function sms($strUser, $strPass)
23 $this->username = $strUser;
24 $this->password = $strPass;
27 /**
28 * Send sms method
29 * @access public
30 * @return string response
33 function send($phoneNo, $sender, $message)
35 /* Prepare the server request */
36 $request = "";
37 $request .= "username=".urlencode($this->username);
38 $request .= "&password=".urlencode($this->password);
39 $request .= "&revision=2.0";
40 $request .= "&type=broadcast";
41 $request .= "&msg=".urlencode($message);
42 $request .= "&to=".urlencode($phoneNo);
44 // larry :: default if not defined - TODO replace
45 if (!$sender) {
46 $request .= "&from=BosmanGGZ";
47 } else {
48 $request .= "&from=".urlencode($sender);
51 $request .= "&route=GD02";
53 /**
54 * Send the request to the server
55 * @TODO make sure the request was sent
58 $response = $this->_send($request);
59 // larry :: debug
60 echo "DEBUG :SMS ENGINE: sms sent with code =".$response." for req= ".$request."\n";
62 /**
63 * Return the server response
64 * @TODO parse the server response
67 return $response;
70 /**
71 * Send sms method
72 * @access private
73 * @return string response
75 function _send($request)
77 if (extension_loaded('curl')) {
78 /**
79 * cURL extension is installed
80 * call the method that sends the sms through cURL
83 $response = $this->_send_curl($request);
84 } elseif (!extension_loaded('sockets')) {
85 /**
86 * Sockets extension is installed
87 * call the method that sends the sms through sockets
90 $response = $this->_send_sock($request);
91 } else {
92 /**
93 * The required extensions are not installed
94 * call the method that sends the sms using file_get_contents
97 $response = file_get_contents("https://www.tm4b.com/client/api/http.php?".$request);
100 /* Return the server response */
101 return $response;
105 * Send SMS through cURL
106 * @access private
107 * @return string response
110 function _send_curl($request)
112 /* Initiate a cURL session */
113 $ch = curl_init();
115 /* Set cURL variables */
116 curl_setopt($ch, CURLOPT_URL, "https://www.tm4b.com/client/api/http.php");
117 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
118 curl_setopt($ch, CURLOPT_POST, 1);
119 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
120 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
122 /* Send the request through cURL */
123 $response = curl_exec($ch);
125 /* End the cURL session */
126 curl_close($ch);
128 /* Return the server response */
129 return $response;
133 * Send SMS using the sockets extension
134 * @access private
135 * @return string response
137 function _send_sock($request)
139 /* Prepare the HTTP headers */
140 $http_header = "POST /client/api/http.php HTTP/1.1\r\n";
141 $http_header .= "Host: tm4b.com\r\n";
142 $http_header .= "User-Agent: HTTP/1.1\r\n";
143 $http_header .= "Content-Type: application/x-www-form-urlencoded\r\n";
144 $http_header .= "Content-Length: ".strlen($request)."\r\n";
145 $http_header .= "Connection: close\r\n\r\n";
146 $http_header .= $request."\r\n";
148 /* Set the host that we are connecting to and the port number */
149 $host = "ssl://tm4b.com";
150 $port = 443;
152 /* Connect to the TM4B server */
153 $out = @fsockopen($host, $port, $errno, $errstr);
155 /* Make sure that the connection succeded */
156 if ($out) {
157 /* Send the request */
158 fputs($out, $http_header);
160 /* Get the response */
161 while (!feof($out)) {
162 $result[] = fgets($out);
165 /* Terminate the connection */
166 fclose($out);
169 /* Get the response from the returned string */
170 $response = $result[9];