Skip to content

Commit 768cad2

Browse files
committed
Updated agentzero::getHints() to enable the hints to be extracted from a headers array or from $_SERVER.
Improved logic in `versions::get()`. Added some `ClaudeBot` tests.
1 parent 101de9e commit 768cad2

3 files changed

Lines changed: 70 additions & 7 deletions

File tree

src/agentzero.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,18 @@ public function __get(string $key) : string|int|null {
150150
*
151151
* @return array<string,string> An array containing relevant client hints sent by the client
152152
*/
153-
public static function getHints() : array {
153+
public static function getHints(?array $headers = null) : array {
154+
155+
// get from $_SERVER
156+
if ($headers === null) {
157+
$values = $_SERVER;
158+
$prefix = 'HTTP_';
159+
} else {
160+
$values = [];
161+
foreach ($headers AS $key => $item) {
162+
$values[\strtoupper(\str_replace('-', '_', $key))] = $item;
163+
}
164+
}
154165
$hints = [
155166
'sec-ch-ua-mobile',
156167
'sec-ch-ua-full-version-list',
@@ -164,8 +175,8 @@ public static function getHints() : array {
164175
$data = [];
165176
foreach ($hints AS $item) {
166177
$upper = \strtoupper(\str_replace('-', '_', $item));
167-
if (!empty($_SERVER['HTTP_'.$upper])) {
168-
$data[$item] = $_SERVER['HTTP_'.$upper];
178+
if (!empty($values[$prefix.$upper])) {
179+
$data[$item] = $values[$prefix.$upper];
169180
}
170181
}
171182
return $data;

src/helpers/versions.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ public static function get(string $browser, string $version, array $config) : ar
105105
$life = $config['versionscachelife'];
106106
if ($cache !== null && ($versions = self::load($source, $cache, $life)) !== false) {
107107
$data = [];
108-
if (isset($versions[$browser])) {
109108

110-
// get latest version of the browser
111-
$data['browserlatest'] = self::latest($versions[$browser], $config['currentdate']);
109+
// get latest version of the browser
110+
if (isset($versions[$browser]) && ($data['browserlatest'] = self::latest($versions[$browser], $config['currentdate'])) !== null) {
112111

113112
// check if version is greater than latest version
114113
$major = \intval($version);
@@ -142,7 +141,7 @@ public static function get(string $browser, string $version, array $config) : ar
142141
$data['browserreleased'] = self::released($versions[$browser], $version);
143142

144143
// calculate status
145-
if (isset($data['browserreleased'])) {
144+
if (isset($data['browserreleased'], $data['browserlatest'])) {
146145
$current = \explode('.', $data['browserlatest'])[0] === \explode('.', $version)[0];
147146
$released = new \DateTime($data['browserreleased']);
148147

tests/crawlersTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,59 @@ public function testAi() : void {
16491649
'appname' => 'PerplexityBot',
16501650
'appversion' => '1.0',
16511651
'url' => 'https://perplexity.ai/perplexitybot'
1652+
],
1653+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36; ClaudeBot/1.0; +claudebot@anthropic.com)' => [
1654+
'string' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36; ClaudeBot/1.0; +claudebot@anthropic.com)',
1655+
'type' => 'robot',
1656+
'category' => 'ai',
1657+
'vendor' => 'Apple',
1658+
'device' => 'Macintosh',
1659+
'processor' => 'Intel',
1660+
'architecture' => 'x86',
1661+
'bits' => 64,
1662+
'kernel' => 'Linux',
1663+
'platform' => 'Mac OS X',
1664+
'platformversion' => '10.15.7',
1665+
'engine' => 'Blink',
1666+
'engineversion' => '131.0.0.0',
1667+
'browser' => 'Chrome',
1668+
'browserversion' => '131.0.0.0',
1669+
'browserreleased' => '2025-01-22',
1670+
'app' => 'Claude Bot',
1671+
'appname' => 'ClaudeBot',
1672+
'appversion' => '1.0'
1673+
],
1674+
'ClaudeBot/1.0 (web crawler; +https://www.anthropic.com/; claudebot@anthropic.com)' => [
1675+
'string' => 'ClaudeBot/1.0 (web crawler; +https://www.anthropic.com/; claudebot@anthropic.com)',
1676+
'type' => 'robot',
1677+
'category' => 'ai',
1678+
'app' => 'Claude Bot',
1679+
'appname' => 'ClaudeBot',
1680+
'appversion' => '1.0',
1681+
'url' => 'https://www.anthropic.com/'
1682+
],
1683+
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +support@anthropic.com)' => [
1684+
'string' => 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +support@anthropic.com)',
1685+
'type' => 'robot',
1686+
'category' => 'ai',
1687+
'app' => 'Claude Bot',
1688+
'appname' => 'ClaudeBot',
1689+
'appversion' => '1.0'
1690+
],
1691+
'ClaudeBot' => [
1692+
'string' => 'ClaudeBot',
1693+
'type' => 'robot',
1694+
'category' => 'ai',
1695+
'app' => 'Claude Bot',
1696+
'appname' => 'ClaudeBot'
1697+
],
1698+
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)' => [
1699+
'string' => 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)',
1700+
'type' => 'robot',
1701+
'category' => 'ai',
1702+
'app' => 'Claude Bot',
1703+
'appname' => 'ClaudeBot',
1704+
'appversion' => '1.0'
16521705
]
16531706
];
16541707
foreach ($strings AS $ua => $item) {

0 commit comments

Comments
 (0)