/** * Constructor. * * @param Horde_Imap_Client_Tokenize $token Tokenized data returned from * the server. */ public function __construct(Horde_Imap_Client_Tokenize $token) { $this->token = $token; /* Check for response status. */ $status = $token->current(); $valid = array('BAD', 'BYE', 'NO', 'OK', 'PREAUTH'); if (in_array($status, $valid)) { $this->status = constant(__CLASS__ . '::' . $status); $resp_text = $token->next(); /* Check for response code. Only occurs if there is a response * status. */ if (is_string($resp_text) && $resp_text[0] == '[') { $resp = new stdClass(); $resp->data = array(); if ($resp_text[strlen($resp_text) - 1] == ']') { $resp->code = substr($resp_text, 1, -1); } else { $resp->code = substr($resp_text, 1); while (($elt = $token->next()) !== false) { if (is_string($elt) && $elt[strlen($elt) - 1] == ']') { $resp->data[] = substr($elt, 0, -1); break; } $resp->data[] = is_string($elt) ? $elt : iterator_to_array($elt); } } $token->next(); $this->responseCode = $resp; } } }
/** * @dataProvider clientSortProvider */ public function testClientSortProvider($sort, $expected, $locale) { $ids = new Horde_Imap_Client_Ids(); $pipeline = $this->socket_ob->pipeline(); foreach ($this->fetch_data as $val) { $token = new Horde_Imap_Client_Tokenize($val); $token->rewind(); $token->next(); $ids->add($token->next()); $this->socket_ob->doServerResponse($pipeline, $val); } $this->socket_ob->fetch_results = $pipeline->fetch; $sorted = $this->sort_ob->clientSort($ids, array('sort' => $sort)); $this->assertEquals(count($expected), count($sorted)); if (!$locale || class_exists('Collator')) { $this->assertEquals($expected, array_values($sorted)); } }
/** * Parse a METADATA response (RFC 5464 [4.4]). * * @param Horde_Imap_Client_Interaction_Pipeline $pipeline Pipeline * object. * @param Horde_Imap_Client_Tokenize $data The server response. * * @throws Horde_Imap_Client_Exception */ protected function _parseMetadata(Horde_Imap_Client_Interaction_Pipeline $pipeline, Horde_Imap_Client_Tokenize $data) { // Mailbox name is in UTF7-IMAP. $mbox = Horde_Imap_Client_Mailbox::get($data->next(), true); // Ignore unsolicited responses. if ($data->next() === true) { while (($entry = $data->next()) !== false) { $pipeline->data['metadata'][strval($mbox)][$entry] = $data->next(); } } }
/** * Read data from incoming IMAP stream. * * @return Horde_Imap_Client_Tokenize The tokenized data. * * @throws Horde_Imap_Client_Exception */ protected function _readStream() { $got_data = false; $literal_len = null; $token = new Horde_Imap_Client_Tokenize(); do { if (feof($this->_stream)) { $this->_temp['logout'] = true; $this->logout(); $this->writeDebug("ERROR: Server closed the connection.\n", Horde_Imap_Client::DEBUG_INFO); throw new Horde_Imap_Client_Exception(Horde_Imap_Client_Translation::t("Mail server closed the connection unexpectedly."), Horde_Imap_Client_Exception::DISCONNECT); } if (is_null($literal_len)) { $this->writeDebug('', Horde_Imap_Client::DEBUG_SERVER); while (($in = fgets($this->_stream)) !== false) { $got_data = true; if (substr($in, -1) == "\n") { $in = rtrim($in); if ($this->_debug) { $this->writeDebug($in . "\n"); } $token->add($in); break; } if ($this->_debug) { $this->writeDebug($in); } $token->add($in); } /* Check for literal data. */ fseek($token->stream->stream, -1, SEEK_END); if ($token->stream->peek() == '}') { $literal_data = $token->stream->getString($token->stream->search('{', true) - 1); $literal_len = substr($literal_data, 2, -1); if (is_numeric($literal_len)) { if ($literal_len) { $binary = $literal_data[0] == '~'; } else { // Skip 0-length literal data. $literal_len = null; } continue; } } break; } $debug_literal = $this->_debug && !empty($this->_params['debug_literal']); $old_len = $literal_len; $this->writeDebug('', Horde_Imap_Client::DEBUG_SERVER); while ($literal_len && !feof($this->_stream)) { $in = fread($this->_stream, min($literal_len, 8192)); $token->add($in); if ($debug_literal) { $this->writeDebug($in); } $got_data = true; $in_len = strlen($in); if ($in_len > $literal_len) { break; } $literal_len -= $in_len; } $literal_len = null; if (!$debug_literal) { $this->writeDebug('[' . ($binary ? 'BINARY' : 'LITERAL') . ' DATA: ' . $old_len . ' bytes]' . "\n"); } } while (true); if (!$got_data) { $this->writeDebug("ERROR: IMAP read/timeout error.\n", Horde_Imap_Client::DEBUG_INFO); $this->logout(); throw new Horde_Imap_Client_Exception(Horde_Imap_Client_Translation::t("Error when communicating with the mail server."), Horde_Imap_Client_Exception::SERVER_READERROR); } return $token; }
/** * Read data from incoming IMAP stream. * * @return Horde_Imap_Client_Tokenize The tokenized data. * * @throws Horde_Imap_Client_Exception */ public function read() { $got_data = false; $literal_len = null; $token = new Horde_Imap_Client_Tokenize(); do { if (feof($this->_stream)) { $this->close(); $this->_params['debug']->info('ERROR: Server closed the connection.'); throw new Horde_Imap_Client_Exception(Horde_Imap_Client_Translation::r("Mail server closed the connection unexpectedly."), Horde_Imap_Client_Exception::DISCONNECT); } if (is_null($literal_len)) { $buffer = ''; while (($in = fgets($this->_stream)) !== false) { $got_data = true; if (substr($in, -1) === "\n") { $in = rtrim($in); $this->_params['debug']->server($buffer . $in); $token->add($in); break; } $buffer .= $in; $token->add($in); } /* Check for literal data. */ if (is_null($len = $token->getLiteralLength())) { break; } // Skip 0-length literal data. if ($len['length']) { $binary = $len['binary']; $literal_len = $len['length']; } continue; } $old_len = $literal_len; while ($literal_len > 0 && !feof($this->_stream)) { $in = fread($this->_stream, min($literal_len, 8192)); $token->add($in); if (!empty($this->_params['debugliteral'])) { $this->_params['debug']->raw($in); } $got_data = true; $literal_len -= strlen($in); } $literal_len = null; if (empty($this->_params['debugliteral'])) { $this->_params['debug']->server('[' . ($binary ? 'BINARY' : 'LITERAL') . ' DATA: ' . $old_len . ' bytes]'); } } while (true); if (!$got_data) { $this->_params['debug']->info('ERROR: read/timeout error.'); throw new Horde_Imap_Client_Exception(Horde_Imap_Client_Translation::r("Error when communicating with the mail server."), Horde_Imap_Client_Exception::SERVER_READERROR); } return $token; }
public function testLiteralStream() { $token = new Horde_Imap_Client_Tokenize(); $token->add('FOO {10}'); $token->addLiteralStream('1234567890'); $token->add(' BAR'); $token->rewind(); $this->assertEquals('FOO', $token->next()); /* Internal stream is converted to string. */ $this->assertEquals('1234567890', $token->next()); $this->assertEquals('BAR', $token->next()); $this->assertTrue($token->eos); /* Check to see stream is returned if nextStream() is called and * a literal is encountered. */ $token->rewind(); $this->assertEquals('FOO', $token->nextStream()); $stream = $token->nextStream(); $this->assertInstanceOf('Horde_Stream_Temp', $stream); $this->assertEquals('1234567890', strval($stream)); $this->assertEquals('BAR', $token->nextStream()); $token = new Horde_Imap_Client_Tokenize(); $token->add('{200}' . str_repeat('Z', 200)); $token->rewind(); $this->assertEquals(str_repeat('Z', 200), $token->next()); $token->rewind(); $stream = $token->nextStream(); $this->assertInstanceOf('Horde_Stream_Temp', $stream); $this->assertEquals(str_repeat('Z', 200), strval($stream)); }
public function testLiteralLength() { $test = 'FOO'; $token = new Horde_Imap_Client_Tokenize($test); $this->assertNull($token->getLiteralLength()); $test = 'FOO {100}'; $token = new Horde_Imap_Client_Tokenize($test); $len = $token->getLiteralLength(); $this->assertNotNull($len); $this->assertFalse($len['binary']); $this->assertEquals(100, $len['length']); $test = 'FOO ~{100}'; $token = new Horde_Imap_Client_Tokenize($test); $len = $token->getLiteralLength(); $this->assertNotNull($len); $this->assertTrue($len['binary']); $this->assertEquals(100, $len['length']); }