-
Notifications
You must be signed in to change notification settings - Fork 2
/
AutoIndexer.php
161 lines (141 loc) · 4.96 KB
/
AutoIndexer.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
<?php
/**
* PHP auto indexer: the tool against directory traversal security vulnerability.
*
* @author Maksim T. <[email protected]>
* @copyright 2019 Maksim T.
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/zapalm/auto-indexer GitHub
* @link https://prestashop.modulez.ru/en/tools-scripts/78-tool-against-directory-traversal-security-vulnerability.html Homepage
*/
namespace zapalm;
use LogicException;
/**
* Auto indexer.
*
* @author Maksim T. <[email protected]>
*/
class AutoIndexer
{
/** @var string The path to the source directory for recursively adding or removing the index.php file. */
private $directoryPath;
/** @var string|null The path to the template file (index.php) for adding. */
private $templatePath;
/**
* Constructor.
*
* @param string $directoryPath The path to the source directory for recursively adding or removing the index.php file.
* @param string|null $templatePath The path to the template file (index.php) for adding.
*
* @throws LogicException
*
* @author Maksim T. <[email protected]>
*/
public function __construct($directoryPath, $templatePath = null)
{
$this->directoryPath = realpath($directoryPath);
if (false === file_exists($this->directoryPath)) {
throw new LogicException('The source directory path is not exists: ' . $this->directoryPath);
}
if (null !== $templatePath) {
$this->templatePath = realpath($templatePath);
if (false === file_exists($this->templatePath)) {
throw new LogicException('The template path is not exists: ' . $this->templatePath);
}
}
}
/**
* Adds index.php files to the configured path.
*
* It's do not replaces the existing index.php files.
*
* @see removeIndex()
*
* @throws LogicException
*
* @author Maksim T. <[email protected]>
*/
public function addIndex()
{
if (null === $this->templatePath) {
throw new LogicException('The template path is not configured.');
}
$this->addIndexRecursively($this->directoryPath);
}
/**
* Removes index.php files from the configured path.
*
* Useful for the cleaning a directory of old index.php files.
*
* @param array $skipDirectories Directories to skip.
*
* @see addIndex()
*
* @author Maksim T. <[email protected]>
*/
public function removeIndex(array $skipDirectories = array('vendor'))
{
$this->removeIndexRecursively($this->directoryPath, $skipDirectories);
}
/**
* Adds index.php file recursively to a given path.
*
* @param string $path Path of the directory for recursively adding the index.php file.
*
* @author Maksim T. <[email protected]>
*/
private function addIndexRecursively($path)
{
// Skip special directories such as .git, .idea and so on.
$skipExclusions = array(
'.github',
);
$fileName = basename($path);
if (0 === strpos($fileName, '.') && false === in_array($fileName, $skipExclusions)) {
echo 'Skip: ' . $path . PHP_EOL;
return;
}
$indexFilePath = $path . DIRECTORY_SEPARATOR . 'index.php';
if (false === file_exists($indexFilePath)) {
copy($this->templatePath, $path . DIRECTORY_SEPARATOR . 'index.php');
echo 'Added to: ' . $path . PHP_EOL;
} else {
echo 'Exists in: ' . $path . PHP_EOL;
}
$directories = glob($path . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
if (false === $directories) {
return;
}
foreach ($directories as $directory) {
$this->addIndexRecursively($directory);
}
}
/**
* Removes the index.php file recursively from a given path.
*
* @param string $path Path of the directory for recursively removing the index.php file.
* @param array $skipDirectories Directories to skip.
*
* @author Maksim T. <[email protected]>
*/
private function removeIndexRecursively($path, array $skipDirectories = array()) {
$fileName = basename($path);
// Skip special directories such as .git, .idea, vendor and so on.
if (0 === strpos($fileName, '.') || in_array($fileName, $skipDirectories)) {
echo 'Skip: ' . $path . PHP_EOL;
return;
}
$indexFilePath = $path . DIRECTORY_SEPARATOR . 'index.php';
if (file_exists($indexFilePath)) {
unlink($indexFilePath);
echo 'Removed from: ' . $path . PHP_EOL;
}
$directories = glob($path . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
if (false === $directories) {
return;
}
foreach ($directories as $directory) {
$this->removeIndexRecursively($directory, $skipDirectories);
}
}
}