|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * this is a router file for the php Built-in web server |
| 4 | + * https://secure.php.net/manual/en/features.commandline.webserver.php |
| 5 | + * |
| 6 | + * It provides the same "rewrites" as the .htaccess for apache, |
| 7 | + * or the nginx.conf.sample for nginx. |
| 8 | + * |
| 9 | + * example usage: php -S 127.0.0.41:8082 -t ./pub/ ./router.php |
| 10 | + */ |
| 11 | + |
| 12 | +$debug = function($val){ |
| 13 | + return; |
| 14 | + if(is_array($val)){ |
| 15 | + $val = json_encode($val); |
| 16 | + } |
| 17 | + echo 'debug: '.$val.PHP_EOL.'<br/>'.PHP_EOL; |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * Caution, this is very experimental stuff |
| 22 | + * no garant for working result |
| 23 | + * has tons of potencial big security holes |
| 24 | + */ |
| 25 | + |
| 26 | +if (php_sapi_name() === 'cli-server') { |
| 27 | + |
| 28 | + $debug($_SERVER["REQUEST_URI"]); |
| 29 | + if (preg_match('/^\/(index|get|static)\.php(\/)?/', $_SERVER["REQUEST_URI"])) { |
| 30 | + return false; // serve the requested resource as-is. |
| 31 | + } |
| 32 | + |
| 33 | + $path = pathinfo($_SERVER["SCRIPT_FILENAME"]); |
| 34 | + $url = pathinfo(substr($_SERVER["REQUEST_URI"], 1)); |
| 35 | + $route = parse_url(substr($_SERVER["REQUEST_URI"], 1))["path"]; |
| 36 | + |
| 37 | + $debug($path); |
| 38 | + $debug($route); |
| 39 | + |
| 40 | + if( $path["basename"] == 'favicon.ico' ){ |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + $debug($route); |
| 45 | + $debug(strpos($route, 'errors/default/css/')); |
| 46 | + |
| 47 | + if(strpos($route, 'pub/errors/default/') === 0){ |
| 48 | + $route = preg_replace('#pub/errors/default/#', 'errors/default/', $route, 1); |
| 49 | + } |
| 50 | + |
| 51 | + $debug($route); |
| 52 | + |
| 53 | + |
| 54 | + if( |
| 55 | + strpos($route, 'media/') === 0 || |
| 56 | + strpos($route, 'opt/') === 0 || |
| 57 | + strpos($route, 'static/') === 0 || |
| 58 | + strpos($route, 'errors/default/css/') === 0 || |
| 59 | + strpos($route, 'errors/default/images/') === 0 |
| 60 | + ){ |
| 61 | + $magentoPackagePubDir = __DIR__."/../pub"; |
| 62 | + |
| 63 | + $file = $magentoPackagePubDir.'/'.$route; |
| 64 | + $debug($file); |
| 65 | + if(file_exists($file)){ |
| 66 | + $debug('file exists'); |
| 67 | + return false; |
| 68 | + }else{ |
| 69 | + $debug('file exists not'); |
| 70 | + if(strpos($route, 'static/') === 0){ |
| 71 | + $route = preg_replace('#static/#', '', $route, 1); |
| 72 | + $_GET['resource'] = $route; |
| 73 | + include($magentoPackagePubDir.'/static.php'); |
| 74 | + exit; |
| 75 | + }elseif(strpos($route, 'media/') === 0){ |
| 76 | + include($magentoPackagePubDir.'/get.php'); |
| 77 | + exit; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +header('HTTP/1.0 404 Not Found'); |
| 83 | + |
| 84 | +} |
0 commit comments