vendor/mpdf/mpdf/src/AssetFetcher.php line 78

Open in your IDE?
  1. <?php
  2. namespace Mpdf;
  3. use Mpdf\File\LocalContentLoaderInterface;
  4. use Mpdf\File\StreamWrapperChecker;
  5. use Mpdf\Http\ClientInterface;
  6. use Mpdf\Log\Context as LogContext;
  7. use Mpdf\PsrHttpMessageShim\Request;
  8. use Mpdf\PsrLogAwareTrait\PsrLogAwareTrait;
  9. use Psr\Log\LoggerInterface;
  10. class AssetFetcher implements \Psr\Log\LoggerAwareInterface
  11. {
  12.     use PsrLogAwareTrait;
  13.     private $mpdf;
  14.     private $contentLoader;
  15.     private $http;
  16.     public function __construct(Mpdf $mpdfLocalContentLoaderInterface $contentLoaderClientInterface $httpLoggerInterface $logger)
  17.     {
  18.         $this->mpdf $mpdf;
  19.         $this->contentLoader $contentLoader;
  20.         $this->http $http;
  21.         $this->logger $logger;
  22.     }
  23.     public function fetchDataFromPath($path$originalSrc null)
  24.     {
  25.         /**
  26.          * Prevents insecure PHP object injection through phar:// wrapper
  27.          * @see https://github.com/mpdf/mpdf/issues/949
  28.          * @see https://github.com/mpdf/mpdf/issues/1381
  29.          */
  30.         $wrapperChecker = new StreamWrapperChecker($this->mpdf);
  31.         if ($wrapperChecker->hasBlacklistedStreamWrapper($path)) {
  32.             throw new \Mpdf\Exception\AssetFetchingException('File contains an invalid stream. Only ' implode(', '$wrapperChecker->getWhitelistedStreamWrappers()) . ' streams are allowed.');
  33.         }
  34.         if ($originalSrc && $wrapperChecker->hasBlacklistedStreamWrapper($originalSrc)) {
  35.             throw new \Mpdf\Exception\AssetFetchingException('File contains an invalid stream. Only ' implode(', '$wrapperChecker->getWhitelistedStreamWrappers()) . ' streams are allowed.');
  36.         }
  37.         $this->mpdf->GetFullPath($path);
  38.         return $this->isPathLocal($path) || ($originalSrc !== null && $this->isPathLocal($originalSrc))
  39.             ? $this->fetchLocalContent($path$originalSrc)
  40.             : $this->fetchRemoteContent($path);
  41.     }
  42.     public function fetchLocalContent($path$originalSrc)
  43.     {
  44.         $data '';
  45.         if ($originalSrc && $this->mpdf->basepathIsLocal && $check = @fopen($originalSrc'rb')) {
  46.             fclose($check);
  47.             $path $originalSrc;
  48.             $this->logger->debug(sprintf('Fetching content of file "%s" with local basepath'$path), ['context' => LogContext::REMOTE_CONTENT]);
  49.             return $this->contentLoader->load($path);
  50.         }
  51.         if ($path && $check = @fopen($path'rb')) {
  52.             fclose($check);
  53.             $this->logger->debug(sprintf('Fetching content of file "%s" with non-local basepath'$path), ['context' => LogContext::REMOTE_CONTENT]);
  54.             return $this->contentLoader->load($path);
  55.         }
  56.         return $data;
  57.     }
  58.     public function fetchRemoteContent($path)
  59.     {
  60.         $data '';
  61.         try {
  62.             $this->logger->debug(sprintf('Fetching remote content of file "%s"'$path), ['context' => LogContext::REMOTE_CONTENT]);
  63.             /** @var \Mpdf\PsrHttpMessageShim\Response $response */
  64.             $response $this->http->sendRequest(new Request('GET'$path));
  65.             if (!str_starts_with((string) $response->getStatusCode(), '2')) {
  66.                 $message sprintf('Non-OK HTTP response "%s" on fetching remote content "%s" because of an error'$response->getStatusCode(), $path);
  67.                 if ($this->mpdf->debug) {
  68.                     throw new \Mpdf\MpdfException($message);
  69.                 }
  70.                 $this->logger->info($message);
  71.                 return $data;
  72.             }
  73.             $data $response->getBody()->getContents();
  74.         } catch (\InvalidArgumentException $e) {
  75.             $message sprintf('Unable to fetch remote content "%s" because of an error "%s"'$path$e->getMessage());
  76.             if ($this->mpdf->debug) {
  77.                 throw new \Mpdf\MpdfException($message0E_ERRORnullnull$e);
  78.             }
  79.             $this->logger->warning($message);
  80.         }
  81.         return $data;
  82.     }
  83.     public function isPathLocal($path)
  84.     {
  85.         return str_starts_with($path'file://') || strpos($path'://') === false// @todo More robust implementation
  86.     }
  87. }