915f22e7b24b1efa9fd9d6cac9dba080a37c3e1a
2 /********************************************************************************
3 * include/NetNNTP.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 ********************************************************************************/
11 * implements some basic functions for NNTP protocol
14 /** socket filehandle */
18 /** posting allowed */
20 /** last NNTP error code */
22 /** last NNTP error text */
27 /** get a line from server
32 $line = preg_replace("/(\r|\n)/","",fgets($this->ns
,1200));
34 print "NNTP >>>> $line \n";
39 /** puts a line on server
40 * @param STRING $_line line to put
43 function pline($_line) {
45 $dline = preg_replace("/\r\n$/","",$_line);
46 $dline = preg_replace("/(\r|\n|\r\n)/","\nNNTP <<<< ",$dline);
47 print "NNTP <<<< $dline \n";
49 return fputs($this->ns
,$_line,strlen($_line));
53 * @param $_host STRING NNTP host
54 * @param $_timeout INTEGER socket timeout
55 * @param $_debug BOOLEAN debug flag (generates verbose output if on)
56 * @param $_reader BOOLEAN sends a "MODE READER" at connection if true
59 function nntp($_host,$_timeout=120,$_debug=0,$_reader=true
) {
60 if (preg_match("/([^:]+):([^:]+)/",$_host,$regs)) {
67 $this->ns
= fsockopen($host, $port, $errno, $errstr, $_timeout);
68 $this->debug
= $_debug;
71 echo "ERREUR: $errno - $errstr <br />\n";
76 $result = $this->gline();
77 $this->posting
= (substr($result,0,3)=="200");
78 if ($_reader && (substr($result,0,1)=="2")) {
79 $this->pline("MODE READER\r\n");
80 $result = $this->gline();
81 $this->posting
= (substr($result,0,3)=="200");
83 return (substr($result,0,1)=="2");
86 # strict NNTP Functions [RFC 977]
87 # see http://www.faqs.org/rfcs/rfc977.html
90 * @param $_user STRING login
91 * @param $_pass INTEGER password
92 * @return BOOLEAN true if authentication was successful
95 function authinfo($_user,$_pass) {
96 $user = preg_replace("/(\r|\n)/","",$_user);
97 $pass = preg_replace("/(\r|\n)/","",$_pass);
98 $this->pline("AUTHINFO USER $user\r\n");
100 $this->pline("AUTHINFO PASS $pass\r\n");
101 $result=$this->gline();
102 if (substr($result,0,1)!="2") {
103 $this->lasterrorcode
= substr($result,0,3);
104 $this->lasterrortext
= substr($result,4);
110 /** retrieves an article
111 * MSGID is a numeric ID a shown in article's headers. MSGNUM is a
112 * server-dependent ID (see X-Ref on many servers) and retriving
113 * an article by this way will change the current article pointer.
114 * If an error occur, false is returned.
115 * @param $_msgid STRING MSGID or MSGNUM of article
116 * @return ARRAY lines of the article
121 function article($_msgid="") {
122 $msgid = preg_replace("/(\r|\n)/","",$_msgid);
123 $this->pline("ARTICLE $msgid\r\n");
124 $result = $this->gline();
125 if (substr($result,0,1)!="2") {
126 $this->lasterrorcode
= substr($result,0,3);
127 $this->lasterrortext
= substr($result,4);
130 $result = $this->gline();
131 while ($result != ".") {
133 $result = $this->gline();
139 * if an error occur, false is returned
140 * @param $_message STRING message to post
141 * @return STRING MSGID of article
144 function post($_message) {
145 if (is_array($_message)) {
146 $message=join("\n",$_message);
150 $this->pline("POST \r\n");
151 $result=$this->gline();
152 if (substr($result,0,1)!="3") {
153 $this->lasterrorcode
= substr($result,0,3);
154 $this->lasterrortext
= substr($result,4);
157 $this->pline($message."\r\n.\r\n");
158 $result = $this->gline();
159 if (substr($result,0,1)!="2") {
160 $this->lasterrorcode
= substr($result,0,3);
161 $this->lasterrortext
= substr($result,4);
164 if (substr($result,0,1)=="2") {
165 if (preg_match("/(<[^@>]+@[^@>]+>)/",$result,$regs)) {
174 /** fetches the body of an article
175 * params are the same as article
176 * @param $_msgid STRING MSGID or MSGNUM of article
177 * @return ARRAY lines of the article
182 function body($_msgid="") {
183 $msgid = preg_replace("/(\r|\n)/","",$_msgid);
184 $this->pline("BODY $msgid\r\n");
185 $result = $this->gline();
186 if (substr($result,0,1)!="2") {
187 $this->lasterrorcode
= substr($result,0,3);
188 $this->lasterrortext
= substr($result,4);
191 $result = $this->gline();
192 while ($result != ".") {
194 $result = $this->gline();
199 /** fetches the headers of an article
200 * params are the same as article
201 * @param $_msgid STRING MSGID or MSGNUM of article
202 * @return ARRAY lines of the article
207 function head($_msgid="") {
208 $msgid = preg_replace("/(\r|\n)/","",$_msgid);
209 $this->pline("HEAD $msgid\r\n");
210 $result = $this->gline();
211 if (substr($result,0,1)!="2") {
212 $this->lasterrorcode
= substr($result,0,3);
213 $this->lasterrortext
= substr($result,4);
216 $result = $this->gline();
217 while ($result != ".") {
219 $result = $this->gline();
224 /** set current group
225 * @param $_group STRING
226 * @return ARRAY array : nb of articles in group, MSGNUM of first article, MSGNUM of last article, and group name
229 function group($_group) {
230 $group = preg_replace("/(\r|\n)/","",$_group);
231 $this->pline("GROUP $group\r\n");
232 $line = $this->gline();
233 if (substr($line,0,1)!="2") {
234 $this->lasterrorcode
= substr($result,0,3);
235 $this->lasterrortext
= substr($result,4);
238 if (preg_match("/^2\d{2} (\d+) (\d+) (\d+) ([^ ]+)/",
240 return array($regs[1],$regs[2],$regs[3],$regs[4]);
245 /** set the article pointer to the previous article in current group
246 * @return STRING MSGID of article
251 $this->pline("LAST \r\n");
252 $line = $this->gline();
253 if (substr($line,0,1)!="2") {
254 $this->lasterrorcode
= substr($result,0,3);
255 $this->lasterrortext
= substr($result,4);
258 if (preg_match("/^2\d{2} \d+ <([^>]+)>/",$line,$regs)) {
259 return "<{$regs[1]}>";
264 /** set the article pointer to the next article in current group
265 * @return STRING MSGID of article
270 $this->pline("NEXT \r\n");
271 $line = $this->gline();
272 if (substr($line,0,1)!="2") {
273 $this->lasterrorcode
= substr($result,0,3);
274 $this->lasterrortext
= substr($result,4);
277 if (preg_match("/^2\d{2} \d+ <([^>]+)>/",$line,$regs)) {
278 return "<{$regs[1]}>";
283 /** set the current article pointer
284 * @param $_msgid STRING MSGID or MSGNUM of article
285 * @return BOOLEAN true if authentication was successful, error code otherwise
290 function nntpstat($_msgid) {
291 $msgid = preg_replace("/(\r|\n)/","",$_msgid);
292 $this->pline("STAT $msgid\r\n");
293 $line = $this->gline();
294 if (substr($line,0,1)!="2") {
295 $this->lasterrorcode
= substr($result,0,3);
296 $this->lasterrortext
= substr($result,4);
299 if (preg_match("/^2\d{2} \d+ <([^>]+)>/",$line,$regs)) {
300 return "<{$regs[1]}>";
305 /** returns true if posting is allowed
306 * @return BOOLEAN true if posting is allowed lines
310 return ($this->posting
);
313 /** gets information about all active newsgroups
314 * @return ARRAY group name => (MSGNUM of first article, MSGNUM of last article, NNTP flags)
319 $this->pline("LIST\r\n");
320 if (substr($this->gline(),0,1)!="2") return false
;
321 $result = $this->gline();
322 while ($result != ".") {
323 preg_match("/([^ ]+) (\d+) (\d+) (.)/",$result,$regs);
324 $array[$regs[1]]=array(intval($regs[2]),intval($regs[3]),
326 $result = $this->gline();
331 /** get information about recent newsgroups
332 * same as list, but information are limited to newgroups created after $_since
333 * @param $_since INTEGER unix timestamp
334 * @param $_distributions STRING distributions
335 * @return ARRAY same format as liste
339 function newgroups($_since,$_distributions="") {
340 #assume $_since is a unix timestamp
341 $distributions = preg_replace("/(\r|\n)/","",$_distributions);
342 $this->pline("NEWGROUPS ".gmdate("ymd His",$_since)
343 ." GMT $distributions\r\n");
344 if (substr($this->gline(),0,1)!="2") return false
;
345 $result = $this->gline();
346 while ($result != ".") {
347 preg_match("/([^ ]+) (\d+) (\d+) ./",$result,$regs);
348 $array[$regs[1]]=array(intval($regs[2]),intval($regs[3]),
350 $result = $this->gline();
355 /** gets a list of new articles
356 * @param $_since INTEGER unix timestamp
357 * @parma $_groups STRING pattern of intersting groups
358 * @return ARRAY MSGID of new articles
361 function newnews($_since,$_groups="*",$_distributions="") {
362 $distributions = preg_replace("/(\r|\n)/","",$_distributions);
363 $groups = preg_replace("/(\r|\n)/","",$_groups);
365 #assume $since is a unix timestamp
366 $this->pline("NEWNEWS $_groups ".gmdate("ymd His",$_since)
367 ." GMT $distributions\r\n");
368 if (substr($this->gline(),0,1)!="2") return false
;
369 $result = $this->gline();
370 while ($result != ".") {
372 $result = $this->gline();
377 /** Tell the remote server that I am not a user client, but probably another news server
378 * @return BOOLEAN true if sucessful
382 $this->pline("SLAVE \r\n");
383 return (substr($this->gline(),0,1)=="2");
386 /** implements IHAVE method
387 * @param $_msgid STRING MSGID of article
388 * @param $_message STRING article
392 function ihave($_msgid,$_message=false
) {
393 $msgid = preg_replace("/(\r|\n)/","",$_msgid);
394 if (is_array($message))
395 $message=join("\n",$_message);
398 $this->pline("IHAVE $msgid \r\n");
399 $result=$this->gline();
400 if ($message && (substr($result,0,1)=="3")) {
401 $this->pline("$message\r\n.\r\n");
402 $result=$this->gline();
404 return (substr($result,0,1)=="2");
407 /** closes connection to server
411 $this->pline("QUIT\r\n");
416 # NNTP Extensions [RFC 2980]
418 /** Returns the date on the remote server
419 * @return INTEGER timestamp
423 $this->pline("DATE \r\n");
424 $result = $this->gline();
425 if (preg_match("/^111 (\d{4})(\d{2})(\d{2})"
426 ."(\d{2})(\d{2})(\d{2})$/",$result,$r)) {
427 return gmmktime($r[4],$r[5],$r[6],$r[2],$r[3],$r[1]);
432 /** returns group descriptions
433 * @param $_pattern STRING pattern of intersting groups
434 * @return ARRAY group name => description
437 function xgtitle($_pattern="*") {
438 $pattern = preg_replace("/(\r|\n)/","",$_pattern);
439 $this->pline("XGTITLE $pattern \r\n");
440 if (substr($this->gline(),0,1)!="2") return false
;
441 $result = $this->gline();
442 while ($result != ".") {
443 preg_match("/([^ \t]+)( |\t)+(.+)$/",$result,$regs);
444 $array[$regs[1]]=$regs[3];
445 $result = $this->gline();
450 /** obtain the header field $hdr for all the messages specified
451 * @param $_hdr STRING name of the header (eg: 'From')
452 * @param $_range STRING range of articles
453 * @return ARRAY MSGNUM => header value
456 function xhdr($_hdr,$_range="") {
457 $hdr = preg_replace("/(\r|\n)/","",$_hdr);
458 $range = preg_replace("/(\r|\n)/","",$_range);
459 $this->pline("XHDR $hdr $range \r\n");
460 if (substr($this->gline(),0,1)!="2") return false
;
461 $result = $this->gline();
462 while ($result != ".") {
463 preg_match("/([^ \t]+) (.*)$/",$result,$regs);
464 $array[$regs[1]]=$regs[2];
465 $result = $this->gline();