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

Commit

Permalink
Merge 09eba0c into 5f18a41
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Mettam committed Apr 1, 2017
2 parents 5f18a41 + 09eba0c commit 4ad75da
Show file tree
Hide file tree
Showing 8 changed files with 1,358 additions and 82 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
composer.lock
phpunit.xml
vendor
.idea
99 changes: 92 additions & 7 deletions README.md
Expand Up @@ -51,16 +51,13 @@ The Browser class allows you to detect a user's browser and version.
* Lynx
* Safari
* Chrome
* Navigator
* GoogleBot
* Yahoo! Slurp
* W3C Validator
* Android Navigator
* UC Browser
* BlackBerry
* IceCat
* Nokia S60 OSS Browser
* Nokia Browser
* MSN Browser
* MSN Bot
* Netscape Navigator
* Galeon
* NetPositive
Expand All @@ -69,14 +66,16 @@ The Browser class allows you to detect a user's browser and version.
* Yandex Browser
* Comodo Dragon
* Samsung Browser
* wkhtmltopdf

### Usage

```php
use Sinergi\BrowserDetector\Browser;

$browser = new Browser();
$browser = new Browser();

//You can also provide a userAgent string if you don't wish to detect the current browser
//$browser = new Browser("Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0");

if ($browser->getName() === Browser::IE && $browser->getVersion() < 11) {
echo 'Please upgrade your browser.';
Expand All @@ -97,6 +96,92 @@ if ($browser->getName() === Browser::IE && $browser->isCompatibilityMode()) {
}
```

## Scripted Agent Detection

The ScriptedAgent class allows you to detect scripted agents (bots, spiders, tools)

### Scripted Agents Detected

Spiders

* GoogleBot
* Baidu
* Bing
* MSN
* Yahoo! Slurp
* W3C Spiders
* Yandex
* Apple
* Paper.li
* Majestic12
* Livelap
* Scoop.it
* Who.is
* Proximic

Web Surveys

* Ahrefs
* MetaURI
* Netcraft
* Browsershots
* MageReport
* SocialRank.io
* Gluten Free
* Ubermetrics
* Verisign IPS-Agent

Exploits

* ShellShock

Web Preview bots

* ICQ
* Google Web
* Facebook
* Bing
* Twitter
* Skype

Tools

* wkHTMLtoPDF
* W3C Validator
* WebDAV
* TLSProbe
* Wget
* Zgrab

Generic

* Google Favicon
* Curl
* Python
* GoLang
* Perl
* Java

Ad bots

* Google
* Microsoft
* AdBeat

### Usage

```php
use Sinergi\BrowserDetector\Browser;

$browser = new Browser();

$scriptedAgent = $browser->detectScriptedAgent();
if ($scriptedAgent!==false)
{
die("Detected ".$scriptedAgent->getName()." which is a ".$scriptedAgent->getType().". Info: ".$scriptedAgent->getInfoURL());
}
```

## OS Detection

The OS class allows you to detect a user's operating system and version.
Expand Down
149 changes: 125 additions & 24 deletions src/Browser.php
Expand Up @@ -25,20 +25,15 @@ class Browser
const MOZILLA = 'Mozilla';
const AMAYA = 'Amaya';
const LYNX = 'Lynx';
const WKHTMLTOPDF = 'wkhtmltopdf';
const SAFARI = 'Safari';
const SAMSUNG_BROWSER = 'SamsungBrowser';
const CHROME = 'Chrome';
const NAVIGATOR = 'Navigator';
const GOOGLEBOT = 'GoogleBot';
const SLURP = 'Yahoo! Slurp';
const W3CVALIDATOR = 'W3C Validator';
const NAVIGATOR = 'Android Navigator';
const BLACKBERRY = 'BlackBerry';
const ICECAT = 'IceCat';
const NOKIA_S60 = 'Nokia S60 OSS Browser';
const NOKIA = 'Nokia Browser';
const MSN = 'MSN Browser';
const MSNBOT = 'MSN Bot';
const NETSCAPE_NAVIGATOR = 'Netscape Navigator';
const GALEON = 'Galeon';
const NETPOSITIVE = 'NetPositive';
Expand All @@ -47,6 +42,11 @@ class Browser
const YANDEX = 'Yandex';
const EDGE = 'Edge';
const DRAGON = 'Dragon';
const NSPLAYER = 'Windows Media Player';
const UCBROWSER = 'UC Browser';
const MICROSOFT_OFFICE = 'Microsoft Office';
const APPLE_NEWS = 'Apple News';
const DALVIK = 'Android';

const VERSION_UNKNOWN = 'unknown';

Expand All @@ -67,18 +67,23 @@ class Browser
/**
* @var bool
*/
private $isRobot = false;
private $isChromeFrame = false;

/**
* @var bool
*/
private $isChromeFrame = false;
private $isWebkit = false;

/**
* @var bool
*/
private $isFacebookWebView = false;

/**
* @var bool
*/
private $isTwitterWebView = false;

/**
* @var bool
*/
Expand All @@ -101,7 +106,7 @@ public function __construct($userAgent = null)
}

/**
* Set the name of the OS.
* Set the name of the Browser.
*
* @param string $name
*
Expand Down Expand Up @@ -169,49 +174,111 @@ public function getVersion()
}

/**
* Set the Browser to be a robot.
* Detects scripted agents (robots / bots)
* Returns a resolved ScriptedAgent object if detected.
* Otherwise returns false.
*
* @param bool $isRobot
* @return ScriptedAgent|bool
*/
public function detectScriptedAgent()
{
$ua = $this->getUserAgent()->getUserAgentString();
if (stripos($ua, 'bot') !== FALSE ||
stripos($ua, 'spider') !== FALSE ||
stripos($ua, 'crawler') !== FALSE ||
stripos($ua, 'preview') !== FALSE ||
stripos($ua, 'slurp') !== FALSE ||
stripos($ua, 'facebookexternalhit') !== FALSE ||
stripos($ua, 'mediapartners') !== FALSE ||
stripos($ua, 'google-adwords') !== FALSE ||
stripos($ua, 'adxvastfetcher') !== FALSE ||
stripos($ua, 'adbeat') !== FALSE ||
stripos($ua, 'google favicon') !== FALSE ||
stripos($ua, 'webdav client') !== FALSE ||
stripos($ua, 'metauri api') !== FALSE ||
stripos($ua, 'tlsprobe') !== FALSE ||
stripos($ua, 'wpif') !== FALSE ||
stripos($ua, 'imgsizer') !== FALSE ||
stripos($ua, 'netcraft ssl server survey') !== FALSE ||
stripos($ua, 'curl/') !== FALSE ||
stripos($ua, 'go-http-client/') !== FALSE ||
stripos($ua, 'python') !== FALSE ||
stripos($ua, 'libwww') !== FALSE ||
stripos($ua, 'wget/') !== FALSE ||
stripos($ua, 'zgrab/') !== FALSE ||
stripos($ua, 'Java/') !== FALSE ||
stripos($ua, '() { :;}; /bin/bash -c') !== FALSE ||
stripos($ua, 'browsershots') !== FALSE ||
stripos($ua, 'magereport') !== FALSE ||
stripos($ua, 'ubermetrics-technologies') !== FALSE ||
stripos($ua, 'W3C') !== FALSE ||
stripos($ua, 'Validator') !== FALSE ||
stripos($ua, 'Jigsaw/') !== FALSE ||
stripos($ua, 'bing') !== FALSE ||
stripos($ua, 'msn') !== FALSE ||
stripos($ua, 'Google Web Preview') !== FALSE ||
stripos($ua, 'ips-agent') !== FALSE ||
(stripos($ua, 'Chrome/51.0.2704.103') !== FALSE && !isset($_SERVER['HTTP_UPGRADE_INSECURE_REQUESTS']) && stristr($_SERVER['HTTP_ACCEPT_LANGUAGE'], "ru-RU") !== FALSE) //ICQ Preview
)
{
$scriptedAgent = new ScriptedAgent($ua);
if ($scriptedAgent->getName()==ScriptedAgent::UNKNOWN)
{
return false;
}
else
{
return $scriptedAgent;
}
}
else
{
return false;
}
}

/**
* @param bool $isChromeFrame
*
* @return $this
*/
public function setIsRobot($isRobot)
public function setIsChromeFrame($isChromeFrame)
{
$this->isRobot = (bool)$isRobot;
$this->isChromeFrame = (bool)$isChromeFrame;

return $this;
}

/**
* Is the browser from a robot (ex Slurp,GoogleBot)?
* Used to determine if the browser is actually "chromeframe".
*
* @return bool
*/
public function getIsRobot()
public function getIsChromeFrame()
{
if (!isset($this->name)) {
BrowserDetector::detect($this, $this->getUserAgent());
}

return $this->isRobot;
return $this->isChromeFrame;
}

/**
* @return bool
*/
public function isRobot()
public function isChromeFrame()
{
return $this->getIsRobot();
return $this->getIsChromeFrame();
}

/**
* @param bool $isChromeFrame
*
* @return $this
*/
public function setIsChromeFrame($isChromeFrame)
public function setIsWebkit($isWebkit)
{
$this->isChromeFrame = (bool)$isChromeFrame;
$this->isWebkit = (bool)$isWebkit;

return $this;
}
Expand All @@ -221,21 +288,21 @@ public function setIsChromeFrame($isChromeFrame)
*
* @return bool
*/
public function getIsChromeFrame()
public function getIsWebkit()
{
if (!isset($this->name)) {
BrowserDetector::detect($this, $this->getUserAgent());
}

return $this->isChromeFrame;
return $this->isWebkit;
}

/**
* @return bool
*/
public function isChromeFrame()
public function isWebkit()
{
return $this->getIsChromeFrame();
return $this->getIsWebkit();
}

/**
Expand Down Expand Up @@ -272,6 +339,40 @@ public function isFacebookWebView()
return $this->getIsFacebookWebView();
}

/**
* @param bool $isTwitterWebView
*
* @return $this
*/
public function setIsTwitterWebView($isTwitterWebView)
{
$this->isTwitterWebView = (bool) $isTwitterWebView;

return $this;
}

/**
* Used to determine if the browser is actually "Twitter".
*
* @return bool
*/
public function getIsTwitterWebView()
{
if (!isset($this->name)) {
BrowserDetector::detect($this, $this->getUserAgent());
}

return $this->isTwitterWebView;
}

/**
* @return bool
*/
public function isTwitterWebView()
{
return $this->getIsTwitterWebView();
}

/**
* @param UserAgent $userAgent
*
Expand Down

0 comments on commit 4ad75da

Please sign in to comment.