|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MallardDuck\Whois; |
|
4
|
|
|
|
|
5
|
|
|
use MallardDuck\Whois\Exceptions\SocketClientException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A simple socket stream client. |
|
9
|
|
|
* |
|
10
|
|
|
* @author mallardduck <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @copyright lucidinternets.com 2020 |
|
13
|
|
|
* |
|
14
|
|
|
* @version 1.1.0 |
|
15
|
|
|
*/ |
|
16
|
|
|
final class SocketClient |
|
17
|
|
|
{ |
|
18
|
|
|
protected $socketUri = null; |
|
19
|
|
|
protected $socket = null; |
|
20
|
|
|
protected $timeout = 30; |
|
21
|
|
|
protected $connected = false; |
|
22
|
|
|
|
|
23
|
42 |
|
public function __construct(string $socketUri, int $timeout = 30) |
|
24
|
|
|
{ |
|
25
|
42 |
|
$this->socketUri = $socketUri; |
|
26
|
42 |
|
$this->timeout = $timeout; |
|
27
|
42 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Fluent method to connect to the socket via the URI set at construction time. |
|
31
|
|
|
* |
|
32
|
|
|
* @return $this |
|
33
|
|
|
* @throws SocketClientException |
|
34
|
|
|
*/ |
|
35
|
36 |
|
public function connect(): self |
|
36
|
|
|
{ |
|
37
|
36 |
|
$fp = @stream_socket_client($this->socketUri, $errno, $errstr, $this->timeout); |
|
38
|
36 |
|
if (!is_resource($fp) && false === $fp) { |
|
39
|
2 |
|
$message = sprintf( |
|
40
|
2 |
|
"Stream Connection Failed: unable to connect to %s. System Error: %s ", |
|
41
|
2 |
|
$this->socketUri, |
|
42
|
|
|
$errstr |
|
43
|
|
|
); |
|
44
|
2 |
|
throw new SocketClientException($message, $errno); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
34 |
|
$this->socket = $fp; |
|
48
|
34 |
|
$this->connected = true; |
|
49
|
34 |
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Check if the current status shows as connected. |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function isConnected(): bool |
|
58
|
|
|
{ |
|
59
|
2 |
|
return $this->connected; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Write (or send) the string to the current socket stream. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $string |
|
66
|
|
|
* |
|
67
|
|
|
* @return false|int |
|
68
|
|
|
* @throws SocketClientException |
|
69
|
|
|
*/ |
|
70
|
34 |
|
public function writeString(string $string) |
|
71
|
|
|
{ |
|
72
|
34 |
|
if (!$this->connected) { |
|
73
|
2 |
|
$message = sprintf("The calling method %s requires the socket to be connected", __FUNCTION__); |
|
74
|
2 |
|
throw new SocketClientException($message); |
|
75
|
|
|
} |
|
76
|
32 |
|
return stream_socket_sendto($this->socket, $string); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get all the response data from the socket stream. |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
* @throws SocketClientException |
|
84
|
|
|
*/ |
|
85
|
32 |
|
public function readAll(): string |
|
86
|
|
|
{ |
|
87
|
32 |
|
if (!$this->connected) { |
|
88
|
2 |
|
$message = sprintf("The calling method %s requires the socket to be connected", __FUNCTION__); |
|
89
|
2 |
|
throw new SocketClientException($message); |
|
90
|
|
|
} |
|
91
|
30 |
|
return stream_get_contents($this->socket); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Fluent method to disconnect from the whois socket. |
|
96
|
|
|
* |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
42 |
|
public function disconnect(): self |
|
100
|
|
|
{ |
|
101
|
42 |
|
if (!is_null($this->socket)) { |
|
102
|
34 |
|
stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR); |
|
103
|
34 |
|
fclose($this->socket); |
|
104
|
|
|
} |
|
105
|
42 |
|
$this->socket = null; |
|
106
|
42 |
|
$this->connected = false; |
|
107
|
|
|
|
|
108
|
42 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
22 |
|
public function __destruct() |
|
112
|
|
|
{ |
|
113
|
22 |
|
$this->disconnect(); |
|
114
|
22 |
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|