ext.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. function get_absolute_filename(string $file_name): string {
  14. $core_extension = realpath(CORE_EXTENSIONS_PATH . '/' . $file_name);
  15. if (false !== $core_extension) {
  16. return $core_extension;
  17. }
  18. $extension = realpath(EXTENSIONS_PATH . '/' . $file_name);
  19. if (false !== $extension) {
  20. return $extension;
  21. }
  22. $third_party_extension = realpath(THIRDPARTY_EXTENSIONS_PATH . '/' . $file_name);
  23. if (false !== $third_party_extension) {
  24. return $third_party_extension;
  25. }
  26. $user = realpath(USERS_PATH . '/' . $file_name);
  27. if (false !== $user) {
  28. return $user;
  29. }
  30. return '';
  31. }
  32. function is_valid_path_extension(string $path, string $extensionPath, bool $isStatic = true): bool {
  33. // It must be under the extension path.
  34. $real_ext_path = realpath($extensionPath);
  35. if ($real_ext_path == false) {
  36. return false;
  37. }
  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') ?? [null, null, null];
  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(string $path): bool {
  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. /** @return never */
  72. function sendBadRequestResponse(string $message = null) {
  73. header('HTTP/1.1 400 Bad Request');
  74. die($message);
  75. }
  76. /** @return never */
  77. function sendNotFoundResponse() {
  78. header('HTTP/1.1 404 Not Found');
  79. die();
  80. }
  81. if (!isset($_GET['f']) ||
  82. !isset($_GET['t'])) {
  83. sendBadRequestResponse('Query string is incomplete.');
  84. }
  85. $file_name = urldecode($_GET['f']);
  86. $file_type = $_GET['t'];
  87. if (empty(SUPPORTED_TYPES[$file_type]) ||
  88. empty(SUPPORTED_TYPES[pathinfo($file_name, PATHINFO_EXTENSION)])) {
  89. sendBadRequestResponse('File type is not supported.');
  90. }
  91. $absolute_filename = get_absolute_filename($file_name);
  92. if (!is_valid_path($absolute_filename)) {
  93. sendBadRequestResponse('File is not supported.');
  94. }
  95. $content_type = SUPPORTED_TYPES[$file_type];
  96. header("Content-Type: {$content_type}");
  97. header("Content-Disposition: inline; filename='{$file_name}'");
  98. $mtime = @filemtime($absolute_filename);
  99. if ($mtime === false) {
  100. sendNotFoundResponse();
  101. }
  102. require(LIB_PATH . '/http-conditional.php');
  103. if (!httpConditional($mtime, 604800, 2)) {
  104. readfile($absolute_filename);
  105. }