Skip to content

Commit d6413b1

Browse files
authored
Merge pull request #16 from DavidGoodwin/master
invalid User-Agent causes strpos needle E_NOTICE
2 parents d60fa8b + a4f2d77 commit d6413b1

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Robot/RobotsTxt.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ private function parseFile(RobotsFile $robotFile)
6565

6666
$this->records = [];
6767
foreach($fileRecords as $record) {
68+
if (!empty($record['ua'])) {
69+
$record['ua'] = array_filter($record['ua']);
70+
}
6871
if (!empty($record['ua']) && !empty($record['rules'])) {
6972
$this->records[] = new Record(new UserAgent($record['ua']), new AccessRules($record['rules']));
7073
}

tests/RobotTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,22 @@ public function testCaseSensitivePaths()
171171
$this->assertTrue($g->isAllowed('Googlebot', '/uppercase.html'));
172172
$this->assertFalse($g->isAllowed('Googlebot', '/UPPERCASE.HTML'));
173173
}
174+
175+
/**
176+
* Do not emit an E_NOTICE with an empty needle from strpos when a user-agent line is invalid (empty).
177+
* This effectively ignores the line.
178+
*/
179+
public function testHandlesEmptyUserAgentLine()
180+
{
181+
$invalid_text = <<<EOF
182+
User-agent: foo
183+
User-agent:
184+
Disallow: /
185+
EOF;
186+
187+
$g = new RobotsTxt($invalid_text);
188+
189+
$this->assertFalse($g->isAllowed('foo', '/something'));
190+
$this->assertTrue($g->isAllowed('Googlebot', '/something'));
191+
}
174192
}

0 commit comments

Comments
 (0)