$part = strtok($uri['input'], '/'); // $uri['input'] == resources/locale/messages.php.
if (in_array($part, array('resources','plugins','cache','skin','attach','thumbnail'))) { //限制了读取目录 无法直接读根目录
$part = ltrim(rtrim($part == 'thumbnail' ?
preg_replace('/thumbnail/', 'cache/thumbnail', $uri['input'], 1) :
$uri['input']), '/');
$part = (($qpos = strpos($part, '?')) !== false) ? substr($part, 0, $qpos) : $part;
if(file_exists($part)) {
require_once ROOT.'/library/function/file.php';
dumpWithEtag($part); //读取
exit;
} else {
header("HTTP/1.0 404 Not Found");exit;
}
}
function dumpWithEtag($path) {
$path = urldecode($path);
$qIndex = strpos($path,'?');
if( $qIndex !== false ) {
$path = substr($path,0,$qIndex);
}
/* I think, it is a bad idea to check '..' and skip.
but this is an annoyance to solve gracefully about whole HTTP request */
/* Kill them all requests with referencing parent directory */
if( strpos( $path, "/.." ) !== false ||
strpos( $path, "\\.." ) !== false || //卡在此处
strcasecmp( substr( $path, -3 ), "php" ) == 0 ||
!file_exists( $path ) ) {
header("HTTP/1.0 404 Not found");
exit;
}
$fs = stat( $path );
if( !$fs || !$fs['size'] ) { header('HTTP/1.1 404 Not Found');exit; }
$etag = sprintf( "textcube-%x", (0x1234*$fs['size'])^$fs['mtime'] );
$lastmodified = gmdate("D, j M Y H:i:s ", $fs['mtime']) . "GMT";
$length = $fs['size'];
if( !headerEtag($etag,$length,$lastmodified) ) {
//echo $path;
//exit;
header('Content-type: '.getMIMEType(null,$path));
$f = fopen($path,"r");
if( !$f ) {
header("HTTP/1.0 404 Not found");
exit;
}
while( ($content=fread($f,8192)) ){
echo $content;
}
fclose($f);
}
}