|
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 vipnytt\RobotsTxtParser\Handler\RenderHandler; |
|
12
|
|
|
use vipnytt\RobotsTxtParser\Parser\UriParser; |
|
13
|
|
|
use vipnytt\RobotsTxtParser\RobotsTxtInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class CleanParamParserCore |
|
17
|
|
|
* |
|
18
|
|
|
* @package vipnytt\RobotsTxtParser\Parser\Directives |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class CleanParamParserCore implements ParserInterface, RobotsTxtInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Clean-param array |
|
24
|
|
|
* @var string[][] |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $cleanParam = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* CleanParamParserCore constructor. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Add |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $line |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
|
|
public function add($line) |
|
42
|
|
|
{ |
|
43
|
|
|
// split into parameter and path |
|
44
|
|
|
$array = array_map('trim', mb_split('\s+', $line, 2)); |
|
45
|
|
|
// strip any invalid characters from path prefix |
|
46
|
|
|
$path = '/'; |
|
47
|
|
|
if (isset($array[1])) { |
|
48
|
|
|
$uriParser = new UriParser(preg_replace('/[^A-Za-z0-9\.-\/\*\_]/', '', $array[1])); |
|
49
|
|
|
$path = $uriParser->encode(); |
|
50
|
|
|
} |
|
51
|
|
|
$param = array_map('trim', explode('&', $array[0])); |
|
52
|
|
|
foreach ($param as $key) { |
|
53
|
|
|
$this->cleanParam[$key][] = $path; |
|
54
|
|
|
} |
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Render |
|
60
|
|
|
* |
|
61
|
|
|
* @param RenderHandler $handler |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
public function render(RenderHandler $handler) |
|
65
|
|
|
{ |
|
66
|
|
|
ksort($this->cleanParam); |
|
67
|
|
|
return $handler->getMode() >= 3 ? $this->renderExtensive($handler) : $this->renderCompressed($handler); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Render extensive |
|
72
|
|
|
* |
|
73
|
|
|
* @param RenderHandler $handler |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
private function renderExtensive(RenderHandler $handler) |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ($this->cleanParam as $param => $paths) { |
|
79
|
|
|
foreach ($paths as $path) { |
|
80
|
|
|
$handler->add(self::DIRECTIVE_CLEAN_PARAM, $param . ' ' . $path); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Render compressed |
|
88
|
|
|
* |
|
89
|
|
|
* @param RenderHandler $handler |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
private function renderCompressed(RenderHandler $handler) |
|
93
|
|
|
{ |
|
94
|
|
|
$pair = $this->cleanParam; |
|
95
|
|
|
while (!empty($pair)) { |
|
96
|
|
|
$equalParams = array_keys($pair, current($pair)); |
|
97
|
|
|
foreach (current($pair) as $path) { |
|
|
|
|
|
|
98
|
|
|
$handler->add(self::DIRECTIVE_CLEAN_PARAM, implode('&', $equalParams) . ' ' . $path); |
|
99
|
|
|
} |
|
100
|
|
|
foreach ($equalParams as $param) { |
|
101
|
|
|
unset($pair[$param]); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
return true; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.