2d5434b7288ee29ab1f1fbe3b63357c86ab0ea69
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 $message = preg_replace("/(^|\n)\./", '\1..', $message);
143 $this->putLine("$message\r\n", false
);
145 return $this->execLine('.');
149 /** exec a command a check result
150 * @param STRING $line line to exec
152 private function execLine($line, $strict_state = true
)
154 if (!$this->putLine($line, true
)) {
157 return $this->checkState($strict_state);
160 /** check if last command was successfull (read one line)
161 * @param BOOL $strict indicate if 1XX codes are interpreted as errors (true) or success (false)
163 private function checkState($strict = true
)
165 $result = $this->getLine();
169 $this->lastresultcode
= substr($result, 0, 3);
170 $this->lastresulttext
= substr($result, 4);
171 $c = $this->lastresultcode
{0};
172 if ($c == '2' ||
(($c == '1' ||
$c == '3') && !$strict)) {
175 $this->lasterrorcode
= $this->lastresultcode
;
176 $this->lasterrortext
= $this->lastresulttext
;
181 # strict NNTP Functions [RFC 977]
182 # see http://www.faqs.org/rfcs/rfc977.html
185 * @param $user STRING login
186 * @param $pass INTEGER password
187 * @return BOOLEAN true if authentication was successful
189 protected function authinfo($user, $pass)
191 if ($this->execLine("AUTHINFO USER $user", false
)) {
192 return $this->execline("AUTHINFO PASS $pass");
197 /** retrieves an article
198 * MSGID is a numeric ID a shown in article's headers. MSGNUM is a
199 * server-dependent ID (see X-Ref on many servers) and retriving
200 * an article by this way will change the current article pointer.
201 * If an error occur, false is returned.
202 * @param $_msgid STRING MSGID or MSGNUM of article
203 * @return ARRAY lines of the article
207 protected function article($msgid = "")
209 if (!$this->execLine("ARTICLE $msgid")) {
212 return $this->fetchResult();
216 * if an error occur, false is returned
217 * @param $_message STRING message to post
218 * @return STRING MSGID of article
220 protected function post($message)
222 if (!$this->execLine("POST ", false
)) {
225 if (!$this->putMessage($message)) {
228 if (preg_match("/(<[^@>]+@[^@>]+>)/", $this->lastresulttext
, $regs)) {
235 /** fetches the body of an article
236 * params are the same as article
237 * @param $_msgid STRING MSGID or MSGNUM of article
238 * @return ARRAY lines of the article
242 protected function body($msgid = '')
244 if ($this->execLine("BODY $msgid")) {
247 return $this->fetchResult();
250 /** fetches the headers of an article
251 * params are the same as article
252 * @param $_msgid STRING MSGID or MSGNUM of article
253 * @return ARRAY lines of the article
257 protected function head($msgid = '')
259 if (!$this->execLine("HEAD $msgid")) {
262 return $this->fetchResult();
265 /** set current group
266 * @param $_group STRING
267 * @return ARRAY array : nb of articles in group, MSGNUM of first article, MSGNUM of last article, and group name
269 protected function group($group)
271 if (!$this->execLine("GROUP $group")) {
274 $array = explode(' ', $this->lastresulttext
);
275 if (count($array) >= 4) {
276 return array_slice($array, 0, 4);
281 /** set the article pointer to the previous article in current group
282 * @return STRING MSGID of article
285 protected function last()
287 if (!$this->execLine("LAST ")) {
290 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
296 /** set the article pointer to the next article in current group
297 * @return STRING MSGID of article
301 protected function next()
303 if (!$this->execLine('NEXT ')) {
306 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
312 /** set the current article pointer
313 * @param $_msgid STRING MSGID or MSGNUM of article
314 * @return BOOLEAN true if authentication was successful, error code otherwise
318 protected function nntpstat($msgid)
320 if (!$this->execLine("STAT $msgid")) {
323 if (preg_match("/^\d+ (<[^>]+>)/", $this->lastresulttext
, $regs)) {
329 /** filter group list
331 private function filterGroups()
333 $list = $this->fetchResult();
336 foreach ($list as $result) {
337 list($group, $last, $first, $p) = explode(' ', $result, 4);
338 if (!is_null(Banana
::$boxpattern) ||
preg_match('@' . Banana
::$boxpattern . '@i', $group)) {
339 $groups[$group] = array(intval($last), intval($first), $p);
345 /** gets information about all active newsgroups
346 * @return ARRAY group name => (MSGNUM of first article, MSGNUM of last article, NNTP flags)
349 protected function listGroups()
351 if (!$this->execLine('LIST')) {
354 return $this->filterGroups();
357 /** format date for news server
358 * @param since UNIX TIMESTAMP
360 protected function formatDate($since)
362 return gmdate("ymd His", $since) . ' GMT';
365 /** get information about recent newsgroups
366 * same as list, but information are limited to newgroups created after $_since
367 * @param $_since INTEGER unix timestamp
368 * @param $_distributions STRING distributions
369 * @return ARRAY same format as liste
372 protected function newgroups($since, $distributions = '')
374 if (!($since = $this->formatDate($since))) {
377 if (!$this->execLine("NEWGROUPS $since $distributions")) {
380 return $this->filterGroups();
383 /** gets a list of new articles
384 * @param $_since INTEGER unix timestamp
385 * @parma $_groups STRING pattern of intersting groups
386 * @return ARRAY MSGID of new articles
388 protected function newnews($groups = '*', $since = 0, $distributions = '')
390 if (!($since = $this->formatDate($since))) {
393 if (!$this->execLine("NEWNEWS $groups $since $distributions")) {
396 return $this->fetchResult();
399 /** Tell the remote server that I am not a user client, but probably another news server
400 * @return BOOLEAN true if sucessful
402 protected function slave()
404 return $this->execLine("SLAVE ");
407 /** implements IHAVE method
408 * @param $_msgid STRING MSGID of article
409 * @param $_message STRING article
412 protected function ihave($msgid, $message = false
)
414 if (!$this->execLine("IHAVE $msgid ")) {
417 return $this->putMessage($message);
420 /** closes connection to server
422 protected function quit()
424 $this->execLine('QUIT');
427 $this->posting
= false
;
430 # NNTP Extensions [RFC 2980]
432 /** Returns the date on the remote server
433 * @return INTEGER timestamp
436 protected function date()
438 if (!$this->execLine('DATE ', false
)) {
441 if (preg_match("/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/", $this->lastresulttext
, $r)) {
442 return gmmktime($r[4], $r[5], $r[6], $r[2], $r[3], $r[1]);
447 /** returns group descriptions
448 * @param $_pattern STRING pattern of intersting groups
449 * @return ARRAY group name => description
452 protected function xgtitle($pattern = '*')
454 if (!$this->execLine("XGTITLE $pattern ")) {
457 $array = $this->fetchResult();
459 foreach ($array as $result) {
460 list($group, $desc) = split("[ \t]", $result, 2);
461 $groups[$group] = $desc;
466 /** obtain the header field $hdr for all the messages specified
467 * @param $_hdr STRING name of the header (eg: 'From')
468 * @param $_range STRING range of articles
469 * @return ARRAY MSGNUM => header value
471 protected function xhdr($hdr, $first = null
, $last = null
)
473 if (is_null($first) && is_null($last)) {
476 $range = $first . '-' . $last;
478 if (!$this->execLine("XHDR $hdr $range ")) {
481 $array = $this->fetchResult();
483 foreach ($array as &$result) {
484 @list
($head, $value) = explode(' ', $result, 2);
485 $headers[$head] = $value;
490 /** obtain the header field $_hdr matching $_pat for all the messages specified
491 * @param $_hdr STRING name of the header (eg: 'From')
492 * @param $_range STRING range of articles
493 * @param $_pat STRING pattern
494 * @return ARRAY MSGNUM => header value
496 protected function xpat($_hdr, $_range, $_pat)
498 if (!$this->execLine("XPAT $hdr $range $pat")) {
501 $array = $this->fetchResult();
503 foreach ($array as &$result) {
504 list($head, $value) = explode(' ', $result, 2);
505 $headers[$head] = $result;
511 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: