/
Socket.php
222 lines (193 loc) · 6.63 KB
/
Socket.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* Copyright 2013-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2013-2017 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Imap_Client
*/
/**
* PHP stream connection to the IMAP server.
*
* NOTE: This class is NOT intended to be accessed outside of the package.
* There is NO guarantees that the API of this class will not change across
* versions.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013-2017 Horde LLC
* @internal
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Imap_Client
*/
class Horde_Imap_Client_Socket_Connection_Socket
extends Horde_Imap_Client_Socket_Connection_Base
{
/**
* If false, does not outpt the current line of client output to debug.
*
* @var boolean
*/
public $client_debug = true;
/**
* Sending buffer.
*
* @var string
*/
protected $_buffer = '';
/**
* Writes data to the IMAP output stream.
*
* @param string $data String data.
* @param boolean $eol Append EOL?
*
* @throws Horde_Imap_Client_Exception
*/
public function write($data, $eol = false)
{
if ($eol) {
$buffer = $this->_buffer;
$debug = $this->client_debug;
$this->_buffer = '';
$this->client_debug = true;
if (fwrite($this->_stream, $buffer . $data . ($eol ? "\r\n" : '')) === false) {
throw new Horde_Imap_Client_Exception(
Horde_Imap_Client_Translation::r("Server write error."),
Horde_Imap_Client_Exception::SERVER_WRITEERROR
);
}
if ($debug) {
$this->_params['debug']->client($buffer . $data);
}
} else {
$this->_buffer .= $data;
}
}
/**
* Writes literal data to the IMAP output stream.
*
* @param mixed $data Either a stream resource, or Horde_Stream
* object.
* @param integer $length The literal length.
* @param boolean $binary If true, this is binary data.
*
* @throws Horde_Imap_Client_Exception
*/
public function writeLiteral($data, $length, $binary = false)
{
$this->_buffer = '';
$success = false;
if ($data instanceof Horde_Stream) {
$data = $data->stream;
}
if (rewind($data)) {
$success = true;
while (!feof($data)) {
if ((($read_data = fread($data, 8192)) === false) ||
(fwrite($this->_stream, $read_data) === false)) {
$success = false;
break;
}
}
}
if (!$success) {
$this->client_debug = true;
throw new Horde_Imap_Client_Exception(
Horde_Imap_Client_Translation::r("Server write error."),
Horde_Imap_Client_Exception::SERVER_WRITEERROR
);
}
if ($this->client_debug && !empty($this->_params['debugliteral'])) {
rewind($data);
while (!feof($data)) {
$this->_params['debug']->raw(fread($data, 8192));
}
} else {
$this->_params['debug']->client('[' . ($binary ? 'BINARY' : 'LITERAL') . ' DATA: ' . $length . ' bytes]');
}
}
/**
* Read data from incoming IMAP stream.
*
* @param integer $size UNUSED: The number of bytes to read from the
* socket.
*
* @return Horde_Imap_Client_Tokenize The tokenized data.
*
* @throws Horde_Imap_Client_Exception
*/
public function read($size = null)
{
$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));
/* Only store in stream if this is something more than a
* nominal number of bytes. */
if ($old_len > 256) {
$token->addLiteralStream($in);
} else {
$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;
}
}