diff --git a/composer.json b/composer.json index 196ace4..a861d93 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "require-dev": { "php": ">=5.4.0", "phpunit/phpunit": ">=3.7", - "codeclimate/php-test-reporter": "0.2.0" + "codeclimate/php-test-reporter": ">=0.2" }, "authors": [ { diff --git a/source/robotstxtparser.php b/source/robotstxtparser.php index 3d0266d..1c79e7f 100644 --- a/source/robotstxtparser.php +++ b/source/robotstxtparser.php @@ -74,7 +74,7 @@ class RobotsTxtParser protected $log = array(); // internally used variables - protected $current_UserAgent = ""; + protected $current_UserAgent = []; protected $current_word = ""; protected $current_char = ""; protected $char_index = 0; @@ -370,11 +370,15 @@ public function setHttpStatusCode($code) */ private function setCurrentUserAgent() { - $this->current_UserAgent = mb_strtolower(trim($this->current_word)); + $ua = mb_strtolower(trim($this->current_word)); + if ($this->previous_directive !== self::DIRECTIVE_USERAGENT) { + $this->current_UserAgent = []; + } + $this->current_UserAgent[] = $ua; // create empty array if not there yet - if (empty($this->rules[$this->current_UserAgent])) { - $this->rules[$this->current_UserAgent] = array(); + if (empty($this->rules[$ua])) { + $this->rules[$ua] = []; } } @@ -437,10 +441,12 @@ private function addRule($append = true) if (empty($this->current_word)){ return; } - if ($append === true) { - $this->rules[$this->current_UserAgent][$this->current_directive][] = $this->current_word; - } else { - $this->rules[$this->current_UserAgent][$this->current_directive] = $this->current_word; + foreach ($this->current_UserAgent as $ua) { + if ($append === true) { + $this->rules[$ua][$this->current_directive][] = $this->current_word; + continue; + } + $this->rules[$ua][$this->current_directive] = $this->current_word; } } diff --git a/test/cases/UserAgentTest.php b/test/cases/UserAgentTest.php index 574f06f..b49adfc 100644 --- a/test/cases/UserAgentTest.php +++ b/test/cases/UserAgentTest.php @@ -22,6 +22,11 @@ public function testUserAgentPermission($robotsTxtContent) $this->assertFalse($parser->isDisallowed("/article")); $this->assertFalse($parser->isAllowed("/temp")); + $this->assertTrue($parser->isDisallowed("/foo", "agentV")); + $this->assertTrue($parser->isAllowed("/bar", "agentV")); + $this->assertTrue($parser->isDisallowed("/foo", "agentW")); + $this->assertTrue($parser->isAllowed("/bar", "agentW")); + $this->assertTrue($parser->isAllowed("/temp", "spiderX/1.0")); $this->assertTrue($parser->isDisallowed("/assets", "spiderX/1.0")); $this->assertTrue($parser->isAllowed("/forum", "spiderX/1.0")); @@ -62,6 +67,11 @@ public function generateDataForTest() Disallow: /temp Disallow: /forum + User-agent: agentV + User-agent: agentW + Disallow: /foo + Allow: /bar + User-agent: spiderX Disallow: Disallow: /admin