Skip to content
This repository was archived by the owner on Apr 9, 2023. It is now read-only.

Commit c28b1e7

Browse files
authored
Merge pull request #3 from beranek1/ip-anonymization
Update ip anonymization
2 parents 9450c85 + 936ee8f commit c28b1e7

File tree

2 files changed

+54
-53
lines changed

2 files changed

+54
-53
lines changed

webanalytics.php

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,10 @@ class web_analytics {
159159
private $h = null;
160160
private $d = null;
161161
private $profile_id = null;
162-
private $isp_id = null;
163162
private $ua = null;
164163
private $c = null;
165164
private $u_country_code = null;
166165
private $u_ip = null;
167-
private $u_host = null;
168166
private $u_language = null;
169167
private $ubid = null;
170168

@@ -190,35 +188,43 @@ function get_country_by_host($host, $topleveltocountry = null) {
190188
}
191189

192190
// Get user language and country from hostname and http header
193-
function get_country_code() {
191+
function get_country_code($host) {
194192
if(isset($this->s["HTTP_CF_IPCOUNTRY"])) {
195193
return $this->s["HTTP_CF_IPCOUNTRY"];
196194
}
197-
return $this->get_country_by_host($this->u_host, $this->topleveltocountry);
195+
return $this->get_country_by_host($host, $this->topleveltocountry);
198196
}
199197

200198
// Anonymize ip address
201-
function anonymize_ip() {
202-
$prefix = "ipv4";
203-
if(filter_var($this->u_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
204-
$prefix = "ipv6";
199+
function anonymize_ip($ip) {
200+
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
201+
$ipparts = explode(":", $ip);
202+
if(count($ipparts) == 8) {
203+
$ip = $ipparts[0].":".$ipparts[1].":".$ipparts[2]."::";
204+
} else {
205+
if($ipparts[2] == "") {
206+
$ip = $ipparts[0].":".$ipparts[1]."::";
207+
} else if($ipparts[1] == "") {
208+
$ip = $ipparts[0]."::";
209+
} else {
210+
$ip = $ipparts[0].":".$ipparts[1].":".$ipparts[2]."::";
211+
}
212+
}
213+
} else if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
214+
$ipparts = explode(".", $ip);
215+
if(count($ipparts) == 4) {
216+
$ip = $ipparts[0].".".$ipparts[1].".".$ipparts[2].".0";
217+
}
205218
}
206-
$this->u_ip = $prefix.".".md5($this->u_ip);
219+
return $ip;
207220
}
208221

209222
function check_database() {
210-
$this->db_manager->create_table("wa_isps", array(
211-
"id" => "VARCHAR(10) PRIMARY KEY",
212-
"domain" => "VARCHAR(127) NOT NULL",
213-
"name" => "TEXT",
214-
"country" => "VARCHAR(2)",
215-
"last_update" => "TIMESTAMP NULL"
216-
));
217223
$this->db_manager->create_table("wa_ips", array(
218224
"ip" => "VARCHAR(45) PRIMARY KEY",
219225
"host" => "VARCHAR(253)",
220226
"country" => "VARCHAR(2)",
221-
"isp_id" => "VARCHAR(10)",
227+
"isp" => "VARCHAR(127)",
222228
"last_update" => "TIMESTAMP NULL"
223229
));
224230
$this->db_manager->create_table("wa_profiles", array(
@@ -264,38 +270,37 @@ function check_database() {
264270
}
265271

266272
// Get ISP's unique id
267-
function get_isp() {
268-
if(isset($this->u_host) && filter_var($this->u_host, FILTER_VALIDATE_IP) == false) {
269-
$domain_parts = explode(".", $this->u_host);
273+
function get_isp($host) {
274+
if(isset($host) && filter_var($host, FILTER_VALIDATE_IP) == false) {
275+
$domain_parts = explode(".", $host);
270276
if(count($domain_parts) >= 2) {
271-
$domain = $domainparts[count($domainparts) - 2] . "." . $domainparts[count($domainparts) - 1];
272-
$row = $this->db_manager->first("wa_isps", "id", array("domain" => $domain));
273-
if($row != null) {
274-
return $row["id"];
275-
}
276-
$id = $this->db_manager->generate_id();
277-
$this->db_manager->add("wa_isps", array(
278-
"id" => $id,
279-
"domain" => $domain,
280-
"country" => $this->u_country_code
281-
));
282-
return $id;
277+
return $domainparts[count($domainparts) - 2] . "." . $domainparts[count($domainparts) - 1];
283278
}
284279
}
285280
return null;
286281
}
287282

288283
// Get network's unique id
289-
function save_ip() {
290-
if(!isset($this->u_ip)) {
291-
return;
284+
function save_ip($ip, $anonymize = FALSE) {
285+
if(!isset($ip)) {
286+
return null;
287+
}
288+
if (filter_var($ip, FILTER_VALIDATE_IP)) {
289+
$host = gethostbyaddr($ip);
290+
}
291+
$isp = $this->get_isp($host);
292+
$this->u_country_code = $this->get_country_code($host);
293+
if($anonymize) {
294+
$ip = $this->anonymize_ip($ip);
295+
$host = null;
292296
}
293297
$this->db_manager->add("wa_ips", array(
294-
"ip" => $this->u_ip,
295-
"host" => $this->u_host,
298+
"ip" => $ip,
299+
"host" => $host,
296300
"country" => $this->u_country_code,
297-
"isp_id" => $this->isp_id
301+
"isp" => $isp
298302
));
303+
return $ip;
299304
}
300305

301306
// Use cookies set by tracking script to get device's unique profile id
@@ -419,19 +424,12 @@ function save_request() {
419424
// Construct: web_analytics({db_manager}, $_SERVER, $_COOKIE)
420425
// If you don't want to anonymize ip adresses: web_analytics({db_manager}, $_SERVER, $_COOKIE, FALSE)
421426
// Please remember to write a privacy policy especially if you don't anonymize ip adresses and live in the EU.
422-
function __construct($db_manager, $server, $cookies, $anonymousips = TRUE) {
427+
function __construct($db_manager, $server, $cookies, $anonymizeips = TRUE) {
423428
if($db_manager->connected) {
424429
$this->db_manager = $db_manager;
425430
$this->s = $server;
426431
$this->ua = isset($this->s['HTTP_USER_AGENT']) ? strtolower($this->s['HTTP_USER_AGENT']) : null;
427432
$this->c = $cookies;
428-
$this->u_ip = isset($this->s['REMOTE_ADDR']) ? $this->s['REMOTE_ADDR'] : null;
429-
if (filter_var($this->u_ip, FILTER_VALIDATE_IP)) {
430-
$this->u_host = gethostbyaddr($this->u_ip);
431-
}
432-
if($anonymousips && isset($this->s['REMOTE_ADDR'])) {
433-
$this->anonymize_ip();
434-
}
435433
if(isset($this->s["HTTP_HOST"])) {
436434
$this->h = $this->s["HTTP_HOST"];
437435
$domain = strtolower($this->h);
@@ -441,10 +439,8 @@ function __construct($db_manager, $server, $cookies, $anonymousips = TRUE) {
441439
} else { $this->d = $domain; }
442440
}
443441
$this->u_language = isset($this->s["HTTP_ACCEPT_LANGUAGE"]) ? substr($this->s['HTTP_ACCEPT_LANGUAGE'], 0, 2) : null;
444-
$this->u_country_code = $this->get_country_code();
445442
$this->check_database();
446-
$this->isp_id = $this->get_isp();
447-
$this->save_ip();
443+
$this->u_ip = $this->save_ip($this->s['REMOTE_ADDR'], $anonymizeips);
448444
$this->profile_id = $this->get_profile();
449445
$this->ubid = $this->indentify_browser();
450446
$this->save_request();

webstatistics.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
}
3333
$total_visitors = $web_analytics_db->count("wa_browsers");
3434
$total_networks = $web_analytics_db->count("wa_ips");
35-
$total_isps = $web_analytics_db->count("wa_isps");
3635
$top_countries = array();
3736
$top_continents = array();
3837
$total_continents = 0;
@@ -63,7 +62,7 @@
6362
$top_languages = array();
6463
$total_languages = 0;
6564
foreach($tplngsr = $web_analytics_db->query("SELECT `language`, COUNT(*) FROM wa_browsers GROUP BY `language` ORDER BY COUNT(*) DESC;") as $language) {
66-
if($language != "" && $language != null) {
65+
if($language[0] != "" && $language[0] != null) {
6766
$top_languages[$language[0]] = $language[1];
6867
$total_languages = $total_languages + 1;
6968
} else {
@@ -74,9 +73,15 @@
7473
foreach($web_analytics_db->query("SELECT `user_agent`, COUNT(*) FROM wa_browsers GROUP BY `user_agent` ORDER BY COUNT(*) DESC;") as $useragent) {
7574
$top_useragents[$useragent[0]] = $useragent[1];
7675
}
76+
$total_isps = 0;
7777
$top_isps = array();
78-
foreach($web_analytics_db->query("SELECT `isp_id`, COUNT(*) FROM wa_ips GROUP BY `isp_id` ORDER BY COUNT(*) DESC;") as $isp) {
79-
$top_isps[$isp[0]] = $isp[1];
78+
foreach($web_analytics_db->query("SELECT `isp`, COUNT(*) FROM wa_ips GROUP BY `isp` ORDER BY COUNT(*) DESC;") as $isp) {
79+
if($isp[0] != "" && $isp[0] != null) {
80+
$top_isps[$isp[0]] = $isp[1];
81+
$total_isps++;
82+
} else {
83+
$top_isps["?"] = $isp[1];
84+
}
8085
}
8186
$top_uris = array();
8287
foreach($web_analytics_db->query("SELECT `uri`, COUNT(*) FROM wa_requests GROUP BY `uri` ORDER BY COUNT(*) DESC;") as $uri) {

0 commit comments

Comments
 (0)