nntp.inc

来自「eGroupWare is a multi-user, web-based gr」· INC 代码 · 共 568 行 · 第 1/2 页

INC
568
字号
<?php/**************************************************************************** copyright            : (C) 2001-2003 Advanced Internet Designs Inc.* email                : forum@prohost.org* $Id: nntp.inc,v 1.2 2003/12/18 15:46:40 iliaa Exp $** This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or * (at your option) any later version.***************************************************************************/class fud_nntp{	var $server, $port=119, $user, $pass, $timeout=25, $newsgroup;	var $error, $lrc;	var $fs;	var $group_na, $group_first, $group_last, $nntp_opt;	// Per Message Varaibles	var $headers, $body;	var $raw_msg;	var $msg_id, $reply_to, $reply_to_msg_id, $from_email, $from_name, $ip, $thread_id;	var $user_id;	var $attachments;	function get_return_code()	{		$this->lrc = fgets($this->fs, 1024);		return trim(substr($this->lrc, 0, strpos($this->lrc,' ')));	}	function compare_return_codes($code, $str='')	{		if (empty($str)) {			$str = $this->get_return_code();		}		return ($str == $code);	}	function auth_cmd()	{		if ($this-> nntp_opt & 128) {			switch ($this->cmd('AUTHINFO user '.$this->user, false)) {				case 281:					return true;					break 2;				case 381:					if ($this->cmd('AUTHINFO pass '.$this->pass, false) != 281) {						$this->error = "Authentication failed\n";						return false;					} else {						return true;					}					break;				default:					return false;					break;			}		} else if (!($this->nntp_opt & 64)) {			if ($this->cmd('AUTHINFO SIMPLE', false) != 350) {				$this->error = "Authentication failed\n";				return false;			} else {				if ($this->cmd($this->user." ".$this->pass, false) == 250) {					return true;				} else {					return false;				}			}		} else {			$this->error = "NNTP Authentication required, but no authentication method specified\n";			return false;		}		return false;	}	function cmd($cmd, $auth=true)	{		fputs($this->fs, $cmd."\r\n");		$code = $this->get_return_code();		if ($auth && ($code == 450 || $code == 480)) {			if (!$this->auth_cmd()) {				return false;			}			$code = $this->cmd($cmd, false);		}		return $code;	}	function connect()	{		$this->fs = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);		if (!@is_resource($this->fs)) {			$this->error = "Unable to establish connection to $this->server on port $this->port failed\nWith Error #$errno : $errstr\n";			return false;		}		if (!socket_set_blocking($this->fs, true)) {			$this->error = "Unable to make socket to blocking mode\n";			return false;		}		$ret = $this->get_return_code();		if (!$this->compare_return_codes(200, $ret) && !$this->compare_return_codes(201, $ret)) {			$this->error = "Failed to recieve proper response from NNTP Server, got ".$this->lrc."\n";			return false;		}		$ret = $this->cmd("MODE reader");		if (!$this->compare_return_codes(200, $ret) && !$this->compare_return_codes(201, $ret)) {			$this->error = "Failed to recieve proper response from NNTP Server when setting 'MODE reader', got ".$this->lrc."\n";			return false;		}		if (($this->cmd("GROUP ".$this->newsgroup) != 211)) {			$this->error = "Unable to use ".$this->newsgroup." newsgroup NTTP Msg: ".$this->lrc."\n";			return false;		} else {			$tmp = explode(" ", $this->lrc);			$this->group_na = $tmp[1];			$this->group_first = $tmp[2];			$this->group_last = $tmp[3];		}		return true;	}	function get_message($id)	{		// Zero the vars		$this->attachments=$this->user_id=$this->headers=$this->body=$this->raw_msg=$this->msg_id=$this->reply_to_msg_id=$this->from_email=$this->from_name=$this->ip=null;		if ($this->cmd("ARTICLE $id") != 220) {			$this->error = "Unable to Fetch Article #".$id.", NTTP Msg: ".$this->lrc."\n";			return false;		}		while (!feof($this->fs)) {			$line = fgets($this->fs, 1024);			if (!$line || $line == ".\r\n" || $line == ".\n") {				break;			}			$this->raw_msg .= $line;		}		if (!preg_match("!^(.*?)\r?\n\r?\n(.*)!s", $this->raw_msg, $m)) {			return false;		}		$this->body = trim($m[2]);		$this->headers = trim($m[1]);		return true;	}	function close_connection()	{		if (@is_resource($this->fs)) {			unset($this->fs);		}	}	function format_headers()	{		$this->headers = str_replace("\r\n", "\n", $this->headers);		$hdr = explode("\n", trim($this->headers));		$this->headers = array();		foreach($hdr as $v) {			$hk = substr($v, 0, ($p = strpos($v, ':')));			// Skip non-valid header lines			if (empty($hk) || ($v[++$p] != ' ' && $v[$p] != "\t")) {				continue;			}			$hk = strtolower(trim($hk));			$this->headers[$hk] = trim(substr($v, $p));		}		// Fetch Message ID		if (isset($this->headers['message-id'])) {			$this->msg_id = substr(trim($this->headers['message-id']), 1, -1);		} else {			nntp_error_log("No message id", $this->raw_msg);		}		// This fetches the id of the message if this is a reply to an existing message		if (!empty($this->headers['references']) && preg_match_all('!<?([^\s<>]+)>?!', trim($this->headers['references']), $match)) {			$this->reply_to_msg_id = array_reverse($match[1]);		} else if (!empty($this->headers['reply-to']) && preg_match('!.*<?([^>]+)>?$!', trim($this->headers['reply-to']), $match)) {			$this->reply_to_msg_id = array($match[1]);		}		// Fetch From email and Possible name		if (preg_match('!(.*?)<(.*?)>!', $this->headers['from'], $matches)) {			$this->from_email = trim($matches[2]);			if (!empty($matches[1])) {				$matches[1] = trim($matches[1]);				if ($matches[1][0] == '"' && substr($matches[1], -1) == '"') {					$this->from_name = substr($matches[1], 1, -1);				} else {					$this->from_name = $matches[1];				}			} else {				$this->from_name = $this->from_email;			}			if (preg_match('![^A-Za-z0-9\-_\s]!', $this->from_name)) {				$this->from_name = substr($this->from_email, 0, strpos($this->from_email, '@'));			}		} else {			$this->from_email = trim($this->headers['from']);			$this->from_name = substr($this->from_email, 0, strpos($this->from_email, '@'));		}		$this->subject = htmlspecialchars(trim(decode_header_value($this->headers['subject'])));		// Attempt to Get Poster's IP from fields commonly used to store it		if (isset($this->headers['nntp-posting-host'])) {			$this->ip = parse_ip($this->headers['nntp-posting-host']);		}		if (!$this->ip && isset($this->headers['x-trace'])) {			$this->ip = parse_ip($this->headers['x-trace']);		}		if (!$this->ip && isset($this->headers['path'])) {			$this->ip = parse_ip($this->headers['path']);		}	}	function fud_uudecode($data)	{		$data = trim($data);		// begin 0-7{3} (.*)\r\n (filename)		if (strncmp($data, 'begin', 5)) {			return;		}		$filename = substr($data, 0, ($e=strpos($data, "\n")));		$filename = substr($filename, strpos($filename, " ", 6)+1);		if (($e2 = strrpos($data, 'end')) === false) {			return;		}		$data = trim(substr($data, $e, ($e2-$e)));		$tmp = explode("\n", $data);		$out = '';		foreach($tmp as $line) {			$p = 0;			$n = ((ord($line[$p]) -32) & 077);			if ($n <= 0) {				break;			}			for (++$p; $n > 0; $n -= 3) {				if ($n >= 3) {					$out .= chr(((ord($line[$p++]) - 32) & 077) << 2 | ((ord($line[$p]) - 32) & 077) >> 4);					$out .= chr(((ord($line[$p++]) - 32) & 077) << 4 | ((ord($line[$p]) - 32) & 077) >> 2);					$out .= chr(((ord($line[$p++]) - 32) & 077) << 6 | ((ord($line[$p++]) - 32) & 077));				} else {					if ($n >= 1) {						$out .= chr(((ord($line[$p]) - 32) & 077) << 2 | ((ord($line[$p+1]) - 32) & 077) >> 4);					}					if ($n >= 2) {						$out .= chr(((ord($line[$p+1]) - 32) & 077) << 4 | ((ord($line[$p+2]) - 32) & 077) >> 2);					}					$p += 4;				}			}		}		$this->attachments[$filename] = $out;	}	function fud_base64decode($data)	{		if (strncmp($data, 'begin-base64', 12)) {			return;		}		$filename = substr($data, 0, ($e=strpos($data, "\n")));		$filename = substr($filename, strpos($filename, " ", 13)+1);		if (($e2 = strpos($data, "====", $e)) === false) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?