Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  /**
   3   * Copyright 2015-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 2015-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Mime
  12   */
  13  
  14  /**
  15   * This class represents a Content-Type MIME header (RFC 2045).
  16   *
  17   * @author    Michael Slusarz <slusarz@horde.org>
  18   * @category  Horde
  19   * @copyright 2015-2017 Horde LLC
  20   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  21   * @package   Mime
  22   * @since     2.8.0
  23   *
  24   * @property-read string $ptype  The primary type.
  25   * @property-read string $stype  The sub type.
  26   * @property-read string $type_charset  The MIME type with the charset
  27   *                                      parameter added (if this is a text/*
  28   *                                      part).
  29   */
  30  class Horde_Mime_Headers_ContentParam_ContentType
  31  extends Horde_Mime_Headers_ContentParam
  32  {
  33      const DEFAULT_CONTENT_TYPE = 'application/octet-stream';
  34  
  35      /**
  36       * Creates a default Content-Type header, conforming to the MIME
  37       * specification as detailed in RFC 2045.
  38       *
  39       * @return Horde_Mime_Headers_ContentParam_ContentType  Content-Type
  40       *                                                      header object.
  41       */
  42      public static function create()
  43      {
  44          $ob = new stdClass;
  45          $ob->value = self::DEFAULT_CONTENT_TYPE;
  46  
  47          return new self(null, $ob);
  48      }
  49  
  50      /**
  51       */
  52      public function __construct($name, $value)
  53      {
  54          parent::__construct('Content-Type', $value);
  55      }
  56  
  57      /**
  58       */
  59      public function __get($name)
  60      {
  61          switch ($name) {
  62          case 'params':
  63              $params = new Horde_Support_CaseInsensitiveArray(
  64                  parent::__get($name)
  65              );
  66              foreach ($params as $key => $val) {
  67                  if (!isset($this[$key])) {
  68                      unset($params[$key]);
  69                  }
  70              }
  71              return $params->getArrayCopy();
  72  
  73          case 'ptype':
  74              $val = $this->value;
  75              return substr($val, 0, strpos($val, '/'));
  76  
  77          case 'stype':
  78              $val = $this->value;
  79              return substr($val, strpos($val, '/') + 1);
  80  
  81          case 'type_charset':
  82              $val = $this->value;
  83              foreach ($this->_escapeParams(array_filter(array('charset' => $this['charset']))) as $k2 => $v2) {
  84                  $val .= '; ' . $k2 . '=' . $v2;
  85              }
  86              return $val;
  87          }
  88  
  89          return parent::__get($name);
  90      }
  91  
  92      /**
  93       */
  94      public function setContentParamValue($data)
  95      {
  96          /* Set the value first, since it will handle any sanity checking. */
  97          parent::setContentParamValue(Horde_String::lower($data));
  98  
  99          $val = $this->value;
 100  
 101          if (strpos($val, '/') === false) {
 102              parent::setContentParamValue(self::DEFAULT_CONTENT_TYPE);
 103          } else {
 104              switch ($this->ptype) {
 105              case 'multipart':
 106                  if (!isset($this['boundary'])) {
 107                      $this['boundary'] = '=_' . new Horde_Support_Randomid();
 108                  }
 109                  break;
 110  
 111              case 'application':
 112              case 'audio':
 113              case 'image':
 114              case 'message':
 115              case 'model':
 116              case 'text':
 117              case 'video':
 118                  // No-op
 119                  break;
 120  
 121              default:
 122                  if (substr($val, 0, 2) !== 'x-') {
 123                      /* Append 'x-' for any unknown primary MIME type. */
 124                      parent::setContentParamValue('x-' . $val);
 125                  }
 126                  break;
 127              }
 128          }
 129      }
 130  
 131      /**
 132       */
 133      public function isDefault()
 134      {
 135          return ($this->full_value === 'text/plain');
 136      }
 137  
 138      /**
 139       */
 140      public static function getHandles()
 141      {
 142          return array(
 143              'content-type'
 144          );
 145      }
 146  
 147      /* ArrayAccess methods. */
 148  
 149      /**
 150       */
 151      public function offsetExists($offset)
 152      {
 153          if (!parent::offsetExists($offset)) {
 154              return false;
 155          }
 156  
 157          if (strcasecmp($offset, 'boundary') === 0) {
 158              return ($this->ptype === 'multipart');
 159          } elseif (strcasecmp($offset, 'charset') === 0) {
 160              return (($this->ptype === 'text') &&
 161                      (parent::offsetGet($offset) !== 'us-ascii'));
 162          }
 163  
 164          return true;
 165      }
 166  
 167      /**
 168       */
 169      public function offsetGet($offset)
 170      {
 171          return isset($this[$offset])
 172              ? parent::offsetGet($offset)
 173              : null;
 174      }
 175  
 176      /**
 177       */
 178      public function offsetSet($offset, $value)
 179      {
 180          /* Store character set as lower case value. */
 181          if (strcasecmp($offset, 'charset') === 0) {
 182              $value = Horde_String::lower($value);
 183          }
 184  
 185          parent::offsetSet($offset, $value);
 186      }
 187  
 188      /**
 189       */
 190      public function offsetUnset($offset)
 191      {
 192          if (($this->ptype !== 'multipart') ||
 193              (strcasecmp($offset, 'boundary') !== 0)) {
 194              parent::offsetUnset($offset);
 195          }
 196      }
 197  
 198  }