2 /********************************************************************************
3 * include/NetNNTP.inc.php : NNTP subroutines
4 * -------------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
11 * implements some basic functions for NNTP protocol
15 /** socket filehandle */
17 /** posting allowed */
19 /** last NNTP error code */
21 /** last NNTP error text */
25 * @param $_host STRING NNTP host
26 * @param $_timeout INTEGER socket timeout
27 * @param $_reader BOOLEAN sends a "MODE READER" at connection if true
30 function nntp($_url, $_timeout=120, $_reader=true
)
33 $url = parse_url($_url);
34 $this->ns
= fsockopen($url['host'], $url['port'], $errno, $errstr, $_timeout);
40 $result = $this->gline();
41 $this->posting
= (substr($result, 0, 3)=="200");
42 if ($_reader && ($result{0}=="2")) {
43 $this->pline("MODE READER\r\n");
44 $result = $this->gline();
45 $this->posting
= ($result{0}=="200");
47 if ($result{0}=="2" && $url['user'] && $url['user']!='anonymous') {
48 return $this->authinfo($url['user'], $url['pass']);
50 return ($result{0}=="2");
55 /** get a line from server
61 return rtrim(fgets($this->ns
, 1200));
64 /** puts a line on server
65 * @param STRING $_line line to put
68 function pline($_line)
70 return fputs($this->ns
, $_line, strlen($_line));
73 # strict NNTP Functions [RFC 977]
74 # see http://www.faqs.org/rfcs/rfc977.html
77 * @param $_user STRING login
78 * @param $_pass INTEGER password
79 * @return BOOLEAN true if authentication was successful
82 function authinfo($_user, $_pass)
84 $user = preg_replace("/(\r|\n)/", "", $_user);
85 $pass = preg_replace("/(\r|\n)/", "", $_pass);
86 $this->pline("AUTHINFO USER $user\r\n");
88 $this->pline("AUTHINFO PASS $pass\r\n");
89 $result=$this->gline();
90 if ($result{0}!="2") {
91 $this->lasterrorcode
= substr($result, 0, 3);
92 $this->lasterrortext
= substr($result, 4);
98 /** retrieves an article
99 * MSGID is a numeric ID a shown in article's headers. MSGNUM is a
100 * server-dependent ID (see X-Ref on many servers) and retriving
101 * an article by this way will change the current article pointer.
102 * If an error occur, false is returned.
103 * @param $_msgid STRING MSGID or MSGNUM of article
104 * @return ARRAY lines of the article
109 function article($_msgid="")
111 $msgid = preg_replace("/(\r|\n)/", "", $_msgid);
112 $this->pline("ARTICLE $msgid\r\n");
113 $result = $this->gline();
114 if ($result{0} != '2') {
115 $this->lasterrorcode
= substr($result, 0, 3);
116 $this->lasterrortext
= substr($result, 4);
119 $result = $this->gline();
120 while ($result != ".") {
122 $result = $this->gline();
128 * if an error occur, false is returned
129 * @param $_message STRING message to post
130 * @return STRING MSGID of article
133 function post($_message)
135 if (is_array($_message)) {
136 $message=join("\n", $_message);
140 $this->pline("POST \r\n");
141 $result=$this->gline();
142 if ($result{0} != '3') {
143 $this->lasterrorcode
= substr($result, 0, 3);
144 $this->lasterrortext
= substr($result, 4);
147 $this->pline($message."\r\n.\r\n");
148 $result = $this->gline();
149 if ($result{0} != '2') {
150 $this->lasterrorcode
= substr($result, 0, 3);
151 $this->lasterrortext
= substr($result, 4);
154 if ($result{0} == '2') {
155 if (preg_match("/(<[^@>]+@[^@>]+>)/", $result, $regs)) {
164 /** fetches the body of an article
165 * params are the same as article
166 * @param $_msgid STRING MSGID or MSGNUM of article
167 * @return ARRAY lines of the article
172 function body($_msgid="")
174 $msgid = preg_replace("/(\r|\n)/", "", $_msgid);
175 $this->pline("BODY $msgid\r\n");
176 $result = $this->gline();
177 if ($result{0} != '2') {
178 $this->lasterrorcode
= substr($result, 0, 3);
179 $this->lasterrortext
= substr($result, 4);
183 while (($result = $this->gline()) != ".") {
189 /** fetches the headers of an article
190 * params are the same as article
191 * @param $_msgid STRING MSGID or MSGNUM of article
192 * @return ARRAY lines of the article
197 function head($_msgid="")
199 $msgid = preg_replace("/(\r|\n)/", "", $_msgid);
200 $this->pline("HEAD $msgid\r\n");
201 $result = $this->gline();
202 if ($result{0}!="2") {
203 $this->lasterrorcode
= substr($result, 0, 3);
204 $this->lasterrortext
= substr($result, 4);
207 $result = $this->gline();
208 while ($result != ".") {
210 $result = $this->gline();
215 /** set current group
216 * @param $_group STRING
217 * @return ARRAY array : nb of articles in group, MSGNUM of first article, MSGNUM of last article, and group name
220 function group($_group)
222 $group = preg_replace("/(\r|\n)/", "", $_group);
223 $this->pline("GROUP $group\r\n");
224 $line = $this->gline();
226 $this->lasterrorcode
= substr($line, 0, 3);
227 $this->lasterrortext
= substr($line, 4);
230 if (preg_match("/^2\d{2} (\d+) (\d+) (\d+) ([^ ]+)/", $line, $regs)) {
231 return array($regs[1], $regs[2], $regs[3], $regs[4]);
236 /** set the article pointer to the previous article in current group
237 * @return STRING MSGID of article
243 $this->pline("LAST \r\n");
244 $line = $this->gline();
246 $this->lasterrorcode
= substr($result, 0, 3);
247 $this->lasterrortext
= substr($result, 4);
250 if (preg_match("/^2\d{2} \d+ <([^>]+)>/", $line, $regs)) {
251 return "<{$regs[1]}>";
256 /** set the article pointer to the next article in current group
257 * @return STRING MSGID of article
263 $this->pline("NEXT \r\n");
264 $line = $this->gline();
266 $this->lasterrorcode
= substr($result, 0, 3);
267 $this->lasterrortext
= substr($result, 4);
270 if (preg_match("/^2\d{2} \d+ <([^>]+)>/", $line, $regs)) {
271 return "<{$regs[1]}>";
276 /** set the current article pointer
277 * @param $_msgid STRING MSGID or MSGNUM of article
278 * @return BOOLEAN true if authentication was successful, error code otherwise
283 function nntpstat($_msgid)
285 $msgid = preg_replace("/(\r|\n)/", "", $_msgid);
286 $this->pline("STAT $msgid\r\n");
287 $line = $this->gline();
289 $this->lasterrorcode
= substr($result, 0, 3);
290 $this->lasterrortext
= substr($result, 4);
293 if (preg_match("/^2\d{2} \d+ <([^>]+)>/", $line, $regs)) {
294 return "<{$regs[1]}>";
299 /** returns true if posting is allowed
300 * @return BOOLEAN true if posting is allowed lines
305 return ($this->posting
);
308 /** gets information about all active newsgroups
309 * @return ARRAY group name => (MSGNUM of first article, MSGNUM of last article, NNTP flags)
315 $this->pline("LIST\r\n");
316 if (substr($this->gline(), 0, 1)!="2") return false
;
317 $result = $this->gline();
319 while ($result != ".") {
320 preg_match("/([^ ]+) (\d+) (\d+) (.)/", $result, $regs);
321 $array[$regs[1]] = array(intval($regs[2]), intval($regs[3]), intval($regs[4]));
322 $result = $this->gline();
327 /** get information about recent newsgroups
328 * same as list, but information are limited to newgroups created after $_since
329 * @param $_since INTEGER unix timestamp
330 * @param $_distributions STRING distributions
331 * @return ARRAY same format as liste
335 function newgroups($_since, $_distributions="")
337 #assume $_since is a unix timestamp
338 $distributions = preg_replace("/(\r|\n)/", "", $_distributions);
339 $this->pline("NEWGROUPS ".gmdate("ymd His", $_since)
340 ." GMT $distributions\r\n");
341 if (substr($this->gline(), 0, 1)!="2") {
344 $result = $this->gline();
346 while ($result != ".") {
347 preg_match("/([^ ]+) (\d+) (\d+) (.)/", $result, $regs);
348 $array[$regs[1]] = array(intval($regs[2]), intval($regs[3]), intval($regs[4]));
349 $result = $this->gline();
354 /** gets a list of new articles
355 * @param $_since INTEGER unix timestamp
356 * @parma $_groups STRING pattern of intersting groups
357 * @return ARRAY MSGID of new articles
360 function newnews($_since, $_groups="*", $_distributions="")
362 $distributions = preg_replace("/(\r|\n)/", "", $_distributions);
363 $groups = preg_replace("/(\r|\n)/", "", $_groups);
365 #assume $since is a unix timestamp
366 $this->pline("NEWNEWS $_groups ".gmdate("ymd His", $_since)." GMT $distributions\r\n");
367 if (substr($this->gline(), 0, 1)!="2") {
370 while (($result = $this->gline()) != ".") {
376 /** Tell the remote server that I am not a user client, but probably another news server
377 * @return BOOLEAN true if sucessful
382 $this->pline("SLAVE \r\n");
383 return (substr($this->gline(), 0, 1)=="2");
386 /** implements IHAVE method
387 * @param $_msgid STRING MSGID of article
388 * @param $_message STRING article
392 function ihave($_msgid, $_message=false
)
394 $msgid = preg_replace("/(\r|\n)/", "", $_msgid);
395 if (is_array($message)) {
396 $message = join("\n", $_message);
398 $message = $_message;
400 $this->pline("IHAVE $msgid \r\n");
401 $result = $this->gline();
402 if ($message && ($result{0}=="3")) {
403 $this->pline("$message\r\n.\r\n");
404 $result = $this->gline();
406 return ($result{0}=="2");
409 /** closes connection to server
414 $this->pline("QUIT\r\n");
419 # NNTP Extensions [RFC 2980]
421 /** Returns the date on the remote server
422 * @return INTEGER timestamp
427 $this->pline("DATE \r\n");
428 $result = $this->gline();
429 if (preg_match("/^111 (\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/", $result, $r)) {
430 return gmmktime($r[4], $r[5], $r[6], $r[2], $r[3], $r[1]);
435 /** returns group descriptions
436 * @param $_pattern STRING pattern of intersting groups
437 * @return ARRAY group name => description
440 function xgtitle($_pattern="*")
442 $pattern = preg_replace("/[\r\n]/", "", $_pattern);
443 $this->pline("XGTITLE $pattern \r\n");
444 if (substr($this->gline(), 0, 1)!="2") return false
;
445 $result = $this->gline();
446 while ($result != ".") {
447 preg_match("/([^ \t]+)[ \t]+(.+)$/", $result, $regs);
448 $array[$regs[1]] = $regs[2];
449 $result = $this->gline();
454 /** obtain the header field $hdr for all the messages specified
455 * @param $_hdr STRING name of the header (eg: 'From')
456 * @param $_range STRING range of articles
457 * @return ARRAY MSGNUM => header value
460 function xhdr($_hdr, $_range="")
462 $hdr = preg_replace("/(\r|\n)/", "", $_hdr);
463 $range = preg_replace("/(\r|\n)/", "", $_range);
464 $this->pline("XHDR $hdr $range \r\n");
465 if (substr($this->gline(), 0, 1)!="2") {
470 while (($result = $this->gline()) != '.') {
471 preg_match("/([^ \t]+) (.*)$/", $result, $regs);
472 $array[$regs[1]] = $regs[2];
477 /** obtain the header field $_hdr matching $_pat for all the messages specified
478 * @param $_hdr STRING name of the header (eg: 'From')
479 * @param $_range STRING range of articles
480 * @param $_pat STRING pattern
481 * @return ARRAY MSGNUM => header value
484 function xpat($_hdr, $_range, $_pat)
486 $hdr = preg_replace("/(\r|\n)/", "", $_hdr);
487 $range = preg_replace("/(\r|\n)/", "", $_range);
488 $pat = preg_replace("/(\r|\n)/", "", $_pat);
489 $this->pline("XPAT $hdr $range $pat\r\n");
490 if (substr($this->gline(), 0, 1)!="2") {
493 $result = $this->gline();
494 while ($result != ".") {
495 preg_match("/([^ \t]+) (.*)$/", $result, $regs);
496 $array[$regs[1]] = $regs[2];
497 $result = $this->gline();