ext.php 1.7 KB

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