Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  /**
   3   * Copyright 2014-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (LGPL). If you
   6   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   7   *
   8   * @category  Horde
   9   * @copyright 2014-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Mime
  12   */
  13  
  14  /**
  15   * Stream filter to determine whether body requires either binary or 8bit
  16   * encoding (RFC 2045 [6]).
  17   *
  18   * @author    Michael Slusarz <slusarz@horde.org>
  19   * @category  Horde
  20   * @copyright 2014-2017 Horde LLC
  21   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  22   * @package   Mime
  23   * @since     2.6.0
  24   */
  25  class Horde_Mime_Filter_Encoding extends php_user_filter
  26  {
  27      /**
  28       * Number of consecutive non-CR/LF characters.
  29       *
  30       * @var integer
  31       */
  32      protected $_crlf = 0;
  33  
  34      /**
  35       * @see stream_filter_register()
  36       */
  37      #[ReturnTypeWillChange]
  38      public function onCreate()
  39      {
  40          $this->params->body = false;
  41  
  42          return true;
  43      }
  44  
  45      /**
  46       * @see stream_filter_register()
  47       */
  48      #[ReturnTypeWillChange]
  49      public function filter($in, $out, &$consumed, $closing)
  50      {
  51          while ($bucket = stream_bucket_make_writeable($in)) {
  52              if ($this->params->body !== 'binary') {
  53                  $len = $bucket->datalen;
  54                  $str = $bucket->data;
  55  
  56                  for ($i = 0; $i < $len; ++$i) {
  57                      $chr = ord($str[$i]);
  58  
  59                      switch ($chr) {
  60                      case 0:
  61                          /* Only binary data can have NULLs. */
  62                          $this->params->body = 'binary';
  63                          break 2;
  64  
  65                      case 10: // LF
  66                      case 13: // CR
  67                          $this->_crlf = 0;
  68                          break;
  69  
  70                      default:
  71                          /* RFC 2045 [2.8]: 8bit data must be less than 998
  72                           * characters in length. Otherwise, we are looking at
  73                           * binary. */
  74                          if (++$this->_crlf > 998) {
  75                              $this->params->body = 'binary';
  76                              break 2;
  77                          } elseif ($chr > 127) {
  78                              $this->params->body = '8bit';
  79                          }
  80                          break;
  81                      }
  82                  }
  83              }
  84  
  85              $consumed += $bucket->datalen;
  86              stream_bucket_append($out, $bucket);
  87          }
  88  
  89          return PSFS_PASS_ON;
  90      }
  91  
  92  }