annotate .ht.router.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Router script for the built-in PHP web server.
Chris@0 6 *
Chris@0 7 * The built-in web server should only be used for development and testing as it
Chris@0 8 * has a number of limitations that makes running Drupal on it highly insecure
Chris@0 9 * and somewhat limited.
Chris@0 10 *
Chris@0 11 * Note that:
Chris@0 12 * - The server is single-threaded, any requests made during the execution of
Chris@0 13 * the main request will hang until the main request has been completed.
Chris@0 14 * - The web server does not enforce any of the settings in .htaccess in
Chris@0 15 * particular a remote user will be able to download files that normally would
Chris@0 16 * be protected from direct access such as .module files.
Chris@0 17 *
Chris@0 18 * The router script is needed to work around a bug in PHP, see
Chris@0 19 * https://bugs.php.net/bug.php?id=61286.
Chris@0 20 *
Chris@0 21 * Usage:
Chris@0 22 * php -S localhost:8888 .ht.router.php
Chris@0 23 *
Chris@0 24 * @see http://php.net/manual/en/features.commandline.webserver.php
Chris@0 25 */
Chris@0 26
Chris@0 27 $url = parse_url($_SERVER['REQUEST_URI']);
Chris@0 28 if (file_exists(__DIR__ . $url['path'])) {
Chris@0 29 // Serve the requested resource as-is.
Chris@0 30 return FALSE;
Chris@0 31 }
Chris@0 32
Chris@0 33 // Work around the PHP bug.
Chris@0 34 $path = $url['path'];
Chris@0 35 $script = 'index.php';
Chris@0 36 if (strpos($path, '.php') !== FALSE) {
Chris@0 37 // Work backwards through the path to check if a script exists. Otherwise
Chris@0 38 // fallback to index.php.
Chris@0 39 do {
Chris@0 40 $path = dirname($path);
Chris@0 41 if (preg_match('/\.php$/', $path) && is_file(__DIR__ . $path)) {
Chris@0 42 // Discovered that the path contains an existing PHP file. Use that as the
Chris@0 43 // script to include.
Chris@0 44 $script = ltrim($path, '/');
Chris@0 45 break;
Chris@0 46 }
Chris@0 47 } while ($path !== '/' && $path !== '.');
Chris@0 48 }
Chris@0 49
Chris@0 50 // Update $_SERVER variables to point to the correct index-file.
Chris@0 51 $index_file_absolute = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $script;
Chris@0 52 $index_file_relative = DIRECTORY_SEPARATOR . $script;
Chris@0 53
Chris@0 54 // SCRIPT_FILENAME will point to the router script itself, it should point to
Chris@0 55 // the full path of index.php.
Chris@0 56 $_SERVER['SCRIPT_FILENAME'] = $index_file_absolute;
Chris@0 57
Chris@0 58 // SCRIPT_NAME and PHP_SELF will either point to index.php or contain the full
Chris@0 59 // virtual path being requested depending on the URL being requested. They
Chris@0 60 // should always point to index.php relative to document root.
Chris@0 61 $_SERVER['SCRIPT_NAME'] = $index_file_relative;
Chris@0 62 $_SERVER['PHP_SELF'] = $index_file_relative;
Chris@0 63
Chris@0 64 // Require the script and let core take over.
Chris@0 65 require $_SERVER['SCRIPT_FILENAME'];