ext.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. $in_ext_path = (substr($path, 0, strlen($real_ext_path)) === $real_ext_path);
  22. if (!$in_ext_path) {
  23. return false;
  24. }
  25. // File to serve must be under a `ext_dir/static/` directory.
  26. $path_relative_to_ext = substr($path, strlen($real_ext_path) + 1);
  27. $path_splitted = explode('/', $path_relative_to_ext);
  28. if (count($path_splitted) < 3 || $path_splitted[1] !== 'static') {
  29. return false;
  30. }
  31. return true;
  32. }
  33. $file_name = urldecode($_GET['f']);
  34. $file_type = $_GET['t'];
  35. $absolute_filename = realpath(EXTENSIONS_PATH . '/' . $file_name);
  36. if (!is_valid_path($absolute_filename)) {
  37. header('HTTP/1.1 400 Bad Request');
  38. die();
  39. }
  40. switch ($file_type) {
  41. case 'css':
  42. header('Content-Type: text/css; charset=UTF-8');
  43. header('Content-Disposition: inline; filename="' . $file_name . '"');
  44. break;
  45. case 'js':
  46. header('Content-Type: application/javascript; charset=UTF-8');
  47. header('Content-Disposition: inline; filename="' . $file_name . '"');
  48. break;
  49. default:
  50. header('HTTP/1.1 400 Bad Request');
  51. die();
  52. }
  53. $mtime = @filemtime($absolute_filename);
  54. if ($mtime === false) {
  55. header('HTTP/1.1 404 Not Found');
  56. die();
  57. }
  58. require(LIB_PATH . '/http-conditional.php');
  59. if (!httpConditional($mtime, 604800, 2)) {
  60. readfile($absolute_filename);
  61. }