Skip to content

Commit

Permalink
MAGETWO-71529: Improvements to the phpserver router #10500
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Korshenko authored Oct 18, 2017
2 parents 0ced2c2 + 5fed230 commit 2a77940
Showing 1 changed file with 45 additions and 27 deletions.
72 changes: 45 additions & 27 deletions phpserver/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,74 +28,92 @@
$val = json_encode($val);
}

echo 'debug: ' . $val . PHP_EOL . '<br/>' . PHP_EOL;
error_log('debug: '.$val);
};

/**
* Note: the code below is experimental and not intended to be used outside development environment.
* The code is protected against running outside of PHP built-in web server.
* Caution, this is very experimental stuff
* no guarantee for working result
* has tons of potential big security holes
*/

if (php_sapi_name() === 'cli-server') {
$debug($_SERVER["REQUEST_URI"]);
$debug("URI: {$_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);
$pathinfo = pathinfo($route);
$ext = isset($pathinfo['extension']) ? $pathinfo['extension'] : '';

if ($path["basename"] == 'favicon.ico') {
return false;
}

$debug($route);
$debug(strpos($route, 'errors/default/css/'));
$debug("route: $route");

if (strpos($route, 'pub/errors/default/') === 0) {
$route = preg_replace('#pub/errors/default/#', 'errors/default/', $route, 1);
}

$debug($route);

if (strpos($route, 'static/version') === 0) {
$redirectRoute = preg_replace("/version\d+\//", "", $route, 1);
$redirectDebugInfo = "redirect static version string to: " . $redirectRoute;
$debug($redirectDebugInfo);
header('Location: /' . $redirectRoute);
exit;
}
$magentoPackagePubDir = __DIR__."/../pub";

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";
$origFile = $magentoPackagePubDir.'/'.$route;

if (strpos($route, 'static/version') === 0) {
$route = preg_replace('#static/(version\d+/)?#', 'static/', $route, 1);
}
$file = $magentoPackagePubDir.'/'.$route;

$file = $magentoPackagePubDir . '/' . $route;
$debug($file);
if (file_exists($file)) {
$debug("file: $file");

if (file_exists($origFile)) {
$debug('file exists');
return false;
} else if (file_exists($file)) {
$mimeTypes = [
'css' => 'text/css',
'js' => 'application/javascript',
'jpg' => 'image/jpg',
'png' => 'image/png',
'gif' => 'image/gif',
'svg' => 'image/svg+xml',
'map' => 'application/json',
'woff' => 'application/x-woff',
'woff2' => 'application/font-woff2',
'html' => 'text/html',
];

$type = isset($mimeTypes[$ext]) && $mimeTypes[$ext];
if ($type) {
header("Content-Type: $type");
}
readfile($file);
return;
} else {
$debug('file does not exist');
if (strpos($route, 'static/') === 0) {
$route = preg_replace('#static/#', '', $route, 1);
$_GET['resource'] = $route;
include $magentoPackagePubDir . '/static.php';
$debug("static: $route");
include($magentoPackagePubDir.'/static.php');
exit;
} elseif (strpos($route, 'media/') === 0) {
include $magentoPackagePubDir . '/get.php';
$debug("media: $route");
include($magentoPackagePubDir.'/get.php');
exit;
}
}
} else {
$debug("thunk to index in $route");
include($magentoPackagePubDir.'/index.php');
}

header('HTTP/1.0 404 Not Found');
}

0 comments on commit 2a77940

Please sign in to comment.