Skip to content

Commit 8e19d13

Browse files
committed
Fixed PHP 5.4 support and added some better robot detection
1 parent d83e26c commit 8e19d13

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/Agent.php

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Agent extends Mobile_Detect {
1010
*
1111
* @var array
1212
*/
13-
protected static $additionalOperatingSystems = [
13+
protected static $additionalOperatingSystems = array(
1414
'Windows' => 'Windows',
1515
'Windows NT' => 'Windows NT',
1616
'OS X' => 'Mac OS X',
@@ -20,30 +20,29 @@ class Agent extends Mobile_Detect {
2020
'OpenBSD' => 'OpenBSD',
2121
'Linux' => 'Linux',
2222
'ChromeOS' => 'CrOS',
23-
];
23+
);
2424

2525
/**
2626
* List of additional browsers.
2727
*
2828
* @var array
2929
*/
30-
protected static $additionalBrowsers = [
30+
protected static $additionalBrowsers = array(
3131
'Opera' => 'Opera|OPR',
3232
'Chrome' => 'Chrome',
3333
'Firefox' => 'Firefox',
3434
'Safari' => 'Safari',
3535
'IE' => 'MSIE|IEMobile|MSIEMobile|Trident/[.0-9]+',
3636
'Netscape' => 'Netscape',
3737
'Mozilla' => 'Mozilla',
38-
];
38+
);
3939

4040
/**
4141
* List of additional browsers.
4242
*
4343
* @var array
4444
*/
45-
protected static $additionalProperties = [
46-
45+
protected static $additionalProperties = array(
4746
// Operating systems
4847
'Windows' => 'Windows NT [VER]',
4948
'Windows NT' => 'Windows NT [VER]',
@@ -57,21 +56,23 @@ class Agent extends Mobile_Detect {
5756
'Netscape' => 'Netscape/[VER]',
5857
'Mozilla' => 'rv:[VER]',
5958
'IE' => ['IEMobile/[VER];', 'IEMobile [VER]', 'MSIE [VER];', 'rv:[VER]'],
60-
];
59+
);
6160

6261
/**
6362
* List of robots.
6463
*
6564
* @var array
6665
*/
67-
protected static $robots = [
68-
'Googlebot' => 'googlebot',
66+
protected static $robots = array(
67+
'Google' => 'googlebot',
6968
'MSNBot' => 'msnbot',
7069
'Baiduspider' => 'baiduspider',
7170
'Bing' => 'bingbot',
7271
'Yahoo' => 'yahoo',
7372
'Lycos' => 'lycos',
74-
];
73+
'Facebook' => 'facebookexternalhit',
74+
'Twitter' => 'Twitterbot',
75+
);
7576

7677
/**
7778
* Get all detection rules. These rules include the additional
@@ -133,7 +134,7 @@ public function languages($acceptLanguage = null)
133134
return explode(',', preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($acceptLanguage))));
134135
}
135136

136-
return [];
137+
return array();
137138
}
138139

139140
/**
@@ -151,7 +152,7 @@ protected function findDetectionRulesAgainstUA(array $rules, $userAgent = null)
151152
if (empty($regex)) continue;
152153

153154
// Check match
154-
if ($this->match($regex, $userAgent)) return $key;
155+
if ($this->match($regex, $userAgent)) return $key ?: reset($this->matchesArray);
155156
}
156157

157158
return false;
@@ -222,6 +223,24 @@ public function isDesktop($userAgent = null, $httpHeaders = null)
222223
return (! $this->isMobile() && ! $this->isTablet() && ! $this->isRobot());
223224
}
224225

226+
/**
227+
* Get the robot name.
228+
*
229+
* @param string $userAgent
230+
* @return string
231+
*/
232+
public function robot($userAgent = null)
233+
{
234+
// Get bot rules
235+
$rules = $this->mergeRules(
236+
static::$robots, // NEW
237+
array(static::$utilities['Bot']),
238+
array(static::$utilities['MobileBot'])
239+
);
240+
241+
return $this->findDetectionRulesAgainstUA($rules, $userAgent);
242+
}
243+
225244
/**
226245
* Check if device is a robot.
227246
*
@@ -232,7 +251,8 @@ public function isRobot($userAgent = null)
232251
{
233252
// Get bot rules
234253
$rules = $this->mergeRules(
235-
[static::$utilities['Bot']],
254+
array(static::$utilities['Bot']),
255+
array(static::$utilities['MobileBot']),
236256
static::$robots // NEW
237257
);
238258

@@ -274,7 +294,7 @@ public function version($propertyName, $type = self::VERSION_TYPE_STRING)
274294
*/
275295
protected function mergeRules()
276296
{
277-
$merged = [];
297+
$merged = array();
278298

279299
foreach (func_get_args() as $rules)
280300
{

tests/AgentTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class AgentTest extends PHPUnit_Framework_TestCase {
2929
'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' => 'Google',
3030
'facebookexternalhit/1.1 (+http(s)://www.facebook.com/externalhit_uatext.php)' => 'Facebook',
3131
'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)' => 'Bing',
32+
'Twitterbot/1.0' => 'Twitter',
3233
);
3334

3435
private $devices = array(
@@ -126,6 +127,7 @@ public function testRobots()
126127
{
127128
$agent->setUserAgent($ua);
128129
$this->assertTrue($agent->isRobot(), $ua);
130+
$this->assertEquals($robot, $agent->robot());
129131
}
130132
}
131133

0 commit comments

Comments
 (0)