1<?php
2/**
3 * Copyright 2013-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 2013-2017 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 POP3 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-2017 Horde LLC
24 * @internal
25 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
26 * @package   Imap_Client
27 */
28class Horde_Imap_Client_Socket_Connection_Pop3
29extends Horde_Imap_Client_Socket_Connection_Base
30{
31    /**
32     */
33    protected $_protocol = 'pop3';
34
35    /**
36     * Writes data to the POP3 output stream.
37     *
38     * @param string $data    String data.
39     * @param boolean $debug  Output line to debug?
40     *
41     * @throws Horde_Imap_Client_Exception
42     */
43    public function write($data, $debug = true)
44    {
45        if (fwrite($this->_stream, $data . "\r\n") === false) {
46            throw new Horde_Imap_Client_Exception(
47                Horde_Imap_Client_Translation::r("Server write error."),
48                Horde_Imap_Client_Exception::SERVER_WRITEERROR
49            );
50        }
51
52        if ($debug) {
53            $this->_params['debug']->client($data);
54        }
55    }
56
57    /**
58     * Read data from incoming POP3 stream.
59     *
60     * @param integer $size  UNUSED: The number of bytes to read from the
61     *                       socket.
62     *
63     * @return string  Line of data.
64     *
65     * @throws Horde_Imap_Client_Exception
66     */
67    public function read($size = null)
68    {
69        if (feof($this->_stream)) {
70            $this->close();
71            $this->_params['debug']->info(
72                'ERROR: Server closed the connection.'
73            );
74            throw new Horde_Imap_Client_Exception(
75                Horde_Imap_Client_Translation::r("Server closed the connection unexpectedly."),
76                Horde_Imap_Client_Exception::DISCONNECT
77            );
78        }
79
80        if (($read = fgets($this->_stream)) === false) {
81            $this->_params['debug']->info('ERROR: read/timeout error.');
82            throw new Horde_Imap_Client_Exception(
83                Horde_Imap_Client_Translation::r("Error when communicating with the mail server."),
84                Horde_Imap_Client_Exception::SERVER_READERROR
85            );
86        }
87
88        $this->_params['debug']->server(rtrim($read, "\r\n"));
89
90        return $read;
91    }
92
93}
94