4
0

ext.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. default:
  53. header('HTTP/1.1 400 Bad Request');
  54. die();
  55. }
  56. $mtime = @filemtime($absolute_filename);
  57. if ($mtime === false) {
  58. header('HTTP/1.1 404 Not Found');
  59. die();
  60. }
  61. require(LIB_PATH . '/http-conditional.php');
  62. if (!httpConditional($mtime, 604800, 2)) {
  63. readfile($absolute_filename);
  64. }