Skip to content

Commit

Permalink
Merge pull request #61 from t1gor/JanPetterMG-patch-1
Browse files Browse the repository at this point in the history
Fix #60
  • Loading branch information
JanPetterMG committed Feb 24, 2016
2 parents c68a7a8 + e1abfed commit e1b052c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
22 changes: 14 additions & 8 deletions source/robotstxtparser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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] = [];
}
}

Expand Down Expand Up @@ -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;
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/cases/UserAgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e1b052c

Please sign in to comment.