1
|
|
|
<?php |
2
|
|
|
namespace vipnytt\RobotsTxtParser\Client\Directives; |
3
|
|
|
|
4
|
|
|
use DateTime; |
5
|
|
|
use DateTimeZone; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class DirectiveClientCommons |
9
|
|
|
* |
10
|
|
|
* @package vipnytt\RobotsTxtParser\Client\Directives |
11
|
|
|
*/ |
12
|
|
|
trait DirectiveClientCommons |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Is time between |
16
|
|
|
* |
17
|
|
|
* @param int $timestamp |
18
|
|
|
* @param string $fromTime |
19
|
|
|
* @param string $toTime |
20
|
|
|
* @param string $format |
21
|
|
|
* @return bool |
22
|
|
|
*/ |
23
|
|
|
private function isBetween($timestamp, $fromTime, $toTime, $format = 'Hi') |
24
|
|
|
{ |
25
|
|
|
$dateTime = new DateTime(); |
26
|
|
|
$timezone = new DateTimeZone('UTC'); |
27
|
|
|
$dtRef = $dateTime->createFromFormat('U', $timestamp, $timezone); |
28
|
|
|
$dtFrom = $dateTime->createFromFormat($format, $fromTime, $timezone); |
29
|
|
|
$dtTo = $dateTime->createFromFormat($format, $toTime, $timezone); |
30
|
|
|
if ($dtFrom > $dtTo) { |
31
|
|
|
$dtTo->modify('+1 day'); |
32
|
|
|
} |
33
|
|
|
return ( |
34
|
|
|
$dtFrom <= $dtRef && |
35
|
|
|
$dtRef <= $dtTo |
36
|
|
|
) || ( |
37
|
|
|
$dtFrom <= $dtRef->modify('+1 day') && |
38
|
|
|
$dtRef <= $dtTo |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Check path rule |
44
|
|
|
* |
45
|
|
|
* @param string $path |
46
|
|
|
* @param string[] $paths |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
private function checkPaths($path, array $paths) |
50
|
|
|
{ |
51
|
|
|
foreach ($paths as $rule) { |
52
|
|
|
$escape = [ |
53
|
|
|
'?' => '\?', |
54
|
|
|
'.' => '\.', |
55
|
|
|
'*' => '.*', |
56
|
|
|
]; |
57
|
|
|
foreach ($escape as $search => $replace) { |
58
|
|
|
$rule = str_replace($search, $replace, $rule); |
59
|
|
|
} |
60
|
|
|
if ($this->checkPathsCallback($rule, $path)) { |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Callback for CheckPath |
69
|
|
|
* |
70
|
|
|
* @param string $rule |
71
|
|
|
* @param string $path |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
private function checkPathsCallback($rule, $path) |
75
|
|
|
{ |
76
|
|
|
/** |
77
|
|
|
* Warning: preg_match need to be replaced |
78
|
|
|
* |
79
|
|
|
* Bug report |
80
|
|
|
* @link https://github.com/t1gor/Robots.txt-Core-Class/issues/62 |
81
|
|
|
* |
82
|
|
|
* An robots.txt parser, where a bug-fix is planned |
83
|
|
|
* @link https://github.com/diggin/Diggin_RobotRules |
84
|
|
|
* |
85
|
|
|
* The solution? |
86
|
|
|
* PHP PEG (parsing expression grammar) |
87
|
|
|
* @link https://github.com/hafriedlander/php-peg |
88
|
|
|
*/ |
89
|
|
|
try { |
90
|
|
|
if (!preg_match('#' . $rule . '#', $path)) { |
91
|
|
|
// Rule does not match |
92
|
|
|
return false; |
93
|
|
|
} elseif ( |
94
|
|
|
mb_stripos($rule, '$') === false || // No special parsing required |
95
|
|
|
mb_substr($rule, 0, -1) == $path // Rule does contain an end anchor, and matches |
96
|
|
|
) { |
97
|
|
|
return true; |
98
|
|
|
} elseif (($wildcardPos = mb_strrpos($rule, '*')) !== false) { |
99
|
|
|
// Rule contains both an end anchor ($) and wildcard (*) |
100
|
|
|
$afterWildcard = mb_substr($rule, $wildcardPos + 1, mb_strlen($rule) - $wildcardPos - 2); |
101
|
|
|
if ($afterWildcard == mb_substr($path, -mb_strlen($afterWildcard))) { |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} catch (\Exception $e) { |
106
|
|
|
// An preg_match bug has occurred |
107
|
|
|
} |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|