From 50db025266759ae4f3e3041d89d52ab5322e809f Mon Sep 17 00:00:00 2001 From: Daniel Fahlke Date: Mon, 20 Jul 2015 00:41:22 +0200 Subject: [PATCH] add router.php for php Built-in webserver --- phpserver/.htaccess | 2 ++ phpserver/README.md | 35 ++++++++++++++++++ phpserver/router.php | 84 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 phpserver/.htaccess create mode 100644 phpserver/README.md create mode 100644 phpserver/router.php diff --git a/phpserver/.htaccess b/phpserver/.htaccess new file mode 100644 index 0000000000000..93169e4eb44ff --- /dev/null +++ b/phpserver/.htaccess @@ -0,0 +1,2 @@ +Order deny,allow +Deny from all diff --git a/phpserver/README.md b/phpserver/README.md new file mode 100644 index 0000000000000..06186bf257545 --- /dev/null +++ b/phpserver/README.md @@ -0,0 +1,35 @@ +PHP Built-in webserver +====================== + +php has a Built-in webserver since version 5.4 +https://secure.php.net/manual/en/features.commandline.webserver.php + +As many applications and frameworks rely on rewrites on webserver side, +the same as magento does, it offers an argument for a router script. +Either the script returns false which means, it should try to deliver the file +as usual via file system lookup, or it executes the specific php scripts via include. + +Example: +requests to `/static/frontend/Magento/blank/en_US/mage/calendar.css` should deliver the file if it exists, or execute `/static.php` if not. + +Without a router script, that is not possible via the php built-in server. + +### How to use it + +example usage: ```php -S 127.0.0.41:8082 -t ./pub/ ./phpserver/router.php``` + +### What exactly does the script + +first we have an low level `$debug` closure, for the case you need to debug execution. + +If the requestpath starts with index.php, get.php, static.php, we return to normal request flow. +If we notice a favicon.ico request, the same. + +Then rewrite paths for `pub/errors/default/` by removing the `pub/` part. (was at least needed for older versions) + +Request starting with `media/`, `opt/`, `static/` test if the file exists. +If Yes, then handle it, if not "forward" `static` to `static.php` and `media` to `get.php` + +If non of the rules matched, return 404. +You may instead include the index.php, if 404 should be handled by magento or you want +urls without `index.php/`. diff --git a/phpserver/router.php b/phpserver/router.php new file mode 100644 index 0000000000000..297491de26089 --- /dev/null +++ b/phpserver/router.php @@ -0,0 +1,84 @@ +'.PHP_EOL; +}; + +/** + * Caution, this is very experimental stuff + * no garant for working result + * has tons of potencial big security holes + */ + +if (php_sapi_name() === 'cli-server') { + + $debug($_SERVER["REQUEST_URI"]); + if (preg_match('/^\/(index|get|static)\.php(\/)?/', $_SERVER["REQUEST_URI"])) { + return false; // serve the requested resource as-is. + } + + $path = pathinfo($_SERVER["SCRIPT_FILENAME"]); + $url = pathinfo(substr($_SERVER["REQUEST_URI"], 1)); + $route = parse_url(substr($_SERVER["REQUEST_URI"], 1))["path"]; + + $debug($path); + $debug($route); + + if( $path["basename"] == 'favicon.ico' ){ + return false; + } + + $debug($route); + $debug(strpos($route, 'errors/default/css/')); + + if(strpos($route, 'pub/errors/default/') === 0){ + $route = preg_replace('#pub/errors/default/#', 'errors/default/', $route, 1); + } + + $debug($route); + + + if( + strpos($route, 'media/') === 0 || + strpos($route, 'opt/') === 0 || + strpos($route, 'static/') === 0 || + strpos($route, 'errors/default/css/') === 0 || + strpos($route, 'errors/default/images/') === 0 + ){ + $magentoPackagePubDir = __DIR__."/../pub"; + + $file = $magentoPackagePubDir.'/'.$route; + $debug($file); + if(file_exists($file)){ + $debug('file exists'); + return false; + }else{ + $debug('file exists not'); + if(strpos($route, 'static/') === 0){ + $route = preg_replace('#static/#', '', $route, 1); + $_GET['resource'] = $route; + include($magentoPackagePubDir.'/static.php'); + exit; + }elseif(strpos($route, 'media/') === 0){ + include($magentoPackagePubDir.'/get.php'); + exit; + } + } + } + +header('HTTP/1.0 404 Not Found'); + +}