[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/horde/framework/Horde/Mime/ -> Related.php (source)

   1  <?php
   2  /**
   3   * This class parses a multipart/related MIME part (RFC 2387) to provide
   4   * information on the part contents.
   5   *
   6   * Copyright 2012-2014 Horde LLC (http://www.horde.org/)
   7   *
   8   * See the enclosed file COPYING for license information (LGPL). If you
   9   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  10   *
  11   * @author   Michael Slusarz <slusarz@horde.org>
  12   * @category Horde
  13   * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
  14   * @package  Mime
  15   */
  16  class Horde_Mime_Related implements IteratorAggregate
  17  {
  18      /**
  19       * Content IDs.
  20       *
  21       * @var array
  22       */
  23      protected $_cids = array();
  24  
  25      /**
  26       * Start ID.
  27       *
  28       * @var string
  29       */
  30      protected $_start;
  31  
  32      /**
  33       * Constructor.
  34       *
  35       * @param Horde_Mime_Part $mime_part  A MIME part object. Must be of
  36       *                                    type multipart/related.
  37       */
  38      public function __construct(Horde_Mime_Part $mime_part)
  39      {
  40          if ($mime_part->getType() != 'multipart/related') {
  41              throw new InvalidArgumentException('MIME part must be of type multipart/related');
  42          }
  43  
  44          $ids = array_keys($mime_part->contentTypeMap());
  45          $related_id = $mime_part->getMimeId();
  46          $id = null;
  47  
  48          /* Build a list of parts -> CIDs. */
  49          foreach ($ids as $val) {
  50              if ((strcmp($related_id, $val) !== 0) &&
  51                  ($cid = $mime_part->getPart($val)->getContentId())) {
  52                  $this->_cids[$val] = $cid;
  53              }
  54          }
  55  
  56          /* Look at the 'start' parameter to determine which part to start
  57           * with. If no 'start' parameter, use the first part (RFC 2387
  58           * [3.1]). */
  59          $start = $mime_part->getContentTypeParameter('start');
  60          if (!empty($start)) {
  61              $id = $this->cidSearch($start);
  62          }
  63  
  64          if (empty($id)) {
  65              reset($ids);
  66              $id = next($ids);
  67          }
  68  
  69          $this->_start = $id;
  70      }
  71  
  72      /**
  73       * Return the start ID.
  74       *
  75       * @return string  The start ID.
  76       */
  77      public function startId()
  78      {
  79          return $this->_start;
  80      }
  81  
  82      /**
  83       * Search for a CID in the related part.
  84       *
  85       * @param string $cid  The CID to search for.
  86       *
  87       * @return string  The MIME ID or false if not found.
  88       */
  89      public function cidSearch($cid)
  90      {
  91          return array_search($cid, $this->_cids);
  92      }
  93  
  94      /**
  95       * Scan for CID strings in HTML data and replace with data returned from
  96       * a callback method.
  97       *
  98       * @param mixed $text         The HTML text (can be Horde_Domhtml object).
  99       * @param callback $callback  Callback method. Receives three arguments:
 100       *                            MIME ID, the attribute name containing the
 101       *                            content ID, and the node object. Expects
 102       *                            return value of URL to display the data.
 103       * @param string $charset     HTML data charset.
 104       *
 105       * @return Horde_Domhtml  A Horde_Domhtml object.
 106       */
 107      public function cidReplace($text, $callback, $charset = 'UTF-8')
 108      {
 109          $dom = ($text instanceof Horde_Domhtml)
 110              ? $text
 111              : new Horde_Domhtml($text, $charset);
 112  
 113          foreach ($dom as $node) {
 114              if ($node instanceof DOMElement) {
 115                  switch (Horde_String::lower($node->tagName)) {
 116                  case 'body':
 117                  case 'td':
 118                      $this->_cidReplace($node, 'background', $callback);
 119                      break;
 120  
 121                  case 'img':
 122                      $this->_cidReplace($node, 'src', $callback);
 123                      break;
 124                  }
 125              }
 126          }
 127  
 128          return $dom;
 129      }
 130  
 131      /**
 132       */
 133      protected function _cidReplace($node, $attribute, $callback)
 134      {
 135          if ($node->hasAttribute($attribute)) {
 136              $val = $node->getAttribute($attribute);
 137              if ((strpos($val, 'cid:') === 0) &&
 138                  ($id = $this->cidSearch(substr($val, 4)))) {
 139                  $node->setAttribute($attribute, call_user_func($callback, $id, $attribute, $node));
 140              }
 141          }
 142      }
 143  
 144      /* IteratorAggregate method. */
 145  
 146      public function getIterator()
 147      {
 148          return new ArrayIterator($this->_cids);
 149      }
 150  
 151  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1