Skip to content

Commit

Permalink
🔧 Improved Browser detection
Browse files Browse the repository at this point in the history
  • Loading branch information
anditv21 committed Dec 10, 2023
1 parent 258bb6e commit f754996
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
31 changes: 19 additions & 12 deletions src/app/models/UsersModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,38 +204,38 @@ protected function logintoken($token)
// Check if the provided token exists in the 'login' table
$this->prepare("SELECT * FROM `login` WHERE `remembertoken` = ?");
$this->statement->execute([$token]);

if ($this->statement->rowCount() > 0) {
$row = $this->statement->fetch();
$username = $row->username;

if ($row) {
// Set a cookie named 'login_cookie' with the provided token, valid for 1 year
setcookie("login_cookie", $token, time() + 31556926, '/');

// Fetch user information from the 'users' table using the username
$this->prepare('SELECT * FROM `users` WHERE `username` = ?');
$this->statement->execute([$username]);
$newrow = $this->statement->fetch();

// If user information is found
if ($newrow) {
// Get user's Info
$ip = $this->getip();
$browser = $this->get_user_Browser();
$os = $this->get_user_os();

// Get the current date and time in the 'Europe/Vienna' timezone
date_default_timezone_set("Europe/Vienna");
$time = date("F d S, G:i");

// Update the 'login' table with the new timestamp, IP, browser, and OS
$this->prepare("UPDATE `login` SET `time` = ?, `ip` = ?, `browser` = ?, `os` = ? WHERE `remembertoken` = ?");
$this->statement->execute([$time, $ip, $browser, $os, $token]);

// Log the user's login via cookie
$this->loguser($username, "Logged in via cookie");

// Return user information if authentication succeeds
return $newrow;
} else {
Expand All @@ -248,7 +248,7 @@ protected function logintoken($token)
return false;
}
}


protected function addrememberToken($token, $username)
{
Expand Down Expand Up @@ -924,7 +924,6 @@ protected function getCurrentColor()
protected function get_user_Browser()
{
$userAgent = $_SERVER['HTTP_USER_AGENT'];

$userBrowser = '';

if (stripos($userAgent, 'Edge') !== false) {
Expand All @@ -933,6 +932,15 @@ protected function get_user_Browser()
$userBrowser = 'Brave';
} elseif (stripos($userAgent, 'Chrome') !== false) {
$userBrowser = 'Google Chrome';

// Check if the 'browser' cookie is set
if (isset($_COOKIE['browser'])) {
// Get the value from the 'browser' cookie
$userBrowser = $_COOKIE['browser'];

// Delete the 'browser' cookie
setcookie('browser', '', time() - 3600, '/');
}
} elseif (stripos($userAgent, 'Safari') !== false && stripos($userAgent, 'Chrome') === false) {
$userBrowser = 'Safari';
} elseif (stripos($userAgent, 'Firefox') !== false) {
Expand All @@ -954,7 +962,7 @@ protected function get_user_Browser()
return $userBrowser;
}



protected function get_user_os()
{
Expand Down Expand Up @@ -1006,5 +1014,4 @@ protected function get_user_os()
}
return $os_platform;
}

}
50 changes: 49 additions & 1 deletion src/assets/js/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
getBrowser();

let capsLockOn = false; // Tracks the state of the Caps Lock key

document.addEventListener('keydown', function(event) {
Expand Down Expand Up @@ -77,4 +79,50 @@ function bulkDownload(tableElement) {
anchor.href = URL.createObjectURL(blob);
anchor.download = "codes.txt";
anchor.click();
}
}

function getBrowser() {
// Detect browser
let browserName = "Unknown";

// Brave
if (!!navigator.brave && !!navigator.brave.isBrave()) {
browserName = "Brave";
}
// Opera
else if (
/Opera|OPR\//i.test(navigator.userAgent) ||
navigator.vendor.includes("Opera")
) {
browserName = "Opera";
}
// Safari
else if (!!navigator.vendor && navigator.vendor.includes("Apple")) {
browserName = "Safari";
}
// Safari
else if (!!window.safari) {
browserName = "Safari";
}
// Firefox
else if (/Firefox/i.test(navigator.userAgent)) {
browserName = "Firefox";
}
// Microsoft Edge
else if (/Edg/i.test(navigator.userAgent)) {
browserName = "Microsoft Edge";
}
// Chrome
else if (!!window.chrome) {
browserName = "Chrome";
}

// Set the cookie
const expirationDate = new Date();
expirationDate.setFullYear(expirationDate.getFullYear() + 1);

// Set the cookie with a one-year expiration date
document.cookie = `browser=${browserName}; expires=${expirationDate.toUTCString()}; path=/`;
}


3 changes: 3 additions & 0 deletions src/includes/head.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<link rel="stylesheet" href="<?= SUB_DIR ?>/bootstrap/css/bootstrap.min.css" />
<!-- Custom CSS -->
<link rel="stylesheet" href="<?= SUB_DIR ?>/assets/css/custom.css" />

<!-- Main JS functions -->
<script src="<?= SUB_DIR ?>/assets/js/main.js"></script>
</head>

</html>

0 comments on commit f754996

Please sign in to comment.