ext.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 is_valid_path_extension($path, $extensionPath) {
  14. // It must be under the extension path.
  15. $real_ext_path = realpath($extensionPath);
  16. //Windows compatibility
  17. $real_ext_path = str_replace('\\', '/', $real_ext_path);
  18. $path = str_replace('\\', '/', $path);
  19. $in_ext_path = (substr($path, 0, strlen($real_ext_path)) === $real_ext_path);
  20. if (!$in_ext_path) {
  21. return false;
  22. }
  23. // File to serve must be under a `ext_dir/static/` directory.
  24. $path_relative_to_ext = substr($path, strlen($real_ext_path) + 1);
  25. list(,$static,$file) = sscanf($path_relative_to_ext, '%[^/]/%[^/]/%s');
  26. if (null === $file || 'static' !== $static) {
  27. return false;
  28. }
  29. return true;
  30. }
  31. /**
  32. * Check if a file can be served by ext.php. A valid file is under a
  33. * CORE_EXTENSIONS_PATH/extension_name/static/ or THIRDPARTY_EXTENSIONS_PATH/extension_name/static/ directory.
  34. *
  35. * You should sanitize path by using the realpath() function.
  36. *
  37. * @param $path the path to the file we want to serve.
  38. * @return true if it can be served, false otherwise.
  39. *
  40. */
  41. function is_valid_path($path) {
  42. return is_valid_path_extension($path, CORE_EXTENSIONS_PATH) || is_valid_path_extension($path, THIRDPARTY_EXTENSIONS_PATH);
  43. }
  44. function sendBadRequestResponse(string $message = null) {
  45. header('HTTP/1.1 400 Bad Request');
  46. die($message);
  47. }
  48. function sendNotFoundResponse() {
  49. header('HTTP/1.1 404 Not Found');
  50. die();
  51. }
  52. if (!isset($_GET['f']) ||
  53. !isset($_GET['t'])) {
  54. sendBadRequestResponse('Query string is incomplete.');
  55. }
  56. $file_name = urldecode($_GET['f']);
  57. $file_type = $_GET['t'];
  58. if (empty(SUPPORTED_TYPES[$file_type])) {
  59. sendBadRequestResponse('File type is not supported.');
  60. }
  61. $absolute_filename = realpath(EXTENSIONS_PATH . '/' . $file_name);
  62. if (!is_valid_path($absolute_filename)) {
  63. sendBadRequestResponse('File is not supported.');
  64. }
  65. $content_type = SUPPORTED_TYPES[$file_type];
  66. header("Content-Type: {$content_type}");
  67. header("Content-Disposition: inline; filename='{$file_name}'");
  68. $mtime = @filemtime($absolute_filename);
  69. if ($mtime === false) {
  70. sendNotFoundResponse();
  71. }
  72. require(LIB_PATH . '/http-conditional.php');
  73. if (!httpConditional($mtime, 604800, 2)) {
  74. readfile($absolute_filename);
  75. }