1 | <?php |
||
23 | class Connection implements ConnectionInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $host; |
||
29 | |||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | private $port; |
||
34 | |||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | private $secure; |
||
39 | |||
40 | /** |
||
41 | * @var resource |
||
42 | */ |
||
43 | private $socket; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | private $timeout; |
||
49 | |||
50 | /** |
||
51 | * Constructor. |
||
52 | * |
||
53 | * @param string $host The hostname of the NNTP server. |
||
54 | * @param int $port The port of the NNTP server. |
||
55 | * @param bool $secure A bool indicating if a secure connection should be established. |
||
56 | * @param int $timeout The socket timeout in seconds. |
||
57 | */ |
||
58 | 2 | public function __construct($host, $port, $secure = false, $timeout = 15) |
|
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 2 | public function connect() |
|
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | public function disconnect() |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function sendCommand(CommandInterface $command) |
||
105 | { |
||
106 | $commandString = $command->execute(); |
||
107 | |||
108 | // NNTP/RFC977 only allows command up to 512 (-2 \r\n) chars. |
||
109 | if (!strlen($commandString) > 510) { |
||
110 | throw new InvalidArgumentException('Failed to write to socket: command exceeded 510 characters'); |
||
111 | } |
||
112 | |||
113 | if (!@fwrite($this->socket, $commandString."\r\n")) { |
||
114 | throw new RuntimeException('Failed to write to socket'); |
||
115 | } |
||
116 | |||
117 | $response = $this->getResponse(); |
||
118 | |||
119 | if ($command->isMultiLine() && ($response->getStatusCode() >= 200 && $response->getStatusCode() <= 399)) { |
||
120 | $response = $command->isCompressed() ? $this->getCompressedResponse($response) : $this->getMultiLineResponse($response); |
||
121 | } |
||
122 | |||
123 | if (in_array($response->getStatusCode(), array(Response::COMMAND_UNKNOWN, Response::COMMAND_UNAVAILABLE))) { |
||
124 | throw new RuntimeException('Sent command is either unknown or unavailable on server'); |
||
125 | } |
||
126 | |||
127 | $expectedResponseCodes = $command->getExpectedResponseCodes(); |
||
128 | |||
129 | // Check if we received a response expected by the command. |
||
130 | if (!isset($expectedResponseCodes[$response->getStatusCode()])) { |
||
131 | throw new RuntimeException(sprintf( |
||
132 | 'Unexpected response received: [%d] %s', |
||
133 | $response->getStatusCode(), |
||
134 | $response->getMessage() |
||
135 | )); |
||
136 | } |
||
137 | |||
138 | $expectedResponseHandler = $expectedResponseCodes[$response->getStatusCode()]; |
||
139 | if (!is_callable(array($command, $expectedResponseHandler))) { |
||
140 | throw new RuntimeException(sprintf('Response handler (%s) is not callable method on given command object', $expectedResponseHandler)); |
||
141 | } |
||
142 | |||
143 | $command->setResponse($response); |
||
144 | $command->$expectedResponseHandler($response); |
||
145 | |||
146 | return $command; |
||
147 | } |
||
148 | |||
149 | public function sendArticle(CommandInterface $command) |
||
150 | { |
||
151 | $commandString = $command->execute(); |
||
152 | |||
153 | if (!@fwrite($this->socket, $commandString."\r\n.\r\n")) { |
||
154 | throw new RuntimeException('Failed to write to socket'); |
||
155 | } |
||
156 | |||
157 | $response = $this->getResponse(); |
||
158 | |||
159 | $expectedResponseCodes = $command->getExpectedResponseCodes(); |
||
160 | |||
161 | // Check if we received a response expected by the command. |
||
162 | if (!isset($expectedResponseCodes[$response->getStatusCode()])) { |
||
163 | throw new RuntimeException(sprintf( |
||
164 | 'Unexpected response received: [%d] %s', |
||
165 | $response->getStatusCode(), |
||
166 | $response->getMessage() |
||
167 | )); |
||
168 | } |
||
169 | |||
170 | $expectedResponseHandler = $expectedResponseCodes[$response->getStatusCode()]; |
||
171 | if (!is_callable(array($command, $expectedResponseHandler))) { |
||
172 | throw new RuntimeException(sprintf('Response handler (%s) is not callable method on given command object', $expectedResponseHandler)); |
||
173 | } |
||
174 | |||
175 | $command->setResponse($response); |
||
176 | $command->$expectedResponseHandler($response); |
||
177 | |||
178 | return $command; |
||
179 | } |
||
180 | |||
181 | 1 | protected function getResponse() |
|
182 | { |
||
183 | 1 | $buffer = ''; |
|
184 | |||
185 | 1 | while (!feof($this->socket)) { |
|
186 | 1 | $buffer .= @fgets($this->socket, 1024); |
|
187 | |||
188 | 1 | if ("\r\n" === substr($buffer, -2)) { |
|
189 | 1 | break; |
|
190 | } |
||
191 | |||
192 | if ($buffer === false) { |
||
193 | $this->disconnect(); |
||
194 | throw new RuntimeException('Incorrect data received from buffer'); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | 1 | return Response::createFromString($buffer); |
|
199 | } |
||
200 | |||
201 | public function getMultiLineResponse(Response $response) |
||
228 | |||
229 | public function getCompressedResponse(Response $response) |
||
265 | |||
266 | /** |
||
267 | * @param string $address |
||
268 | */ |
||
269 | 2 | protected function getSocketUrl($address) |
|
278 | } |
||
279 |