Last active
October 7, 2017 02:06
-
-
Save SharkyRawr/c3461ef3ae30ba638fa34a8c93244df6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('CACHE_ENABLED', true); | |
define('CACHE_TTL', 60*60); // 1 minute | |
define('DEBUG', FALSE); | |
// NEEDS PATH_INFO env var !!! | |
// nginx: fastcgi_param PATH_INFO $fastcgi_path_info; | |
$fr = $_SERVER['PATH_INFO']; | |
if (DEBUG) | |
{ | |
error_reporting(E_ALL); | |
ini_set("log_errors", 1); | |
ini_set("error_log", "fixerrors.txt"); | |
} | |
function pre_r($var, $return = false) { | |
$t = '<pre>'.print_r($var, true).'</pre>'; | |
if ($return) return $t; | |
echo $t; | |
} | |
function redir($to) | |
{ | |
//header('Location: '.$fixedpath, true, 301); | |
// LightSpeed webserver | |
//header('X-LiteSpeed-Location: '.$to); | |
// nginx | |
header('X-Accel-Redirect: '.$to); | |
header('Content-Type: application/octet-stream'); | |
} | |
function dbg(...$msgs) { | |
if (DEBUG) { | |
echo implode(' ', $msgs).'<br>'; | |
} | |
} | |
function cached_scandir($dir) { | |
if(!CACHE_ENABLED) return scandir(dir); | |
$exists = false; | |
$key = strtolower($dir); | |
$result = json_decode(apcu_fetch($key, $exists)); | |
if($exists === TRUE) { | |
dbg('cached_scandir', $key, 'HIT:', pre_r($result, true)); | |
return $result; | |
} | |
dbg('cached_scandir MISS'); | |
$result = scandir($dir); | |
apcu_store($key, json_encode($result), CACHE_TTL); | |
return $result; | |
} | |
dbg($fr); | |
$parts = explode('/', $fr); | |
array_shift($parts); | |
if (file_exists(implode('/', $parts))) { | |
if (DEBUG) | |
{ | |
echo 'redirect: '.$fr; | |
} else { | |
redir($fr); | |
} | |
die(); | |
} | |
dbg(pre_r($parts)); | |
$fixedpath = ""; | |
$parent = ""; | |
for($i = 0; $i < count($parts); $i++) | |
{ | |
$p = $parts[$i]; | |
$parent = $fixedpath; | |
if(file_exists($fixedpath.$p) === TRUE) | |
{ | |
dbg($fixedpath.$p.' exists, continue...<br>'); | |
$fixedpath .= $p; | |
if(is_dir($fixedpath)) | |
{ | |
$fixedpath .= '/'; | |
} | |
continue; | |
} | |
// Check for different case on filesystem | |
$files = cached_scandir($parent); | |
dbg($i.' - Scanning: '.$parent.'<br>'); | |
$found = false; | |
foreach($files as $f) | |
{ | |
if ($f == '.' || $f == '..') continue; | |
if(strcasecmp($f, $p) === 0) { | |
// FOUND! | |
$found = true; | |
$fixedpath .= $f; | |
if(is_dir($fixedpath)) | |
{ | |
$fixedpath .= '/'; | |
} | |
break; | |
} | |
} | |
if($found === FALSE) break; | |
} | |
dbg('<hr>$found:', $found); | |
if (is_dir($fixedpath) === TRUE) | |
{ | |
// We are resolving files, not directories... this could be prettier! | |
dbg('$fixedpath is a directory, failing on purpose'); | |
$found = false; | |
} | |
dbg('resolved $fixedpath to:', var_export($fixedpath, true)); | |
if($found === FALSE || file_exists($fixedpath) === FALSE) | |
{ | |
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404); | |
die('404 Not Found'); | |
} | |
if (DEBUG) | |
{ | |
echo 'redirect: '.$fixedpath; | |
} else { | |
redir('/'.$fixedpath); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment