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 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   * Methods for the Socket driver used for a CATENATE command.
  16   *
  17   * NOTE: This class is NOT intended to be accessed outside of a Base object.
  18   * There is NO guarantees that the API of this class will not change across
  19   * versions.
  20   *
  21   * @author    Michael Slusarz <slusarz@horde.org>
  22   * @category  Horde
  23   * @copyright 2012-2017 Horde LLC
  24   * @internal
  25   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  26   * @package   Imap_Client
  27   */
  28  class Horde_Imap_Client_Socket_Catenate
  29  {
  30      /**
  31       * Socket object.
  32       *
  33       * @var Horde_Imap_Client_Socket
  34       */
  35      protected $_socket;
  36  
  37      /**
  38       * Constructor.
  39       *
  40       * @param Horde_Imap_Client_Socket $socket  Socket object.
  41       */
  42      public function __construct(Horde_Imap_Client_Socket $socket)
  43      {
  44          $this->_socket = $socket;
  45      }
  46  
  47      /**
  48       * Given an IMAP URL, fetches the corresponding part.
  49       *
  50       * @param Horde_Imap_Client_Url_Imap $url  An IMAP URL.
  51       *
  52       * @return resource  The section contents in a stream. Returns null if
  53       *                   the part could not be found.
  54       *
  55       * @throws Horde_Imap_Client_Exception
  56       */
  57      public function fetchFromUrl(Horde_Imap_Client_Url_Imap $url)
  58      {
  59          $ids_ob = $this->_socket->getIdsOb($url->uid);
  60  
  61          // BODY[]
  62          if (is_null($url->section)) {
  63              $query = new Horde_Imap_Client_Fetch_Query();
  64              $query->fullText(array(
  65                  'peek' => true
  66              ));
  67  
  68              $fetch = $this->_socket->fetch($url->mailbox, $query, array(
  69                  'ids' => $ids_ob
  70              ));
  71              return $fetch[$url->uid]->getFullMsg(true);
  72          }
  73  
  74          $section = trim($url->section);
  75  
  76          // BODY[<#.>HEADER.FIELDS<.NOT>()]
  77          if (($pos = stripos($section, 'HEADER.FIELDS')) !== false) {
  78              $hdr_pos = strpos($section, '(');
  79              $cmd = substr($section, 0, $hdr_pos);
  80  
  81              $query = new Horde_Imap_Client_Fetch_Query();
  82              $query->headers(
  83                  'section',
  84                  explode(' ', substr($section, $hdr_pos + 1, strrpos($section, ')') - $hdr_pos)),
  85                  array(
  86                      'id' => ($pos ? substr($section, 0, $pos - 1) : 0),
  87                      'notsearch' => (stripos($cmd, '.NOT') !== false),
  88                      'peek' => true
  89                  )
  90              );
  91  
  92              $fetch = $this->_socket->fetch($url->mailbox, $query, array(
  93                  'ids' => $ids_ob
  94              ));
  95              return $fetch[$url->uid]->getHeaders('section', Horde_Imap_Client_Data_Fetch::HEADER_STREAM);
  96          }
  97  
  98          // BODY[#]
  99          if (is_numeric(substr($section, -1))) {
 100              $query = new Horde_Imap_Client_Fetch_Query();
 101              $query->bodyPart($section, array(
 102                  'peek' => true
 103              ));
 104  
 105              $fetch = $this->_socket->fetch($url->mailbox, $query, array(
 106                  'ids' => $ids_ob
 107              ));
 108              return $fetch[$url->uid]->getBodyPart($section, true);
 109          }
 110  
 111          // BODY[<#.>HEADER]
 112          if (($pos = stripos($section, 'HEADER')) !== false) {
 113              $id = $pos
 114                  ? substr($section, 0, $pos - 1)
 115                  : 0;
 116  
 117              $query = new Horde_Imap_Client_Fetch_Query();
 118              $query->headerText(array(
 119                  'id' => $id,
 120                  'peek' => true
 121              ));
 122  
 123              $fetch = $this->_socket->fetch($url->mailbox, $query, array(
 124                  'ids' => $ids_ob
 125              ));
 126              return $fetch[$url->uid]->getHeaderText($id, Horde_Imap_Client_Data_Fetch::HEADER_STREAM);
 127          }
 128  
 129          // BODY[<#.>TEXT]
 130          if (($pos = stripos($section, 'TEXT')) !== false) {
 131              $id = $pos
 132                  ? substr($section, 0, $pos - 1)
 133                  : 0;
 134  
 135              $query = new Horde_Imap_Client_Fetch_Query();
 136              $query->bodyText(array(
 137                  'id' => $id,
 138                  'peek' => true
 139              ));
 140  
 141              $fetch = $this->_socket->fetch($url->mailbox, $query, array(
 142                  'ids' => $ids_ob
 143              ));
 144              return $fetch[$url->uid]->getBodyText($id, true);
 145          }
 146  
 147          // BODY[<#.>MIMEHEADER]
 148          if (($pos = stripos($section, 'MIME')) !== false) {
 149              $id = $pos
 150                  ? substr($section, 0, $pos - 1)
 151                  : 0;
 152  
 153              $query = new Horde_Imap_Client_Fetch_Query();
 154              $query->mimeHeader($id, array(
 155                  'peek' => true
 156              ));
 157  
 158              $fetch = $this->_socket->fetch($url->mailbox, $query, array(
 159                  'ids' => $ids_ob
 160              ));
 161              return $fetch[$url->uid]->getMimeHeader($id, Horde_Imap_Client_Data_Fetch::HEADER_STREAM);
 162          }
 163  
 164          return null;
 165      }
 166  
 167  }