1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the NNTP library. |
5
|
|
|
* |
6
|
|
|
* (c) Robin van der Vleuten <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Rvdv\Nntp; |
13
|
|
|
|
14
|
|
|
use Rvdv\Nntp\Command\CommandInterface; |
15
|
|
|
use Rvdv\Nntp\Connection\ConnectionInterface; |
16
|
|
|
use Rvdv\Nntp\Exception\RuntimeException; |
17
|
|
|
use Rvdv\Nntp\Response\Response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Robin van der Vleuten <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Client implements ClientInterface |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ConnectionInterface |
26
|
|
|
*/ |
27
|
|
|
private $connection; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor |
31
|
|
|
* |
32
|
|
|
* @param ConnectionInterface $connection |
33
|
|
|
*/ |
34
|
9 |
|
public function __construct(ConnectionInterface $connection) |
35
|
|
|
{ |
36
|
9 |
|
$this->connection = $connection; |
37
|
9 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the connection instance. |
41
|
|
|
* |
42
|
|
|
* @return ConnectionInterface |
43
|
|
|
*/ |
44
|
1 |
|
public function getConnection() |
45
|
|
|
{ |
46
|
1 |
|
return $this->connection; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
1 |
|
public function connect() |
53
|
|
|
{ |
54
|
1 |
|
return $this->connection->connect(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
2 |
|
public function disconnect() |
61
|
|
|
{ |
62
|
2 |
|
if (!$this->connection->disconnect()) { |
63
|
1 |
|
throw new RuntimeException('Error while disconnecting from NNTP server'); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
4 |
|
public function authenticate($username, $password = null) |
73
|
|
|
{ |
74
|
4 |
|
$command = $this->authInfo(Command\AuthInfoCommand::AUTHINFO_USER, (string) $username); |
75
|
4 |
|
$response = $command->getResponse(); |
76
|
|
|
|
77
|
4 |
|
if ($response->getStatusCode() === Response::PASSWORD_REQUIRED) { |
78
|
2 |
|
if (null === $password) { |
79
|
1 |
|
throw new RuntimeException('NNTP server asks for further authentication but no password is given'); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
$command = $this->authInfo(Command\AuthInfoCommand::AUTHINFO_PASS, (string) $password); |
83
|
1 |
|
$response = $command->getResponse(); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
3 |
|
if ($response->getStatusCode() !== Response::AUTHENTICATION_ACCEPTED) { |
87
|
1 |
|
throw new RuntimeException(sprintf('Could not authenticate with given username/password: %s', (string) $response)); |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
return $response; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function connectAndAuthenticate($username = null, $password = null) |
97
|
|
|
{ |
98
|
|
|
$response = $this->connect(); |
99
|
|
|
|
100
|
|
|
if (!in_array($response->getStatusCode(), array(Response::POSTING_ALLOWED, RESPONSE::POSTING_PROHIBITED))) { |
101
|
|
|
throw new RuntimeException(sprintf('Unsuccessful response received: %s', (string) $response)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($username !== null) { |
105
|
|
|
return $this->authenticate($username, $password); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $response; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
5 |
|
public function authInfo($type, $value) |
115
|
|
|
{ |
116
|
5 |
|
return $this->sendCommand(new Command\AuthInfoCommand($type, $value)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
1 |
|
public function group($name) |
123
|
|
|
{ |
124
|
1 |
|
return $this->sendCommand(new Command\GroupCommand($name)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
1 |
|
public function help() |
131
|
|
|
{ |
132
|
1 |
|
return $this->sendCommand(new Command\HelpCommand()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
1 |
|
public function overviewFormat() |
139
|
|
|
{ |
140
|
1 |
|
return $this->sendCommand(new Command\OverviewFormatCommand()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function post($groups, $subject, $body, $from, $headers = null) |
147
|
|
|
{ |
148
|
|
|
$command = $this->sendCommand(new Command\PostCommand()); |
149
|
|
|
$response = $command->getResponse(); |
150
|
|
|
|
151
|
|
|
if ($response->getStatusCode() === Response::SEND_ARTICLE) { |
152
|
|
|
$command = $this->postArticle($groups, $subject, $body, $from, $headers); |
153
|
|
|
$response = $command->getResponse(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($response->getStatusCode() !== Response::ARTICLE_RECEIVED) { |
157
|
|
|
throw new RuntimeException(sprintf('Posting failed: %s', (string) $response)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $response; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
|
|
public function postArticle($groups, $subject, $body, $from, $headers = null) |
167
|
|
|
{ |
168
|
|
|
return $this->sendArticle(new Command\PostArticleCommand($groups, $subject, $body, $from, $headers)); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
|
|
public function quit() |
175
|
|
|
{ |
176
|
|
|
return $this->sendCommand(new Command\QuitCommand()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
1 |
|
public function xfeature($feature) |
183
|
|
|
{ |
184
|
1 |
|
return $this->sendCommand(new Command\XFeatureCommand($feature)); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritdoc} |
189
|
|
|
*/ |
190
|
1 |
|
public function xover($from, $to, array $format) |
191
|
|
|
{ |
192
|
1 |
|
return $this->sendCommand(new Command\XoverCommand($from, $to, $format)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
1 |
|
public function xzver($from, $to, array $format) |
199
|
|
|
{ |
200
|
1 |
|
return $this->sendCommand(new Command\XzverCommand($from, $to, $format)); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
1 |
|
public function sendCommand(CommandInterface $command) |
207
|
|
|
{ |
208
|
1 |
|
return $this->connection->sendCommand($command); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritdoc} |
213
|
|
|
*/ |
214
|
5 |
|
public function sendArticle(CommandInterface $command) |
215
|
|
|
{ |
216
|
5 |
|
return $this->connection->sendArticle($command); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|