-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
553 lines (490 loc) · 17.1 KB
/
Client.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
// +-----------------------------------------------------------------------+
// | |
// | Copyright © 2003 Heino H. Gehlsen. All Rights Reserved. |
// | http://www.heino.gehlsen.dk/software/license |
// | |
// +-----------------------------------------------------------------------+
// | |
// | This work (including software, documents, or other related items) is |
// | being provided by the copyright holders under the following license. |
// | By obtaining, using and/or copying this work, you (the licensee) |
// | agree that you have read, understood, and will comply with the |
// | following terms and conditions: |
// | |
// | Permission to use, copy, modify, and distribute this software and |
// | its documentation, with or without modification, for any purpose and |
// | without fee or royalty is hereby granted, provided that you include |
// | the following on ALL copies of the software and documentation or |
// | portions thereof, including modifications, that you make: |
// | |
// | 1. The full text of this NOTICE in a location viewable to users of |
// | the redistributed or derivative work. |
// | |
// | 2. Any pre-existing intellectual property disclaimers, notices, or |
// | terms and conditions. If none exist, a short notice of the |
// | following form (hypertext is preferred, text is permitted) should |
// | be used within the body of any redistributed or derivative code: |
// | "Copyright © 2003 Heino H. Gehlsen. All Rights Reserved. |
// | http://www.heino.gehlsen.dk/software/license" |
// | |
// | 3. Notice of any changes or modifications to the files, including |
// | the date changes were made. (We recommend you provide URIs to |
// | the location from which the code is derived.) |
// | |
// | THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT |
// | HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, |
// | INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR |
// | FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE |
// | OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, |
// | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. |
// | |
// | COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, |
// | SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE |
// | SOFTWARE OR DOCUMENTATION. |
// | |
// | The name and trademarks of copyright holders may NOT be used in |
// | advertising or publicity pertaining to the software without specific, |
// | written prior permission. Title to copyright in this software and any |
// | associated documentation will at all times remain with copyright |
// | holders. |
// | |
// +-----------------------------------------------------------------------+
// | |
// | This license is based on the "W3C® SOFTWARE NOTICE AND LICENSE". |
// | No changes have been made to the "W3C® SOFTWARE NOTICE AND LICENSE", |
// | except for the references to the copyright holder, which has either |
// | been changes or removed. |
// | |
// +-----------------------------------------------------------------------+
// $Id: Client.php,v 1.2.2.2 2005/01/14 20:03:22 heino Exp $
require_once 'Net/NNTP/Protocol/Client.php';
// {{{ constants
/* NNTP Authentication modes */
define('NET_NNTP_CLIENT_AUTH_ORIGINAL', 'original');
define('NET_NNTP_CLIENT_AUTH_SIMPLE', 'simple');
define('NET_NNTP_CLIENT_AUTH_GENERIC', 'generic');
// }}}
// {{{ Net_NNTP_Client
/**
* Implementation of the client side of NNTP (Network News Transfer Protocol)
*
* The Net_NNTP_Client class is a frontend class to the Net_NNTP_Protocol_Client class.
*
* @category Net
* @package Net_NNTP
* @author Heino H. Gehlsen <heino@gehlsen.dk>
* @version $Id: Client.php,v 1.2.2.2 2005/01/14 20:03:22 heino Exp $
* @access public
* @see Net_NNTP_Protocol_Client
* @since Class available since Release 0.11.0
*/
class Net_NNTP_Client extends Net_NNTP_Protocol_Client
{
// {{{ properties
/**
* Used for storing information about the currently selected group
*
* @var array
* @access private
* @since 0.3
*/
var $_currentGroup = null;
// }}}
// {{{ constructor
/**
* Constructor
*
* @access public
*/
function Net_NNTP_Client()
{
parent::Net_NNTP_Protocol_Client();
}
// }}}
// {{{ connect()
/**
* Connect to the NNTP-server.
*
* @param optional string $host The adress of the NNTP-server to connect to.
* @param optional int $port The port to connect to.
*
* @return mixed (bool) true on success or (object) pear_error on failure
* @access public
* @see Net_NNTP_Client::quit()
* @see Net_NNTP_Client::authenticate()
* @see Net_NNTP_Client::connectAuthenticated()
*/
function connect($host = NET_NNTP_PROTOCOL_CLIENT_DEFAULT_HOST,
$port = NET_NNTP_PROTOCOL_CLIENT_DEFAULT_PORT)
{
return parent::connect($host, $port);
}
// }}}
// {{{ quit()
/**
* Close connection to the newsserver
*
* @access public
* @see Net_NNTP_Client::connect()
*/
function quit()
{
return $this->cmdQuit();
}
// }}}
// {{{ authenticate()
/**
* Authenticate
*
* Auth process (not yet standarized but used any way)
* http://www.mibsoftware.com/userkt/nntpext/index.html
*
* @param string $user The username
* @param optional string $pass The password
* @param optional string $mode The authentication mode (original, simple, generic).
*
* @return mixed (bool) true on success or (object) pear_error on failure
* @access public
* @see Net_NNTP_Client::connect()
* @see Net_NNTP_Client::connectAuthenticated()
*/
function authenticate($user, $pass, $mode = NET_NNTP_CLIENT_AUTH_ORIGINAL)
{
// Username is a must...
if ($user == null) {
return PEAR::throwError('No username supplied', null);
}
// Use selected authentication method
switch ($mode) {
case NET_NNTP_CLIENT_AUTH_ORIGINAL:
return $this->cmdAuthinfo($user, $pass);
break;
case NET_NNTP_CLIENT_AUTH_SIMPLE:
return $this->cmdAuthinfoSimple($user, $pass);
break;
case NET_NNTP_CLIENT_AUTH_GENERIC:
return $this->cmdAuthinfoGeneric($user, $pass);
break;
default:
return PEAR::throwError("The auth mode: '$mode' is unknown", null);
}
}
// }}}
// {{{ isConnected()
/**
* Test whether a connection is currently open.
*
* @return bool true or false
* @access public
* @see Net_NNTP_Client::connect()
* @see Net_NNTP_Client::quit()
*/
function isConnected()
{
return parent::isConnected();
}
// }}}
// {{{ selectGroup()
/**
* Selects a newsgroup
*
* @param string $newsgroup Newsgroup name
*
* @return mixed (array) Info about the newsgroup on success or (object) pear_error on failure
* @access public
* @see Net_NNTP_Client::group()
* @see Net_NNTP_Client::first()
* @see Net_NNTP_Client::last()
* @see Net_NNTP_Client::count()
* @see Net_NNTP_Client::getGroups()
*/
function selectGroup($newsgroup)
{
$response_arr = $this->cmdGroup($newsgroup);
if (PEAR::isError($response_arr)) {
return $response_arr;
}
// Store group info in the object
$this->_currentGroup = $response_arr;
return $response_arr;
}
// }}}
// {{{ getGroups()
/**
* Fetches a list of all avaible newsgroups
*
* @return mixed (array) nested array with informations about existing newsgroups on success or (object) pear_error on failure
* @access public
* @see Net_NNTP_Client::selectGroup()
* @see Net_NNTP_Client::getDescriptions()
*/
function getGroups()
{
// Get groups
$groups = $this->cmdList();
if (PEAR::isError($groups)) {
return $groups;
}
return $groups;
}
// }}}
// {{{ getDescriptions()
/**
* Fetches a list of all avaible newsgroup descriptions.
*
* @return mixed (array) nested array with description of existing newsgroups on success or (object) pear_error on failure
* @access public
* @see Net_NNTP_Client::getGroups()
*/
function getDescriptions()
{
// Get group descriptions
$descriptions = $this->cmdListNewsgroups();
if (PEAR::isError($descriptions)) {
return $descriptions;
}
return $descriptions;
}
// }}}
// {{{ getOverview()
/**
* Fetch message header fields from message number $first to $last
*
* The format of the returned array is:
* $messages[message_id][header_name]
*
* @param integer $first first article to fetch
* @param integer $last last article to fetch
*
* @return mixed (array) nested array of message and their headers on success or (object) pear_error on failure
* @access public
* @see Net_NNTP_Client::getOverviewFormat()
* @see Net_NNTP_Client::getReferencesOverview()
*/
function getOverview($first, $last)
{
$overview = $this->cmdXOver($first.'-'.$last);
if (PEAR::isError($overview)) {
return $overview;
}
return $overview;
}
// }}}
// {{{ getOverviewFmt()
/**
* Returns a list of avaible headers which are send from NNTP-server to the client for every news message
*
* @return mixed (array) header names on success or (object) pear_error on failure
* @access public
* @see Net_NNTP_Client::getOverview()
*/
function getOverviewFormat()
{
return $this->cmdListOverviewFmt();
}
// }}}
// {{{ post()
/**
* Post an article to a number of newsgroups.
*
* (Among the aditional headers you might think of adding could be:
* "NNTP-Posting-Host: <ip-of-author>", which should contain the IP-address
* of the author of the post, so the message can be traced back to him.
* Or "Organization: <org>" which contain the name of the organization
* the post originates from)
*
* @param string $newsgroups The newsgroup to post to.
* @param string $subject The subject of the post.
* @param string $body The body of the post itself.
* @param string $from Name + email-adress of sender.
* @param optional string $aditional Aditional headers to send.
*
* @return mixed (string) server response on success or (object) pear_error on failure
* @access public
*/
function post($newsgroups, $subject, $body, $from, $aditional = null)
{
return $this->cmdPost($newsgroups, $subject, $body, $from, $aditional);
}
// }}}
// {{{ getArticleRaw()
/**
* Get a article (raw data)
*
* @param mixed $article Either the message-id or the message-number on the server of the article to fetch.
* @param optional bool $implode When true the result array is imploded to a string, defaults to false.
*
* @return mixed (array/string) The article on success or (object) pear_error on failure
* @access public
* @see Net_NNTP_Client::getArticle()
* @see Net_NNTP_Client::getHeaderRaw()
* @see Net_NNTP_Client::getBodyRaw()
*/
function getArticleRaw($article, $implode = false)
{
$data = $this->cmdArticle($article);
if (PEAR::isError($data)) {
return $data;
}
if ($implode == true) {
$data = implode("\r\n", $data);
}
return $data;
}
// }}}
// {{{ getHeaderRaw()
/**
* Get the header of an article (raw data)
*
* @param mixed $article Either the (string) message-id or the (int) message-number on the server of the article to fetch.
* @param optional bool $implode When true the result array is imploded to a string, defaults to false.
*
* @return mixed (array/string) header fields on success or (object) pear_error on failure
* @access public
* @see Net_NNTP_Client::getHeader()
* @see Net_NNTP_Client::getArticleRaw()
* @see Net_NNTP_Client::getBodyRaw()
*/
function getHeaderRaw($article, $implode = false)
{
$data = $this->cmdHead($article);
if (PEAR::isError($data)) {
return $data;
}
if ($implode == true) {
$data = implode("\r\n", $data);
}
return $data;
}
// }}}
// {{{ getBodyRaw()
/**
* Get the body of an article (raw data)
*
* @param mixed $article Either the message-id or the message-number on the server of the article to fetch.
* @param optional bool $implode When true the result array is imploded to a string, defaults to false.
*
* @return mixed (array/string) body on success or (object) pear_error on failure
* @access public
* @see Net_NNTP_Client::getBody()
* @see Net_NNTP_Client::getHeaderRaw()
* @see Net_NNTP_Client::getArticleRaw()
*/
function getBodyRaw($article, $implode = false)
{
$data = $this->cmdBody($article);
if (PEAR::isError($data)) {
return $data;
}
if ($implode == true) {
$data = implode("\r\n", $data);
}
return $data;
}
// }}}
// {{{ getDate()
/**
* Get the NNTP-server's internal date
*
* Get the date from the newsserver format of returned date:
*
* @param optional int $format
* - 0: $date - timestamp
* - 1: $date['y'] - year
* $date['m'] - month
* $date['d'] - day
*
* @return mixed (mixed) date on success or (object) pear_error on failure
* @access public
* @since 0.3
*/
function getDate($format = 1)
{
$date = $this->cmdDate();
if (PEAR::isError($date)) {
return $date;
}
switch ($format) {
case 1:
return array('y' => substr($date, 0, 4), 'm' => substr($date, 4, 2), 'd' => substr($date, 6, 2));
break;
case 0:
default:
return $date;
break;
}
}
// }}}
// {{{ count()
/**
* Number of articles in currently selected group
*
* @return integer number of article in group
* @access public
* @since 0.3
* @see Net_NNTP_Client::group()
* @see Net_NNTP_Client::first()
* @see Net_NNTP_Client::last()
* @see Net_NNTP_Client::selectGroup()
*/
function count()
{
return $this->_currentGroup['count'];
}
// }}}
// {{{ last()
/**
* Maximum article number in currently selected group
*
* @return integer number of last article
* @access public
* @since 0.3
* @see Net_NNTP_Client::first()
* @see Net_NNTP_Client::group()
* @see Net_NNTP_Client::count()
* @see Net_NNTP_Client::selectGroup()
*/
function last()
{
return $this->_currentGroup['last'];
}
// }}}
// {{{ first()
/**
* Minimum article number in currently selected group
*
* @return integer number of first article
* @access public
* @since 0.3
* @see Net_NNTP_Client::last()
* @see Net_NNTP_Client::group()
* @see Net_NNTP_Client::count()
* @see Net_NNTP_Client::selectGroup()
*/
function first()
{
return $this->_currentGroup['first'];
}
// }}}
// {{{ group()
/**
* Currently selected group
*
* @return string group name
* @access public
* @since 0.3
* @see Net_NNTP_Client::first()
* @see Net_NNTP_Client::last()
* @see Net_NNTP_Client::count()
* @see Net_NNTP_Client::selectGroup()
*/
function group()
{
return $this->_currentGroup['group'];
}
// }}}
}
// }}}
?>