Skip to content

Instantly share code, notes, and snippets.

@mklkj
Last active August 22, 2017 15:45
Show Gist options
  • Save mklkj/6612469551c4286f881c626657fcea2e to your computer and use it in GitHub Desktop.
Save mklkj/6612469551c4286f881c626657fcea2e to your computer and use it in GitHub Desktop.
wget -r php server router
<?php
$request = $_SERVER['REQUEST_URI'];
$root = __DIR__;
// workaround https://bugs.php.net/bug.php?id=61286
$_SERVER['SCRIPT_NAME'] = '/index.php';
$requestFile = $root.$request;
$index = $root.'/index.php';
error_log(str_repeat('-', 80));
error_log('$request: '.$request);
error_log('$root: '.$root);
error_log('$requestFile: '.$requestFile);
error_log('$index: '.$index);
error_log($requestFile.'/index.php');
if (preg_match('/\.(js|css|ttf)/i', $requestFile) and file_exists($requestFile)) {
error_log('0');
if (file_exists($requestFile)) {
if (preg_match('/\.js/', $requestFile)) {
header("Content-type: application/javascript");
} elseif (preg_match('/\.css/', $requestFile)) {
header("Content-type: text/css");
} elseif (preg_match('/\.ttf/', $requestFile)) {
header("Content-type: font/ttf");
}
echo file_get_contents($requestFile);
} else {
return false;
}
} elseif (preg_match('/\.(png|jpg|jpeg|gif|svg|pdf)/i', $requestFile)) {
error_log('1');
error_log('preg_match(png|jpg|jpeg|gif|svg|pdf)');
error_log($request);
if (strpos($requestFile, 'showthumb.php') !== false) {
$a = explode('showthumb.php', $requestFile);
$requestFile = $a[0].'showthumb.php'.str_replace('/', '%2F', $a[1]);
}
if (file_exists($requestFile)) {
header('Content-type: '.mime_content_type($requestFile));
echo file_get_contents($requestFile);
} else {
return false;
}
} elseif (file_exists($requestFile) and !is_dir($requestFile)) {
error_log('2');
error_log($requestFile);
echo file_get_contents($requestFile);
} elseif(is_dir($requestFile)) {
$dir = rtrim(str_replace($root.'/', '', $requestFile), '/');
$g = glob($dir.'/*');
echo '<!DOCTYPE html><h2>Index of /'.$dir.'</h2><ul>';
usort($g, function($a, $b) {
if (is_dir($a) == is_dir($b)) {
return strnatcasecmp($a,$b);
} else {
return is_dir($a) ? -1 : 1;
}
});
echo implode('', array_map(function($a) {
global $dir;
return '<li><a href="/'.$a.'">'.str_replace($dir.'/', '', $a).'</a>';
}, $g));
}elseif (file_exists($requestFile.'.1')) {
error_log('3');
error_log($request.'.1');
echo file_get_contents($requestFile.'.1');
} elseif (file_exists($requestFile.'/index.php.1')) {
error_log('4');
error_log($requestFile.'/index.php.1');
echo file_get_contents($requestFile.'/index.php.1');
} elseif (file_exists($requestFile.'/index.php')) {
error_log('5');
error_log($requestFile.'/index.php');
echo file_get_contents($requestFile.'/index.php');
} elseif (file_exists($requestFile.'/index.html')) {
error_log('6');
error_log($requestFile.'/index.html');
echo file_get_contents($requestFile.'/index.html');
} else {
error_log('7: error 404');
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment