-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
HtaccessAnalyzer.php
106 lines (92 loc) · 2.15 KB
/
HtaccessAnalyzer.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
<?php
/**
* This file is part of Contao.
*
* Copyright (c) 2005-2015 Leo Feyer
*
* @license LGPL-3.0+
*/
namespace Contao\CoreBundle\Analyzer;
/**
* Analyzes an .htaccess file.
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class HtaccessAnalyzer
{
/**
* @var \SplFileInfo
*/
private $file;
/**
* Stores the file object.
*
* @param \SplFileInfo $file The file object
*
* @throws \InvalidArgumentException If $file is not a file
*/
public function __construct(\SplFileInfo $file)
{
if (!$file->isFile()) {
throw new \InvalidArgumentException("$file is not a file.");
}
$this->file = $file;
}
/**
* Creates a new object instance.
*
* @param \SplFileInfo $file The file object
*
* @return static The object instance
*/
public static function create(\SplFileInfo $file)
{
return new static($file);
}
/**
* Checks whether the .htaccess file grants access via HTTP.
*
* @return bool True if the .htaccess file grants access via HTTP
*/
public function grantsAccess()
{
$content = array_filter(file($this->file));
foreach ($content as $line) {
if ($this->hasRequireGranted($line)) {
return true;
}
}
return false;
}
/**
* Scans a line for an access definition.
*
* @param string $line The current line
*
* @return bool True if the line has an access definition
*/
private function hasRequireGranted($line)
{
if ($this->isComment($line)) {
return false;
}
if (false !== stripos($line, 'Allow from all')) {
return true;
}
if (false !== stripos($line, 'Require all granted')) {
return true;
}
return false;
}
/**
* Checks whether a line is a comment.
*
* @param string $line The current line
*
* @return bool True if the line is a comment
*/
private function isComment($line)
{
return 0 === strncmp('#', ltrim($line), 1);
}
}