用PHP连接 NNTP/NNRP 新闻服务器的几个函数

        
function  nnrp_open($server)  {
                global  
$NNRP_GLOBAL_STATUS;

                
$nnrp  =  fsockopen($server,  119);
                if  (
$nnrp  <  0)  return  -1;

                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  2)  <>  "20")  return  -1;

                
fputs($nnrp,  "MODE  READERrn");

                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  2)  <>  "20")  return  -1;

                if  (
substr($line,  2,  1)  ==  "0")  {
                        
$NNRP_GLOBAL_STATUS[$nnrp][ "POST"]  =  1;
                }  else  {
                        
$NNRP_GLOBAL_STATUS[$nnrp][ "POST"]  =  0;
                }

                
$NNRP_GLOBAL_STATUS"group"]  =  "";
                
$NNRP_GLOBAL_STATUS"count"]  =  0;
                
$NNRP_GLOBAL_STATUS"first"]  =  0;
                
$NNRP_GLOBAL_STATUS"last"]  =  0;

                return  
$nnrp;
        }

        function  
nnrp_authinfo($nnrp,  $user,  $pass)  {
                global  
$NNRP_GLOBAL_STATUS;

                
fputs($nnrp,  "AUTHINFO  USER  $userrn");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "381")  return  -1;

                
fputs($nnrp,  "AUTHINFO  PASS  $passrn");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "281")  return  -1;

                return  
0;
        }

        function  
nnrp_list($nnrp)  {
                global  
$NNRP_GLOBAL_STATUS;

                
fputs($nnrp,  "LISTrn");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "215")  return  -1;

                
$i  =  0;
                while  (
substr($line  =  fgets($nnrp,  1024),  0,  1)  <>
".")  {
                        while  ((
strlen($line)  >  0)  &&  (($line[strlen
($line)-1]  ==  "n")  ||  ($line[strlen($line)-1]  ==  "r")))  $line  =
substr($line,  0,  strlen($line)-1);
                        
$tok  =  strtok($line,  "  ");
                        
$groups[$i]  =  $tok;
                        
$tok  =  strtok($line,  "  ");
                        
$groups[$i][ "last"]  =  $tok;
                        
$tok  =  strtok($line,  "  ");
                        
$groups[$i][ "first"]  =  $tok;
                        
$tok  =  strtok($line,  "  ");
                        
$groups[$i][ "post"]  =  (($tok  ==  "y")  ?  true  :
false);
                        
$i++;
                }
                
$groups"count"]  =  $i;
                return  
$groups;
        }

        function  
nnrp_newgroups($nnrp,  $timestamp,  $distr"")  {
                global  
$NNRP_GLOBAL_STATUS;

                if  (
strlen($distr)  >  0)  {
                        
$distr  =  "<".$distr">";
                }

                
$datestring  =  gmdate"ymd  His",  $timestamp);

                
fputs($nnrp,  "NEWGROUPS  $datestring  GMT  $distrrn");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "231")  return  -1;

                
$i  =  0;
                while  (
substr($line  =  fgets($nnrp,  1024),  0,  1)  <>
".")  {
                        while  ((
strlen($line)  >  0)  &&  (($line[strlen
($line)-1]  ==  "n")  ||  ($line[strlen($line)-1]  ==  "r")))  $line  =
substr($line,  0,  strlen($line)-1);
                        
$tok  =  strtok($line,  "  ");
                        
$groups[$i]  =  $tok;
                        
$tok  =  strtok($line,  "  ");
                        
$groups[$i][ "last"]  =  $tok;
                        
$tok  =  strtok($line,  "  ");
                        
$groups[$i][ "first"]  =  $tok;
                        
$tok  =  strtok($line,  "  ");
                        
$groups[$i][ "post"]  =  (($tok  ==  "y")  ?  true  :
false);
                        
$i++;
                }
                
$groups"count"]  =  $i;
                return  
$groups;
        }

        function  
nnrp_newnews($nnrp,  $newsgroups,  $timestamp,  $distr"")
{
                global  
$NNRP_GLOBAL_STATUS;

                if  (
strlen($distr)  >  0)  {
                        
$distr  =  "<".$distr">";
                }

                
$datestring  =  gmdate"ymd  His",  $timestamp);

                
fputs($nnrp,  "NEWNEWS  $newsgroups  $datestring  GMT
$distrrn"
);
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "230")  return  -1;

                
$i  =  0;
                while  (
substr($line  =  fgets($nnrp,  1024),  0,  1)  <>
".")  {
                        while  ((
strlen($line)  >  0)  &&  (($line[strlen
($line)-1]  ==  "n")  ||  ($line[strlen($line)-1]  ==  "r")))  $line  =
substr($line,  0,  strlen($line)-1);
                        
$messages[$i]  =  $line;
                        
$i++;
                }
                
$messages"count"]  =  $i;
                return  
$messages;
        }

        function  
nnrp_post($nnrp,  $newsgroups,  $from,  $subject,  $message,
$reply_to"",  $organization"",  $follow_up"",  $references"")  {
                global  
$NNRP_GLOBAL_STATUS;
                global  
$SERVER_NAME;

                
fputs($nnrp,  "POSTrn");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "340")  return  -1;

                
fputs($nnrp,  "From:  $fromrn");
                if  (
strlen($reply_to)  >  0)  {
                        
fputs($nnrp,  "Reply-To:  $reply_torn");
                }
                
fputs($nnrp,  "Sender:  gateway@$SERVER_NAMErn");
                
fputs($nnrp,  "Newsgroups:  $newsgroupsrn");
                
fputs($nnrp,  "Subject:  $subjectrn");
                if  (
strlen($organization)  ==  0)  {
                        
$organization  =  "NNRP  Gateway  at  http://
$SERVER_NAME/rn"
;
                }
                
fputs($nnrp,  "Organization:  $organizationrn");
                if  (
strlen($follow_up)  >  0)  {
                        
fputs($nnrp,  "Followup-To:  $follow_uprn");
                }
                if  (
strlen($references)  >  0)  {
                        
fputs($nnrp,  "References:  $referencesrn");
                }
                
fputs($nnrp,  "rn");

                
$newmessage  =  ereg_replace"([^rn])$",  "\1rn",
$message);
                
$newmessage  =  ereg_replace"nr""rn",$newmessage);
                
$newmessage  =  ereg_replace"([^r])n""\1rn",
$newmessage);
                
$newmessage  =  ereg_replace"r([^n])""rn\1",
$newmessage);
                
$newmessage  =  ereg_replace"r$""rn",  $newmessage);
                
$newmessage  =  ereg_replace"^n""rn",  $newmessage);

                
$newmessage  =  ereg_replace"rn.([^.])",  "rn..\1",
$newmessage);
                
$newmessage  =  ereg_replace"rn.$",  "rn..",
$newmessage);

                
fputs($nnrp,  "$newmessage");
                
fputs($nnrp,  ".rn");

                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "240")  return  -1;

                return  
0;
        }

        function  
nnrp_listgroup($nnrp,  $group)  {
                global  
$NNRP_GLOBAL_STATUS;

                
fputs($nnrp,  "LISTGROUP  $grouprn");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "211")  return  -1;

                
$i  =  0;
                while  (
substr($line  =  fgets($nnrp,  1024),  0,  1)  <>
".")  {
                        while  ((
strlen($line)  >  0)  &&  (($line[strlen
($line)-1]  ==  "n")  ||  ($line[strlen($line)-1]  ==  "r")))  $line  =
substr($line,  0,  strlen($line)-1);
                        
$tok  =  strtok($line,  "  ");
                        
$articles[$i]  =  intval($tok);
                        
$i++;
                }
                
$articles"count"]  =  $i;
                return  
$articles;
        }

        function  
nnrp_group($nnrp,  $group)  {
                global  
$NNRP_GLOBAL_STATUS;

                
fputs($nnrp,  "GROUP  $grouprn");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "211")  return  -1;

                while  ((
strlen($line)  >  0)  &&  (($line[strlen($line)-1]
==  
"n")  ||  ($line[strlen($line)-1]  ==  "r")))  $line  =  substr($line,
0,  strlen($line)-1);

                
$tok  =  strtok($line,  "  ");                /*  $tok  =  211
*/
                
$tok  =  strtok"  ");                                /*
$tok  =  nr  */
                
$NNRP_GLOBAL_STATUS"count"]  =  $info"count"]  =  intval
($tok);
                
$tok  =  strtok"  ");                                /*
$tok  =  low  */
                
$NNRP_GLOBAL_STATUS"first"]  =  $info"first"]  =  intval
($tok);
                
$tok  =  strtok"  ");                                /*
$tok  =  high  */
                
$NNRP_GLOBAL_STATUS"last"]  =  $info"last"]  =  intval
($tok);

                
$NNRP_GLOBAL_STATUS"group"]  =  $group;

                return  
$info;
        }

        function  
nnrp_article($nnrp,  $article)  {
                global  
$NNRP_GLOBAL_STATUS;

                
fputs($nnrp,  "ARTICLE  $articlern");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "220")  return  -1;

                
$i  =  0;
                while  (
substr($line  =  fgets($nnrp,  1024),  0,  1)  <>
".")  {
                        while  ((
strlen($line)  >  0)  &&  (($line[strlen
($line)-1]  ==  "n")  ||  ($line[strlen($line)-1]  ==  "r")))  $line  =
substr($line,  0,  strlen($line)-1);
                        
$data[$i]  =  $line;
                        
$i++;
                }
                
$data"count"]  =  $i;

                return  
$data;
        }

        function  
nnrp_overviewformat($nnrp,  $full=false)  {
                global  
$NNRP_GLOBAL_STATUS;

                
fputs($nnrp,  "LIST  overview.fmtrn");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "215")  return  -1;

                
$i  =  0;
                while  (
substr($line  =  fgets($nnrp,  1024),  0,  1)  <>
".")  {
                        while  ((
strlen($line)  >  0)  &&  (($line[strlen
($line)-1]  ==  "n")  ||  ($line[strlen($line)-1]  ==  "r")))  $line  =
substr($line,  0,  strlen($line)-1);
                        if  (
$full  ==  false)  {
                                
$headers[$i]  =  strtok($line,  ":");
                        }  else  {
                                
$headers[$i][ "name"]  =  strtok($line,  ":");
                                
$headers[$i][ "full"]  =  (strtolower(strtok(
"n"))  ==  "full");
                        }
                        
$i++;
                }
                
$headers"count"]  =  $i;

                return  
$headers;
        }

        function  
nnrp_overview($nnrp,  $first"none",  $last"none")  {
                global  
$NNRP_GLOBAL_STATUS;

                if  (
$first  ==  "none")  {
                        
$first  =  $NNRP_GLOBAL_STATUS"first"];
                        
$last    =  $NNRP_GLOBAL_STATUS"last"];
                }  else  {
                        if  (
$last  ==  "none")  {
                                
$last    =  $first;
                        }
                }

                if  ((
$headers  =  nnrp_overviewformat($nnrp,  true))  ==  -1)
{
                        return  -
1;
                }

                
fputs($nnrp,  "XOVER  ".$first"-".$last"rn");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "224")  return  -1;

                
$i  =  0;
                while  (
substr($line  =  fgets($nnrp,  1024),  0,  1)  <>
".")  {
                        while  ((
strlen($line)  >  0)  &&  (($line[strlen
($line)-1]  ==  "n")  ||  ($line[strlen($line)-1]  ==  "r")))  $line  =
substr($line,  0,  strlen($line)-1);
                        
$art  =  intval(strtok($line,  "t"));
                        
$j  =  0;
                        while  (
$j  <  $headers"count"])  {
                                if  (
$headers[$j][ "full"])  {
                                        
$overview[$art][$headers[$j][ "name"]]
=  
substr(strtok"t"),  strlen($headers[$j][ "name"])+2,  1024);
                                }  else  {
                                        
$overview[$art][$headers[$j][ "name"]]
=  
strtok"t");
                                }
                                
$j++;
                        }
                        
$i++;
                }
                
$overview"count"]  =  $i;

                return  
$overview;
        }

        function  
nnrp_head($nnrp,  $article)  {
                global  
$NNRP_GLOBAL_STATUS;

                
fputs($nnrp,  "HEAD  $articlern");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "221")  return  -1;

                
$i  =  0;
                while  (
substr($line  =  fgets($nnrp,  1024),  0,  1)  <>
".")  {
                        while  ((
strlen($line)  >  0)  &&  (($line[strlen
($line)-1]  ==  "n")  ||  ($line[strlen($line)-1]  ==  "r")))  $line  =
substr($line,  0,  strlen($line)-1);
                        
$data[$i]  =  $line;
                        
$i++;
                }
                
$data"count"]  =  $i;

                return  
$data;
        }

        function  
nnrp_body($nnrp,  $article)  {
                global  
$NNRP_GLOBAL_STATUS;

                
fputs($nnrp,  "BODY  $articlern");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "222")  return  -1;

                
$i  =  0;
                while  (
substr($line  =  fgets($nnrp,  1024),  0,  1)  <>
".")  {
                        while  ((
strlen($line)  >  0)  &&  (($line[strlen
($line)-1]  ==  "n")  ||  ($line[strlen($line)-1]  ==  "r")))  $line  =
substr($line,  0,  strlen($line)-1);
                        
$data[$i]  =  $line;
                        
$i++;
                }
                
$data"count"]  =  $i;

                return  
$data;
        }

        function  
nnrp_close($nnrp)  {
                global  
$NNRP_GLOBAL_STATUS;

                
fputs($nnrp,  "QUITrn");
                
$line  =  fgets($nnrp,  1024);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"]  =  substr($line,
0,  3);
                
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"]  =  substr
($line,  4,  1024);
                if  (
substr($line,  0,  3)  <>  "205")  return  -1;

                
$NNRP_GLOBAL_STATUS"group"]  =  "";
                
$NNRP_GLOBAL_STATUS"count"]  =  0;
                
$NNRP_GLOBAL_STATUS"first"]  =  0;
                
$NNRP_GLOBAL_STATUS"last"]  =  0;

                
fclose($nnrp);
                return  
0;
        }

        function  
nnrp_lastresult($nnrp)  {
                global  
$NNRP_GLOBAL_STATUS;

                return  
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULT"];
        }

        function  
nnrp_lastresulttext($nnrp)  {
                global  
$NNRP_GLOBAL_STATUS;

                return  
$NNRP_GLOBAL_STATUS[$nnrp][ "LASTRESULTTEXT"];
        }
?>