-
Notifications
You must be signed in to change notification settings - Fork 16
/
Parser.php
525 lines (456 loc) · 13.9 KB
/
Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?php
/**
* -- PHP Htaccess Parser --
* Parser.php created at 02-12-2014
*
* Copyright 2014-2024 Estevão Soares dos Santos
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
namespace Tivie\HtaccessParser;
const IGNORE_WHITELINES = 2;
const IGNORE_COMMENTS = 4;
const AS_ARRAY = 8;
use ArrayAccess;
use SplFileObject;
use Tivie\HtaccessParser\Exception\Exception;
use Tivie\HtaccessParser\Exception\DomainException;
use Tivie\HtaccessParser\Exception\InvalidArgumentException;
use Tivie\HtaccessParser\Exception\SyntaxException;
use Tivie\HtaccessParser\Token\BaseToken;
use Tivie\HtaccessParser\Token\Block;
use Tivie\HtaccessParser\Token\Comment;
use Tivie\HtaccessParser\Token\Directive;
use Tivie\HtaccessParser\Token\WhiteLine;
/**
* Class Parser
* Htaccess Parser implemented in PHP
*
* @package Tivie\HtaccessParser
* @copyright 2014-2024 Estêvão Soares dos Santos
*/
class Parser
{
/**
* @var SplFileObject
*/
protected SplFileObject $file;
/**
* @var array|ArrayAccess|null
*/
protected array|ArrayAccess|null $container = null;
/**
* @var int Defaults to IGNORE_WHITELINES
*/
protected int $mode = 2;
private int $_cpMode = 2;
/**
* @var bool
*/
protected bool $rewind = true;
/**
* Create a new Htaccess Parser object
*
* @param SplFileObject|null $htaccessFile [optional] The .htaccess file to read.
* Must be set before running the parse method
*/
public function __construct(SplFileObject|null $htaccessFile = null)
{
if ($htaccessFile) {
$this->file = $htaccessFile;
}
}
/**
* Set the .htaccess file to parse
*
* @api
* @param SplFileObject $file
* @return $this
*/
public function setFile(SplFileObject $file): static
{
$this->file = $file;
return $this;
}
/**
* Set the receiving container of the parsed htaccess
*
* @api
* @param ArrayAccess|array $container Can be an array, an ArrayObject or an object that implements ArrayAccess
* @return $this
*/
public function setContainer(ArrayAccess|array $container): static
{
$this->container = $container;
return $this;
}
/**
* If the Parser should use arrays instead of Token Objects (that implement TokenInterface).
* Setting this to true returns a simple multidimensional array with scalars (no objects).
* Default is false.
*
* @api
* @param boolean $bool
* @return $this
*/
public function useArrays(bool $bool = true): static
{
return $this->bitwiseCtrl(!!$bool, AS_ARRAY);
}
/**
* If the parser should ignore whitelines (blank lines)
*
* @api
* @param boolean $bool
* @return $this
*/
public function ignoreWhitelines(bool $bool = true): static
{
return $this->bitwiseCtrl(!!$bool, IGNORE_WHITELINES);
}
/**
* If the parser should ignore comment lines. Default is false
*
* @api
* @param boolean $bool
* @return $this
*/
public function ignoreComments(bool $bool = true): static
{
return $this->bitwiseCtrl(!!$bool, IGNORE_COMMENTS);
}
/**
* If the parser should rewind the .htaccess file pointer before reading. Default is true
*
* @api
* @param bool $bool
* @return $this
*/
public function rewindFile(bool $bool = true): static
{
$this->rewind = !!$bool;
return $this;
}
/**
* Set the parser mode. (primarily for unit tests, use individual methods instead)
*
* @param int $mode
* @return $this
*/
public function setMode(int $mode): static
{
$this->mode = $mode;
return $this;
}
/**
* Parse a .htaccess file
*
* @param SplFileObject|null $file [optional] The .htaccess file. If null is passed and the file wasn't previously
* set, it will raise an exception
* @param int|null $optFlags [optional] Option flags
* - IGNORE_WHITELINES [2] Ignores whitelines (default)
* - IGNORE_COMMENTS [4] Ignores comments
* - As_ARRAY [8] Returns an array
* @param bool|null $rewind [optional] If the file pointer should be moved to the start (default is true)
* @return array|ArrayAccess|HtaccessContainer
* @throws Exception
* @throws SyntaxException
* @api
*/
public function parse(SplFileObject $file = null, int|null $optFlags = null, bool|null $rewind = null): HtaccessContainer|ArrayAccess|array
{
//Prepare passed options
$file = ($file !== null) ? $file : $this->file;
$optFlags = ($optFlags !== null) ? $optFlags : $this->mode;
$rewind = ($rewind !== null) ? !!$rewind : $this->rewind;
if (!$file instanceof SplFileObject) {
throw new Exception(".htaccess file is not set. You must set it (with Parser::setFile) before calling parse");
}
if (!$file->isReadable()) {
$path = $file->getRealPath();
throw new Exception(".htaccess file '$path'' is not readable");
}
// Rewind file pointer
if ($rewind) {
$file->rewind();
}
// Current Parse Mode
$this->_cpMode = $optFlags;
// Modes
$asArray = (AS_ARRAY & $optFlags);
// Container
if ($asArray) {
$htaccess = array();
} else {
$htaccess = ($this->container !== null) ? $this->container : new HtaccessContainer();
}
//Dump file line by line into $htaccess
while ($file->valid()) {
//Get line
$line = $file->getCurrentLine();
//Parse Line
$parsedLine = $this->parseLine($line, $file);
if (!is_null($parsedLine)) {
$htaccess[] = $parsedLine;
}
}
return $htaccess;
}
/**
* @param string $line
* @param SplFileObject $file
* @return BaseToken|null
* @throws DomainException
* @throws InvalidArgumentException
* @throws SyntaxException
*/
private function parseLine(string $line, SplFileObject $file): BaseToken|null
{
$ignoreWhiteLines = (IGNORE_WHITELINES & $this->_cpMode);
$ignoreComments = (IGNORE_COMMENTS & $this->_cpMode);
//Trim line
$line = trim($line);
$lineBreaks = array();
if ($this->isMultiLine($line)) {
$line = $this->parseMultiLine($line, $file, $lineBreaks);
}
if ($this->isWhiteLine($line)) {
return (!$ignoreWhiteLines) ? $this->parseWhiteLine() : null;
}
if ($this->isComment($line)) {
return (!$ignoreComments) ? $this->parseCommentLine($line, $lineBreaks) : null;
}
if ($this->isDirective($line)) {
return $this->parseDirectiveLine($line, $file, $lineBreaks);
}
if ($this->isBlock($line)) {
return $this->parseBlockLine($line, $file, $lineBreaks);
}
//Syntax not recognized so we throw SyntaxException
throw new SyntaxException($file->key(), $line, "Unexpected line");
}
/**
* Check if line is a white line
*
* @param string $line
* @return bool
*/
protected function isWhiteLine(string $line): bool
{
$line = trim($line);
return ($line == null);
}
/**
* Check if line is spanned across multiple lines
*
* @param string $line
* @return bool
*/
protected function isMultiLine(string $line): bool
{
$line = trim($line);
return (preg_match('/\\\\$/', $line) > 0);
}
/**
* Check if line is a comment
*
* @param string $line
* @return bool
*/
protected function isComment(string $line): bool
{
$line = trim($line);
return (preg_match('/^#/', $line) > 0);
}
/**
* Check if line is a directive
*
* @param string $line
* @return bool
*/
protected function isDirective(string $line): bool
{
$line = trim($line);
$pattern = '/^[^#<]/';
return (preg_match($pattern, $line) > 0);
}
/**
* Check if line is a block
*
* @param string $line
* @return bool
*/
protected function isBlock(string $line): bool
{
$line = trim($line);
return (preg_match('/^<[^\/].*>$/', $line) > 0);
}
/**
* Check if line is a Block end
*
* @param string $line
* @param string|null $blockName [optional] The block's name
* @return bool
*/
protected function isBlockEnd(string $line, string $blockName = null): bool
{
$line = trim($line);
$pattern = '/^\<\/';
$pattern .= ($blockName) ? $blockName : '[^\s\>]+';
$pattern .= '\>$/i';
return (preg_match($pattern, $line) > 0);
}
/**
* Parse a Multi Line
*
* @param string $line
* @param SplFileObject $file
* @param array $lineBreaks
* @return string
*/
protected function parseMultiLine(string $line, SplFileObject $file, array &$lineBreaks): string
{
while ($this->isMultiLine($line) && $file->valid()) {
$lineBreaks[] = strlen($line);
$line2 = $file->getCurrentLine();
// trim the ending slash
$line = rtrim($line, '\\');
// concatenate with next line
$line = trim($line . $line2);
}
return $line;
}
/**
* Parse a White Line
*
* @return WhiteLine
*/
protected function parseWhiteLine(): WhiteLine
{
return new WhiteLine();
}
/**
* Parse a Comment Line
*
* @param string $line
* @param array $lineBreaks
* @return Comment
* @throws DomainException
*/
protected function parseCommentLine(string $line, array $lineBreaks): Comment
{
$comment = new Comment();
$comment->setText($line)
->setLineBreaks($lineBreaks);
return $comment;
}
/**
* Parse a Directive Line
*
* @param string $line
* @param SplFileObject $file
* @param array $lineBreaks
* @return Directive
* @throws DomainException
* @throws InvalidArgumentException
* @throws SyntaxException
*/
protected function parseDirectiveLine(string $line, SplFileObject $file, array $lineBreaks): Directive
{
$directive = new Directive();
$args = $this->directiveRegex($line);
$name = array_shift($args);
if ($name === null) {
$lineNum = $file->key();
throw new SyntaxException($lineNum, $line, "Could not parse the name of the directive");
}
$directive->setName($name)
->setArguments($args)
->setLineBreaks($lineBreaks);
return $directive;
}
/**
* Parse a Block Line
*
* @param string $line
* @param SplFileObject $file
* @param array $lineBreaks
* @return Block
* @throws DomainException
* @throws SyntaxException
*/
protected function parseBlockLine(string $line, SplFileObject $file, array $lineBreaks): Block
{
$block = new Block();
$args = $this->blockRegex($line);
$name = array_shift($args);
if ($name === null) {
$lineNum = $file->key();
throw new SyntaxException($lineNum, $line, "Could not parse the name of the block");
}
$block->setName($name)
->setArguments($args)
->setLineBreaks($lineBreaks);
// Now we parse the children
$newLine = $file->getCurrentLine();
while (!$this->isBlockEnd($newLine, $name)) {
$parsedLine = $this->parseLine($newLine, $file);
if (!is_null($parsedLine)) {
$block->addChild($parsedLine);
}
$newLine = $file->getCurrentLine();
}
return $block;
}
private function bitwiseCtrl($bool, $flag): static
{
if ($bool) {
$this->mode = $this->mode | $flag;
} else {
$this->mode = $this->mode & ~$flag;
}
return $this;
}
private function directiveRegex($str): array
{
$pattern = '/"(?:\\.|[^\"])*"|\S+/';
$matches = array();
$trimmedMatches = array();
if (preg_match_all($pattern, $str, $matches) && isset($matches[0])) {
foreach ($matches[0] as $match) {
$match = trim($match);
if ($match != '') {
$trimmedMatches[] = $match;
}
}
return $trimmedMatches;
}
return array();
}
private function blockRegex($line): array
{
$pattern = '/(?:[\s|<]")([^<>"]+)(?:"[\s|>])|([^<>\s]+)/';
$final = array();
if (preg_match_all($pattern, $line, $matches) > 0) {
array_walk($matches[0], function ($val, $key) use (&$final) {
if ($val != null) {
$val = trim($val);
$val = trim($val, '<>');
$final[$key] = $val;
}
});
ksort($final);
}
return $final;
}
}