0a1f3c115ec82941a8a4c7e496ad33f0610bfc8f
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
;
32 private $bt = array();
35 * @param $host STRING NNTP host
36 * @parma $port STRING NNTP port
37 * @param $timeout INTEGER socket timeout
38 * @param $reader BOOLEAN sends a "MODE READER" at connection if true
40 public function __construct($host, $port = 119, $timeout = 120, $reader = true
)
42 if (Banana
::$debug_nntp) {
45 $this->ns
= fsockopen($host, $port, $errno, $errstr, $timeout);
46 $this->lasterrorcode
= $errno;
47 $this->lasterrortext
= $errstr;
48 if (is_null($this->ns
)) {
53 $this->posting
= ($this->lastresultcode
== '200');
54 if ($reader && $this->posting
) {
55 $this->execLine('MODE READER');
56 $this->posting
= ($this->lastresultcode
== '200');
58 if (!$this->posting
) {
63 public function __destruct()
70 public function isValid()
72 return !is_null($this->ns
) && $this->posting
;
75 public function lastErrNo()
77 return $this->lasterrorcode
;
80 public function lastError()
82 return $this->lasterrortext
;
85 public function backtrace()
96 /** get a line from server
99 private function getLine()
101 return rtrim(@fgets
($this->ns
, 1200));
104 /** fetch data (and on delimitor)
105 * @param STRING $delim string indicating and of transmission
107 private function fetchResult($callback = null
)
110 while (($result = $this->getLine()) != '.') {
111 if (!is_null($callback)) {
112 list($key, $result) = call_user_func($callback, $result);
113 if (is_null($result)) {
119 $array[$key] = $result;
125 if ($this->debug
&& $this->bt
) {
126 $this->bt
[count($this->bt
) - 1]['response'] = count($array);
131 /** puts a line on server
132 * @param STRING $line line to put
134 private function putLine($line, $format = false
)
137 $line = str_replace(array("\r", "\n"), '', $line);
141 $db_line = preg_replace('/PASS .*/', 'PASS *******', $line);
142 $this->bt
[] = array('action' => $db_line, 'time' => microtime(true
));
144 return @fputs
($this->ns
, $line, strlen($line));
147 /** put a message (multiline)
149 private function putMessage($message = false
)
151 if (is_array($message)) {
152 $message = join("\n", $_message);
155 $message = preg_replace("/(^|\n)\./", '\1..', $message);
156 $this->putLine("$message\r\n", false
);
158 return $this->execLine('.');
162 /** exec a command a check result
163 * @param STRING $line line to exec
165 private function execLine($line, $strict_state = true
)
167 if (!$this->putLine($line, true
)) {
170 return $this->checkState($strict_state);
173 /** check if last command was successfull (read one line)
174 * @param BOOL $strict indicate if 1XX codes are interpreted as errors (true) or success (false)
176 private function checkState($strict = true
)
178 $result = $this->getLine();
179 $this->lastresultcode
= substr($result, 0, 3);
180 $this->lastresulttext
= substr($result, 4);
181 if ($this->debug
&& $this->bt
) {
182 $trace =& $this->bt
[count($this->bt
) - 1];
183 $trace['time'] = microtime(true
) - $trace['time'];
184 $trace['code'] = $this->lastresultcode
;
185 $trace['message'] = $this->lastresulttext
;
186 $trace['response'] = 0;
188 $c = $this->lastresultcode
{0};
189 if ($c == '2' ||
(($c == '1' ||
$c == '3') && !$strict)) {
192 $this->lasterrorcode
= $this->lastresultcode
;
193 $this->lasterrortext
= $this->lastresulttext
;
198 # strict NNTP Functions [RFC 977]
199 # see http://www.faqs.org/rfcs/rfc977.html
202 * @param $user STRING login
203 * @param $pass INTEGER password
204 * @return BOOLEAN true if authentication was successful
206 protected function authinfo($user, $pass)
208 if ($this->execLine("AUTHINFO USER $user", false
)) {
209 return $this->execline("AUTHINFO PASS $pass");
214 /** retrieves an article
215 * MSGID is a numeric ID a shown in article's headers. MSGNUM is a
216 * server-dependent ID (see X-Ref on many servers) and retriving
217 * an article by this way will change the current article pointer.
218 * If an error occur, false is returned.
219 * @param $_msgid STRING MSGID or MSGNUM of article
220 * @return ARRAY lines of the article
224 protected function article($msgid = "")
226 if (!$this->execLine("ARTICLE $msgid")) {
229 return $this->fetchResult();
233 * if an error occur, false is returned
234 * @param $_message STRING message to post
235 * @return STRING MSGID of article
237 protected function post($message)
239 if (!$this->execLine("POST ", false
)) {
242 if (!$this->putMessage($message)) {
245 if (preg_match("/(<[^@>]+@[^@>]+>)/", $this->lastresulttext
, $regs)) {
252 /** fetches the body of an article
253 * params are the same as article
254 * @param $_msgid STRING MSGID or MSGNUM of article
255 * @return ARRAY lines of the article
259 protected function body($msgid = '')
261 if ($this->execLine("BODY $msgid")) {
264 return $this->fetchResult();
267 /** fetches the headers of an article
268 * params are the same as article
269 * @param $_msgid STRING MSGID or MSGNUM of article
270 * @return ARRAY lines of the article
274 protected function head($msgid = '')
276 if (!$this->execLine("HEAD $msgid")) {
279 return $this->fetchResult();
282 /** set current group
283 * @param $_group STRING
284 * @return ARRAY array : nb of articles in group, MSGNUM of first article, MSGNUM of last article, and group name
286 protected function group($group)
288 if (!$this->execLine("GROUP $group")) {
291 $array = explode(' ', $this->lastresulttext
);
292 if (count($array) >= 4) {
293 return array_slice($array, 0, 4);
298 /** set the article pointer to the previous article in current group
299 * @return STRING MSGID of article
302 protected function last()
304 if (!$this->execLine("LAST ")) {
307 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
313 /** set the article pointer to the next article in current group
314 * @return STRING MSGID of article
318 protected function next()
320 if (!$this->execLine('NEXT ')) {
323 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
329 /** set the current article pointer
330 * @param $_msgid STRING MSGID or MSGNUM of article
331 * @return BOOLEAN true if authentication was successful, error code otherwise
335 protected function nntpstat($msgid)
337 if (!$this->execLine("STAT $msgid")) {
340 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
346 /** filter group list
348 private function filterGroups()
350 $list = $this->fetchResult();
353 foreach ($list as $result) {
354 list($group, $last, $first, $p) = explode(' ', $result, 4);
355 if (!Banana
::$boxpattern ||
preg_match('@' . Banana
::$boxpattern . '@i', $group)) {
356 $groups[$group] = array(intval($last), intval($first), $p);
362 /** gets information about all active newsgroups
363 * @return ARRAY group name => (MSGNUM of first article, MSGNUM of last article, NNTP flags)
366 protected function listGroups()
368 if (!$this->execLine('LIST')) {
371 return $this->filterGroups();
374 /** format date for news server
375 * @param since UNIX TIMESTAMP
377 protected function formatDate($since)
379 return gmdate("ymd His", $since) . ' GMT';
382 /** get information about recent newsgroups
383 * same as list, but information are limited to newgroups created after $_since
384 * @param $_since INTEGER unix timestamp
385 * @param $_distributions STRING distributions
386 * @return ARRAY same format as liste
389 protected function newgroups($since, $distributions = '')
391 if (!($since = $this->formatDate($since))) {
394 if (!$this->execLine("NEWGROUPS $since $distributions")) {
397 return $this->filterGroups();
400 /** gets a list of new articles
401 * @param $_since INTEGER unix timestamp
402 * @parma $_groups STRING pattern of intersting groups
403 * @return ARRAY MSGID of new articles
405 protected function newnews($groups = '*', $since = 0, $distributions = '')
407 if (!($since = $this->formatDate($since))) {
410 if (!$this->execLine("NEWNEWS $groups $since $distributions")) {
413 return $this->fetchResult();
416 /** Tell the remote server that I am not a user client, but probably another news server
417 * @return BOOLEAN true if sucessful
419 protected function slave()
421 return $this->execLine("SLAVE ");
424 /** implements IHAVE method
425 * @param $_msgid STRING MSGID of article
426 * @param $_message STRING article
429 protected function ihave($msgid, $message = false
)
431 if (!$this->execLine("IHAVE $msgid ")) {
434 return $this->putMessage($message);
437 /** closes connection to server
439 protected function quit()
441 $this->execLine('QUIT');
444 $this->posting
= false
;
447 # NNTP Extensions [RFC 2980]
449 /** Returns the date on the remote server
450 * @return INTEGER timestamp
453 protected function date()
455 if (!$this->execLine('DATE ', false
)) {
458 if (preg_match("/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/", $this->lastresulttext
, $r)) {
459 return gmmktime($r[4], $r[5], $r[6], $r[2], $r[3], $r[1]);
464 /** returns group descriptions
465 * @param $_pattern STRING pattern of intersting groups
466 * @return ARRAY group name => description
469 protected function xgtitle($pattern = '*')
471 if (!$this->execLine("XGTITLE $pattern ")) {
474 $array = $this->fetchResult();
476 foreach ($array as $result) {
477 list($group, $desc) = split("[ \t]", $result, 2);
478 $groups[$group] = $desc;
483 /** obtain the header field $hdr for all the messages specified
484 * @param $_hdr STRING name of the header (eg: 'From')
485 * @param $_range STRING range of articles
486 * @return ARRAY MSGNUM => header value
488 protected function xhdr($hdr, $first = null
, $last = null
)
490 if (is_null($first) && is_null($last)) {
493 $range = $first . '-' . $last;
495 if (!$this->execLine("XHDR $hdr $range ")) {
498 $array = $this->fetchResult();
500 foreach ($array as &$result) {
501 @list
($head, $value) = explode(' ', $result, 2);
502 $headers[$head] = $value;
507 /** obtain the header field $_hdr matching $_pat for all the messages specified
508 * @param $_hdr STRING name of the header (eg: 'From')
509 * @param $_range STRING range of articles
510 * @param $_pat STRING pattern
511 * @return ARRAY MSGNUM => header value
513 protected function xpat($_hdr, $_range, $_pat)
515 if (!$this->execLine("XPAT $hdr $range $pat")) {
518 $array = $this->fetchResult();
520 foreach ($array as &$result) {
521 list($head, $value) = explode(' ', $result, 2);
522 $headers[$head] = $result;
528 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: