ext.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. if (!isset($_GET['f']) ||
  3. !isset($_GET['t'])) {
  4. header('HTTP/1.1 400 Bad Request');
  5. die();
  6. }
  7. require(__DIR__ . '/../constants.php');
  8. function is_valid_path_extension($path, $extensionPath) {
  9. // It must be under the extension path.
  10. $real_ext_path = realpath($extensionPath);
  11. //Windows compatibility
  12. $real_ext_path = str_replace('\\', '/', $real_ext_path);
  13. $path = str_replace('\\', '/', $path);
  14. $in_ext_path = (substr($path, 0, strlen($real_ext_path)) === $real_ext_path);
  15. if (!$in_ext_path) {
  16. return false;
  17. }
  18. // File to serve must be under a `ext_dir/static/` directory.
  19. $path_relative_to_ext = substr($path, strlen($real_ext_path) + 1);
  20. $path_splitted = explode('/', $path_relative_to_ext);
  21. if (count($path_splitted) < 3 || $path_splitted[1] !== 'static') {
  22. return false;
  23. }
  24. return true;
  25. }
  26. /**
  27. * Check if a file can be served by ext.php. A valid file is under a
  28. * CORE_EXTENSIONS_PATH/extension_name/static/ or THIRDPARTY_EXTENSIONS_PATH/extension_name/static/ directory.
  29. *
  30. * You should sanitize path by using the realpath() function.
  31. *
  32. * @param $path the path to the file we want to serve.
  33. * @return true if it can be served, false otherwise.
  34. *
  35. */
  36. function is_valid_path($path) {
  37. return is_valid_path_extension($path, CORE_EXTENSIONS_PATH) || is_valid_path_extension($path, THIRDPARTY_EXTENSIONS_PATH);
  38. }
  39. $file_name = urldecode($_GET['f']);
  40. $file_type = $_GET['t'];
  41. $absolute_filename = realpath(EXTENSIONS_PATH . '/' . $file_name);
  42. if (!is_valid_path($absolute_filename)) {
  43. header('HTTP/1.1 400 Bad Request');
  44. die();
  45. }
  46. switch ($file_type) {
  47. case 'css':
  48. header('Content-Type: text/css; charset=UTF-8');
  49. header('Content-Disposition: inline; filename="' . $file_name . '"');
  50. break;
  51. case 'js':
  52. header('Content-Type: application/javascript; charset=UTF-8');
  53. header('Content-Disposition: inline; filename="' . $file_name . '"');
  54. break;
  55. case 'png':
  56. header('Content-Type: image/png');
  57. header('Content-Disposition: inline; filename="' . $file_name . '"');
  58. break;
  59. case 'jpeg':
  60. case 'jpg':
  61. header('Content-Type: image/jpeg');
  62. header('Content-Disposition: inline; filename="' . $file_name . '"');
  63. break;
  64. case 'gif':
  65. header('Content-Type: image/gif');
  66. header('Content-Disposition: inline; filename="' . $file_name . '"');
  67. break;
  68. case 'svg':
  69. header('Content-Type: image/svg+xml');
  70. header('Content-Disposition: inline; filename="' . $file_name . '"');
  71. break;
  72. default:
  73. header('HTTP/1.1 400 Bad Request');
  74. die();
  75. }
  76. $mtime = @filemtime($absolute_filename);
  77. if ($mtime === false) {
  78. header('HTTP/1.1 404 Not Found');
  79. die();
  80. }
  81. require(LIB_PATH . '/http-conditional.php');
  82. if (!httpConditional($mtime, 604800, 2)) {
  83. readfile($absolute_filename);
  84. }