2 /********************************************************************************
3 * include/nntpcore.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 ********************************************************************************/
10 require_once dirname(__FILE__
) . '/banana.inc.php';
13 * implements some basic functions for NNTP protocol
17 /** socket filehandle */
19 /** posting allowed */
21 /** last NNTP error code */
22 private $lasterrorcode;
23 /** last NNTP error text */
24 private $lasterrortext;
25 /** last NNTP result code */
26 private $lastresultcode;
27 /** last NNTP result */
28 private $lastresulttext;
31 private $debug = false
;
34 * @param $host STRING NNTP host
35 * @parma $port STRING NNTP port
36 * @param $timeout INTEGER socket timeout
37 * @param $reader BOOLEAN sends a "MODE READER" at connection if true
39 public function __construct($host, $port = 119, $timeout = 120, $reader = true
)
41 if (Banana
::$debug_nntp) {
44 $this->ns
= fsockopen($host, $port, $errno, $errstr, $timeout);
45 $this->lasterrorcode
= $errno;
46 $this->lasterrortext
= $errstr;
47 if (is_null($this->ns
)) {
52 $this->posting
= ($this->lastresultcode
== '200');
53 if ($reader && $this->posting
) {
54 $this->execLine('MODE READER');
55 $this->posting
= ($this->lastresultcode
== '200');
57 if (!$this->posting
) {
62 public function __destruct()
69 public function isValid()
71 return !is_null($this->ns
) && $this->posting
;
74 public function lastErrNo()
76 return $this->lasterrorcode
;
79 public function lastError()
81 return $this->lasterrortext
;
86 /** get a line from server
89 private function getLine()
91 return rtrim(fgets($this->ns
, 1200));
94 /** fetch data (and on delimitor)
95 * @param STRING $delim string indicating and of transmission
97 private function fetchResult($callback = null
)
100 while (($result = $this->getLine()) != '.') {
101 if (!is_null($callback)) {
102 list($key, $result) = call_user_func($callback, $result);
103 if (is_null($result)) {
109 $array[$key] = $result;
118 /** puts a line on server
119 * @param STRING $line line to put
121 private function putLine($line, $format = false
)
124 $line = str_replace(array("\r", "\n"), '', $line);
128 $db_line = preg_replace('/PASS .*/', 'PASS *******', $line);
131 return fputs($this->ns
, $line, strlen($line));
134 /** put a message (multiline)
136 private function putMessage($message = false
)
138 if (is_array($message)) {
139 $message = join("\n", $_message);
142 $this->putLine("$message\r\n", false
);
144 return $this->execLine('.');
148 /** exec a command a check result
149 * @param STRING $line line to exec
151 private function execLine($line, $strict_state = true
)
153 if (!$this->putLine($line, true
)) {
156 return $this->checkState($strict_state);
159 /** check if last command was successfull (read one line)
160 * @param BOOL $strict indicate if 1XX codes are interpreted as errors (true) or success (false)
162 private function checkState($strict = true
)
164 $result = $this->getLine();
168 $this->lastresultcode
= substr($result, 0, 3);
169 $this->lastresulttext
= substr($result, 4);
170 $c = $this->lastresultcode
{0};
171 if ($c == '2' ||
(($c == '1' ||
$c == '3') && !$strict)) {
174 $this->lasterrorcode
= $this->lastresultcode
;
175 $this->lasterrortext
= $this->lastresulttext
;
180 # strict NNTP Functions [RFC 977]
181 # see http://www.faqs.org/rfcs/rfc977.html
184 * @param $user STRING login
185 * @param $pass INTEGER password
186 * @return BOOLEAN true if authentication was successful
188 protected function authinfo($user, $pass)
190 if ($this->execLine("AUTHINFO USER $user", false
)) {
191 return $this->execline("AUTHINFO PASS $pass");
196 /** retrieves an article
197 * MSGID is a numeric ID a shown in article's headers. MSGNUM is a
198 * server-dependent ID (see X-Ref on many servers) and retriving
199 * an article by this way will change the current article pointer.
200 * If an error occur, false is returned.
201 * @param $_msgid STRING MSGID or MSGNUM of article
202 * @return ARRAY lines of the article
206 protected function article($msgid = "")
208 if (!$this->execLine("ARTICLE $msgid")) {
211 return $this->fetchResult();
215 * if an error occur, false is returned
216 * @param $_message STRING message to post
217 * @return STRING MSGID of article
219 protected function post($message)
221 if (!$this->execLine("POST ", false
)) {
224 if (!$this->putMessage($message)) {
227 if (preg_match("/(<[^@>]+@[^@>]+>)/", $this->lastresulttext
, $regs)) {
234 /** fetches the body of an article
235 * params are the same as article
236 * @param $_msgid STRING MSGID or MSGNUM of article
237 * @return ARRAY lines of the article
241 protected function body($msgid = '')
243 if ($this->execLine("BODY $msgid")) {
246 return $this->fetchResult();
249 /** fetches the headers of an article
250 * params are the same as article
251 * @param $_msgid STRING MSGID or MSGNUM of article
252 * @return ARRAY lines of the article
256 protected function head($msgid = '')
258 if (!$this->execLine("HEAD $msgid")) {
261 return $this->fetchResult();
264 /** set current group
265 * @param $_group STRING
266 * @return ARRAY array : nb of articles in group, MSGNUM of first article, MSGNUM of last article, and group name
268 protected function group($group)
270 if (!$this->execLine("GROUP $group")) {
273 $array = explode(' ', $this->lastresulttext
);
274 if (count($array) >= 4) {
275 return array_slice($array, 0, 4);
280 /** set the article pointer to the previous article in current group
281 * @return STRING MSGID of article
284 protected function last()
286 if (!$this->execLine("LAST ")) {
289 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
295 /** set the article pointer to the next article in current group
296 * @return STRING MSGID of article
300 protected function next()
302 if (!$this->execLine('NEXT ')) {
305 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
311 /** set the current article pointer
312 * @param $_msgid STRING MSGID or MSGNUM of article
313 * @return BOOLEAN true if authentication was successful, error code otherwise
317 protected function nntpstat($msgid)
319 if (!$this->execLine("STAT $msgid")) {
322 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
328 /** filter group list
330 private function filterGroups()
332 $list = $this->fetchResult();
335 foreach ($list as $result) {
336 list($group, $last, $first, $p) = explode(' ', $result, 4);
337 if (!is_null(Banana
::$boxpattern) ||
preg_match('@' . Banana
::$boxpattern . '@i', $group)) {
338 $groups[$group] = array(intval($last), intval($first), $p);
344 /** gets information about all active newsgroups
345 * @return ARRAY group name => (MSGNUM of first article, MSGNUM of last article, NNTP flags)
348 protected function listGroups()
350 if (!$this->execLine('LIST')) {
353 return $this->filterGroups();
356 /** format date for news server
357 * @param since UNIX TIMESTAMP
359 protected function formatDate($since)
361 return gmdate("ymd His", $since) . ' GMT';
364 /** get information about recent newsgroups
365 * same as list, but information are limited to newgroups created after $_since
366 * @param $_since INTEGER unix timestamp
367 * @param $_distributions STRING distributions
368 * @return ARRAY same format as liste
371 protected function newgroups($since, $distributions = '')
373 if (!($since = $this->formatDate($since))) {
376 if (!$this->execLine("NEWGROUPS $since $distributions")) {
379 return $this->filterGroups();
382 /** gets a list of new articles
383 * @param $_since INTEGER unix timestamp
384 * @parma $_groups STRING pattern of intersting groups
385 * @return ARRAY MSGID of new articles
387 protected function newnews($groups = '*', $since = 0, $distributions = '')
389 if (!($since = $this->formatDate($since))) {
392 if (!$this->execLine("NEWNEWS $groups $since $distributions")) {
395 return $this->fetchResult();
398 /** Tell the remote server that I am not a user client, but probably another news server
399 * @return BOOLEAN true if sucessful
401 protected function slave()
403 return $this->execLine("SLAVE ");
406 /** implements IHAVE method
407 * @param $_msgid STRING MSGID of article
408 * @param $_message STRING article
411 protected function ihave($msgid, $message = false
)
413 if (!$this->execLine("IHAVE $msgid ")) {
416 return $this->putMessage($message);
419 /** closes connection to server
421 protected function quit()
423 $this->execLine('QUIT');
426 $this->posting
= false
;
429 # NNTP Extensions [RFC 2980]
431 /** Returns the date on the remote server
432 * @return INTEGER timestamp
435 protected function date()
437 if (!$this->execLine('DATE ', false
)) {
440 if (preg_match("/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/", $this->lastresulttext
, $r)) {
441 return gmmktime($r[4], $r[5], $r[6], $r[2], $r[3], $r[1]);
446 /** returns group descriptions
447 * @param $_pattern STRING pattern of intersting groups
448 * @return ARRAY group name => description
451 protected function xgtitle($pattern = '*')
453 if (!$this->execLine("XGTITLE $pattern ")) {
456 $array = $this->fetchResult();
458 foreach ($array as $result) {
459 list($group, $desc) = split("[ \t]", $result, 2);
460 $groups[$group] = $desc;
465 /** obtain the header field $hdr for all the messages specified
466 * @param $_hdr STRING name of the header (eg: 'From')
467 * @param $_range STRING range of articles
468 * @return ARRAY MSGNUM => header value
470 protected function xhdr($hdr, $first = null
, $last = null
)
472 if (is_null($first) && is_null($last)) {
475 $range = $first . '-' . $last;
477 if (!$this->execLine("XHDR $hdr $range ")) {
480 $array = $this->fetchResult();
482 foreach ($array as &$result) {
483 @list
($head, $value) = explode(' ', $result, 2);
484 $headers[$head] = $value;
489 /** obtain the header field $_hdr matching $_pat for all the messages specified
490 * @param $_hdr STRING name of the header (eg: 'From')
491 * @param $_range STRING range of articles
492 * @param $_pat STRING pattern
493 * @return ARRAY MSGNUM => header value
495 protected function xpat($_hdr, $_range, $_pat)
497 if (!$this->execLine("XPAT $hdr $range $pat")) {
500 $array = $this->fetchResult();
502 foreach ($array as &$result) {
503 list($head, $value) = explode(' ', $result, 2);
504 $headers[$head] = $result;
510 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: