Recursively generate "safe" index.php when none exists
#21
by
doekia
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
<?php
/*
* safe-index.php Recursively generate "safe" index.php when none exists
*
* Copyright (c)2008 doekia Enter-Solutions GPL
*
* Simply run this from the "docroot" so generated index will implement a
* proper http redirect to it (docroot)
*
*/
function index($path)
{
static $gmdate = null;
$gmdate === null && $gmdate = gmdate('D, d M Y H:i:s');
empty($path) && $path = '.';
if (file_exists($path.'/index.php')) return;
echo $path.PHP_EOL;
$sub = preg_replace('#[^/]+#','..',$path);
$path == '.' && $sub = '.';
$content = <<< EOF
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: $gmdate GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: $sub/");
exit;
EOF;
file_put_contents($path.'/index.php',$content);
}
$scan = new RecursiveIteratorIterator( new RecursiveDirectoryIterator('.', RecursiveIteratorIterator::SELF_FIRST));
foreach($scan as $file) {
if($file->isDir() ) {
$item = $file->getPathname();
if (basename($item) === '.') {
index(substr($item,2,-2));
}
}
}