Skip to content
Snippets Groups Projects
Commit f3108c36 authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

fixed useragent master not working when the header is not set

parent 65d4fc98
No related branches found
No related tags found
No related merge requests found
...@@ -37,25 +37,36 @@ class UserAgentMaster ...@@ -37,25 +37,36 @@ class UserAgentMaster
$device = "mobile"; $device = "mobile";
} }
$newAgent = null;
if (!empty($_SERVER['HTTP_USER_AGENT'])) { if (!empty($_SERVER['HTTP_USER_AGENT'])) {
// Push an entry to a list in Redis // Push an entry to a list in Redis
// App\Console\Commands\SaveUseragents.php is called regulary to save the list into a sqlite database // App\Console\Commands\SaveUseragents.php is called regulary to save the list into a sqlite database
Redis::rpush('useragents', json_encode(["platform" => $agent->platform(), "browser" => $agent->browser(), "device" => $device, "useragent" => $_SERVER['HTTP_USER_AGENT']])); Redis::rpush('useragents', json_encode(["platform" => $agent->platform(), "browser" => $agent->browser(), "device" => $device, "useragent" => $_SERVER['HTTP_USER_AGENT']]));
Redis::expire('useragents', 301); Redis::expire('useragents', 301);
// Try to retrieve a random User-Agent of the same category from the sqlite database
$newAgent = \App\UserAgent::where("platform", $agent->platform())
->where("browser", $agent->browser())
->where("device", $device)
->inRandomOrder()
->limit(1)
->get();
} else {
// Probably no Useragent provided lets use a random one
// Try to retrieve a random User-Agent of the same category from the sqlite database
$newAgent = \App\UserAgent::where("platform", $agent->platform())
->where("device", $device)
->inRandomOrder()
->limit(1)
->get();
} }
// Try to retrieve a random User-Agent of the same category from the sqlite database
$newAgent = \App\UserAgent::where("platform", $agent->platform())
->where("browser", $agent->browser())
->where("device", $device)
->inRandomOrder()
->limit(1)
->get();
if (sizeof($newAgent) >= 1) { if (sizeof($newAgent) >= 1) {
// If there is an Entry in the database use it // If there is an Entry in the database use it
$newAgent = $newAgent[0]->useragent; $newAgent = $newAgent[0]->useragent;
} else { } elseif (!empty($_SERVER['HTTP_USER_AGENT'])) {
// Else anonymize the version of the current User-Agent // Else anonymize the version of the current User-Agent
$agentPieces = explode(" ", $_SERVER['HTTP_USER_AGENT']); $agentPieces = explode(" ", $_SERVER['HTTP_USER_AGENT']);
for ($i = 0; $i < count($agentPieces); $i++) { for ($i = 0; $i < count($agentPieces); $i++) {
...@@ -63,6 +74,8 @@ class UserAgentMaster ...@@ -63,6 +74,8 @@ class UserAgentMaster
$agentPieces[$i] = preg_replace("/([^\/]*)\/\w+/s", "$1/0.0", $agentPieces[$i]); $agentPieces[$i] = preg_replace("/([^\/]*)\/\w+/s", "$1/0.0", $agentPieces[$i]);
} }
$newAgent = implode(" ", $agentPieces); $newAgent = implode(" ", $agentPieces);
} else {
$newAgent = '';
} }
// Replace the User-Agent // Replace the User-Agent
$_SERVER['HTTP_USER_AGENT'] = $newAgent; $_SERVER['HTTP_USER_AGENT'] = $newAgent;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment