| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- if(PHP_SAPI !== "cli-server") {
- header("Location: /");
- exit();
- }
- error_reporting(E_ALL & ~E_DEPRECATED);
- $name = basename(__FILE__);
- $uri = $_SERVER["REQUEST_URI"];
- $uri_parts = explode("?", $uri);
- $_REQUEST["path"] = $uri_parts[0];
- chdir(__DIR__);
- $file_path = realpath(ltrim($uri_parts[0], '/'));
- error_log($file_path);
- if($file_path && is_dir($file_path)) {
- // attempt to find an index file
- foreach(['index.php', 'index.html'] as $index_file) {
- if($file_path = realpath($file_path . DIRECTORY_SEPARATOR . $index_file)) {
- break;
- }
- }
- }
- if($file_path && is_file($file_path)) {
- // 1. check that file is not outside of this directory for security
- // 2. check for circular reference to self
- // 3. don't serve dotfiles
- $is_current_directory = strpos($file_path, __DIR__ . DIRECTORY_SEPARATOR) === 0;
- $is_this_file = $file_path == __FILE__;
- $is_dot_file = substr($name, 0, 1) == '.';
- if($is_current_directory && !$is_this_file && !$is_dot_file) {
- $extension = strtolower(substr($file_path, -4));
- if($extension === '.php') {
- // php file; serve through interpreter
- include $file_path;
- }
- else {
- if($extension === ".css") {
- header("Content-Type: text/css");
- }
- if($extension === ".js") {
- header("Content-Type: text/javascript");
- }
- // asset file; serve from filesystem
- readfile($file_path);
- }
- }
- else {
- // disallowed file
- header("HTTP/1.1 404 Not Found");
- echo "404 Not Found";
- }
- }
- else {
- // rewrite to our index file
- include __DIR__ . DIRECTORY_SEPARATOR . 'index.php';
- }
|