Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple routing script for built-in php server #1395

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions .ht.router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* This file is part of the Cockpit project.
*
* (c) Artur Heinze - 🅰🅶🅴🅽🆃🅴🅹🅾, http://agentejo.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* Handle php command line webserver (dev-only)
*
* usage: [ php -S localhost:8080 index.php ]
*
* @see http://php.net/manual/en/features.commandline.webserver.php
*/
if (PHP_SAPI == 'cli-server') {

// ----------------------------------------------------------------------
// File access
// ----------------------------------------------------------------------

// Deny access to application and system files from being viewed
if (preg_match('#(composer\.(json|lock)|package\.json|(README|CONTRIBUTING)\.md|cp|Dockerfile|LICENSE|\.(sqlite|sdb|s3db|db|yaml|yml))$#', $_SERVER['REQUEST_URI'])) {
header('HTTP/1.0 403 Forbidden');
exit('HTTP/1.0 403 Forbidden');
}

// ----------------------------------------------------------------------
// MIME Types
// ----------------------------------------------------------------------

/* handle static files (eg. assets/app/css/style.css) */

$file = $_SERVER['SCRIPT_FILENAME'];
$ext = pathinfo($file, PATHINFO_EXTENSION);

// Allow any files or directories that exist to be displayed directly
if (is_file($file)) {

// custom Mime Types
if ($ext == 'tag') {
header('Content-Type: application/javascript');
readfile($file);
exit;
}

// default Mime Types.
if ($ext != 'php') {
return false;
}

}

// ----------------------------------------------------------------------
// Rewrite Engine
// ----------------------------------------------------------------------

/* rewrite other URLs to index.php */

$htdocs = $_SERVER['DOCUMENT_ROOT'];
$path = str_replace('/', DIRECTORY_SEPARATOR, parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$index = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'index.php';
$folder = dirname($index);

if (is_file($htdocs . $path)) {

// Requested URI matches an existing "php" file
$index = $path;

} else {

// Trasverse directories and search for "index.php"
do {

$script = $folder . DIRECTORY_SEPARATOR . 'index.php';

if (is_file($htdocs . $script)) {
$index = $script;
break;
}

$folder = dirname($folder);

} while ($folder !== DIRECTORY_SEPARATOR && $folder !== '.');

}

/* handle php files (eg. install/index.php) */

$index = DIRECTORY_SEPARATOR . ltrim($index, DIRECTORY_SEPARATOR);
$file = $htdocs . $index;

// Update $_SERVER variables to point to the correct index-file.
$_SERVER['SCRIPT_FILENAME'] = $file;
$_SERVER['SCRIPT_NAME'] = $index;
$_SERVER['PHP_SELF'] = $index;

// Fix "dot" routes (see: https://bugs.php.net/bug.php?id=61286)
$_SERVER['PATH_INFO'] = $path;

// Deny access to files and directories whose names begin with a period
if (preg_match('#/\.|^\.(?!well-known/)#', $_SERVER['REQUEST_URI'])) {
header('HTTP/1.0 403 Forbidden');
exit('HTTP/1.0 403 Forbidden');
}

// Allow any "php" files that exist to be displayed directly
if (is_file($file) && dirname($index) != DIRECTORY_SEPARATOR) {
include_once $file;
exit;
}

/* Code execution returns to the main "index.php" file */

}
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
}
],

"scripts": {
"dev": [
"Composer\\Config::disableProcessTimeout",
"php -r \"exec((['Darwin'=>'open', 'WINNT'=>'explorer'][PHP_OS] ?? 'xdg-open').' '.escapeshellarg('http://localhost:8080'));\"",
"php -S localhost:8080 index.php"
]
},

"require": {
"php": "^7.1.3",
"ext-json": "*",
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
date_default_timezone_set('UTC');

// handle php webserver
if (PHP_SAPI == 'cli-server' && is_file(__DIR__.parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
if (PHP_SAPI == 'cli-server' && false===include_once(__DIR__.'/.ht.router.php')) {
return false;
}

Expand Down