Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   1  <?php
   2  
   3  /**
   4   * This file is part of FPDI
   5   *
   6   * @package   setasign\Fpdi
   7   * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
   8   * @license   http://opensource.org/licenses/mit-license The MIT License
   9   */
  10  
  11  namespace setasign\Fpdi\PdfParser\Filter;
  12  
  13  /**
  14   * Class for handling zlib/deflate encoded data
  15   */
  16  class Flate implements FilterInterface
  17  {
  18      /**
  19       * Checks whether the zlib extension is loaded.
  20       *
  21       * Used for testing purpose.
  22       *
  23       * @return boolean
  24       * @internal
  25       */
  26      protected function extensionLoaded()
  27      {
  28          return \extension_loaded('zlib');
  29      }
  30  
  31      /**
  32       * Decodes a flate compressed string.
  33       *
  34       * @param string|false $data The input string
  35       * @return string
  36       * @throws FlateException
  37       */
  38      public function decode($data)
  39      {
  40          if ($this->extensionLoaded()) {
  41              $oData = $data;
  42              $data = (($data !== '') ? @\gzuncompress($data) : '');
  43              if ($data === false) {
  44                  // let's try if the checksum is CRC32
  45                  $fh = fopen('php://temp', 'w+b');
  46                  fwrite($fh, "\x1f\x8b\x08\x00\x00\x00\x00\x00" . $oData);
  47                  stream_filter_append($fh, 'zlib.inflate', STREAM_FILTER_READ, ['window' => 30]);
  48                  fseek($fh, 0);
  49                  $data = @stream_get_contents($fh);
  50                  fclose($fh);
  51  
  52                  if ($data) {
  53                      return $data;
  54                  }
  55  
  56                  // Try this fallback
  57                  $tries = 0;
  58  
  59                  $oDataLen = strlen($oData);
  60                  while ($tries < 6 && ($data === false || (strlen($data) < ($oDataLen - $tries - 1)))) {
  61                      $data = @(gzinflate(substr($oData, $tries)));
  62                      $tries++;
  63                  }
  64  
  65                  // let's use this fallback only if the $data is longer than the original data
  66                  if (strlen($data) > ($oDataLen - $tries - 1)) {
  67                      return $data;
  68                  }
  69  
  70                  if (!$data) {
  71                      throw new FlateException(
  72                          'Error while decompressing stream.',
  73                          FlateException::DECOMPRESS_ERROR
  74                      );
  75                  }
  76              }
  77          } else {
  78              throw new FlateException(
  79                  'To handle FlateDecode filter, enable zlib support in PHP.',
  80                  FlateException::NO_ZLIB
  81              );
  82          }
  83  
  84          return $data;
  85      }
  86  }