ext.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. if (!isset($_GET['f']) ||
  3. !isset($_GET['t'])) {
  4. header('HTTP/1.1 400 Bad Request');
  5. die();
  6. }
  7. require(__DIR__ . '/../constants.php');
  8. /**
  9. * Check if a file can be served by ext.php. A valid file is under a
  10. * EXTENSIONS_PATH/extension_name/static/ directory.
  11. *
  12. * You should sanitize path by using the realpath() function.
  13. *
  14. * @param $path the path to the file we want to serve.
  15. * @return true if it can be served, false else.
  16. *
  17. */
  18. function is_valid_path($path) {
  19. // It must be under the extension path.
  20. $real_ext_path = realpath(EXTENSIONS_PATH);
  21. //Windows compatibility
  22. $real_ext_path = str_replace('\\', '/', $real_ext_path);
  23. $path = str_replace('\\', '/', $path);
  24. $in_ext_path = (substr($path, 0, strlen($real_ext_path)) === $real_ext_path);
  25. if (!$in_ext_path) {
  26. return false;
  27. }
  28. // File to serve must be under a `ext_dir/static/` directory.
  29. $path_relative_to_ext = substr($path, strlen($real_ext_path) + 1);
  30. $path_splitted = explode('/', $path_relative_to_ext);
  31. if (count($path_splitted) < 3 || $path_splitted[1] !== 'static') {
  32. return false;
  33. }
  34. return true;
  35. }
  36. $file_name = urldecode($_GET['f']);
  37. $file_type = $_GET['t'];
  38. $absolute_filename = realpath(EXTENSIONS_PATH . '/' . $file_name);
  39. if (!is_valid_path($absolute_filename)) {
  40. header('HTTP/1.1 400 Bad Request');
  41. die();
  42. }
  43. switch ($file_type) {
  44. case 'css':
  45. header('Content-Type: text/css; charset=UTF-8');
  46. header('Content-Disposition: inline; filename="' . $file_name . '"');
  47. break;
  48. case 'js':
  49. header('Content-Type: application/javascript; charset=UTF-8');
  50. header('Content-Disposition: inline; filename="' . $file_name . '"');
  51. break;
  52. case 'png':
  53. header('Content-Type: image/png');
  54. header('Content-Disposition: inline; filename="' . $file_name . '"');
  55. break;
  56. case 'jpeg':
  57. case 'jpg':
  58. header('Content-Type: image/jpeg');
  59. header('Content-Disposition: inline; filename="' . $file_name . '"');
  60. break;
  61. case 'gif':
  62. header('Content-Type: image/gif');
  63. header('Content-Disposition: inline; filename="' . $file_name . '"');
  64. break;
  65. case 'svg':
  66. header('Content-Type: image/svg+xml');
  67. header('Content-Disposition: inline; filename="' . $file_name . '"');
  68. break;
  69. default:
  70. header('HTTP/1.1 400 Bad Request');
  71. die();
  72. }
  73. $mtime = @filemtime($absolute_filename);
  74. if ($mtime === false) {
  75. header('HTTP/1.1 404 Not Found');
  76. die();
  77. }
  78. require(LIB_PATH . '/http-conditional.php');
  79. if (!httpConditional($mtime, 604800, 2)) {
  80. readfile($absolute_filename);
  81. }