vendor/swiftmailer/lib/classes/Swift/CharacterStream/NgCharacterStream.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  CharacterStream implementation using an array in Swift Mailer.
       
     5 
       
     6  This program is free software: you can redistribute it and/or modify
       
     7  it under the terms of the GNU General Public License as published by
       
     8  the Free Software Foundation, either version 3 of the License, or
       
     9  (at your option) any later version.
       
    10 
       
    11  This program is distributed in the hope that it will be useful,
       
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  GNU General Public License for more details.
       
    15 
       
    16  You should have received a copy of the GNU General Public License
       
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18 
       
    19  */
       
    20 
       
    21 
       
    22 
       
    23 /**
       
    24  * A CharacterStream implementation which stores characters in an internal array.
       
    25  * @package Swift
       
    26  * @subpackage CharacterStream
       
    27  * @author Xavier De Cock <xdecock@gmail.com>
       
    28  */
       
    29 
       
    30 Class Swift_CharacterStream_NgCharacterStream
       
    31   implements Swift_CharacterStream
       
    32 {
       
    33 
       
    34   /**
       
    35    * The char reader (lazy-loaded) for the current charset.
       
    36    * @var Swift_CharacterReader
       
    37    * @access private
       
    38    */
       
    39   private $_charReader;
       
    40 
       
    41   /**
       
    42    * A factory for creatiing CharacterReader instances.
       
    43    * @var Swift_CharacterReaderFactory
       
    44    * @access private
       
    45    */
       
    46   private $_charReaderFactory;
       
    47 
       
    48   /**
       
    49    * The character set this stream is using.
       
    50    * @var string
       
    51    * @access private
       
    52    */
       
    53   private $_charset;
       
    54   
       
    55   /**
       
    56    * The datas stored as is
       
    57    *
       
    58    * @var string
       
    59    */
       
    60   private $_datas = "";
       
    61   
       
    62   /**
       
    63    * Number of bytes in the stream
       
    64    *
       
    65    * @var int
       
    66    */
       
    67   private $_datasSize = 0;
       
    68   
       
    69   /**
       
    70    * Map
       
    71    *
       
    72    * @var mixed
       
    73    */
       
    74   private $_map;
       
    75   
       
    76   /**
       
    77    * Map Type
       
    78    *
       
    79    * @var int
       
    80    */
       
    81   private $_mapType = 0;
       
    82   
       
    83   /**
       
    84    * Number of characters in the stream
       
    85    *
       
    86    * @var int
       
    87    */
       
    88   private $_charCount = 0;
       
    89   
       
    90   /**
       
    91    * Position in the stream
       
    92    *
       
    93    * @var unknown_type
       
    94    */
       
    95   private $_currentPos = 0;
       
    96   
       
    97   /**
       
    98    * The constructor
       
    99    *
       
   100    * @param Swift_CharacterReaderFactory $factory
       
   101    * @param unknown_type $charset
       
   102    */
       
   103   public function __construct(Swift_CharacterReaderFactory $factory,
       
   104     $charset)
       
   105   {
       
   106     $this->setCharacterReaderFactory($factory);
       
   107     $this->setCharacterSet($charset);
       
   108   }
       
   109   
       
   110   /* -- Changing parameters of the stream -- */
       
   111 
       
   112   /**
       
   113    * Set the character set used in this CharacterStream.
       
   114    * @param string $charset
       
   115    */
       
   116   public function setCharacterSet($charset)
       
   117   {
       
   118     $this->_charset = $charset;
       
   119     $this->_charReader = null;
       
   120   	$this->_mapType = 0;
       
   121   }
       
   122 
       
   123   /**
       
   124    * Set the CharacterReaderFactory for multi charset support.
       
   125    * @param Swift_CharacterReaderFactory $factory
       
   126    */
       
   127   public function setCharacterReaderFactory(
       
   128     Swift_CharacterReaderFactory $factory)
       
   129   {
       
   130     $this->_charReaderFactory = $factory;
       
   131   }
       
   132 
       
   133   /**
       
   134    * @see Swift_CharacterStream::flushContents()
       
   135    *
       
   136    */
       
   137   public function flushContents()
       
   138   {
       
   139   	$this->_datas = null;
       
   140   	$this->_map = null;
       
   141   	$this->_charCount = 0;
       
   142   	$this->_currentPos = 0;
       
   143   	$this->_datasSize = 0;
       
   144   }
       
   145   
       
   146   /**
       
   147    * @see Swift_CharacterStream::importByteStream()
       
   148    *
       
   149    * @param Swift_OutputByteStream $os
       
   150    */
       
   151   public function importByteStream(Swift_OutputByteStream $os)
       
   152   {
       
   153     $this->flushContents();
       
   154     $blocks=512;
       
   155     $os->setReadPointer(0);
       
   156     while(false!==($read = $os->read($blocks)))
       
   157       $this->write($read);
       
   158   }
       
   159   
       
   160   /**
       
   161    * @see Swift_CharacterStream::importString()
       
   162    *
       
   163    * @param string $string
       
   164    */
       
   165   public function importString($string)
       
   166   {
       
   167     $this->flushContents();
       
   168     $this->write($string);
       
   169   }
       
   170   
       
   171   /**
       
   172    * @see Swift_CharacterStream::read()
       
   173    *
       
   174    * @param int $length
       
   175    * @return string
       
   176    */
       
   177   public function read($length)
       
   178   {
       
   179   	if ($this->_currentPos>=$this->_charCount)
       
   180   	{
       
   181   	  return false;
       
   182   	}
       
   183   	$ret=false;
       
   184   	$length = ($this->_currentPos+$length > $this->_charCount)
       
   185   	  ? $this->_charCount - $this->_currentPos
       
   186   	  : $length;
       
   187   	  switch ($this->_mapType)
       
   188   	{
       
   189       case Swift_CharacterReader::MAP_TYPE_FIXED_LEN:
       
   190         $len = $length*$this->_map;
       
   191         $ret = substr($this->_datas,
       
   192             $this->_currentPos * $this->_map,
       
   193             $len);
       
   194         $this->_currentPos += $length;
       
   195         break;
       
   196       
       
   197       case Swift_CharacterReader::MAP_TYPE_INVALID:
       
   198         $end = $this->_currentPos + $length;
       
   199         $end = $end > $this->_charCount
       
   200           ?$this->_charCount
       
   201           :$end;
       
   202         $ret = '';
       
   203         for (; $this->_currentPos < $length; ++$this->_currentPos)
       
   204         {
       
   205           if (isset ($this->_map[$this->_currentPos]))
       
   206           {
       
   207             $ret .= '?';
       
   208           }
       
   209           else
       
   210           {
       
   211             $ret .= $this->_datas[$this->_currentPos];
       
   212           }
       
   213         }
       
   214         break;
       
   215       
       
   216       case Swift_CharacterReader::MAP_TYPE_POSITIONS:
       
   217         $end = $this->_currentPos + $length;
       
   218         $end = $end > $this->_charCount
       
   219           ?$this->_charCount
       
   220           :$end;
       
   221         $ret = '';
       
   222         $start = 0;
       
   223         if ($this->_currentPos>0)
       
   224         {
       
   225           $start = $this->_map['p'][$this->_currentPos-1];
       
   226         }
       
   227         $to = $start;
       
   228         for (; $this->_currentPos < $end; ++$this->_currentPos)
       
   229         {
       
   230           if (isset($this->_map['i'][$this->_currentPos])) {
       
   231           	$ret .= substr($this->_datas, $start, $to - $start).'?';
       
   232           	$start = $this->_map['p'][$this->_currentPos];
       
   233           } else {
       
   234           	$to = $this->_map['p'][$this->_currentPos];
       
   235           }
       
   236         }
       
   237         $ret .= substr($this->_datas, $start, $to - $start);
       
   238         break;
       
   239   	}
       
   240   	return $ret;
       
   241   }
       
   242   
       
   243   /**
       
   244    * @see Swift_CharacterStream::readBytes()
       
   245    *
       
   246    * @param int $length
       
   247    * @return int[]
       
   248    */
       
   249   public function readBytes($length)
       
   250   {
       
   251     $read=$this->read($length);
       
   252   	if ($read!==false)
       
   253   	{
       
   254       $ret = array_map('ord', str_split($read, 1));
       
   255       return $ret;
       
   256   	}
       
   257   	return false;
       
   258   }
       
   259   
       
   260   /**
       
   261    * @see Swift_CharacterStream::setPointer()
       
   262    *
       
   263    * @param int $charOffset
       
   264    */
       
   265   public function setPointer($charOffset)
       
   266   {
       
   267   	if ($this->_charCount<$charOffset){
       
   268   		$charOffset=$this->_charCount;
       
   269   	}
       
   270   	$this->_currentPos = $charOffset;
       
   271   }
       
   272   
       
   273   /**
       
   274    * @see Swift_CharacterStream::write()
       
   275    *
       
   276    * @param string $chars
       
   277    */
       
   278   public function write($chars)
       
   279   {
       
   280   	if (!isset($this->_charReader))
       
   281     {
       
   282       $this->_charReader = $this->_charReaderFactory->getReaderFor(
       
   283         $this->_charset);
       
   284       $this->_map = array();
       
   285       $this->_mapType = $this->_charReader->getMapType();
       
   286     }
       
   287   	$ignored='';
       
   288   	$this->_datas .= $chars;
       
   289     $this->_charCount += $this->_charReader->getCharPositions(substr($this->_datas, $this->_datasSize), $this->_datasSize, $this->_map, $ignored);
       
   290     if ($ignored!==false) {
       
   291       $this->_datasSize=strlen($this->_datas)-strlen($ignored);
       
   292     }
       
   293     else
       
   294     {
       
   295       $this->_datasSize=strlen($this->_datas);
       
   296     }
       
   297   }
       
   298 }