'false', 'name'=>'', 'firstID'=>'', 'lastID'=>'' ); var $errorLog; function nntp(){ $this->lnterm="\r\n$"; $this->rterm=".\r\n"; } function startNNTPconnection(){ //try multiple servers for connect //fix by:jordy foreach($this->server as $key=>$value){ if($this->OpenNNTPconnection($this->server[$key], $this->port[$key], $this->user[$key], $this->password[$key])){ //we made a connection, break loop return true; }else{ $this->group['status']=false; $this->errorLog='CON_ERROR'; $this->closeNNTPconnection(); } } return false; } /* * Close a NNTP connection * */ function closeNNTPconnection() { if ($this->nntp_con != false) { $this->writeNNTPline("QUIT"); fclose($this->nntp_con); } } function changegroup($group){ //Now we have to query the NewsServer about the First and last article if(!$this->writeNNTPline("GROUP $group")){return false;} if(!$r=$this->readNNTPline()){return false;} if (substr($r,0,1) != 2){//We firstly compare the code, if it's not 2xx, error! $this->group['status']=false; $this->group['name']=''; //give some extra info with the error $this->errorLog='ERR_NO_GROUP ('.$r.')'; return false; } //Code is correct, we can go on $groupinfo=explode(" ",$r); $this->group['status']=true; $this->group['name']=$group; $this->group['firstID']=$groupinfo[2]; $this->group['lastID']=$groupinfo[3]; return true; } function xover($startid,$stopid) { if(!$this->writeNNTPline("XOVER $startid-$stopid")){return false;} if(!$r=$this->readNNTPline()){return false;} //SOME SERVERS JUST GO '.' if there is not overview!!!! if (substr($r,0,3) != "224") {//224 for xover follows $this->group['status']=false; $this->errorLog='ERR_XOVER ('.$r.')'; if (substr($r,0,1) == ".") {//224 for xover follows $this->errorLog='your host uses non-compliant xover responses, you can ignore this error'; } return false; } return true; } function body($messageid){ $this->errorLog=''; if(!$this->writeNNTPline("BODY <$messageid>")){return false;} if(!$r=$this->readNNTPline()){return false;} if (substr($r,0,3) != "222") {//222 n article retrieved - body follows $this->group['status']=false; $this->errorLog='ERR_BODY ('.$r.')'; return false; } return true; } function readNNTPbulk(){ //ini_set("max_execution_time",600); if ($this->nntp_con !== false) { //I was getting lots of silent failures on a shared host that I didnt get //on my own machine. The read buffer was WAY to large (16 megs) and PHP tried to allocate //all 16 megs every time and exceded the maximum allowable memory. // NFOs over the size of $CONF_parsexover['CONF_maxnfobytes']=20000; are not processed //so we really dont need that huge buffer....so this should be set between 10 and 20K //I set to 10 because in my experience, it takes at least 2 or 3 packets to get the whole //info file anyways......... $read = 10000;//16777216; $contents=''; stream_set_timeout($this->nntp_con, 30); // 30 seconds for the server to answer while(1){ //stream_set_timeout($this->nntp_con, 30); // 30 seconds for the server to answer if(!@$contents.=fread($this->nntp_con,$read)){ $this->errorLog='readNNTPbulk:Failed to read NNTP bulk.'; return false; }//or die //we need to test to see if the socket timedout..... $info = stream_get_meta_data($this->nntp_con); if($info['timed_out']){ $this->errorLog='readNNTPbulk:Stream/socket timed out.'; return false; } //if reached end of data, return the bulk data if(substr($contents,-3)==$this->rterm){ //remove the last line return substr($contents,0,-3); } } } $this->errorLog='readNNTPbulk:Not connected to NNTP.'; return false; } /* * read one line from the NNTP-server */ function readNNTPline() { if ($this->nntp_con !== false) { stream_set_timeout($this->nntp_con, 30); // 30 seconds for the server to answer if(!@$t=fgets($this->nntp_con,100000)){ $this->errorLog='readNNTPline:Failed to read NNTP line.'; return false; }//if die //we need to test to see if the socket timedout..... $info = stream_get_meta_data($this->nntp_con); if($info['timed_out']){ $this->errorLog='readNNTPline:Stream/socket timed out.'; return false; } //should we check that it is not a 400 timed out message? //strip the /r/n and return the line return rtrim($t); } $this->errorLog='readNNTPline:Not connected to NNTP.'; return false; } /* * Put line to NNTP * */ function writeNNTPline($line){ //problem here: with this is that we have to do some other commands previous to rewriting //the most recent NNTP line. //EG: if we are doing XOVER we first have to do GROUP, so xover would fail here. //management of this type should be handled in the class above this one (filler.class for ikbin) /* if ($this->nntp_con === false || feof($this->nntp_con)){ // Tries to restore connection if it was dropped $this->startNNTPconnection(); } */ if($this->nntp_con !== false){ if(!@fputs($this->nntp_con,"$line\r\n")){ $this->errorLog="writeNNTPline:Failed to write to NNTP ($line)."; return false; } return true; } $this->errorLog='writeNNTPline:Not connected to NNTP.'; return false; } /* * opens the connection to the NNTP-Server * * $server: adress of the NNTP-Server * $port: port of the server */ function OpenNNTPconnection($server,$port,$login,$passwd) { if(!@$this->nntp_con = fsockopen ($server, $port, $errno, $errstr, 30)){return false;} if(!$r=$this->readNNTPline()){return false;} // kill the first line if (substr($r,0,2) != "20") { $this->nntp_con=false; return false; } else { if(!empty($login)){ if(!$this->writeNNTPline("authinfo user $login")){return false;} if(!$r=$this->readNNTPline()){return false;} } if(!empty($passwd)){ if(!$this->writeNNTPline("authinfo pass $passwd")){return false;} if(!$r=$this->readNNTPline()){return false;} if (substr($r,0,3) != "281") { $this->nntp_con=false; return false; } } // Mode Reader Fix by 'BrianLucas' if ($this->nntp_con != false) { if(!$this->writeNNTPline("mode reader")){return false;} if(!$r=$this->readNNTPline()){return false;} // and once more if (substr($r,0,2) != "20") { $this->nntp_con=false; return false; } } } return true; } //-----------------------------------------------Some old/new functions..... /* function readNNTPbulk_nonblock(){ $timeout=600; if ($this->nntp_con != false) { //if it is false, we need to try to make the connection again $read = 16777216; $contents=''; $stop = time() + $timeout; while(1){ $packet=fread($this->nntp_con,$read); if($packet){ $contents.=$packet; if(substr($contents,-3)==".\r\n"){ //remove the last line return substr($contents,0,-3); } }elseif(time() > $stop ){//timeout return false; } } } //we should never get here, but if we do, do this.... return false; } function readNNTPline_nonblock() { $timeout=600; ini_set("max_execution_time",600); $stop = time() + $timeout; if ($this->nntp_con != false) {//try again if it is false! while(1){ $xop=fgets($this->nntp_con,100000);//or die if($xop){ $xo.=$xop; if(substr($xo,-2)=="\r\n"){ //echo $xo.'|';flush(); //return rtrim($xo,"\r\n"); return ereg_replace("\r\n$","",$xo); } }elseif(time() > $stop ){//timeout, check connection and try again... return false; } } } //we should never get this far return false; } */ }//class ?>