ext.php 3.0 KB

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