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