|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the php-epp2 library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Gunter Grodotzki <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE file |
|
9
|
|
|
* that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace AfriCC\EPP; |
|
13
|
|
|
|
|
14
|
|
|
use AfriCC\EPP\Frame\Command\Logout as LogoutCommand; |
|
15
|
|
|
use AfriCC\EPP\Frame\ResponseFactory; |
|
16
|
|
|
use Exception; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* A high level TCP (SSL) based client for the Extensible Provisioning Protocol (EPP) |
|
20
|
|
|
* |
|
21
|
|
|
* @see http://tools.ietf.org/html/rfc5734 |
|
22
|
|
|
* |
|
23
|
|
|
* As this class deals directly with sockets it's untestable |
|
24
|
|
|
* @codeCoverageIgnore |
|
25
|
|
|
*/ |
|
26
|
|
|
class Client extends AbstractClient implements ClientInterface |
|
27
|
|
|
{ |
|
28
|
|
|
protected $socket; |
|
29
|
|
|
protected $chunk_size; |
|
30
|
|
|
protected $verify_peer_name; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(array $config, ObjectSpec $objectSpec = null) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct($config, $objectSpec); |
|
35
|
|
|
|
|
36
|
|
|
if (!empty($config['chunk_size'])) { |
|
37
|
|
|
$this->chunk_size = (int) $config['chunk_size']; |
|
38
|
|
|
} else { |
|
39
|
|
|
$this->chunk_size = 1024; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (isset($config['verify_peer_name'])) { |
|
43
|
|
|
$this->verify_peer_name = (bool) $config['verify_peer_name']; |
|
44
|
|
|
} else { |
|
45
|
|
|
$this->verify_peer_name = true; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($this->port === false) { |
|
49
|
|
|
// if not set, default port is 700 |
|
50
|
|
|
$this->port = 700; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function __destruct() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->close(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Setup context in case of ssl connection |
|
61
|
|
|
* |
|
62
|
|
|
* @return resource|null |
|
63
|
|
|
*/ |
|
64
|
|
|
private function setupContext() |
|
65
|
|
|
{ |
|
66
|
|
|
if (!$this->ssl) { |
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$context = stream_context_create(); |
|
71
|
|
|
|
|
72
|
|
|
$options_array = [ |
|
73
|
|
|
'verify_peer' => false, |
|
74
|
|
|
'verify_peer_name' => $this->verify_peer_name, |
|
75
|
|
|
'allow_self_signed' => true, |
|
76
|
|
|
'local_cert' => $this->local_cert, |
|
77
|
|
|
'passphrase' => $this->passphrase, |
|
78
|
|
|
'cafile' => $this->ca_cert, |
|
79
|
|
|
'local_pk' => $this->pk_cert, |
|
80
|
|
|
]; |
|
81
|
|
|
|
|
82
|
|
|
// filter out empty user provided values |
|
83
|
|
|
$options_array = array_filter( |
|
84
|
|
|
$options_array, |
|
85
|
|
|
function ($var) { |
|
86
|
|
|
return !is_null($var); |
|
87
|
|
|
} |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
$options = ['ssl' => $options_array]; |
|
91
|
|
|
|
|
92
|
|
|
// stream_context_set_option accepts array of options in form of $arr['wrapper']['option'] = value |
|
93
|
|
|
stream_context_set_option($context, $options); |
|
94
|
|
|
|
|
95
|
|
|
return $context; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Setup connection socket |
|
100
|
|
|
* |
|
101
|
|
|
* @param resource|null $context SSL context or null in case of tcp connection |
|
102
|
|
|
* |
|
103
|
|
|
* @throws Exception |
|
104
|
|
|
*/ |
|
105
|
|
|
private function setupSocket($context = null) |
|
106
|
|
|
{ |
|
107
|
|
|
$proto = $this->ssl ? 'ssl' : 'tcp'; |
|
108
|
|
|
$target = sprintf('%s://%s:%d', $proto, $this->host, $this->port); |
|
109
|
|
|
|
|
110
|
|
|
$errno = 0; |
|
111
|
|
|
$errstr = ''; |
|
112
|
|
|
|
|
113
|
|
|
$this->socket = @stream_socket_client($target, $errno, $errstr, $this->connect_timeout, STREAM_CLIENT_CONNECT, $context); |
|
114
|
|
|
|
|
115
|
|
|
if ($this->socket === false) { |
|
116
|
|
|
throw new Exception($errstr, $errno); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// set stream time out |
|
120
|
|
|
if (!stream_set_timeout($this->socket, $this->timeout)) { |
|
121
|
|
|
throw new Exception('unable to set stream timeout'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// set to non-blocking |
|
125
|
|
|
if (!stream_set_blocking($this->socket, 0)) { |
|
126
|
|
|
throw new Exception('unable to set blocking'); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
* |
|
133
|
|
|
* @see \AfriCC\EPP\ClientInterface::connect() |
|
134
|
|
|
*/ |
|
135
|
|
|
public function connect($newPassword = false) |
|
136
|
|
|
{ |
|
137
|
|
|
$context = $this->setupContext(); |
|
138
|
|
|
$this->setupSocket($context); |
|
139
|
|
|
|
|
140
|
|
|
// get greeting |
|
141
|
|
|
$greeting = $this->getFrame(); |
|
142
|
|
|
|
|
143
|
|
|
// login |
|
144
|
|
|
$this->login($newPassword); |
|
145
|
|
|
|
|
146
|
|
|
// return greeting |
|
147
|
|
|
return $greeting; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Closes a previously opened EPP connection |
|
152
|
|
|
*/ |
|
153
|
|
|
public function close() |
|
154
|
|
|
{ |
|
155
|
|
|
if ($this->active()) { |
|
156
|
|
|
// send logout frame |
|
157
|
|
|
$this->request(new LogoutCommand($this->objectSpec)); |
|
158
|
|
|
|
|
159
|
|
|
return fclose($this->socket); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
protected function getFrame() |
|
166
|
|
|
{ |
|
167
|
|
|
$header = $this->recv(4); |
|
168
|
|
|
|
|
169
|
|
|
// Unpack first 4 bytes which is our length |
|
170
|
|
|
$unpacked = unpack('N', $header); |
|
171
|
|
|
$length = $unpacked[1]; |
|
172
|
|
|
|
|
173
|
|
|
if ($length < 5) { |
|
174
|
|
|
throw new Exception(sprintf('Got a bad frame header length of %d bytes from peer', $length)); |
|
175
|
|
|
} else { |
|
176
|
|
|
$length -= 4; |
|
177
|
|
|
|
|
178
|
|
|
return $this->recv($length); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
protected function sendFrame(FrameInterface $frame) |
|
183
|
|
|
{ |
|
184
|
|
|
$buffer = (string) $frame; |
|
185
|
|
|
$header = pack('N', mb_strlen($buffer, 'ASCII') + 4); |
|
186
|
|
|
|
|
187
|
|
|
return $this->send($header . $buffer); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
protected function log($message, $color = '0;32') |
|
191
|
|
|
{ |
|
192
|
|
|
if ($message === '' || !$this->debug) { |
|
193
|
|
|
return; |
|
194
|
|
|
} |
|
195
|
|
|
echo sprintf("\033[%sm%s\033[0m", $color, $message); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* check if socket is still active |
|
200
|
|
|
* |
|
201
|
|
|
* @return bool |
|
202
|
|
|
*/ |
|
203
|
|
|
private function active() |
|
204
|
|
|
{ |
|
205
|
|
|
return !is_resource($this->socket) || feof($this->socket) ? false : true; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* receive socket data |
|
210
|
|
|
* |
|
211
|
|
|
* @param int $length |
|
212
|
|
|
* |
|
213
|
|
|
* @throws Exception |
|
214
|
|
|
* |
|
215
|
|
|
* @return string |
|
216
|
|
|
*/ |
|
217
|
|
|
private function recv($length) |
|
218
|
|
|
{ |
|
219
|
|
|
$result = ''; |
|
220
|
|
|
|
|
221
|
|
|
$info = stream_get_meta_data($this->socket); |
|
222
|
|
|
$hard_time_limit = time() + $this->timeout + 2; |
|
223
|
|
|
|
|
224
|
|
|
while (!$info['timed_out'] && !feof($this->socket)) { |
|
225
|
|
|
// Try read remaining data from socket |
|
226
|
|
|
$buffer = @fread($this->socket, $length - mb_strlen($result, 'ASCII')); |
|
227
|
|
|
|
|
228
|
|
|
// If the buffer actually contains something then add it to the result |
|
229
|
|
|
if ($buffer !== false) { |
|
230
|
|
|
$this->log($buffer); |
|
231
|
|
|
$result .= $buffer; |
|
232
|
|
|
|
|
233
|
|
|
// break if all data received |
|
234
|
|
|
if (mb_strlen($result, 'ASCII') === $length) { |
|
235
|
|
|
break; |
|
236
|
|
|
} |
|
237
|
|
|
} else { |
|
238
|
|
|
// sleep 0.25s |
|
239
|
|
|
usleep(250000); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
// update metadata |
|
243
|
|
|
$info = stream_get_meta_data($this->socket); |
|
244
|
|
|
if (time() >= $hard_time_limit) { |
|
245
|
|
|
throw new Exception('Timeout while reading from EPP Server'); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
// check for timeout |
|
250
|
|
|
if ($info['timed_out']) { |
|
251
|
|
|
throw new Exception('Timeout while reading data from socket'); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return $result; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* send data to socket |
|
259
|
|
|
* |
|
260
|
|
|
* @param string $buffer |
|
261
|
|
|
*/ |
|
262
|
|
|
private function send($buffer) |
|
263
|
|
|
{ |
|
264
|
|
|
$info = stream_get_meta_data($this->socket); |
|
265
|
|
|
$hard_time_limit = time() + $this->timeout + 2; |
|
266
|
|
|
$length = mb_strlen($buffer, 'ASCII'); |
|
267
|
|
|
|
|
268
|
|
|
$pos = 0; |
|
269
|
|
|
while (!$info['timed_out'] && !feof($this->socket)) { |
|
270
|
|
|
// Some servers don't like a lot of data, so keep it small per chunk |
|
271
|
|
|
$wlen = $length - $pos; |
|
272
|
|
|
|
|
273
|
|
|
if ($wlen > $this->chunk_size) { |
|
274
|
|
|
$wlen = $this->chunk_size; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
// try write remaining data from socket |
|
278
|
|
|
$written = @fwrite($this->socket, mb_substr($buffer, $pos, $wlen, 'ASCII'), $wlen); |
|
279
|
|
|
|
|
280
|
|
|
// If we read something, bump up the position |
|
281
|
|
|
if ($written) { |
|
282
|
|
|
if ($this->debug) { |
|
283
|
|
|
$this->log(mb_substr($buffer, $pos, $wlen, 'ASCII'), '1;31'); |
|
284
|
|
|
} |
|
285
|
|
|
$pos += $written; |
|
286
|
|
|
|
|
287
|
|
|
// break if all written |
|
288
|
|
|
if ($pos === $length) { |
|
289
|
|
|
break; |
|
290
|
|
|
} |
|
291
|
|
|
} else { |
|
292
|
|
|
// sleep 0.25s |
|
293
|
|
|
usleep(250000); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
// update metadata |
|
297
|
|
|
$info = stream_get_meta_data($this->socket); |
|
298
|
|
|
if (time() >= $hard_time_limit) { |
|
299
|
|
|
throw new Exception('Timeout while writing to EPP Server'); |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
// check for timeout |
|
304
|
|
|
if ($info['timed_out']) { |
|
305
|
|
|
throw new Exception('Timeout while writing data to socket'); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
if ($pos !== $length) { |
|
309
|
|
|
throw new Exception('Writing short %d bytes', $length - $pos); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
return $pos; |
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
|
|
|