|
28 | 28 | $val = json_encode($val);
|
29 | 29 | }
|
30 | 30 |
|
31 |
| - echo 'debug: ' . $val . PHP_EOL . '<br/>' . PHP_EOL; |
| 31 | + error_log('debug: '.$val); |
32 | 32 | };
|
33 | 33 |
|
34 | 34 | /**
|
35 |
| - * Note: the code below is experimental and not intended to be used outside development environment. |
36 |
| - * The code is protected against running outside of PHP built-in web server. |
| 35 | + * Caution, this is very experimental stuff |
| 36 | + * no guarantee for working result |
| 37 | + * has tons of potential big security holes |
37 | 38 | */
|
38 | 39 |
|
39 | 40 | if (php_sapi_name() === 'cli-server') {
|
40 |
| - $debug($_SERVER["REQUEST_URI"]); |
| 41 | + $debug("URI: {$_SERVER["REQUEST_URI"]}"); |
41 | 42 | if (preg_match('/^\/(index|get|static)\.php(\/)?/', $_SERVER["REQUEST_URI"])) {
|
42 | 43 | return false; // serve the requested resource as-is.
|
43 | 44 | }
|
44 | 45 |
|
45 | 46 | $path = pathinfo($_SERVER["SCRIPT_FILENAME"]);
|
46 | 47 | $url = pathinfo(substr($_SERVER["REQUEST_URI"], 1));
|
47 | 48 | $route = parse_url(substr($_SERVER["REQUEST_URI"], 1))["path"];
|
48 |
| - |
49 |
| - $debug($path); |
50 |
| - $debug($route); |
| 49 | + $pathinfo = pathinfo($route); |
| 50 | + $ext = isset($pathinfo['extension']) ? $pathinfo['extension'] : ''; |
51 | 51 |
|
52 | 52 | if ($path["basename"] == 'favicon.ico') {
|
53 | 53 | return false;
|
54 | 54 | }
|
55 | 55 |
|
56 |
| - $debug($route); |
57 |
| - $debug(strpos($route, 'errors/default/css/')); |
| 56 | + $debug("route: $route"); |
58 | 57 |
|
59 | 58 | if (strpos($route, 'pub/errors/default/') === 0) {
|
60 | 59 | $route = preg_replace('#pub/errors/default/#', 'errors/default/', $route, 1);
|
61 | 60 | }
|
62 | 61 |
|
63 |
| - $debug($route); |
64 |
| - |
65 |
| - if (strpos($route, 'static/version') === 0) { |
66 |
| - $redirectRoute = preg_replace("/version\d+\//", "", $route, 1); |
67 |
| - $redirectDebugInfo = "redirect static version string to: " . $redirectRoute; |
68 |
| - $debug($redirectDebugInfo); |
69 |
| - header('Location: /' . $redirectRoute); |
70 |
| - exit; |
71 |
| - } |
| 62 | + $magentoPackagePubDir = __DIR__."/../pub"; |
72 | 63 |
|
73 | 64 | if (strpos($route, 'media/') === 0 ||
|
74 | 65 | strpos($route, 'opt/') === 0 ||
|
75 | 66 | strpos($route, 'static/') === 0 ||
|
76 | 67 | strpos($route, 'errors/default/css/') === 0 ||
|
77 | 68 | strpos($route, 'errors/default/images/') === 0
|
78 | 69 | ) {
|
79 |
| - $magentoPackagePubDir = __DIR__ . "/../pub"; |
| 70 | + $origFile = $magentoPackagePubDir.'/'.$route; |
| 71 | + |
| 72 | + if (strpos($route, 'static/version') === 0) { |
| 73 | + $route = preg_replace('#static/(version\d+/)?#', 'static/', $route, 1); |
| 74 | + } |
| 75 | + $file = $magentoPackagePubDir.'/'.$route; |
80 | 76 |
|
81 |
| - $file = $magentoPackagePubDir . '/' . $route; |
82 |
| - $debug($file); |
83 |
| - if (file_exists($file)) { |
| 77 | + $debug("file: $file"); |
| 78 | + |
| 79 | + if (file_exists($origFile)) { |
84 | 80 | $debug('file exists');
|
85 | 81 | return false;
|
| 82 | + } else if (file_exists($file)) { |
| 83 | + $mimeTypes = [ |
| 84 | + 'css' => 'text/css', |
| 85 | + 'js' => 'application/javascript', |
| 86 | + 'jpg' => 'image/jpg', |
| 87 | + 'png' => 'image/png', |
| 88 | + 'gif' => 'image/gif', |
| 89 | + 'svg' => 'image/svg+xml', |
| 90 | + 'map' => 'application/json', |
| 91 | + 'woff' => 'application/x-woff', |
| 92 | + 'woff2' => 'application/font-woff2', |
| 93 | + 'html' => 'text/html', |
| 94 | + ]; |
| 95 | + |
| 96 | + $type = isset($mimeTypes[$ext]) && $mimeTypes[$ext]; |
| 97 | + if ($type) { |
| 98 | + header("Content-Type: $type"); |
| 99 | + } |
| 100 | + readfile($file); |
| 101 | + return; |
86 | 102 | } else {
|
87 | 103 | $debug('file does not exist');
|
88 | 104 | if (strpos($route, 'static/') === 0) {
|
89 |
| - $route = preg_replace('#static/#', '', $route, 1); |
90 | 105 | $_GET['resource'] = $route;
|
91 |
| - include $magentoPackagePubDir . '/static.php'; |
| 106 | + $debug("static: $route"); |
| 107 | + include($magentoPackagePubDir.'/static.php'); |
92 | 108 | exit;
|
93 | 109 | } elseif (strpos($route, 'media/') === 0) {
|
94 |
| - include $magentoPackagePubDir . '/get.php'; |
| 110 | + $debug("media: $route"); |
| 111 | + include($magentoPackagePubDir.'/get.php'); |
95 | 112 | exit;
|
96 | 113 | }
|
97 | 114 | }
|
| 115 | + } else { |
| 116 | + $debug("thunk to index in $route"); |
| 117 | + include($magentoPackagePubDir.'/index.php'); |
98 | 118 | }
|
99 |
| - |
100 |
| - header('HTTP/1.0 404 Not Found'); |
101 | 119 | }
|
0 commit comments