4b8e9c026f745a1c2aa5359cb762f887892f9e7b
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 if (!is_utf8($this->lasterrortext
)) {
83 return utf8_encode($this->lasterrortext
);
85 return $this->lasterrortext
;
89 public function backtrace()
100 /** get a line from server
103 private function getLine()
105 return rtrim(@fgets
($this->ns
, 1200), "\r\n");
108 /** fetch data (and on delimitor)
109 * @param STRING $delim string indicating and of transmission
111 private function &fetchResult()
114 while (($result = $this->getLine()) != '.') {
117 if ($this->debug
&& $this->bt
) {
118 $this->bt
[count($this->bt
) - 1]['response'] = count($array);
123 /** puts a line on server
124 * @param STRING $line line to put
126 private function putLine($line, $format = false
)
129 $line = str_replace(array("\r", "\n"), '', $line);
133 $db_line = preg_replace('/PASS .*/', 'PASS *******', $line);
134 $this->bt
[] = array('action' => $db_line, 'time' => microtime(true
));
136 return @fputs
($this->ns
, $line, strlen($line));
139 /** put a message (multiline)
141 private function putMessage($message = false
)
143 if (is_array($message)) {
144 $message = join("\n", $_message);
147 $message = preg_replace("/(^|\n)\./", '\1..', $message);
148 $this->putLine("$message\r\n", false
);
150 return $this->execLine('.');
154 /** exec a command a check result
155 * @param STRING $line line to exec
157 private function execLine($line, $strict_state = true
)
159 if (!$this->putLine($line, true
)) {
162 return $this->checkState($strict_state);
165 /** check if last command was successfull (read one line)
166 * @param BOOL $strict indicate if 1XX codes are interpreted as errors (true) or success (false)
168 private function checkState($strict = true
)
170 $result = $this->getLine();
171 $this->lastresultcode
= substr($result, 0, 3);
172 $this->lastresulttext
= substr($result, 4);
173 if ($this->debug
&& $this->bt
) {
174 $trace =& $this->bt
[count($this->bt
) - 1];
175 $trace['time'] = microtime(true
) - $trace['time'];
176 $trace['code'] = $this->lastresultcode
;
177 $trace['message'] = $this->lastresulttext
;
178 $trace['response'] = 0;
180 $c = $this->lastresultcode
{0};
181 if ($c == '2' ||
(($c == '1' ||
$c == '3') && !$strict)) {
184 $this->lasterrorcode
= $this->lastresultcode
;
185 $this->lasterrortext
= $this->lastresulttext
;
190 # strict NNTP Functions [RFC 977]
191 # see http://www.faqs.org/rfcs/rfc977.html
194 * @param $user STRING login
195 * @param $pass INTEGER password
196 * @return BOOLEAN true if authentication was successful
198 protected function authinfo($user, $pass)
200 if ($this->execLine("AUTHINFO USER $user", false
)) {
201 return $this->execline("AUTHINFO PASS $pass");
206 /** retrieves an article
207 * MSGID is a numeric ID a shown in article's headers. MSGNUM is a
208 * server-dependent ID (see X-Ref on many servers) and retriving
209 * an article by this way will change the current article pointer.
210 * If an error occur, false is returned.
211 * @param $_msgid STRING MSGID or MSGNUM of article
212 * @return ARRAY lines of the article
216 protected function article($msgid = "")
218 if (!$this->execLine("ARTICLE $msgid")) {
221 return $this->fetchResult();
225 * if an error occur, false is returned
226 * @param $_message STRING message to post
227 * @return STRING MSGID of article
229 protected function post($message)
231 if (!$this->execLine("POST ", false
)) {
234 if (!$this->putMessage($message)) {
237 if (preg_match("/(<[^@>]+@[^@>]+>)/", $this->lastresulttext
, $regs)) {
244 /** fetches the body of an article
245 * params are the same as article
246 * @param $_msgid STRING MSGID or MSGNUM of article
247 * @return ARRAY lines of the article
251 protected function body($msgid = '')
253 if ($this->execLine("BODY $msgid")) {
256 return $this->fetchResult();
259 /** fetches the headers of an article
260 * params are the same as article
261 * @param $_msgid STRING MSGID or MSGNUM of article
262 * @return ARRAY lines of the article
266 protected function head($msgid = '')
268 if (!$this->execLine("HEAD $msgid")) {
271 return $this->fetchResult();
274 /** set current group
275 * @param $_group STRING
276 * @return ARRAY array : nb of articles in group, MSGNUM of first article, MSGNUM of last article, and group name
278 protected function group($group)
280 if (!$this->execLine("GROUP $group")) {
283 $array = explode(' ', $this->lastresulttext
);
284 if (count($array) >= 4) {
285 return array_slice($array, 0, 4);
290 /** set the article pointer to the previous article in current group
291 * @return STRING MSGID of article
294 protected function last()
296 if (!$this->execLine("LAST ")) {
299 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
305 /** set the article pointer to the next article in current group
306 * @return STRING MSGID of article
310 protected function next()
312 if (!$this->execLine('NEXT ')) {
315 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
321 /** set the current article pointer
322 * @param $_msgid STRING MSGID or MSGNUM of article
323 * @return BOOLEAN true if authentication was successful, error code otherwise
327 protected function nntpstat($msgid)
329 if (!$this->execLine("STAT $msgid")) {
332 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
338 /** filter group list
340 private function filterGroups()
342 $list = $this->fetchResult();
345 foreach ($list as $result) {
346 list($group, $last, $first, $p) = explode(' ', $result, 4);
347 if (!Banana
::$boxpattern ||
preg_match('@' . Banana
::$boxpattern . '@i', $group)) {
348 $groups[$group] = array(intval($last), intval($first), $p);
354 /** gets information about all active newsgroups
355 * @return ARRAY group name => (MSGNUM of first article, MSGNUM of last article, NNTP flags)
358 protected function listGroups()
360 if (!$this->execLine('LIST')) {
363 return $this->filterGroups();
366 /** format date for news server
367 * @param since UNIX TIMESTAMP
369 protected function formatDate($since)
371 return gmdate("ymd His", $since) . ' GMT';
374 /** get information about recent newsgroups
375 * same as list, but information are limited to newgroups created after $_since
376 * @param $_since INTEGER unix timestamp
377 * @param $_distributions STRING distributions
378 * @return ARRAY same format as liste
381 protected function newgroups($since, $distributions = '')
383 if (!($since = $this->formatDate($since))) {
386 if (!$this->execLine("NEWGROUPS $since $distributions")) {
389 return $this->filterGroups();
392 /** gets a list of new articles
393 * @param $_since INTEGER unix timestamp
394 * @parma $_groups STRING pattern of intersting groups
395 * @return ARRAY MSGID of new articles
397 protected function newnews($groups = '*', $since = 0, $distributions = '')
399 if (!($since = $this->formatDate($since))) {
402 if (!$this->execLine("NEWNEWS $groups $since $distributions")) {
405 return $this->fetchResult();
408 /** Tell the remote server that I am not a user client, but probably another news server
409 * @return BOOLEAN true if sucessful
411 protected function slave()
413 return $this->execLine("SLAVE ");
416 /** implements IHAVE method
417 * @param $_msgid STRING MSGID of article
418 * @param $_message STRING article
421 protected function ihave($msgid, $message = false
)
423 if (!$this->execLine("IHAVE $msgid ")) {
426 return $this->putMessage($message);
429 /** closes connection to server
431 protected function quit()
433 $this->execLine('QUIT');
436 $this->posting
= false
;
439 # NNTP Extensions [RFC 2980]
441 /** Returns the date on the remote server
442 * @return INTEGER timestamp
445 protected function date()
447 if (!$this->execLine('DATE ', false
)) {
450 if (preg_match("/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/", $this->lastresulttext
, $r)) {
451 return gmmktime($r[4], $r[5], $r[6], $r[2], $r[3], $r[1]);
456 /** returns group descriptions
457 * @param $_pattern STRING pattern of intersting groups
458 * @return ARRAY group name => description
461 protected function xgtitle($pattern = '*')
463 if (!$this->execLine("XGTITLE $pattern ")) {
466 $array =& $this->fetchResult();
468 foreach ($array as &$result) {
469 @list
($group, $desc) = split("[ \t]", $result, 2);
470 $groups[$group] = $desc;
475 /** obtain the header field $hdr for all the messages specified
476 * @param $_hdr STRING name of the header (eg: 'From')
477 * @param $_range STRING range of articles
478 * @return ARRAY MSGNUM => header value
480 protected function xhdr($hdr, $first = null
, $last = null
)
482 if (is_null($first) && is_null($last)) {
485 $range = $first . '-' . $last;
487 if (!$this->execLine("XHDR $hdr $range ")) {
490 $array =& $this->fetchResult();
492 foreach ($array as &$result) {
493 @list
($head, $value) = explode(' ', $result, 2);
494 $headers[$head] = $value;
499 /** obtain the header field $_hdr matching $_pat for all the messages specified
500 * @param $_hdr STRING name of the header (eg: 'From')
501 * @param $_range STRING range of articles
502 * @param $_pat STRING pattern
503 * @return ARRAY MSGNUM => header value
505 protected function xpat($_hdr, $_range, $_pat)
507 if (!$this->execLine("XPAT $hdr $range $pat")) {
510 $array =& $this->fetchResult();
512 foreach ($array as &$result) {
513 list($head, $value) = explode(' ', $result, 2);
514 $headers[$head] = $result;
520 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: