ext.php 3.5 KB

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