SAND-framework/application/class/Browser.php

232 lines
7.7 KiB
PHP

<?php
namespace MVC\Classe;
class Browser
{
public $user;
public $userAgent;
public function __construct()
{
$this->userAgent = $_SERVER['HTTP_USER_AGENT'];
$this->user = $this->get_browser_name();
//Logger::addLog('http.browser',$this->user);
}
public static function get()
{
// Make case insensitive.
$t = strtolower($_SERVER['HTTP_USER_AGENT']);
// If the string *starts* with the string, strpos returns 0 (i.e., FALSE). Do a ghetto hack and start with a space.
// "[strpos()] may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE."
// http://php.net/manual/en/function.strpos.php
$t = " " . $t;
// Humans / Regular Users
if (strpos($t, 'opera') || strpos($t, 'opr/')) {
return 'Opera';
} elseif (strpos($t, 'edge')) {
return 'Edge';
} elseif (strpos($t, 'chrome')) {
return 'Chrome';
} elseif (strpos($t, 'safari')) {
return 'Safari';
} elseif (strpos($t, 'firefox')) {
return 'Firefox';
} elseif (strpos($t, 'msie') || strpos($t, 'trident/7')) {
return 'Internet Explorer';
}
}
public static function get_firefox_version() {
// Make case insensitive.
$t = strtolower($_SERVER['HTTP_USER_AGENT']);
// If the string *starts* with the string, strpos returns 0 (i.e., FALSE). Do a ghetto hack and start with a space.
// "[strpos()] may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE."
// http://php.net/manual/en/function.strpos.php
$t = " " . $t;
// Firefox Users
if (strpos($t, 'firefox')) {
preg_match('/rv:(.*)\)/', $_SERVER['HTTP_USER_AGENT'], $matches, PREG_OFFSET_CAPTURE);
if(isset($matches[1])) {
return intval($matches[1][0]);
}else{
return 'no-version';
}
}
return 'not-firefox';
}
public static function get_ip() {
// IP si internet partagé
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
return $_SERVER['HTTP_CLIENT_IP'];
}
// IP derrière un proxy
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
// Sinon : IP normale
else {
return (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '');
}
}
public static function get_os() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$os_platform = "Inconnu";
$os_array = array(
'/windows nt 10/i' => 'Windows 10',
'/windows nt 6.3/i' => 'Windows 8.1',
'/windows nt 6.2/i' => 'Windows 8',
'/windows nt 6.1/i' => 'Windows 7',
'/windows nt 6.0/i' => 'Windows Vista',
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
'/windows nt 5.1/i' => 'Windows XP',
'/windows xp/i' => 'Windows XP',
'/windows nt 5.0/i' => 'Windows 2000',
'/windows me/i' => 'Windows ME',
'/win98/i' => 'Windows 98',
'/win95/i' => 'Windows 95',
'/win16/i' => 'Windows 3.11',
'/macintosh|mac os x/i' => 'Mac OS X',
'/mac_powerpc/i' => 'Mac OS 9',
'/linux/i' => 'Linux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/webos/i' => 'Mobile'
);
foreach ($os_array as $regex => $value) {
if (preg_match($regex, $user_agent)) {
$os_platform = $value;
}
}
return $os_platform;
}
protected function get_browser_name()
{
// Make case insensitive.
$t = strtolower($this->userAgent);
// If the string *starts* with the string, strpos returns 0 (i.e., FALSE). Do a ghetto hack and start with a space.
// "[strpos()] may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE."
// http://php.net/manual/en/function.strpos.php
$t = " " . $t;
// Humans / Regular Users
if (strpos($t, 'opera') || strpos($t, 'opr/')) {
return 'Opera';
} elseif (strpos($t, 'edge')) {
return 'Edge';
} elseif (strpos($t, 'chrome')) {
return 'Chrome';
} elseif (strpos($t, 'safari')) {
return 'Safari';
} elseif (strpos($t, 'firefox')) {
return 'Firefox';
} elseif (strpos($t, 'msie') || strpos($t, 'trident/7')) {
return 'Internet Explorer';
}
// Application Users
elseif (strpos($t, 'curl')) {
return '[App] Curl';
}
// Search Engines
elseif (strpos($t, 'google')) {
return '[Bot] Googlebot';
} elseif (strpos($t, 'bing')) {
return '[Bot] Bingbot';
} elseif (strpos($t, 'slurp')) {
return '[Bot] Yahoo! Slurp';
} elseif (strpos($t, 'duckduckgo')) {
return '[Bot] DuckDuckBot';
} elseif (strpos($t, 'baidu')) {
return '[Bot] Baidu';
} elseif (strpos($t, 'yandex')) {
return '[Bot] Yandex';
} elseif (strpos($t, 'sogou')) {
return '[Bot] Sogou';
} elseif (strpos($t, 'exabot')) {
return '[Bot] Exabot';
} elseif (strpos($t, 'msn')) {
return '[Bot] MSN';
}
// Common Tools and Bots
elseif (strpos($t, 'mj12bot')) {
return '[Bot] Majestic';
} elseif (strpos($t, 'ahrefs')) {
return '[Bot] Ahrefs';
} elseif (strpos($t, 'semrush')) {
return '[Bot] SEMRush';
} elseif (strpos($t, 'rogerbot') || strpos($t, 'dotbot')) {
return '[Bot] Moz or OpenSiteExplorer';
} elseif (strpos($t, 'frog') || strpos($t, 'screaming')) {
return '[Bot] Screaming Frog';
}
// Miscellaneous
elseif (strpos($t, 'facebook')) {
return '[Bot] Facebook';
} elseif (strpos($t, 'pinterest')) {
return '[Bot] Pinterest';
}
// Check for strings commonly used in bot user agents
elseif (strpos($t, 'crawler') || strpos($t, 'api') ||
strpos($t, 'spider') || strpos($t, 'http') ||
strpos($t, 'bot') || strpos($t, 'archive') ||
strpos($t, 'info') || strpos($t, 'data')) {
return '[Bot] Other';
}
return 'Other (Unknown)';
}
public function isBot()
{
if (preg_match('#Bot#', $this->user)) {
return true;
} else {
return false;
}
}
// Alternative TO https://www.php.net/manual/fr/function.get-browser.php
// Function written and tested December, 2018
public function isAppRequest()
{
switch(APP_STATE) {
case "CLOSED":
case "MAINTAINED":
return false;
break;
case "OPEN":
if(\MVC\Classe\Application::is_under_update()) {
return false;
break;
}
default:
if (preg_match('#App#', $this->user)) {
return true;
} else {
return false;
}
}
}
}