Location: PHPKode > scripts > networknews > networknews/nntp.inc
<?php
class nntp {
  var $handle;

  function connect($server = "localhost", $port = 119, $timeout = 10) {
    if(!$cfgTimeOut) {
      // without timeout
      $this->$handle = fsockopen($server, $port);
    } else {
      // with timeout
      $this->$handle = fsockopen($server, $port, &$errno, &$errstr, $cfgTimeOut);
    }

    if($this->$handle) {
      $this->getreply($blank);
      return true;
    } else {
      return false;
    }
  }

  function login($user, $pass) {
    // identification required on private server
    $this->send ("AUTHINFO USER ".$user);
    $this->getreply($blank);
    $this->send ("AUTHINFO PASS ".$passwd);
    return ($this->getreply($blank) == "281");
  }

  function getgroups() {
    $this->send ("LIST");
    if ($this->getreply($blank) == "215") {
      $data = $this->getdata();
      $groups = split("\r\n", $data);

      for($groupid=0; $groupid<count($groups); $groupid++) {
        $item = split(" ", $groups[$groupid]);
        $groups[$groupid] = array("name" => $item[0], "first" => $item[2], "last" => $item[1], "readonly" => ($item[3]=="n"));
      }

      return $groups;
    }
  }

  function getgroupinfo($group) {
    $this->send("GROUP ".$group);
    $tmp = $this->getreply($data);
    if ($tmp == "411") {
      return false;
    } else {
      $item = split(" ", $data);
      return array("name" => $item[3], "first" => $item[1], "last" => $item[2], "count" => $item[0]);
    }
  }

  function getarticlehead($group, $number) {
    if ($group) {$this->getgroupinfo($group);}
    $this->send("HEAD ".$number);
    $tmp = $this->getreply($data);
    if (($tmp == "412") | ($tmp == "420") | ($tmp == "423") | ($tmp == "430")) {
      return false;
    } else {
      return $this->getdata();
    }
  }

  function getarticlebody($group, $number) {
    if ($group) {$this->getgroupinfo($group);}
    $this->send("BODY ".$number);
    $tmp = $this->getreply($data);
    if (($tmp == "412") | ($tmp == "420") | ($tmp == "423") | ($tmp == "430")) {
      return false;
    } else {
      return $this->getdata();
    }
  }

  function send($data) {
    fputs($this->$handle, $data."\r\n");
  }

  function getreply(&$message) {
    $data = "";
    while (substr($data, strlen($data)-2, 2) != "\r\n") {
      $char = fgets($this->$handle, 1);
      $data = $data . $char;
    }

    $pos = strpos($data, " ");
    if ($pos) {
      $code = substr($data, 0, $pos);
      $message = substr($data, $pos +1);
    } else {
      $code = $data;
      $message="";
    }
    return $code;
  }

  function getdata() {
    $data = "";
    while (substr($data, strlen($data)-5, 5) != "\r\n.\r\n") {
      $char = fgets($this->$handle, 1024);
      $data = $data . $char;
    }

    $data = substr($data, 0, strlen($data)-5);

    return $data;
  }
}

class nntpcommon {
  function parseheader($data) {
    $head = split("\r\n", $data);
    for($lineid=0; $lineid<count($head); $lineid++) {
      $line = $head[$lineid];

      $pos = strpos($line, ": ");
      if ($pos) {
        $headers[strtolower(substr($line, 0, $pos))] = substr($line, $pos +1);
      } else {
      }
    }
    return $headers;
  }

  function parseemail($data) {
    $items = imap_rfc822_parse_adrlist($data, "");

    for($itemid=0; $itemid<count($items); $itemid++){
      $emails[] = array("name" => $items[$itemid]->personal, "address" => $items[$itemid]->mailbox."@".$items[$itemid]->host);
    }
    return $emails;
  }
}
?>
Return current item: networknews