Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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      public function onCreate()
  38      {
  39          $this->params->body = false;
  40  
  41          return true;
  42      }
  43  
  44      /**
  45       * @see stream_filter_register()
  46       */
  47      public function filter($in, $out, &$consumed, $closing)
  48      {
  49          while ($bucket = stream_bucket_make_writeable($in)) {
  50              if ($this->params->body !== 'binary') {
  51                  $len = $bucket->datalen;
  52                  $str = $bucket->data;
  53  
  54                  for ($i = 0; $i < $len; ++$i) {
  55                      $chr = ord($str[$i]);
  56  
  57                      switch ($chr) {
  58                      case 0:
  59                          /* Only binary data can have NULLs. */
  60                          $this->params->body = 'binary';
  61                          break 2;
  62  
  63                      case 10: // LF
  64                      case 13: // CR
  65                          $this->_crlf = 0;
  66                          break;
  67  
  68                      default:
  69                          /* RFC 2045 [2.8]: 8bit data must be less than 998
  70                           * characters in length. Otherwise, we are looking at
  71                           * binary. */
  72                          if (++$this->_crlf > 998) {
  73                              $this->params->body = 'binary';
  74                              break 2;
  75                          } elseif ($chr > 127) {
  76                              $this->params->body = '8bit';
  77                          }
  78                          break;
  79                      }
  80                  }
  81              }
  82  
  83              $consumed += $bucket->datalen;
  84              stream_bucket_append($out, $bucket);
  85          }
  86  
  87          return PSFS_PASS_ON;
  88      }
  89  
  90  }