PHP Classes

File: nntp.inc

Recommend this page to a friend!
  Classes of tony leake   nntp   nntp.inc   Download  
File: nntp.inc
Role: ???
Content type: text/plain
Description: nntp class
Class: nntp
Author: By
Last change:
Date: 22 years ago
Size: 6,417 bytes
 

Contents

Class file image Download
// NNTP wrapper class // Tony Leake Jan 2001 // tony_leake@hotmail.com // Error handling added by Hans Westerbeek, hans@pulse.nl // This software may be used and distributed according to the terms // of the GNU Public License which may be found at: // http://www.gnu.org/copyleft/gpl.html // You are welcome to use/modify/this class, if you use it // 'as is' in a project, I would appreciate an e-mail to let me know // that it has been useful. If you modify it into something more complete // I would appreciate a copy just for curiosity. // This class was inspired by (and uses some ideas from) a script by Nate Parsons. // which i found at: // http://www.phpbuilder.com/forum/read.php3?num=1&id=107915&thread=103470 // This class simply allows you to make posts onto a news server. // If you know the message id of a message you can also reply to it. // It was developed to allow me to make a posting and then reply to // my own posting (for a service status page). // It does not implement the full NNTP protocol and error checking is // rudimentary. If you need it to do anything except what I used it for // you will need to modify it which hopefully will be fairly easy. // USAGE// // To start a new news thread // $news=new nntp("news server address", "newsgroup name"); // $news->from ="someone@somewhere.com"; // $news->subject = $heading; // $news->body = "$body"; // $news->domain ="mydomain.com"; // $news->id=$id //should be a unique number. you could make the class do this pretty easily. // $news->send("new"); // to reply to a thread you must also add this line // $news->references ="<$id@domain.com>"; where $id is the orriginal id number and // domain.com is the original domain used for the posting // $news->send("reply"); // hw: // define a list of error codes define(NNTP_SELECT_GROUP_FAILED, 1); define(NNTP_POST_FAILED, 2); define(NNTP_UNEXISTING_MSG, 3); define(NNTP_CANNOT_POST, 4); define(NNTP_COULD_NOT_CONNECT, 5); define(NNTP_SOCKET_FAILURE, 6); class NNTPPoster { var $server; var $group; var $from; var $subject; var $body; var $fp; var $references; var $id; var $ret = 0; var $domain; var $currentErrorCode = false; var $currentTextualError = ""; function NNTPPoster($server, $group) { $this->server = $server; $this->group = $group; } function send($action) { $this->connect(); $this->selectGroup(); $this->post($action); } function connect() { $this->fp=fsockopen($this->server, 119, &$error, &$description, 5); if(!$this->fp) { $this->currentTextualError = "Could not connect to news server ".$this->server." ".$error.", ".$description; $this->currentErrorCode = NNTP_COULD_NOT_CONNECT; return false; } if (fgets($this->fp, 1024) != 200) { $this->currentTextualError = "Connection to news server ".$this->server." failed: ".$description; $this->currentErrorCode = NNTP_SOCKET_FAILURE; return false; } return true; } function selectGroup() { fputs($this->fp, "group $this->group\n"); $x=fgets($this->fp, 1024); if (substr($x, 0,3) != 211) { $this->currentErrorCode = NNTP_SELECT_GROUP_FAILED; $this->currentTextualError = "Failed to change to NewsGroup \"".$this->group."\": server reported \"".$x."\""; return false; } return true; } function post($action) { fputs($this->fp, "post\n"); $x=fgets($this->fp, 1024); if (substr($x, 0,3) != 340) { $this->currentTextualError = "Cannot post to newsgroup, server reported: \"".$this->group."\": server reported \"".$x."\""; $this->currentErrorCode = NNTP_POST_FAILED; return false; } mt_srand ((double) microtime() * 10000000 ); $int_id = "<".mt_rand(10000000,99999999)."@$this->domain>"; $Today = date("l, j M y g:i:s") . " GMT"; $id = "<".$int_id."@$this->domain>"; if ($action =="new") { $message = "Subject: $this->subject\nFrom: $this->from\nPath:London!\nNewsgroups: $this->group\nMessage-ID: <$this->id@$this->domain>\nDate: $Today\n\n$this->body\n.\n"; } else { // format reply message $this->messageExists(); $message = "Subject: $this->subject\nFrom: $this->from\nPath:London!\nNewsgroups: $this->group\nReferences: $this->references\nMessage-ID: $int_id\nDate: $Today\n\n$this->body\n.\n"; } fputs($this->fp, $message); $x=fgets($this->fp, 1024); if (substr($x, 0,3) != 240) { $this->currentTextualError = "Posting to newsgroup failed, server reported: ".$x; $this->currentErrorCode = NNTP_POST_FAILED; return false; } $this->ret=1; return true; } function messageExists() { // we are currently in post mode need to get out of it fputs($this->fp, "\n.\n"); $x=fgets($this->fp, 1024); // now we can test if the message really exists fputs($this->fp, "stat $this->references\n"); $x=fgets($this->fp, 1024); if (substr($x, 0,3) != 223) { $this->currentTextualError = "No message existed with the quoted id. It is possible that the original message was not posted to the newsgroups. The server reported ".$x; $this->currentErrorCode = UNEXISTING_MSG; return false; } // now we can go back into post fputs($this->fp, "post\n"); $x=fgets($this->fp, 1024); if (substr($x, 0,3) != 340) { $this->currentTextualError = "Posting to newsgroup failed, server reported: ".$x; $this->currentErrorCode = NNTP_POST_FAILED; return false; } return true; } function getTextualError() { return $this->currentTextualError; } function getErrorCode() { return $this->currentErrorCode; } }