|
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
|
|
|
// Split by directive and rule |
|
30
|
|
|
$pair = array_map('trim', explode(':', $line, 2)); |
|
31
|
|
|
// Check if the line contains a rule |
|
32
|
|
|
if (empty($pair[1]) || |
|
33
|
|
|
empty($pair[0]) || |
|
34
|
|
|
!in_array(($pair[0] = str_ireplace(array_keys(self::ALIAS_DIRECTIVES), array_values(self::ALIAS_DIRECTIVES), strtolower($pair[0]))), $whiteList) |
|
35
|
|
|
) { |
|
36
|
|
|
// Line does not contain any supported directive |
|
37
|
|
|
return false; |
|
38
|
|
|
} |
|
39
|
|
|
return $pair; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Client timestamp range as specified in the `Robot exclusion standard` version 2.0 draft |
|
44
|
|
|
* @link http://www.conman.org/people/spc/robots2.html#format.directives.visit-time |
|
45
|
|
|
* |
|
46
|
|
|
* @param $string |
|
47
|
|
|
* @return string[]|false |
|
48
|
|
|
*/ |
|
49
|
|
|
private function draftParseTime($string) |
|
50
|
|
|
{ |
|
51
|
|
|
$array = explode('-', str_replace('+', '', filter_var($string, FILTER_SANITIZE_NUMBER_INT)), 3); |
|
52
|
|
|
if (count($array) !== 2 || |
|
53
|
|
|
($fromTime = date_create_from_format('Hi', $array[0], $dtz = new DateTimeZone('UTC'))) === false || |
|
54
|
|
|
($toTime = date_create_from_format('Hi', $array[1], $dtz)) === false |
|
55
|
|
|
) { |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
return [ |
|
59
|
|
|
'from' => date_format($fromTime, 'Hi'), |
|
60
|
|
|
'to' => date_format($toTime, 'Hi'), |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|