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

Commit b0f46a2

Browse files
committed
Clean up and update to database manager
1 parent 3f8741a commit b0f46a2

File tree

2 files changed

+114
-97
lines changed

2 files changed

+114
-97
lines changed

webanalytics.php

Lines changed: 91 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,29 @@ function create_table($name, $keys) {
122122
$this->query($query);
123123
}
124124

125+
function update($table, $values, $filter) {
126+
$query = "UPDATE `".$table."` SET ";
127+
$i = 1;
128+
foreach ($values as $key => $value) {
129+
$query .= "`".$key."` = '".$value."'";
130+
if($i != count($values)) {
131+
$query .= ", ";
132+
}
133+
$i++;
134+
}
135+
$query .= " WHERE ";
136+
$i = 1;
137+
foreach ($filter as $key => $value) {
138+
$query .= "`".$key."` = '".$value."'";
139+
if($i != count($values)) {
140+
$query .= " AND ";
141+
}
142+
$i++;
143+
}
144+
$query .= ";";
145+
$this->query($query);
146+
}
147+
125148
function connect() {
126149
$this->connection = new mysqli($this->host, $this->user, $this->password, $this->database);
127150
if($this->connection->connect_errno) {
@@ -390,20 +413,17 @@ function get_isp() {
390413
"country" => "VARCHAR(2)",
391414
"last_update" => "TIMESTAMP NULL"
392415
));
393-
$domain = null;
394416
if(isset($this->u_host) && filter_var($this->u_host, FILTER_VALIDATE_IP) == false) {
395417
$domain_parts = explode(".", $this->u_host);
396418
if(count($domain_parts) >= 2) {
397419
$domain = $domainparts[count($domainparts) - 2] . "." . $domainparts[count($domainparts) - 1];
398-
}
399-
}
400-
if($domain != null) {
401-
$nrow = $this->db_manager->get_one_row("SELECT id FROM isps WHERE domain = '".$domain."' LIMIT 1;");
402-
if($nrow != null) {
403-
$this->isp_id = $nrow[0];
404-
} else {
405-
$this->isp_id = $this->db_manager->generate_id();
406-
$this->db_manager->ex_gen_query("isps", array("domain" => $domain, "country" => $this->u_country_code), $this->isp_id);
420+
$nrow = $this->db_manager->get_one_row("SELECT id FROM isps WHERE domain = '".$domain."' LIMIT 1;");
421+
if($nrow != null) {
422+
$this->isp_id = $nrow[0];
423+
} else {
424+
$this->isp_id = $this->db_manager->generate_id();
425+
$this->db_manager->ex_gen_query("isps", array("domain" => $domain, "country" => $this->u_country_code), $this->isp_id);
426+
}
407427
}
408428
}
409429
}
@@ -423,7 +443,7 @@ function get_network() {
423443
if($nrow != null) {
424444
$this->unid = $nrow[0];
425445
if($nrow[1] == null || $nrow[1] == "") {
426-
$this->db_manager->query("UPDATE networks SET host = '".$this->u_host."' WHERE id = '".$this->unid."';");
446+
$this->db_manager->update("networks", array("host" => $this->u_host), array("id" => $this->unid));
427447
}
428448
} else {
429449
$this->unid = $this->db_manager->generate_id(15);
@@ -477,18 +497,12 @@ function get_profile() {
477497
if(isset($this->c["device_profile"]) && isset($this->c["browser_profile"])) {
478498
$this->u_profile = array();
479499
$device_profile = json_decode($this->c["device_profile"], true);
480-
if(isset($device_profile["screen_width"]) && isset($device_profile["screen_height"])) {
481-
$this->u_profile["screen_width"] = intval($device_profile["screen_width"]);
482-
$this->u_profile["screen_height"] = intval($device_profile["screen_height"]);
483-
}
484-
if(isset($device_profile["interface_width"]) && isset($device_profile["interface_height"])) {
485-
$this->u_profile["interface_width"] = intval($device_profile["interface_width"]);
486-
$this->u_profile["interface_height"] = intval($device_profile["interface_height"]);
487-
}
488-
if(isset($device_profile["color_depth"]) && isset($device_profile["pixel_depth"])) {
489-
$this->u_profile["color_depth"] = intval($device_profile["color_depth"]);
490-
$this->u_profile["pixel_depth"] = intval($device_profile["pixel_depth"]);
491-
}
500+
$this->u_profile["screen_width"] = isset($device_profile["screen_width"]) ? intval($device_profile["screen_width"]) : null;
501+
$this->u_profile["screen_height"] = isset($device_profile["screen_height"]) ? intval($device_profile["screen_height"]) : null;
502+
$this->u_profile["interface_width"] = isset($device_profile["interface_width"]) ? intval($device_profile["interface_width"]) : null;
503+
$this->u_profile["interface_height"] = isset($device_profile["interface_height"]) ? intval($device_profile["interface_height"]) : null;
504+
$this->u_profile["color_depth"] = isset($device_profile["color_depth"]) ? intval($device_profile["color_depth"]) : null;
505+
$this->u_profile["pixel_depth"] = isset($device_profile["pixel_depth"]) ? intval($device_profile["pixel_depth"]) : null;
492506
setcookie("device_profile", json_encode($device_profile), time()+60*60*24*90, "/", $this->d);
493507
$browser_profile = json_decode($this->c["browser_profile"], true);
494508
if(isset($browser_profile["cookies_enabled"]) && isset($browser_profile["java_enabled"])) {
@@ -543,7 +557,7 @@ function indentify_browser() {
543557
));
544558
$this->db_manager->create_table("browsers", array(
545559
"id" => "VARCHAR(15) PRIMARY KEY",
546-
"ip" => "VARCHAR(45)",
560+
"ip" => "VARCHAR(45) NOT NULL",
547561
"country" => "VARCHAR(2)",
548562
"language" => "VARCHAR(2)",
549563
"mobile" => "TINYINT(1)",
@@ -554,86 +568,76 @@ function indentify_browser() {
554568
"last_update" => "TIMESTAMP NULL"
555569
));
556570
if(isset($this->unid)) {
557-
$identified = false;
558571
if(isset($this->c["webid"])) {
559572
$cookie_cid = $this->c["webid"];
560573
if(strlen($cookie_cid) == 20) {
561574
$cidrow = $this->db_manager->get_one_row("SELECT browser_id FROM trackers WHERE id = '".$cookie_cid."' AND domain = '".$this->d."' LIMIT 1;");
562575
if($cidrow != null) {
563-
$this->db_manager->query("UPDATE trackers SET time = '".date('Y-m-d H:i:s')."' WHERE id = '".$cookie_cid."';");
564-
$row = $this->db_manager->get_one_row("SELECT ip, network_id FROM browsers WHERE id = '".$cidrow[0]."' LIMIT 1;");
576+
$this->db_manager->update("trackers", array("time" => date('Y-m-d H:i:s')), array("id" => $cookie_cid));
577+
$row = $this->db_manager->get_one_row("SELECT id FROM browsers WHERE id = '".$cidrow[0]."' LIMIT 1;");
565578
if($row != null) {
566579
setcookie("webid", $cookie_cid, time()+60*60*24*180, "/", $this->d);
567580
$this->ubid = $cidrow[0];
568-
if($this->u_ip != $row[0] && $this->u_ip != null && $this->u_ip != "") {
569-
$this->db_manager->query("UPDATE browsers SET ip = '".$this->u_ip."' WHERE id = '".$this->ubid."';");
570-
}
571-
if($row[1] != null && $row[1] != "") {
572-
if($this->unid != $row[1] && $this->unid != null && $this->unid != "") {
573-
$this->db_manager->query("UPDATE browsers SET network_id = '".$this->unid."' WHERE id = '".$this->ubid."';");
574-
}
575-
} else {
576-
$this->db_manager->query("UPDATE browsers SET network_id = '".$this->unid."' WHERE id = '".$this->ubid."';");
577-
}
578-
if($this->profile_id != null) {
579-
$this->db_manager->query("UPDATE browsers SET profile_id = '".$this->profile_id."' WHERE id = '".$this->ubid."';");
580-
}
581-
$this->db_manager->query("UPDATE browsers SET agent_id = '".$this->agent_id."', last_update = '".date('Y-m-d H:i:s')."' WHERE id = '".$this->ubid."';");
582-
$identified = true;
581+
$this->db_manager->update("browsers", array(
582+
"ip" => $this->u_ip,
583+
"network_id" => $this->unid,
584+
"profile_id" => $this->profile_id,
585+
"agent_id" => $this->agent_id,
586+
"last_update" => date('Y-m-d H:i:s')
587+
), array("id" => $this->ubid));
588+
return;
583589
}
584590
}
585591
}
586592
}
587-
if($identified == false) {
588-
$cid = $this->db_manager->generate_id(20);
589-
$result = null;
590-
if($this->u_language != null) {
591-
$result = $this->db_manager->query("SELECT id FROM browsers WHERE network_id = '".$this->unid."' AND agent_id = '".$this->agent_id."' AND language = '".$this->u_language."' AND last_update >= '".date('Y-m-d H:i:s', strtotime("-48 hours"))."';");
593+
$cid = $this->db_manager->generate_id(20);
594+
$result = null;
595+
if($this->u_language != null) {
596+
$result = $this->db_manager->query("SELECT id FROM browsers WHERE network_id = '".$this->unid."' AND agent_id = '".$this->agent_id."' AND language = '".$this->u_language."' AND last_update >= '".date('Y-m-d H:i:s', strtotime("-48 hours"))."';");
597+
} else {
598+
if($this->u_bot == 1) {
599+
$result = $this->db_manager->query("SELECT id FROM browsers WHERE network_id = '".$this->unid."' AND agent_id = '".$this->agent_id."' AND language IS NULL AND bot = 1 AND last_update >= '".date('Y-m-d H:i:s', strtotime("-90 days"))."';");
592600
} else {
593-
if($this->u_bot == 1) {
594-
$result = $this->db_manager->query("SELECT id FROM browsers WHERE network_id = '".$this->unid."' AND agent_id = '".$this->agent_id."' AND language IS NULL AND bot = 1 AND last_update >= '".date('Y-m-d H:i:s', strtotime("-90 days"))."';");
595-
} else {
596-
$result = $this->db_manager->query("SELECT id FROM browsers WHERE network_id = '".$this->unid."' AND agent_id = '".$this->agent_id."' AND language IS NULL AND last_update >= '".date('Y-m-d H:i:s', strtotime("-48 hours"))."';");
597-
}
601+
$result = $this->db_manager->query("SELECT id FROM browsers WHERE network_id = '".$this->unid."' AND agent_id = '".$this->agent_id."' AND language IS NULL AND last_update >= '".date('Y-m-d H:i:s', strtotime("-48 hours"))."';");
598602
}
599-
$data_ubid = "";
600-
$ubid_count = 0;
601-
if($result instanceof mysqli_result) {
602-
while($row = $result->fetch_row()) {
603-
$data_ubid = $row[0];
604-
$ubid_count++;
605-
}
606-
$result->close();
603+
}
604+
$data_ubid = "";
605+
$ubid_count = 0;
606+
if($result instanceof mysqli_result) {
607+
while($row = $result->fetch_row()) {
608+
$data_ubid = $row[0];
609+
$ubid_count++;
607610
}
608-
if($ubid_count == 1) {
609-
$cidrow = null;
610-
if($this->agent_id != null) {
611-
$cidrow = $this->db_manager->get_one_row("SELECT id, domain, last FROM trackers WHERE browser_id = '".$data_ubid."' AND agent_id = '".$this->agent_id."' ORDER BY time DESC LIMIT 1;");
612-
} else {
613-
$cidrow = $this->db_manager->get_one_row("SELECT id, domain, last FROM trackers WHERE browser_id = '".$data_ubid."' AND agent_id IS NULL ORDER BY time DESC LIMIT 1;");
614-
}
615-
$cidregenerate = true;
616-
if($cidrow != null) {
617-
if(strtotime($cidrow[2]) >= strtotime("-90 days") && $cidrow[1] == $this->d) {
618-
$cidregenerate = false;
619-
}
620-
}
621-
if($cidregenerate == false) {
622-
setcookie("webid", $cidrow[0], time()+60*60*24*180, "/", $this->d);
623-
$this->db_manager->query("UPDATE trackers SET time = '".date('Y-m-d H:i:s')."' WHERE id = '".$cidrow[0]."';");
624-
} else {
625-
$this->db_manager->query("DELETE FROM trackers WHERE browser_id = '".$data_ubid."' AND agent_id = '".$this->agent_id."' AND domain = '".$this->d."';");
626-
$this->db_manager->ex_gen_query("trackers", array("domain" => $this->d, "browser_id" => $data_ubid, "agent_id" => $this->agent_id), $cid);
627-
setcookie("webid", $cid, time()+60*60*24*180, "/", $this->d);
611+
$result->close();
612+
}
613+
if($ubid_count == 1) {
614+
$cidrow = null;
615+
if($this->agent_id != null) {
616+
$cidrow = $this->db_manager->get_one_row("SELECT id, domain, last FROM trackers WHERE browser_id = '".$data_ubid."' AND agent_id = '".$this->agent_id."' ORDER BY time DESC LIMIT 1;");
617+
} else {
618+
$cidrow = $this->db_manager->get_one_row("SELECT id, domain, last FROM trackers WHERE browser_id = '".$data_ubid."' AND agent_id IS NULL ORDER BY time DESC LIMIT 1;");
619+
}
620+
$cidregenerate = true;
621+
if($cidrow != null) {
622+
if(strtotime($cidrow[2]) >= strtotime("-90 days") && $cidrow[1] == $this->d) {
623+
$cidregenerate = false;
628624
}
629-
$this->ubid = $data_ubid;
630-
$this->db_manager->query("UPDATE browsers SET last_update = '".date('Y-m-d H:i:s')."' WHERE id = '".$this->ubid."';");
625+
}
626+
if($cidregenerate == false) {
627+
setcookie("webid", $cidrow[0], time()+60*60*24*180, "/", $this->d);
628+
$this->db_manager->update("trackers", array("time" => date('Y-m-d H:i:s')), array("id" => $cidrow[0]));
631629
} else {
632-
$this->ubid = $this->db_manager->generate_id(15);
633-
$this->db_manager->ex_gen_query("trackers", array("domain" => $this->d, "browser_id" => $this->ubid, "agent_id" => $this->agent_id), $cid);
630+
$this->db_manager->query("DELETE FROM trackers WHERE browser_id = '".$data_ubid."' AND agent_id = '".$this->agent_id."' AND domain = '".$this->d."';");
631+
$this->db_manager->ex_gen_query("trackers", array("domain" => $this->d, "browser_id" => $data_ubid, "agent_id" => $this->agent_id), $cid);
634632
setcookie("webid", $cid, time()+60*60*24*180, "/", $this->d);
635-
$this->db_manager->ex_gen_query("browsers", array("ip" => $this->u_ip, "country" => $this->u_country_code, "language" => $this->u_language, "mobile" => $this->u_mobile, "bot" => $this->u_bot, "agent_id" => $this->agent_id, "network_id" => $this->unid, "profile_id" => $this->profile_id), $this->ubid);
636633
}
634+
$this->ubid = $data_ubid;
635+
$this->db_manager->update("browsers", array("last_update" => date('Y-m-d H:i:s')), array("id" => $this->ubid));
636+
} else {
637+
$this->ubid = $this->db_manager->generate_id(15);
638+
$this->db_manager->ex_gen_query("trackers", array("domain" => $this->d, "browser_id" => $this->ubid, "agent_id" => $this->agent_id), $cid);
639+
setcookie("webid", $cid, time()+60*60*24*180, "/", $this->d);
640+
$this->db_manager->ex_gen_query("browsers", array("ip" => $this->u_ip, "country" => $this->u_country_code, "language" => $this->u_language, "mobile" => $this->u_mobile, "bot" => $this->u_bot, "agent_id" => $this->agent_id, "network_id" => $this->unid, "profile_id" => $this->profile_id), $this->ubid);
637641
}
638642
}
639643
}
@@ -657,18 +661,8 @@ function save_request() {
657661
$this->r_protocol = isset($this->s['REQUEST_SCHEME']) ? $this->s["REQUEST_SCHEME"] : null;
658662
$this->r_port = isset($this->s["SERVER_PORT"]) ? $this->s['SERVER_PORT'] : null;
659663
$this->r_rayid = isset($this->s["HTTP_CF_RAY"]) ? $this->s["HTTP_CF_RAY"] : null;
660-
if(isset($this->s["REQUEST_URI"])) {
661-
$uri = $this->s["REQUEST_URI"];
662-
if($uri != null && $uri != "" && $uri != "/") {
663-
$this->r_target = $uri;
664-
}
665-
}
666-
if(isset($this->s["HTTP_REFERER"])) {
667-
$origin = $this->s["HTTP_REFERER"];
668-
if($origin != null && $origin != "") {
669-
$this->r_origin = $origin;
670-
}
671-
}
664+
$this->r_target = isset($this->s["REQUEST_URI"]) ? $this->s["REQUEST_URI"] : null;
665+
$this->r_origin = isset($this->s["HTTP_REFERER"]) ? $this->s["HTTP_REFERER"] : null;
672666
$this->r_accept = isset($this->d['HTTP_ACCEPT']) ? "".explode(",", $this->s['HTTP_ACCEPT'])[0]."" : null;
673667
$this->rid = $this->db_manager->generate_id(15);
674668
$this->r_domain = strtolower($this->h);

webstatistics.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,29 @@ function create_table($name, $keys) {
323323
$this->query($query);
324324
}
325325

326+
function update($table, $values, $filter) {
327+
$query = "UPDATE `".$table."` SET ";
328+
$i = 1;
329+
foreach ($values as $key => $value) {
330+
$query .= "`".$key."` = '".$value."'";
331+
if($i != count($values)) {
332+
$query .= ", ";
333+
}
334+
$i++;
335+
}
336+
$query .= " WHERE ";
337+
$i = 1;
338+
foreach ($filter as $key => $value) {
339+
$query .= "`".$key."` = '".$value."'";
340+
if($i != count($values)) {
341+
$query .= " AND ";
342+
}
343+
$i++;
344+
}
345+
$query .= ";";
346+
$this->query($query);
347+
}
348+
326349
function connect() {
327350
$this->connection = new mysqli($this->host, $this->user, $this->password, $this->database);
328351
if($this->connection->connect_errno) {

0 commit comments

Comments
 (0)