[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/horde/framework/Horde/Imap/Client/Socket/Connection/ -> Socket.php (source)

   1  <?php
   2  /**
   3   * Copyright 2013-2014 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file COPYING 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 2013-2014 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * PHP stream connection to the IMAP server.
  16   *
  17   * NOTE: This class is NOT intended to be accessed outside of the package.
  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 2013-2014 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_Connection_Socket
  29  extends Horde_Imap_Client_Socket_Connection_Base
  30  {
  31      /**
  32       * Sending buffer.
  33       *
  34       * @var string
  35       */
  36      protected $_buffer = '';
  37  
  38      /**
  39       * Writes data to the IMAP output stream.
  40       *
  41       * @param string $data  String data.
  42       * @param boolean $eol  Append EOL?
  43       *
  44       * @throws Horde_Imap_Client_Exception
  45       */
  46      public function write($data, $eol = false)
  47      {
  48          if ($eol) {
  49              $buffer = $this->_buffer;
  50              $this->_buffer = '';
  51  
  52              if (fwrite($this->_stream, $buffer . $data . ($eol ? "\r\n" : '')) === false) {
  53                  throw new Horde_Imap_Client_Exception(
  54                      Horde_Imap_Client_Translation::r("Server write error."),
  55                      Horde_Imap_Client_Exception::SERVER_WRITEERROR
  56                  );
  57              }
  58  
  59              $this->_params['debug']->client($buffer . $data);
  60          } else {
  61              $this->_buffer .= $data;
  62          }
  63      }
  64  
  65      /**
  66       * Writes literal data to the IMAP output stream.
  67       *
  68       * @param mixed $data      Either a stream resource, or Horde_Stream
  69       *                         object.
  70       * @param integer $length  The literal length.
  71       * @param boolean $binary  If true, this is binary data.
  72       *
  73       * @throws Horde_Imap_Client_Exception
  74       */
  75      public function writeLiteral($data, $length, $binary = false)
  76      {
  77          $this->_buffer = '';
  78  
  79          if ($data instanceof Horde_Stream) {
  80              $data = $data->stream;
  81          }
  82  
  83          if (!rewind($data)) {
  84              throw new Horde_Imap_Client_Exception(
  85                  Horde_Imap_Client_Translation::r("Server write error."),
  86                  Horde_Imap_Client_Exception::SERVER_WRITEERROR
  87              );
  88          }
  89  
  90          while (!feof($data)) {
  91              if ((($read_data = fread($data, 8192)) === false) ||
  92                  (fwrite($this->_stream, $read_data) === false)) {
  93                  throw new Horde_Imap_Client_Exception(
  94                      Horde_Imap_Client_Translation::r("Server write error."),
  95                      Horde_Imap_Client_Exception::SERVER_WRITEERROR
  96                  );
  97              }
  98          }
  99  
 100          if (!empty($this->_params['debugliteral'])) {
 101              rewind($data);
 102              while (!feof($data)) {
 103                  $this->_params['debug']->raw(fread($data, 8192));
 104              }
 105          } else {
 106              $this->_params['debug']->client('[' . ($binary ? 'BINARY' : 'LITERAL') . ' DATA: ' . $length . ' bytes]');
 107          }
 108      }
 109  
 110      /**
 111       * Read data from incoming IMAP stream.
 112       *
 113       * @return Horde_Imap_Client_Tokenize  The tokenized data.
 114       *
 115       * @throws Horde_Imap_Client_Exception
 116       */
 117      public function read()
 118      {
 119          $got_data = false;
 120          $literal_len = null;
 121          $token = new Horde_Imap_Client_Tokenize();
 122  
 123          do {
 124              if (feof($this->_stream)) {
 125                  $this->close();
 126                  $this->_params['debug']->info(
 127                      'ERROR: Server closed the connection.'
 128                  );
 129                  throw new Horde_Imap_Client_Exception(
 130                      Horde_Imap_Client_Translation::r("Mail server closed the connection unexpectedly."),
 131                      Horde_Imap_Client_Exception::DISCONNECT
 132                  );
 133              }
 134  
 135              if (is_null($literal_len)) {
 136                  $buffer = '';
 137  
 138                  while (($in = fgets($this->_stream)) !== false) {
 139                      $got_data = true;
 140  
 141                      if (substr($in, -1) === "\n") {
 142                          $in = rtrim($in);
 143                          $this->_params['debug']->server($buffer . $in);
 144                          $token->add($in);
 145                          break;
 146                      }
 147  
 148                      $buffer .= $in;
 149                      $token->add($in);
 150                  }
 151  
 152                  /* Check for literal data. */
 153                  if (is_null($len = $token->getLiteralLength())) {
 154                      break;
 155                  }
 156  
 157                  // Skip 0-length literal data.
 158                  if ($len['length']) {
 159                      $binary = $len['binary'];
 160                      $literal_len = $len['length'];
 161                  }
 162  
 163                  continue;
 164              }
 165  
 166              $old_len = $literal_len;
 167  
 168              while (($literal_len > 0) && !feof($this->_stream)) {
 169                  $in = fread($this->_stream, min($literal_len, 8192));
 170                  $token->add($in);
 171                  if (!empty($this->_params['debugliteral'])) {
 172                      $this->_params['debug']->raw($in);
 173                  }
 174  
 175                  $got_data = true;
 176                  $literal_len -= strlen($in);
 177              }
 178  
 179              $literal_len = null;
 180  
 181              if (empty($this->_params['debugliteral'])) {
 182                  $this->_params['debug']->server('[' . ($binary ? 'BINARY' : 'LITERAL') . ' DATA: ' . $old_len . ' bytes]');
 183              }
 184          } while (true);
 185  
 186          if (!$got_data) {
 187              $this->_params['debug']->info('ERROR: read/timeout error.');
 188              throw new Horde_Imap_Client_Exception(
 189                  Horde_Imap_Client_Translation::r("Error when communicating with the mail server."),
 190                  Horde_Imap_Client_Exception::SERVER_READERROR
 191              );
 192          }
 193  
 194          return $token;
 195      }
 196  
 197  }


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