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

Commit

Permalink
Merge pull request #3 from beranek1/ip-anonymization
Browse files Browse the repository at this point in the history
Update ip anonymization
  • Loading branch information
beranek1 committed Jul 29, 2019
2 parents 9450c85 + 936ee8f commit c28b1e7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
94 changes: 45 additions & 49 deletions webanalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,10 @@ class web_analytics {
private $h = null;
private $d = null;
private $profile_id = null;
private $isp_id = null;
private $ua = null;
private $c = null;
private $u_country_code = null;
private $u_ip = null;
private $u_host = null;
private $u_language = null;
private $ubid = null;

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

// Get user language and country from hostname and http header
function get_country_code() {
function get_country_code($host) {
if(isset($this->s["HTTP_CF_IPCOUNTRY"])) {
return $this->s["HTTP_CF_IPCOUNTRY"];
}
return $this->get_country_by_host($this->u_host, $this->topleveltocountry);
return $this->get_country_by_host($host, $this->topleveltocountry);
}

// Anonymize ip address
function anonymize_ip() {
$prefix = "ipv4";
if(filter_var($this->u_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$prefix = "ipv6";
function anonymize_ip($ip) {
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$ipparts = explode(":", $ip);
if(count($ipparts) == 8) {
$ip = $ipparts[0].":".$ipparts[1].":".$ipparts[2]."::";
} else {
if($ipparts[2] == "") {
$ip = $ipparts[0].":".$ipparts[1]."::";
} else if($ipparts[1] == "") {
$ip = $ipparts[0]."::";
} else {
$ip = $ipparts[0].":".$ipparts[1].":".$ipparts[2]."::";
}
}
} else if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$ipparts = explode(".", $ip);
if(count($ipparts) == 4) {
$ip = $ipparts[0].".".$ipparts[1].".".$ipparts[2].".0";
}
}
$this->u_ip = $prefix.".".md5($this->u_ip);
return $ip;
}

function check_database() {
$this->db_manager->create_table("wa_isps", array(
"id" => "VARCHAR(10) PRIMARY KEY",
"domain" => "VARCHAR(127) NOT NULL",
"name" => "TEXT",
"country" => "VARCHAR(2)",
"last_update" => "TIMESTAMP NULL"
));
$this->db_manager->create_table("wa_ips", array(
"ip" => "VARCHAR(45) PRIMARY KEY",
"host" => "VARCHAR(253)",
"country" => "VARCHAR(2)",
"isp_id" => "VARCHAR(10)",
"isp" => "VARCHAR(127)",
"last_update" => "TIMESTAMP NULL"
));
$this->db_manager->create_table("wa_profiles", array(
Expand Down Expand Up @@ -264,38 +270,37 @@ function check_database() {
}

// Get ISP's unique id
function get_isp() {
if(isset($this->u_host) && filter_var($this->u_host, FILTER_VALIDATE_IP) == false) {
$domain_parts = explode(".", $this->u_host);
function get_isp($host) {
if(isset($host) && filter_var($host, FILTER_VALIDATE_IP) == false) {
$domain_parts = explode(".", $host);
if(count($domain_parts) >= 2) {
$domain = $domainparts[count($domainparts) - 2] . "." . $domainparts[count($domainparts) - 1];
$row = $this->db_manager->first("wa_isps", "id", array("domain" => $domain));
if($row != null) {
return $row["id"];
}
$id = $this->db_manager->generate_id();
$this->db_manager->add("wa_isps", array(
"id" => $id,
"domain" => $domain,
"country" => $this->u_country_code
));
return $id;
return $domainparts[count($domainparts) - 2] . "." . $domainparts[count($domainparts) - 1];
}
}
return null;
}

// Get network's unique id
function save_ip() {
if(!isset($this->u_ip)) {
return;
function save_ip($ip, $anonymize = FALSE) {
if(!isset($ip)) {
return null;
}
if (filter_var($ip, FILTER_VALIDATE_IP)) {
$host = gethostbyaddr($ip);
}
$isp = $this->get_isp($host);
$this->u_country_code = $this->get_country_code($host);
if($anonymize) {
$ip = $this->anonymize_ip($ip);
$host = null;
}
$this->db_manager->add("wa_ips", array(
"ip" => $this->u_ip,
"host" => $this->u_host,
"ip" => $ip,
"host" => $host,
"country" => $this->u_country_code,
"isp_id" => $this->isp_id
"isp" => $isp
));
return $ip;
}

// Use cookies set by tracking script to get device's unique profile id
Expand Down Expand Up @@ -419,19 +424,12 @@ function save_request() {
// Construct: web_analytics({db_manager}, $_SERVER, $_COOKIE)
// If you don't want to anonymize ip adresses: web_analytics({db_manager}, $_SERVER, $_COOKIE, FALSE)
// Please remember to write a privacy policy especially if you don't anonymize ip adresses and live in the EU.
function __construct($db_manager, $server, $cookies, $anonymousips = TRUE) {
function __construct($db_manager, $server, $cookies, $anonymizeips = TRUE) {
if($db_manager->connected) {
$this->db_manager = $db_manager;
$this->s = $server;
$this->ua = isset($this->s['HTTP_USER_AGENT']) ? strtolower($this->s['HTTP_USER_AGENT']) : null;
$this->c = $cookies;
$this->u_ip = isset($this->s['REMOTE_ADDR']) ? $this->s['REMOTE_ADDR'] : null;
if (filter_var($this->u_ip, FILTER_VALIDATE_IP)) {
$this->u_host = gethostbyaddr($this->u_ip);
}
if($anonymousips && isset($this->s['REMOTE_ADDR'])) {
$this->anonymize_ip();
}
if(isset($this->s["HTTP_HOST"])) {
$this->h = $this->s["HTTP_HOST"];
$domain = strtolower($this->h);
Expand All @@ -441,10 +439,8 @@ function __construct($db_manager, $server, $cookies, $anonymousips = TRUE) {
} else { $this->d = $domain; }
}
$this->u_language = isset($this->s["HTTP_ACCEPT_LANGUAGE"]) ? substr($this->s['HTTP_ACCEPT_LANGUAGE'], 0, 2) : null;
$this->u_country_code = $this->get_country_code();
$this->check_database();
$this->isp_id = $this->get_isp();
$this->save_ip();
$this->u_ip = $this->save_ip($this->s['REMOTE_ADDR'], $anonymizeips);
$this->profile_id = $this->get_profile();
$this->ubid = $this->indentify_browser();
$this->save_request();
Expand Down
13 changes: 9 additions & 4 deletions webstatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
}
$total_visitors = $web_analytics_db->count("wa_browsers");
$total_networks = $web_analytics_db->count("wa_ips");
$total_isps = $web_analytics_db->count("wa_isps");
$top_countries = array();
$top_continents = array();
$total_continents = 0;
Expand Down Expand Up @@ -63,7 +62,7 @@
$top_languages = array();
$total_languages = 0;
foreach($tplngsr = $web_analytics_db->query("SELECT `language`, COUNT(*) FROM wa_browsers GROUP BY `language` ORDER BY COUNT(*) DESC;") as $language) {
if($language != "" && $language != null) {
if($language[0] != "" && $language[0] != null) {
$top_languages[$language[0]] = $language[1];
$total_languages = $total_languages + 1;
} else {
Expand All @@ -74,9 +73,15 @@
foreach($web_analytics_db->query("SELECT `user_agent`, COUNT(*) FROM wa_browsers GROUP BY `user_agent` ORDER BY COUNT(*) DESC;") as $useragent) {
$top_useragents[$useragent[0]] = $useragent[1];
}
$total_isps = 0;
$top_isps = array();
foreach($web_analytics_db->query("SELECT `isp_id`, COUNT(*) FROM wa_ips GROUP BY `isp_id` ORDER BY COUNT(*) DESC;") as $isp) {
$top_isps[$isp[0]] = $isp[1];
foreach($web_analytics_db->query("SELECT `isp`, COUNT(*) FROM wa_ips GROUP BY `isp` ORDER BY COUNT(*) DESC;") as $isp) {
if($isp[0] != "" && $isp[0] != null) {
$top_isps[$isp[0]] = $isp[1];
$total_isps++;
} else {
$top_isps["?"] = $isp[1];
}
}
$top_uris = array();
foreach($web_analytics_db->query("SELECT `uri`, COUNT(*) FROM wa_requests GROUP BY `uri` ORDER BY COUNT(*) DESC;") as $uri) {
Expand Down

0 comments on commit c28b1e7

Please sign in to comment.