-
Notifications
You must be signed in to change notification settings - Fork 5
/
htaccess.php
152 lines (132 loc) · 3.98 KB
/
htaccess.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
<?php
/**
* Convert .htaccess to httpd.conf entries
*
* @author Paul Reinheimer
* @copyright Copyright (c) 2009, Paul Reinheimer
* @license
* @link http://blog.preinheimer.com/index.php?/archives/340-.htaccess-to-httpd.conf.html
* @version 1.1
*
* No warranties expressed or implied
* Special thanks to Rich Bowen http://drbacchus.com/
* Minor contributors: Jakub Cernek [http://jakub.cernek.cz/]
*
* Improvements? Let me know! Bugs? Send a patch.
* Unsupported Software lies below.
*
* Example Usage:
* /var/www/domain.com> php htaccess.php > ~/htaccess.conf
* Filtering to exclude (substring match)
* /var/www/domain.com> php htaccess.php evilDirectory > ~/htaccess.conf
*/
//Start from the present working directory, recurse from here. Using the real path avoids a bug where .htaccess files in the PWD are omitted from results on some systems
$startPath = realpath("./");
$ite= new RecursiveDirectoryIterator($startPath);
//Lets give people a hand, skip these directories. Especially helpful when run on dev systems
$filters = array(".svn", ".cvs", ".git");
//Merge base set of filters with any from the command line
if(count($argv) > 0)
{
unset($argv[0]);
$filters = array_merge($filters, $argv);
}
//Iterate recursively through everything from here on in, of course filtering out stuff from the filter list
foreach (new fileFilter(new RecursiveIteratorIterator($ite), $filters) as $filename=>$cur)
{
$htaccessFiles[] = $filename;
}
//No files? Quit now!
if (count($htaccessFiles) == 0)
{
die("No .htaccess files found");
}
//Sort the list, place least depth first. This is important to allow overrides from sub-directories to occur correctly
usort($htaccessFiles, 'sorter');
//Warnings encountered
$flags = 0;
//Iterate over found files (sorted now) and read them in.
foreach($htaccessFiles as $file)
{
//Grab the file and print out the <Directory $path> bit
$path = realpath(pathinfo($file, PATHINFO_DIRNAME));
echo "<Directory $path>\n";
$lines = file($file);
if(count($lines) > 0)
{
//Tab the file in, check for RedirectBase which may cause problems
foreach($lines as $line)
{
echo "\t$line";
if(stripos($line, "RedirectBase") !== FALSE)
{
//Not tabbed! See what happens there?
echo "# WARNING The above line contains RedirectBase which may not convert directly to a conf file. Please check manually\n";
$flags++;
}
}
//Handle issues where files don't end with a newline
if (in_array(substr($line, -1), array("\n", "\r")))
{
echo "</Directory>\n\n";
}else
{
echo "\n</Directory>\n\n";
}
}else{
//File was empty, leave the stub in
echo "\n</Directory>\n\n";
}
}
//Check for warnings
if ($flags > 0)
{
echo "# A total of $flags warnings were encountered. Please read through the file and correct any noted problems\n";
}else
{
echo "# No warnings detected \n";
}
echo "# Please test before going live, no guarantees! \n";
//Sort by the number of path segments, least first
function sorter($a, $b)
{
$a = count(explode("/", $a));
$b = count(explode("/", $b));
if($a == $b)
{
return 0;
}
if($a > $b)
{
return 1;
}
return -1;
}
//Filter out specified files.
class fileFilter extends FilterIterator
{
private $filters;
public function __construct(Iterator $iterator, $filters)
{
parent::__construct($iterator);
$this->filters = $filters;
}
public function accept()
{
$dir = $this->getInnerIterator()->current();
foreach($this->filters as $filter)
{
if(strpos($dir, $filter) !== false)
{
return false;
}
}
$filename = DIRECTORY_SEPARATOR . '.htaccess';
if (substr($dir, - strlen($filename), strlen($filename)) == $filename)
{
return true;
}
//echo "Skipping $dir\n";
return false;
}
}