-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hologram.php
181 lines (168 loc) · 7.53 KB
/
hologram.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
<?php
if (isset($_GET['key'])) {
header('Content-Type: application/json');
// Check if ID parameter is sent, else explode the key string to extract ID
if (isset($_GET['id'])) {
$key = $_GET['key'];
$id = $_GET['id'];
} else {
$exp = explode("|", $_GET['key'], 2);
$key = $exp[0];
$id = $exp[1];
}
// If cloudflare is being used, then use the Cloudflare provided Client IP
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
if (isset($_GET['timezone']))
date_default_timezone_set($_GET['timezone']);
else
date_default_timezone_set("Europe/London"); // Default Timezone
// Extract the From Number from URL, if it doesn't exist use default
if (isset($_GET['from']))
$from = $_GET['from'];
else
$from = "7367125"; // Default from number
// Extract the overflow value from URL, if it doesn't exist use default
if (isset($_GET['overflow']))
$overflow = $_GET['overflow'];
else
$overflow = "multiple"; // Default message split type
// Extract the Body message from URL, if it doesn't exist use default
if (isset($_GET['body']) || isset($_GET['title'])){
// Prefix the body message with Title value (if it's been specified)
if (isset($_GET['title'])) {
if(isset($_GET['body'])) {
if (substr($_GET['title'], -6) == "{\$nl\$}")
$body = "[" . date("H:i") . "] " . substr($_GET['title'], 0, -6) . ":{\$nl\$}" . $_GET['body'];
else
$body = "[" . date("H:i") . "] " . $_GET['title'] . ": " . $_GET['body'];
} else {
if (substr($_GET['title'], -6) == "{\$nl\$}")
$body = "[" . date("H:i") . "] " . substr($_GET['title'], 0, -6) . ":{\$nl\$}";
else
$body = "[" . date("H:i") . "] " . $_GET['title'];
}
} else {
$body = $_GET['body'];
}
// Evaluate body variables
$find = array( "{\$time\$}" , "{\$time2\$}" , "{\$pre\$}" , "{\$ip\$}" , "{\$nl\$}" , "{\$nl2\$}" , "{\$nl3\$}", "`");
$replace = array( date("H:i") , date("H:i:s") , "[" . date("H:i") . "]: " , $_SERVER['REMOTE_ADDR'], "\\n" , "\\n\\n" , "\n\n\n", "'");
$body = str_replace($find, $replace, $body);
// Remove all special characters (that are not supported in Hologram's SMS)
$body = preg_replace("/[^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!# \"%&\\'^|()*,.?+-\/\[\]{};:<=>¡¿_@\$£¥\\\u00A4èéùìòÇØøÆæßÉÅåÄÖÑܧäöñüà\\\u0394\\\u03A6\\\u0393\\\u039B\\\u03A9\\\u03A0\\\u03A8\\\u03A3\\\u0398\\\u039E\n\r]/", "", $body);
// If the message is more the 160 characters (Max SMS length)
if (strlen($body) > 160) {
// TRIM: Trim off the excess
if ($overflow == "trim") {
$body = substr($body,0,160);
} else if ($overflow == "fail") {
// FAIL: Don't send the message, show an error and exit
$responce = array("success" => false, "error" => 2, "info" => "Message exceeded 160 characters!");
echo json_encode($responce);
exit();
} else {
// Create an Array each value containing 160 characters of the message
$body_array = str_split($body, 160);
}
}
} else {
$body = "[" . date("H:i") . "] Hello world! (No message specified)"; // Default body message
}
// Skip if the sandbox parameter specified
if (!isset($_GET['sandbox'])) {
// If the body is an array (ie. It needs to be sent in multiple messages)
if ($body_array) {
foreach($body_array as $sms) {
$data = array("deviceid" => $id, "fromnumber" => $from, "body" => $sms);
$data_string = json_encode($data);
$data_string = str_replace("\\n", 'n', $data_string);
$ch = curl_init('https://dashboard.hologram.io/api/1/sms/incoming?apikey=' . $key);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
}
$times = count($body_array);
// Else send a single SMS
} else {
$data = array("deviceid" => $id, "fromnumber" => $from, "body" => $body);
$data_string = json_encode($data);
$data_string = str_replace("\\n", 'n', $data_string);
$ch = curl_init('https://dashboard.hologram.io/api/1/sms/incoming?apikey=' . $key);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$times = 1;
}
// If the footer parameter was specified then send another SMS with it's value
if (isset($_GET['footer'])) {
// Remove all special characters (that are not supported in Hologram's SMS)
$footer = preg_replace("[^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!# \"%&\\'^|()*,.?+-\/;:<=>¡¿_@$£¥\\u00A4èéùìòÇØøÆæßÉÅåÄÖÑܧäöñüà\u0394\u03A6 \u0393\u039B\u03A9\u03A0\u03A8\u03A3\u0398\u039E\\n\\r]", "", $_GET['footer']);
$data = array("deviceid" => $id, "fromnumber" => $from, "body" => $footer);
$data_string = json_encode($data);
$data_string = str_replace("\\n", 'n', $data_string);
$ch = curl_init('https://dashboard.hologram.io/api/1/sms/incoming?apikey=' . $key);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result2 = curl_exec($ch);
}
// Show example output if ?sandbox parameter specified
} else {
if ($body_array)
$times = count($body_array);
else
$times = 1;
$responce = array("success" => true, "error" => 0, "info" => "Message delivered! [" . $times . " sent]", "message" => $body, "testing" => true);
$example = json_encode($responce);
$example = str_replace("\\\\n", '\\n', $example);
echo $example;
exit();
}
// If the CURL output is empty
if (!$result) {
$responce = array("success" => false, "error" => 3, "info" => "Failed to connect to the Hologram API!");
echo json_encode($responce);
exit();
}
// Decode the CURL output to an array
$result_array = json_decode($result, true);
if ($result_array[0]["success"] == true) {
$responce = array("success" => true, "error" => 0, "info" => "Message delivered! [" . $times . " sent]", "message" => $body);
} else {
$responce = array("success" => false, "error" => 4, "info" => $result_array["error"], "message" => $body);
}
echo str_replace("\\n", 'n', json_encode($responce));
// Callback if no parameter specified
} else if (!empty($_GET)) {
header('Content-Type: application/json');
// Callback if API key or ID not specified (but a parameter was sent)
$responce = array("success" => false, "error" => 1, "info" => "No API key or device ID specified!");
echo json_encode($responce);
}
?>
<?php if (empty($_GET)): ?>
<form method="get">
<input type="text" name="key" placeholder="Hologram API Key">
<input type="number" name="id" placeholder="Device ID">
<input type="number" name="from" placeholder="From number"><br>
<input type="text" name="title" placeholder="Message title">
<input type="field" name="body" placeholder="Message body"><br><br>
<input type="submit">
</form>
<?php endif; ?>