1 | <?php |
||
22 | class XRobotsTagParser |
||
23 | { |
||
24 | const HEADER_RULE_IDENTIFIER = 'x-robots-tag'; |
||
25 | const USERAGENT_DEFAULT = ''; |
||
26 | |||
27 | const DIRECTIVE_ALL = 'all'; |
||
28 | const DIRECTIVE_NONE = 'none'; |
||
29 | const DIRECTIVE_NO_ARCHIVE = 'noarchive'; |
||
30 | const DIRECTIVE_NO_FOLLOW = 'nofollow'; |
||
31 | const DIRECTIVE_NO_IMAGE_INDEX = 'noimageindex'; |
||
32 | const DIRECTIVE_NO_INDEX = 'noindex'; |
||
33 | const DIRECTIVE_NO_ODP = 'noodp'; |
||
34 | const DIRECTIVE_NO_SNIPPET = 'nosnippet'; |
||
35 | const DIRECTIVE_NO_TRANSLATE = 'notranslate'; |
||
36 | const DIRECTIVE_UNAVAILABLE_AFTER = 'unavailable_after'; |
||
37 | |||
38 | private $url = ''; |
||
39 | private $userAgent = self::USERAGENT_DEFAULT; |
||
40 | |||
41 | private $headers = []; |
||
42 | private $currentRule = ''; |
||
43 | private $currentUserAgent = self::USERAGENT_DEFAULT; |
||
44 | private $currentDirective = ''; |
||
45 | private $currentValue = ''; |
||
46 | |||
47 | private $options = []; |
||
48 | private $rules = []; |
||
49 | |||
50 | /** |
||
51 | * Constructor |
||
52 | * |
||
53 | * @param string $url |
||
54 | * @param string $userAgent |
||
55 | * @param array $options |
||
56 | */ |
||
57 | public function __construct($url, $userAgent = self::USERAGENT_DEFAULT, $options = []) |
||
76 | |||
77 | /** |
||
78 | * Request HTTP headers |
||
79 | * |
||
80 | * @return bool |
||
81 | */ |
||
82 | private function getHeaders() |
||
95 | |||
96 | /** |
||
97 | * Parse HTTP headers |
||
98 | * |
||
99 | * @return void |
||
100 | */ |
||
101 | private function parse() |
||
114 | |||
115 | /** |
||
116 | * Detect directives in rule |
||
117 | * |
||
118 | * @return void |
||
119 | */ |
||
120 | private function detectDirectives() |
||
137 | |||
138 | /** |
||
139 | * Directives supported |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | protected function directiveArray() |
||
158 | |||
159 | /** |
||
160 | * Add rule |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | private function addRule() |
||
172 | |||
173 | /** |
||
174 | * Cleanup before next rule is read |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | private function cleanup() |
||
185 | |||
186 | /** |
||
187 | * Return all applicable rules |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getRules() |
||
205 | |||
206 | /** |
||
207 | * Export all rules for all UserAgents |
||
208 | * |
||
209 | * @return array |
||
210 | */ |
||
211 | public function export() |
||
215 | } |