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 2012-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 2012-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * Stream filter to analyze an IMAP string to determine how to send to the
  16   * server.
  17   *
  18   * @author    Michael Slusarz <slusarz@horde.org>
  19   * @category  Horde
  20   * @copyright 2012-2017 Horde LLC
  21   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  22   * @package   Imap_Client
  23   */
  24  class Horde_Imap_Client_Data_Format_Filter_String extends php_user_filter
  25  {
  26      /**
  27       * Skip status.
  28       *
  29       * @var boolean
  30       */
  31      protected $_skip = false;
  32  
  33      /**
  34       * @see stream_filter_register()
  35       */
  36      public function onCreate()
  37      {
  38          $this->params->binary = false;
  39          $this->params->literal = false;
  40          $this->params->nonascii = false;
  41          // no_quote_list is used below as a config option
  42          $this->params->quoted = false;
  43  
  44          return true;
  45      }
  46  
  47      /**
  48       * @see stream_filter_register()
  49       */
  50      public function filter($in, $out, &$consumed, $closing)
  51      {
  52          $p = $this->params;
  53  
  54          while ($bucket = stream_bucket_make_writeable($in)) {
  55              if (!$this->_skip) {
  56                  $len = $bucket->datalen;
  57                  $str = $bucket->data;
  58  
  59                  for ($i = 0; $i < $len; ++$i) {
  60                      $chr = ord($str[$i]);
  61  
  62                      switch ($chr) {
  63                      case 0: // null
  64                          $p->binary = true;
  65                          $p->literal = true;
  66  
  67                          // No need to scan input anymore.
  68                          $this->_skip = true;
  69                          break 2;
  70  
  71                      case 10: // LF
  72                      case 13: // CR
  73                          $p->literal = true;
  74                          break;
  75  
  76                      case 32: // SPACE
  77                      case 34: // "
  78                      case 40: // (
  79                      case 41: // )
  80                      case 92: // \
  81                      case 123: // {
  82                      case 127: // DEL
  83                          // These are all invalid ATOM characters.
  84                          $p->quoted = true;
  85                          break;
  86  
  87                      case 37: // %
  88                      case 42: // *
  89                          // These are not quoted if being used as wildcards.
  90                          if (empty($p->no_quote_list)) {
  91                              $p->quoted = true;
  92                          }
  93                          break;
  94  
  95                      default:
  96                          if ($chr < 32) {
  97                              // CTL characters must be, at a minimum, quoted.
  98                              $p->quoted = true;
  99                          } elseif ($chr > 127) {
 100                              $p->nonascii = true;
 101                              // 8-bit chars must be in a literal.
 102                              $p->literal = true;
 103                          }
 104                          break;
 105                      }
 106                  }
 107              }
 108  
 109              $consumed += $bucket->datalen;
 110              stream_bucket_append($out, $bucket);
 111          }
 112  
 113          if ($p->literal) {
 114              $p->quoted = false;
 115          }
 116  
 117          return PSFS_PASS_ON;
 118      }
 119  
 120  }