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 $data = rtrim(@fgets
($this->ns
, 1200), "\r\n");
109 /** fetch data (and on delimitor)
110 * @param STRING $delim string indicating and of transmission
112 private function &fetchResult()
115 while (($result =& $this->getLine()) != '.') {
118 if ($this->debug
&& $this->bt
) {
119 $trace =& $this->bt
[count($this->bt
) - 1];
120 $trace['response'] = count($array);
121 $trace['time'] = microtime(true
) - $trace['start'];
126 /** puts a line on server
127 * @param STRING $line line to put
129 private function putLine($line, $format = false
)
132 $line = str_replace(array("\r", "\n"), '', $line);
136 $db_line = preg_replace('/PASS .*/', 'PASS *******', $line);
137 $this->bt
[] = array('action' => $db_line, 'start' => microtime(true
));
139 return @fputs
($this->ns
, $line, strlen($line));
142 /** put a message (multiline)
144 private function putMessage($message = false
)
146 if (is_array($message)) {
147 $message = join("\n", $_message);
150 $message = preg_replace("/(^|\n)\./", '\1..', $message);
151 $this->putLine("$message\r\n", false
);
153 return $this->execLine('.');
157 /** exec a command a check result
158 * @param STRING $line line to exec
160 private function execLine($line, $strict_state = true
)
162 if (!$this->putLine($line, true
)) {
165 return $this->checkState($strict_state);
168 /** check if last command was successfull (read one line)
169 * @param BOOL $strict indicate if 1XX codes are interpreted as errors (true) or success (false)
171 private function checkState($strict = true
)
173 $result = $this->getLine();
174 $this->lastresultcode
= substr($result, 0, 3);
175 $this->lastresulttext
= substr($result, 4);
176 if ($this->debug
&& $this->bt
) {
177 $trace =& $this->bt
[count($this->bt
) - 1];
178 $trace['time'] = microtime(true
) - $trace['start'];
179 $trace['code'] = $this->lastresultcode
;
180 $trace['message'] = $this->lastresulttext
;
181 $trace['response'] = 0;
183 $c = $this->lastresultcode
{0};
184 if ($c == '2' ||
(($c == '1' ||
$c == '3') && !$strict)) {
187 $this->lasterrorcode
= $this->lastresultcode
;
188 $this->lasterrortext
= $this->lastresulttext
;
193 # strict NNTP Functions [RFC 977]
194 # see http://www.faqs.org/rfcs/rfc977.html
197 * @param $user STRING login
198 * @param $pass INTEGER password
199 * @return BOOLEAN true if authentication was successful
201 protected function authinfo($user, $pass)
203 if ($this->execLine("AUTHINFO USER $user", false
)) {
204 return $this->execline("AUTHINFO PASS $pass");
209 /** retrieves an article
210 * MSGID is a numeric ID a shown in article's headers. MSGNUM is a
211 * server-dependent ID (see X-Ref on many servers) and retriving
212 * an article by this way will change the current article pointer.
213 * If an error occur, false is returned.
214 * @param $_msgid STRING MSGID or MSGNUM of article
215 * @return ARRAY lines of the article
219 protected function article($msgid = "")
221 if (!$this->execLine("ARTICLE $msgid")) {
224 return $this->fetchResult();
228 * if an error occur, false is returned
229 * @param $_message STRING message to post
230 * @return STRING MSGID of article
232 protected function post($message)
234 if (!$this->execLine("POST ", false
)) {
237 if (!$this->putMessage($message)) {
240 if (preg_match("/(<[^@>]+@[^@>]+>)/", $this->lastresulttext
, $regs)) {
247 /** fetches the body of an article
248 * params are the same as article
249 * @param $_msgid STRING MSGID or MSGNUM of article
250 * @return ARRAY lines of the article
254 protected function body($msgid = '')
256 if ($this->execLine("BODY $msgid")) {
259 return $this->fetchResult();
262 /** fetches the headers of an article
263 * params are the same as article
264 * @param $_msgid STRING MSGID or MSGNUM of article
265 * @return ARRAY lines of the article
269 protected function head($msgid = '')
271 if (!$this->execLine("HEAD $msgid")) {
274 return $this->fetchResult();
277 /** set current group
278 * @param $_group STRING
279 * @return ARRAY array : nb of articles in group, MSGNUM of first article, MSGNUM of last article, and group name
281 protected function group($group)
283 if (!$this->execLine("GROUP $group")) {
286 $array = explode(' ', $this->lastresulttext
);
287 if (count($array) >= 4) {
288 return array_slice($array, 0, 4);
293 /** set the article pointer to the previous article in current group
294 * @return STRING MSGID of article
297 protected function last()
299 if (!$this->execLine("LAST ")) {
302 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
308 /** set the article pointer to the next article in current group
309 * @return STRING MSGID of article
313 protected function next()
315 if (!$this->execLine('NEXT ')) {
318 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
324 /** set the current article pointer
325 * @param $_msgid STRING MSGID or MSGNUM of article
326 * @return BOOLEAN true if authentication was successful, error code otherwise
330 protected function nntpstat($msgid)
332 if (!$this->execLine("STAT $msgid")) {
335 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
341 /** filter group list
343 private function filterGroups()
345 $list = $this->fetchResult();
348 foreach ($list as $result) {
349 list($group, $last, $first, $p) = explode(' ', $result, 4);
350 if (!Banana
::$boxpattern ||
preg_match('@' . Banana
::$boxpattern . '@i', $group)) {
351 $groups[$group] = array(intval($last), intval($first), $p);
357 /** gets information about all active newsgroups
358 * @return ARRAY group name => (MSGNUM of first article, MSGNUM of last article, NNTP flags)
361 protected function listGroups()
363 if (!$this->execLine('LIST')) {
366 return $this->filterGroups();
369 /** format date for news server
370 * @param since UNIX TIMESTAMP
372 protected function formatDate($since)
374 return gmdate("ymd His", $since) . ' GMT';
377 /** get information about recent newsgroups
378 * same as list, but information are limited to newgroups created after $_since
379 * @param $_since INTEGER unix timestamp
380 * @param $_distributions STRING distributions
381 * @return ARRAY same format as liste
384 protected function newgroups($since, $distributions = '')
386 if (!($since = $this->formatDate($since))) {
389 if (!$this->execLine("NEWGROUPS $since $distributions")) {
392 return $this->filterGroups();
395 /** gets a list of new articles
396 * @param $_since INTEGER unix timestamp
397 * @parma $_groups STRING pattern of intersting groups
398 * @return ARRAY MSGID of new articles
400 protected function newnews($groups = '*', $since = 0, $distributions = '')
402 if (!($since = $this->formatDate($since))) {
405 if (!$this->execLine("NEWNEWS $groups $since $distributions")) {
408 return $this->fetchResult();
411 /** Tell the remote server that I am not a user client, but probably another news server
412 * @return BOOLEAN true if sucessful
414 protected function slave()
416 return $this->execLine("SLAVE ");
419 /** implements IHAVE method
420 * @param $_msgid STRING MSGID of article
421 * @param $_message STRING article
424 protected function ihave($msgid, $message = false
)
426 if (!$this->execLine("IHAVE $msgid ")) {
429 return $this->putMessage($message);
432 /** closes connection to server
434 protected function quit()
436 $this->execLine('QUIT');
439 $this->posting
= false
;
442 # NNTP Extensions [RFC 2980]
444 /** Returns the date on the remote server
445 * @return INTEGER timestamp
448 protected function date()
450 if (!$this->execLine('DATE ', false
)) {
453 if (preg_match("/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/", $this->lastresulttext
, $r)) {
454 return gmmktime($r[4], $r[5], $r[6], $r[2], $r[3], $r[1]);
459 /** returns group descriptions
460 * @param $_pattern STRING pattern of intersting groups
461 * @return ARRAY group name => description
464 protected function xgtitle($pattern = '*')
466 if (!$this->execLine("XGTITLE $pattern ")) {
469 $array =& $this->fetchResult();
471 foreach ($array as &$result) {
472 @list
($group, $desc) = split("[ \t]", $result, 2);
473 $groups[$group] = $desc;
478 /** obtain the header field $hdr for all the messages specified
479 * @param $_hdr STRING name of the header (eg: 'From')
480 * @param $_range STRING range of articles
481 * @return ARRAY MSGNUM => header value
483 protected function xhdr($hdr, $first = null
, $last = null
)
485 if (is_null($first) && is_null($last)) {
488 $range = $first . '-' . $last;
490 if (!$this->execLine("XHDR $hdr $range ")) {
493 $array =& $this->fetchResult();
495 foreach ($array as &$result) {
496 @list
($head, $value) = explode(' ', $result, 2);
497 $headers[$head] = $value;
502 /** obtain the header field $_hdr matching $_pat for all the messages specified
503 * @param $_hdr STRING name of the header (eg: 'From')
504 * @param $_range STRING range of articles
505 * @param $_pat STRING pattern
506 * @return ARRAY MSGNUM => header value
508 protected function xpat($_hdr, $_range, $_pat)
510 if (!$this->execLine("XPAT $hdr $range $pat")) {
513 $array =& $this->fetchResult();
515 foreach ($array as &$result) {
516 list($head, $value) = explode(' ', $result, 2);
517 $headers[$head] = $result;
523 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: