forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms_tmb4.php
176 lines (149 loc) · 4.37 KB
/
sms_tmb4.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
////////////////////////////////////////////////////////////////////
// Class: TM4B SMS Api
// Usage:
// <code>
// require_once("sms_tm4b.php");
// $sms = new sms( "user", "pass" );
// $sms->send("123456789","sender","message");
// </code>
//
// Package: sms_tm4b
// Created by: Avasiloaei Dorin
// Modified by: Larry Lart
////////////////////////////////////////////////////////////////////
class sms
{
// init vars
var $username = "";
var $password = "";
function sms( $strUser, $strPass )
{
$this->username = $strUser;
$this->password = $strPass;
}
/**
* Send sms method
* @access public
* @return string response
*/
function send($phoneNo, $sender, $message)
{
/* Prepare the server request */
$request = "";
$request .= "username=".urlencode($this->username);
$request .= "&password=".urlencode($this->password);
$request .= "&revision=2.0";
$request .= "&type=broadcast";
$request .= "&msg=".urlencode($message);
$request .= "&to=".urlencode($phoneNo);
// larry :: default if not defined - TODO replace
if( !$sender )
$request .= "&from=BosmanGGZ";
else
$request .= "&from=".urlencode($sender);
$request .= "&route=GD02";
/**
* Send the request to the server
* @TODO make sure the request was sent
*/
$response = $this->_send($request);
// larry :: debug
echo "DEBUG :SMS ENGINE: sms sent with code =".$response." for req= ".$request."\n";
/**
* Return the server response
* @TODO parse the server response
*/
return $response;
}
/**
* Send sms method
* @access private
* @return string response
*/
function _send($request)
{
if(extension_loaded('curl'))
{
/**
* cURL extension is installed
* call the method that sends the sms through cURL
*/
$response = $this->_send_curl($request);
}
elseif(!extension_loaded('sockets'))
{
/**
* Sockets extension is installed
* call the method that sends the sms through sockets
*/
$response = $this->_send_sock($request);
}
else
{
/**
* The required extensions are not installed
* call the method that sends the sms using file_get_contents
*/
$response = file_get_contents("https://www.tm4b.com/client/api/http.php?".$request);
}
/* Return the server response */
return $response;
}
/**
* Send SMS through cURL
* @access private
* @return string response
*/
function _send_curl($request)
{
/* Initiate a cURL session */
$ch = curl_init();
/* Set cURL variables */
curl_setopt($ch, CURLOPT_URL, "https://www.tm4b.com/client/api/http.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
/* Send the request through cURL */
$response = curl_exec($ch);
/* End the cURL session */
curl_close($ch);
/* Return the server response */
return $response;
}
/**
* Send SMS using the sockets extension
* @access private
* @return string response
*/
function _send_sock($request)
{
/* Prepare the HTTP headers */
$http_header = "POST /client/api/http.php HTTP/1.1\r\n";
$http_header .= "Host: tm4b.com\r\n";
$http_header .= "User-Agent: HTTP/1.1\r\n";
$http_header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$http_header .= "Content-Length: ".strlen($request)."\r\n";
$http_header .= "Connection: close\r\n\r\n";
$http_header .= $request."\r\n";
/* Set the host that we are connecting to and the port number */
$host = "ssl:https://tm4b.com";
$port = 443;
/* Connect to the TM4B server */
$out = @fsockopen($host, $port, $errno, $errstr);
/* Make sure that the connection succeded */
if($out)
{
/* Send the request */
fputs($out, $http_header);
/* Get the response */
while(!feof($out)) $result[] = fgets($out);
/* Terminate the connection */
fclose($out);
}
/* Get the response from the returned string */
$response = $result[9];
}
}
?>