forked from CalexCore/amity.rpc.php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submitanalytics.php
56 lines (46 loc) · 1.25 KB
/
submitanalytics.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
<?php
//*desc: Submits the callers IP address to the node map
require_once('./lib/config.php');
require_once('./lib/analytics_helper.php');
$ip = 'NA';
$ua = 'NA';
//Check to see if the CF-Connecting-IP header exists.
if(isset($_SERVER["HTTP_CF_CONNECTING_IP"])){
//If it does, assume that PHP app is behind Cloudflare.
$ip = $_SERVER["HTTP_CF_CONNECTING_IP"];
} else{
//Otherwise, use REMOTE_ADDR.
$ip = $_SERVER['REMOTE_ADDR'];
}
if ($ip == "127.0.0.1") {
http_response_code(403);
return;
}
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$ua = $_SERVER['HTTP_USER_AGENT'];
}
//block any user agent that does not start with amity-cli
//a basic check as the ua string can be spoofed, but
//means someone actually has to put in effort to spam it
if ($ua == "NA" || substr($ua, 0, 9) != "amity-cli") {
http_response_code(403);
return;
}
$params = array(
"version" => substr($ua, 10),
"address" => $ip,
"time" => strtotime('today midnight'),
);
$json = send_request(ANALYTICS_HOST, ANALYTICS_PORT, "submit", $params);
$arr = json_decode($json);
if (!isset($arr) || !isset($arr->status)) {
http_response_code(500);
return;
}
if ($arr->status == "OK") {
http_response_code(200);
}
else {
http_response_code(500);
}
?>