-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.php
200 lines (142 loc) · 4.5 KB
/
ip.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
<?php
require 'config.php';
/**Getting Ip Address Information and Parsing It for Post Interaction Analysis**/
function newIploc()
{
$src = getSrc(); // get src in the url can be blank i.e. facebook google yelp
$ip = getIP(); // get actual user ip
$rslt = ipApi($ip); //request data about ip addr from ip-api.com
if ($rslt ==="problem") return "problem"; //catch if an issue occurred with the curl
$rslt = parseIpApi($rslt, $src); //parse ip-api data
if ($rslt ==="problem") return "problem"; //catch if an issue occurred with the parsing
$rslt = dbWrite($rslt); //write to db --returned result should be id for user
return $rslt; //id of db user for this event should be rslt
}
/**Get Traffic Source**/
function getSrc()
{
if (isset($_REQUEST['src'])) {
return $src = htmlspecialchars($_REQUEST['src']);
} else {
return $src = "organic";
}
}
/**Get IP ADDRESS of User**/
function getIP()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
//ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
//ip pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$ip = explode(",", $ip);
$ip = $ip[0];
return $ip;
}
/**Get Data on User from IP Address**/
function ipApi($ip)
{
return $rslt = Curl::curlUrlEnc($url = 'https://pro.ip-api.com/json/'.$ip.'?key='.Credentials::IpLoc_API_Key);
}
/**Parse Data on User from IP API **/
function parseIpApi ($rslt, $src){
$json = json_decode($rslt, true);
$stat = $json['status'];
if ($stat !== "success"){
return "problem";
}
elseif ($stat == "success") {
$query = $json['query'];
$country = $json['country'];
$countryCode = $json['countryCode'];
$region = $json['region'];
$regionName = $json['regionName'];
$city = $json['city'];
$zip = $json['zip'];
$lat = $json['lat'];
$lon = $json['lon'];
$timezone = $json['timezone'];
$isp = $json['isp'];
$org = $json['org'];
$as = $json['as'];
if (isset ($json['mobile'])) {
$mobile = intval($json['mobile']);
} else {
$mobile = 0;
}
return array(
$query,
$stat,
$country,
$countryCode,
$region,
$regionName,
$city,
$zip,
$lat,
$lon,
$timezone,
$isp,
$org,
$as,
$mobile,
$src
);
}
}
function dbWrite($rslt)
{
list($query,
$stat,
$country,
$countryCode,
$region,
$regionName,
$city,
$zip,
$lat,
$lon,
$timezone,
$isp,
$org,
$as1,
$mobile,
$src) = $rslt;
try {
$con = DB::getDB();
$stmt = $con->prepare("INSERT INTO iploc (id, created, cust_id, query, status, country, countryCode, region, regionName, city, zip, lat, lon, timezone, isp, org, asname, mobile, source) VALUES (NULL, now(), NULL, :query, :stat, :country, :countryCode, :region, :regionName, :city, :zip, :lat, :lon, :timezone, :isp, :org, :as1, :mobile, :src)");
$stmt->bindParam(':query', $query);
$stmt->bindParam(':stat', $stat);
$stmt->bindParam(':country', $country);
$stmt->bindParam(':countryCode', $countryCode);
$stmt->bindParam(':region', $region);
$stmt->bindParam(':regionName', $regionName);
$stmt->bindParam(':city', $city);
$stmt->bindParam(':zip', $zip);
$stmt->bindParam(':lat', $lat);
$stmt->bindParam(':lon', $lon);
$stmt->bindParam(':timezone', $timezone);
$stmt->bindParam(':isp', $isp);
$stmt->bindParam(':org', $org);
$stmt->bindParam(':as1', $as1);
$stmt->bindParam(':mobile', $mobile);
$stmt->bindParam(':src', $src);
$stmt->execute();
$id = $con->lastInsertId();
}
catch (PDOException $e)
{
echo "Error: " . $e->getMessage();
exit();
}
$con = null;
return $id;
}
//instantiate script
$rslt = newIploc();
//echo $rslt;
//return;