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.
   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   * Decoding parsing code adapted from rfc822-parser.c (Dovecot 2.2.13)
   9   *   Original code released under LGPL-2.1
  10   *   Copyright (c) 2002-2014 Timo Sirainen <tss@iki.fi>
  11   *
  12   * @category  Horde
  13   * @copyright 2002-2015 Timo Sirainen
  14   * @copyright 2014-2017 Horde LLC
  15   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  16   * @package   Mime
  17   */
  18  
  19  /**
  20   * Decode MIME content parameter data (RFC 2045; 2183; 2231).
  21   *
  22   * @author    Timo Sirainen <tss@iki.fi>
  23   * @author    Michael Slusarz <slusarz@horde.org>
  24   * @category  Horde
  25   * @copyright 2002-2015 Timo Sirainen
  26   * @copyright 2014-2017 Horde LLC
  27   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  28   * @package   Mime
  29   * @since     2.5.0
  30   */
  31  class Horde_Mime_ContentParam_Decode extends Horde_Mail_Rfc822
  32  {
  33      /**
  34       * Decode content parameter data.
  35       *
  36       * @param string $data  Parameter data.
  37       *
  38       * @return array  List of parameter key/value combinations.
  39       */
  40      public function decode($data)
  41      {
  42          $out = array();
  43  
  44          $this->_data = $data;
  45          $this->_datalen = strlen($data);
  46          $this->_ptr = 0;
  47  
  48          while ($this->_curr() !== false) {
  49              $this->_rfc822SkipLwsp();
  50  
  51              $this->_rfc822ParseMimeToken($param);
  52  
  53              if (is_null($param) || ($this->_curr() != '=')) {
  54                  break;
  55              }
  56  
  57              ++$this->_ptr;
  58              $this->_rfc822SkipLwsp();
  59  
  60              $value = '';
  61  
  62              if ($this->_curr() == '"') {
  63                  try {
  64                      $this->_rfc822ParseQuotedString($value);
  65                  } catch (Horde_Mail_Exception $e) {
  66                      break;
  67                  }
  68              } else {
  69                  $this->_rfc822ParseMimeToken($value);
  70                  if (is_null($value)) {
  71                      break;
  72                  }
  73              }
  74  
  75              $out[$param] = $value;
  76  
  77              $this->_rfc822SkipLwsp();
  78              if ($this->_curr() != ';') {
  79                  break;
  80              }
  81  
  82              ++$this->_ptr;
  83          }
  84  
  85          return $out;
  86      }
  87  
  88      /**
  89       * Determine if character is a non-escaped element in MIME parameter data
  90       * (See RFC 2045 [Appendix A]).
  91       *
  92       * @param string $c  Character to test.
  93       *
  94       * @return boolean  True if non-escaped character.
  95       */
  96      public static function isAtextNonTspecial($c)
  97      {
  98          switch ($ord = ord($c)) {
  99          case 34:
 100          case 40:
 101          case 41:
 102          case 44:
 103          case 47:
 104          case 58:
 105          case 59:
 106          case 60:
 107          case 61:
 108          case 62:
 109          case 63:
 110          case 64:
 111          case 91:
 112          case 92:
 113          case 93:
 114              /* "(),/:;<=>?@[\] */
 115              return false;
 116  
 117          default:
 118              /* CTLs, SPACE, DEL, non-ASCII */
 119              return (($ord > 32) && ($ord < 127));
 120          }
 121      }
 122  
 123      /**
 124       */
 125      protected function _rfc822ParseMimeToken(&$str)
 126      {
 127          for ($i = $this->_ptr, $size = strlen($this->_data); $i < $size; ++$i) {
 128              if (!self::isAtextNonTspecial($this->_data[$i])) {
 129                  break;
 130              }
 131          }
 132  
 133          if ($i === $this->_ptr) {
 134              $str = null;
 135          } else {
 136              $str = substr($this->_data, $this->_ptr, $i - $this->_ptr);
 137              $this->_ptr += ($i - $this->_ptr);
 138              $this->_rfc822SkipLwsp();
 139          }
 140      }
 141  
 142  }