1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* vipnytt/RobotsTxtParser |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/VIPnytt/RobotsTxtParser |
6
|
|
|
* @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace vipnytt\RobotsTxtParser\Parser\Directives; |
10
|
|
|
|
11
|
|
|
use DateTimeZone; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class DirectiveParserTrait |
15
|
|
|
* |
16
|
|
|
* @package vipnytt\RobotsTxtParser\Directive |
17
|
|
|
*/ |
18
|
|
|
trait DirectiveParserTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Generate directive/rule pair |
22
|
|
|
* |
23
|
|
|
* @param string $line |
24
|
|
|
* @param string[] $whiteList |
25
|
|
|
* @return string[]|false |
26
|
|
|
*/ |
27
|
|
|
private function generateRulePair($line, array $whiteList) |
28
|
|
|
{ |
29
|
|
|
$whiteList = array_map('strtolower', $whiteList); |
30
|
|
|
// Split by directive and rule |
31
|
|
|
$pair = array_map('trim', explode(':', $line, 2)); |
32
|
|
|
// Check if the line contains a rule |
33
|
|
|
if (empty($pair[1]) || |
34
|
|
|
empty($pair[0]) || |
35
|
|
|
!in_array(($pair[0] = str_ireplace(array_keys(self::ALIAS_DIRECTIVES), array_values(self::ALIAS_DIRECTIVES), strtolower($pair[0]))), $whiteList) |
36
|
|
|
) { |
37
|
|
|
// Line does not contain any supported directive |
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
return [ |
41
|
|
|
'directive' => $pair[0], |
42
|
|
|
'value' => $pair[1], |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Client timestamp range as specified in the `Robot exclusion standard` version 2.0 draft |
48
|
|
|
* @link http://www.conman.org/people/spc/robots2.html#format.directives.visit-time |
49
|
|
|
* |
50
|
|
|
* @param $string |
51
|
|
|
* @return string[]|false |
52
|
|
|
*/ |
53
|
|
|
private function draftParseTime($string) |
54
|
|
|
{ |
55
|
|
|
$array = preg_replace('/[^0-9]/', '', explode('-', $string, 3)); |
56
|
|
|
if (count($array) !== 2 || |
57
|
|
|
($fromTime = date_create_from_format('Hi', $array[0], $dtz = new DateTimeZone('UTC'))) === false || |
58
|
|
|
($toTime = date_create_from_format('Hi', $array[1], $dtz)) === false |
59
|
|
|
) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
return [ |
63
|
|
|
'from' => date_format($fromTime, 'Hi'), |
64
|
|
|
'to' => date_format($toTime, 'Hi'), |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|