router.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * this is a router file for the php Built-in web server
  8. * https://secure.php.net/manual/en/features.commandline.webserver.php
  9. *
  10. * It provides the same "rewrites" as the .htaccess for apache,
  11. * or the nginx.conf.sample for nginx.
  12. *
  13. * example usage: php -S 127.0.0.41:8082 -t ./pub/ ./router.php
  14. */
  15. /**
  16. * Set it to true to enable debug mode
  17. */
  18. define('DEBUG_ROUTER', false);
  19. $debug = function ($val) {
  20. if (!DEBUG_ROUTER) {
  21. return;
  22. }
  23. if (is_array($val)) {
  24. $val = json_encode($val);
  25. }
  26. error_log('debug: '.$val);
  27. };
  28. /**
  29. * Caution, this is very experimental stuff
  30. * no guarantee for working result
  31. * has tons of potential big security holes
  32. */
  33. if (php_sapi_name() === 'cli-server') {
  34. $debug("URI: {$_SERVER["REQUEST_URI"]}");
  35. if (preg_match('/^\/(index|get|static)\.php(\/)?/', $_SERVER["REQUEST_URI"])) {
  36. return false; // serve the requested resource as-is.
  37. }
  38. $path = pathinfo($_SERVER["SCRIPT_FILENAME"]);
  39. $url = pathinfo(substr($_SERVER["REQUEST_URI"], 1));
  40. $route = parse_url(substr($_SERVER["REQUEST_URI"], 1))["path"];
  41. $pathinfo = pathinfo($route);
  42. $ext = isset($pathinfo['extension']) ? $pathinfo['extension'] : '';
  43. if ($path["basename"] == 'favicon.ico') {
  44. return false;
  45. }
  46. $debug("route: $route");
  47. if (strpos($route, 'pub/errors/default/') === 0) {
  48. $route = preg_replace('#pub/errors/default/#', 'errors/default/', $route, 1);
  49. }
  50. $magentoPackagePubDir = __DIR__."/../pub";
  51. if (strpos($route, 'media/') === 0 ||
  52. strpos($route, 'opt/') === 0 ||
  53. strpos($route, 'static/') === 0 ||
  54. strpos($route, 'errors/default/css/') === 0 ||
  55. strpos($route, 'errors/default/images/') === 0
  56. ) {
  57. $origFile = $magentoPackagePubDir.'/'.$route;
  58. if (strpos($route, 'static/version') === 0) {
  59. $route = preg_replace('#static/(version\d+/)?#', 'static/', $route, 1);
  60. }
  61. $file = $magentoPackagePubDir.'/'.$route;
  62. $debug("file: $file");
  63. if (file_exists($origFile) || file_exists($file)) {
  64. if (file_exists($origFile)) {
  65. $file = $origFile;
  66. }
  67. $debug('file exists');
  68. $mimeTypes = [
  69. 'css' => 'text/css',
  70. 'js' => 'application/javascript',
  71. 'jpg' => 'image/jpg',
  72. 'png' => 'image/png',
  73. 'gif' => 'image/gif',
  74. 'svg' => 'image/svg+xml',
  75. 'map' => 'application/json',
  76. 'woff' => 'application/x-woff',
  77. 'woff2' => 'application/font-woff2',
  78. 'html' => 'text/html',
  79. ];
  80. if (isset($mimeTypes[$ext])) {
  81. header("Content-Type: $mimeTypes[$ext]");
  82. }
  83. readfile($file);
  84. return;
  85. } else {
  86. $debug('file does not exist');
  87. if (strpos($route, 'static/') === 0) {
  88. $route = preg_replace('#static/#', '', $route, 1);
  89. $_GET['resource'] = $route;
  90. $debug("static: $route");
  91. include($magentoPackagePubDir.'/static.php');
  92. exit;
  93. } elseif (strpos($route, 'media/') === 0) {
  94. $debug("media: $route");
  95. include($magentoPackagePubDir.'/get.php');
  96. exit;
  97. }
  98. }
  99. } else {
  100. $debug("thunk to index in $route");
  101. include($magentoPackagePubDir.'/index.php');
  102. }
  103. }