[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/StreamFilters/ -> ByteArrayReplacementFilter.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of SwiftMailer.
   5   * (c) 2004-2009 Chris Corbyn
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * Processes bytes as they pass through a buffer and replaces sequences in it.
  13   *
  14   * This stream filter deals with Byte arrays rather than simple strings.
  15   *
  16   * @author  Chris Corbyn
  17   */
  18  class Swift_StreamFilters_ByteArrayReplacementFilter implements Swift_StreamFilter
  19  {
  20      /** The replacement(s) to make */
  21      private $replace;
  22  
  23      /** The Index for searching */
  24      private $index;
  25  
  26      /** The Search Tree */
  27      private $tree = [];
  28  
  29      /**  Gives the size of the largest search */
  30      private $treeMaxLen = 0;
  31  
  32      private $repSize;
  33  
  34      /**
  35       * Create a new ByteArrayReplacementFilter with $search and $replace.
  36       *
  37       * @param array $search
  38       * @param array $replace
  39       */
  40      public function __construct($search, $replace)
  41      {
  42          $this->index = [];
  43          $this->tree = [];
  44          $this->replace = [];
  45          $this->repSize = [];
  46  
  47          $tree = null;
  48          $i = null;
  49          $last_size = $size = 0;
  50          foreach ($search as $i => $search_element) {
  51              if (null !== $tree) {
  52                  $tree[-1] = min(count($replace) - 1, $i - 1);
  53                  $tree[-2] = $last_size;
  54              }
  55              $tree = &$this->tree;
  56              if (is_array($search_element)) {
  57                  foreach ($search_element as $k => $char) {
  58                      $this->index[$char] = true;
  59                      if (!isset($tree[$char])) {
  60                          $tree[$char] = [];
  61                      }
  62                      $tree = &$tree[$char];
  63                  }
  64                  $last_size = $k + 1;
  65                  $size = max($size, $last_size);
  66              } else {
  67                  $last_size = 1;
  68                  if (!isset($tree[$search_element])) {
  69                      $tree[$search_element] = [];
  70                  }
  71                  $tree = &$tree[$search_element];
  72                  $size = max($last_size, $size);
  73                  $this->index[$search_element] = true;
  74              }
  75          }
  76          if (null !== $i) {
  77              $tree[-1] = min(count($replace) - 1, $i);
  78              $tree[-2] = $last_size;
  79              $this->treeMaxLen = $size;
  80          }
  81          foreach ($replace as $rep) {
  82              if (!is_array($rep)) {
  83                  $rep = [$rep];
  84              }
  85              $this->replace[] = $rep;
  86          }
  87          for ($i = count($this->replace) - 1; $i >= 0; --$i) {
  88              $this->replace[$i] = $rep = $this->filter($this->replace[$i], $i);
  89              $this->repSize[$i] = count($rep);
  90          }
  91      }
  92  
  93      /**
  94       * Returns true if based on the buffer passed more bytes should be buffered.
  95       *
  96       * @param array $buffer
  97       *
  98       * @return bool
  99       */
 100      public function shouldBuffer($buffer)
 101      {
 102          $endOfBuffer = end($buffer);
 103  
 104          return isset($this->index[$endOfBuffer]);
 105      }
 106  
 107      /**
 108       * Perform the actual replacements on $buffer and return the result.
 109       *
 110       * @param array $buffer
 111       * @param int   $minReplaces
 112       *
 113       * @return array
 114       */
 115      public function filter($buffer, $minReplaces = -1)
 116      {
 117          if (0 == $this->treeMaxLen) {
 118              return $buffer;
 119          }
 120  
 121          $newBuffer = [];
 122          $buf_size = count($buffer);
 123          $last_size = 0;
 124          for ($i = 0; $i < $buf_size; ++$i) {
 125              $search_pos = $this->tree;
 126              $last_found = PHP_INT_MAX;
 127              // We try to find if the next byte is part of a search pattern
 128              for ($j = 0; $j <= $this->treeMaxLen; ++$j) {
 129                  // We have a new byte for a search pattern
 130                  if (isset($buffer[$p = $i + $j]) && isset($search_pos[$buffer[$p]])) {
 131                      $search_pos = $search_pos[$buffer[$p]];
 132                      // We have a complete pattern, save, in case we don't find a better match later
 133                      if (isset($search_pos[-1]) && $search_pos[-1] < $last_found
 134                          && $search_pos[-1] > $minReplaces) {
 135                          $last_found = $search_pos[-1];
 136                          $last_size = $search_pos[-2];
 137                      }
 138                  }
 139                  // We got a complete pattern
 140                  elseif (PHP_INT_MAX !== $last_found) {
 141                      // Adding replacement datas to output buffer
 142                      $rep_size = $this->repSize[$last_found];
 143                      for ($j = 0; $j < $rep_size; ++$j) {
 144                          $newBuffer[] = $this->replace[$last_found][$j];
 145                      }
 146                      // We Move cursor forward
 147                      $i += $last_size - 1;
 148                      // Edge Case, last position in buffer
 149                      if ($i >= $buf_size) {
 150                          $newBuffer[] = $buffer[$i];
 151                      }
 152  
 153                      // We start the next loop
 154                      continue 2;
 155                  } else {
 156                      // this byte is not in a pattern and we haven't found another pattern
 157                      break;
 158                  }
 159              }
 160              // Normal byte, move it to output buffer
 161              $newBuffer[] = $buffer[$i];
 162          }
 163  
 164          return $newBuffer;
 165      }
 166  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1