get.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Public media files entry point
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. use Magento\Framework\App\Cache\Frontend\Factory;
  9. use Magento\Framework\App\ObjectManagerFactory;
  10. use Magento\Framework\HTTP\PhpEnvironment\Request;
  11. use Magento\Framework\Stdlib\Cookie\PhpCookieReader;
  12. require dirname(__DIR__) . '/app/bootstrap.php';
  13. $mediaDirectory = null;
  14. $allowedResources = [];
  15. $configCacheFile = BP . '/var/resource_config.json';
  16. $isAllowed = function ($resource, array $allowedResources) {
  17. foreach ($allowedResources as $allowedResource) {
  18. if (0 === stripos($resource, $allowedResource)) {
  19. return true;
  20. }
  21. }
  22. return false;
  23. };
  24. $request = new \Magento\MediaStorage\Model\File\Storage\Request(
  25. new Request(
  26. new PhpCookieReader(),
  27. new Magento\Framework\Stdlib\StringUtils()
  28. )
  29. );
  30. $relativePath = $request->getPathInfo();
  31. if (file_exists($configCacheFile) && is_readable($configCacheFile)) {
  32. $config = json_decode(file_get_contents($configCacheFile), true);
  33. //checking update time
  34. if (filemtime($configCacheFile) + $config['update_time'] > time()) {
  35. $mediaDirectory = $config['media_directory'];
  36. $allowedResources = $config['allowed_resources'];
  37. // Serve file if it's materialized
  38. if ($mediaDirectory) {
  39. if (!$isAllowed($relativePath, $allowedResources)) {
  40. require_once 'errors/404.php';
  41. exit;
  42. }
  43. $mediaAbsPath = $mediaDirectory . '/' . $relativePath;
  44. if (is_readable($mediaAbsPath)) {
  45. if (is_dir($mediaAbsPath)) {
  46. require_once 'errors/404.php';
  47. exit;
  48. }
  49. $transfer = new \Magento\Framework\File\Transfer\Adapter\Http(
  50. new \Magento\Framework\HTTP\PhpEnvironment\Response(),
  51. new \Magento\Framework\File\Mime()
  52. );
  53. $transfer->send($mediaAbsPath);
  54. exit;
  55. }
  56. }
  57. }
  58. }
  59. // Materialize file in application
  60. $params = $_SERVER;
  61. if (empty($mediaDirectory)) {
  62. $params[ObjectManagerFactory::INIT_PARAM_DEPLOYMENT_CONFIG] = [];
  63. $params[Factory::PARAM_CACHE_FORCED_OPTIONS] = ['frontend_options' => ['disable_save' => true]];
  64. }
  65. $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
  66. /** @var \Magento\MediaStorage\App\Media $app */
  67. $app = $bootstrap->createApplication(
  68. \Magento\MediaStorage\App\Media::class,
  69. [
  70. 'mediaDirectory' => $mediaDirectory,
  71. 'configCacheFile' => $configCacheFile,
  72. 'isAllowed' => $isAllowed,
  73. 'relativeFileName' => $relativePath,
  74. ]
  75. );
  76. $bootstrap->run($app);