ext.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. $user = realpath(USERS_PATH . '/' . $file_name);
  30. if (false !== $user) {
  31. return $user;
  32. }
  33. return '';
  34. }
  35. function is_valid_path_extension($path, $extensionPath, $isStatic = true) {
  36. // It must be under the extension path.
  37. $real_ext_path = realpath($extensionPath);
  38. //Windows compatibility
  39. $real_ext_path = str_replace('\\', '/', $real_ext_path);
  40. $path = str_replace('\\', '/', $path);
  41. $in_ext_path = (substr($path, 0, strlen($real_ext_path)) === $real_ext_path);
  42. if (!$in_ext_path) {
  43. return false;
  44. }
  45. // User files do not need further validations
  46. if (!$isStatic) {
  47. return true;
  48. }
  49. // Static files to serve must be under a `ext_dir/static/` directory.
  50. $path_relative_to_ext = substr($path, strlen($real_ext_path) + 1);
  51. list(,$static,$file) = sscanf($path_relative_to_ext, '%[^/]/%[^/]/%s');
  52. if (null === $file || 'static' !== $static) {
  53. return false;
  54. }
  55. return true;
  56. }
  57. /**
  58. * Check if a file can be served by ext.php. A valid file is under a
  59. * CORE_EXTENSIONS_PATH/extension_name/static/ or THIRDPARTY_EXTENSIONS_PATH/extension_name/static/ directory.
  60. *
  61. * You should sanitize path by using the realpath() function.
  62. *
  63. * @param string $path the path to the file we want to serve.
  64. * @return bool true if it can be served, false otherwise.
  65. *
  66. */
  67. function is_valid_path($path) {
  68. return is_valid_path_extension($path, CORE_EXTENSIONS_PATH) || is_valid_path_extension($path, THIRDPARTY_EXTENSIONS_PATH)
  69. || is_valid_path_extension($path, USERS_PATH, false);
  70. }
  71. function sendBadRequestResponse(string $message = null) {
  72. header('HTTP/1.1 400 Bad Request');
  73. die($message);
  74. }
  75. function sendNotFoundResponse() {
  76. header('HTTP/1.1 404 Not Found');
  77. die();
  78. }
  79. if (!isset($_GET['f']) ||
  80. !isset($_GET['t'])) {
  81. sendBadRequestResponse('Query string is incomplete.');
  82. }
  83. $file_name = urldecode($_GET['f']);
  84. $file_type = $_GET['t'];
  85. if (empty(SUPPORTED_TYPES[$file_type])) {
  86. sendBadRequestResponse('File type is not supported.');
  87. }
  88. $absolute_filename = get_absolute_filename($file_name);
  89. if (!is_valid_path($absolute_filename)) {
  90. sendBadRequestResponse('File is not supported.');
  91. }
  92. $content_type = SUPPORTED_TYPES[$file_type];
  93. header("Content-Type: {$content_type}");
  94. header("Content-Disposition: inline; filename='{$file_name}'");
  95. $mtime = @filemtime($absolute_filename);
  96. if ($mtime === false) {
  97. sendNotFoundResponse();
  98. }
  99. require(LIB_PATH . '/http-conditional.php');
  100. if (!httpConditional($mtime, 604800, 2)) {
  101. readfile($absolute_filename);
  102. }