-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmsapi.php
286 lines (272 loc) · 7.53 KB
/
smsapi.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* Gets the messages(SMSs) sent by SMSsync as a POST request.
*
*/
require_once( '/includes/database_functions.php' );
function get_message()
{
$error = NULL;
// Set success to false as the default success status
$success = false;
/**
* Get the phone number that sent the SMS.
*/
if (isset($_POST['from']))
{
$from = $_POST['from'];
}
else
{
$error = 'The from variable was not set';
}
/**
* Get the SMS aka the message sent.
*/
if (isset($_POST['message']))
{
$message = $_POST['message'];
}
else
{
$error = 'The message variable was not set';
}
/**
* Get the secret key set on SMSsync side
* for matching on the server side.
*/
if (isset($_POST['secret']))
{
$secret = $_POST['secret'];
}
/**
* Get the timestamp of the SMS
*/
if(isset($_POST['sent_timestamp']))
{
$sent_timestamp = $_POST['sent_timestamp'];
}
/**
* Get the phone number of the device SMSsync is
* installed on.
*/
if (isset($_POST['sent_to']))
{
$sent_to = $_POST['sent_to'];
}
/**
* Get the unique message id
*/
if (isset($_POST['message_id']))
{
$message_id = $_POST['message_id'];
}
/**
* Get device ID
*/
if (isset($_POST['device_id']))
{
$device_id = $_POST['device_id'];
}
/**
* Now we have retrieved the data sent over by SMSsync
* via HTTP. Next thing to do is to do something with
* the data. Either echo it or write it to a file or even
* store it in a database. This is entirely up to you.
* After, return a JSON string back to SMSsync to know
* if the web service received the message successfully or not.
*
* In this demo, we are just going to save the data
* received into a text file.
*
*/
if ((strlen($from) > 0) AND (strlen($message) > 0) AND
(strlen($sent_timestamp) > 0 )
AND (strlen($message_id) > 0))
{
/* The screte key set here is 123456. Make sure you enter
* that on SMSsync.
*/
if ( ( $secret == '123456'))
{
$success = true;
} else
{
$error = "The secret value sent from the device does not match the one on the server";
}
// now let's write the info sent by SMSsync
//to a file called test.txt
insert_message($from,$message,$sent_timestamp,$message_id,$sent_to,$device_id);
// $string = "From: ".$from."\n";
// $string .= "Message: ".$message."\n";
// $string .= "Timestamp: ".$sent_timestamp."\n";
// $string .= "Messages Id:" .$message_id."\n";
// $string .= "Sent to: ".$sent_to."\n";
// $string .= "Device ID: ".$device_id."\n\n\n";
// write_message_to_file($string);
}
/**
* Comment the code below out if you want to send an instant
* reply as SMS to the user.
*
* This feature requires the "Get reply from server" checked on SMSsync.
*/
send_instant_message($from);
/**
* Now send a JSON formatted string to SMSsync to
* acknowledge that the web service received the message
*/
$response = json_encode([
"payload"=> [
"success"=>$success,
"error" => $error
]
]);
//send_response($response);
}
/**
* Writes the received responses to a file. This acts as a database.
*/
function write_message_to_file($message)
{
$myFile = "C:/Users/Robi/Desktop/test.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
@fwrite($fh, $message);
@fclose($fh);
}
/**
* Implements the task feature. Sends messages to SMSsync to be sent as
* SMS to users.
*/
function send_task()
{
/**
* Comment the code below out if you want to send an instant
* reply as SMS to the user.
*
* This feature requires the "Get reply from server" checked on SMSsync.
*/
if (isset($_GET['task']) AND $_GET['task'] === 'send')
{
$m = "Sample Task Message";
$f = "+000-000-0000";
$s = "true";
$reply[0] = [
"to" => $f,
"message" => $m,
"uuid" => "1ba368bd-c467-4374-bf28"
];
// Send JSON response back to SMSsync
$response = json_encode(
["payload"=>[
"success"=>$s,
"task"=>"send",
"secret" => "123456",
"messages"=>array_values($reply)]
]);
send_response($response);
}
}
/**
* This sends an instant response when the server receive messages(SMSs) from
* SMSsync. This requires the settings "Get Reply from Server" enabled on
* SMSsync.
*/
function send_instant_message($to)
{
$m = "Your message has been received";
$f = "+000-000-0000";
$s = true;
$reply[0] = [
"to" => $to,
"message" => $m,
"uuid" => "1ba368bd-c467-4374-bf28"
];
// Send JSON response back to SMSsync
$response = json_encode(
["payload"=>[
"success"=>$s,
"task"=>"send",
"secret" => "123456",
"messages"=>array_values($reply)]
]);
send_response($response);
}
function send_response($response)
{
// Avoid caching
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-type: application/json; charset=utf-8");
echo $response;
}
function get_sent_message_uuids()
{
$data = file_get_contents('php://input');
$queued_messages = file_get_contents('php://input');
// Writing this to a file for demo purposes.
// In production, you will have to process the JSON string
// and remove the messages from the database or where ever the
// messages are stored so the next Task run, the server won't add
// these messages.
write_message_to_file($queued_messages."\n\n");
send_message_uuids_waiting_for_a_delivery_report($queued_messages);
}
/**
* Sends message UUIDS to SMSsync for their sms delivery status report.
* When SMSsync send messages from the server as SMS to phone numbers, SMSsync
* can send back status delivery report for these messages.
*/
function send_message_uuids_waiting_for_a_delivery_report($queued_messages)
{
// Send back the received messages UUIDs back to SMSsync
$json_obj = json_decode($queued_messages);
$response = json_encode(
[
"message_uuids"=>$json_obj->queued_messages
]);
send_response($response);
}
function send_messages_uuids_for_sms_delivery_report()
{
if(isset($_GET['task']) AND $_GET['task'] == 'result'){
$response = json_encode(
[
"message_uuids" => ['1ba368bd-c467-4374-bf28']
]);
send_response($response);
}
}
/**
* Get status delivery report on sent messages
*
*/
function get_sms_delivery_report()
{
if($_GET['task'] === 'result' AND $_GET['secret']=== '123456')
{
$message_results = file_get_contents('php://input');
write_message_to_file("message ".$message_results."\n\n");
}
}
// Execute functions above
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
if(isset($_GET['task']) AND $_GET['task'] === 'result'){
get_sms_delivery_report();
}
else if( isset($_GET['task']) && $_GET['task'] === 'sent')
{
get_sent_message_uuids();
}
else
{
get_message();
}
}
else
{
send_task();
send_messages_uuids_for_sms_delivery_report();
}
?>