Fixes for NNTP. Was not returning results. Also, cleanups and now meets coding standards.

This commit is contained in:
skeeter 2001-03-30 00:58:38 +00:00
parent e274ab6a23
commit 2026b25976

View File

@ -21,182 +21,217 @@
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
\**************************************************************************/ \**************************************************************************/
/* $Id$ */ /* $Id$ */
class network class network
{ {
var $socket; var $socket;
var $addcrlf = TRUE; var $addcrlf = TRUE;
var $error; var $error;
var $errorset = 0; var $errorset = 0;
function network($addcrlf=true) function network($addcrlf=true)
{ {
$this->errorset = 0; $this->errorset = 0;
$this->set_addcrlf($addcrlf); $this->set_addcrlf($addcrlf);
}
function set_addcrlf($value)
{
$this->addcrlf = $value;
}
function add_crlf($str)
{
if($this->addcrlf) {
$str .= "\r\n";
}
return $str;
}
function set_error($code,$msg,$desc)
{
$this->error = array("code","msg","desc");
$this->error["code"] = $code;
$this->error["msg"] = $msg;
$this->error["desc"] = $desc;
// $this->close_port();
$this->errorset = 1;
return 0;
}
function open_port($server,$port,$timeout=15)
{
global $phpgw_info;
switch($port) {
case 80:
case 443:
if((isset($phpgw_info["server"]["httpproxy_server"]) && $phpgw_info["server"]["httpproxy_server"]) &&
(isset($phpgw_info["server"]["httpproxy_port"]) && $phpgw_info["server"]["httpproxy_port"])) {
$server = $phpgw_info["server"]["httpproxy_server"];
$port = (int)$phpgw_info["server"]["httpproxy_port"];
} }
break;
}
if(floor(phpversion()) == 4)
$this->socket = fsockopen($server,$port,&$errcode,&$errmsg,$timeout);
else
$this->socket = fsockopen($server,$port,&$errcode,&$errmsg);
if (!$this->socket) {
return $this->set_error("Error","$errcode:$errmsg","Connection to $server:$port failed - could not open socket.");
} else {
return 1;
}
}
function close_port() function set_addcrlf($value)
{ {
return fclose($this->socket); $this->addcrlf = $value;
} }
function read_port() function add_crlf($str)
{ {
return fgets($this->socket, 1024); if($this->addcrlf)
} {
$str .= "\r\n";
}
return $str;
}
function bs_read_port($bytes) function set_error($code,$msg,$desc)
{ {
return fread($this->socket, $bytes); $this->error = array('code','msg','desc');
} $this->error['code'] = $code;
$this->error['msg'] = $msg;
$this->error['desc'] = $desc;
// $this->close_port();
$this->errorset = 1;
return 0;
}
function write_port($str) function open_port($server,$port,$timeout=15)
{ {
$ok = fputs($this->socket,$this->add_crlf($str)); global $phpgw_info;
if (!$ok)
{
return $this->set_error("Error","Connection Lost","lost connection to server");
} else {
return 1;
}
}
function bs_write_port($str,$bytes=0) switch($port)
{ {
if ($bytes) case 80:
$ok = fwrite($this->socket,$this->add_crlf($str),$bytes); case 443:
else if((isset($phpgw_info['server']['httpproxy_server']) && $phpgw_info['server']['httpproxy_server']) &&
$ok = fwrite($this->socket,$this->add_crlf($str)); (isset($phpgw_info['server']['httpproxy_port']) && $phpgw_info['server']['httpproxy_port']))
{
$server = $phpgw_info['server']['httpproxy_server'];
$port = (int)$phpgw_info['server']['httpproxy_port'];
}
break;
}
if(floor(phpversion()) == 4)
{
$this->socket = fsockopen($server,$port,&$errcode,&$errmsg,$timeout);
}
else
{
$this->socket = fsockopen($server,$port,&$errcode,&$errmsg);
}
if (!$this->socket)
{
return $this->set_error('Error',$errcode.':'.$errmsg,'Connection to '.$server.':'.$port.' failed - could not open socket.');
}
else
{
return 1;
}
}
if (!$ok) function close_port()
{ {
return $this->set_error("Error","Connection Lost","lost connection to server"); return fclose($this->socket);
} else { }
return 1;
}
}
function msg2socket($str,$expected_response,$response) function read_port()
{ {
if(!$this->socket && substr($expected_response,1,1) == "+") { return fgets($this->socket, 1024);
return $this->set_error("521", }
"socket does not exist",
"The required socket does not exist. The settings for your mail server may be wrong.");
}
if (!$this->write_port($str)) {
if(substr($expected_response,1,1) == "+") {
return $this->set_error("420",
"lost connection",
"Lost connection to pop server.");
} else {
return 0;
}
}
$response = $this->read_port();
if (!ereg(strtoupper($expected_response),strtoupper($response))) {
if(substr($expected_response,1,1) == "+") {
return $this->set_error("550",
"",
"");
}
$pos = strpos(" ",$response);
return $this->set_error(substr($response,0,$pos),
"invalid response($expected_response)",
substr($response,($pos + 1),(strlen($response)-$pos)));
} else {
return 1;
}
}
// return contents of a web url as an array or false if timeout function bs_read_port($bytes)
function gethttpsocketfile($file) {
{ return fread($this->socket, $bytes);
global $phpgw_info; }
$server = str_replace("http://","",$file); function write_port($str)
$file = strstr($server,"/"); {
$server = str_replace("$file","",$server); $ok = fputs($this->socket,$this->add_crlf($str));
if ($phpgw_info["server"]["httpproxy_server"]) { if (!$ok)
if ($this->open_port($server,80, 15)) { {
if (! $this->write_port("GET http://" . $server . $file . " HTTP/1.0\n\n")) { return $this->set_error('Error','Connection Lost','lost connection to server');
return False; }
} else
$i = 0; {
while ($line = $this->read_port()) { return 1;
if (feof($this->socket)) { }
break; }
}
$lines[] = $line; function bs_write_port($str,$bytes=0)
$i++; {
} if ($bytes)
$this->close_port(); {
return $lines; $ok = fwrite($this->socket,$this->add_crlf($str),$bytes);
} else { }
return False; else
} {
} else { $ok = fwrite($this->socket,$this->add_crlf($str));
if ($this->open_port($server, 80, 15)) { }
if (!$this->write_port("GET $file HTTP/1.0\nHost: $server\n\n")) { if (!$ok)
return 0; {
} return $this->set_error('Error','Connection Lost','lost connection to server');
while ($line = $this->read_port()) { }
$lines[] = $line; else
} {
$this->close_port(); return 1;
return $lines; }
} else { }
return 0;
} function msg2socket($str,$expected_response,&$response)
} {
} if(!$this->socket && substr($expected_response,1,1) == '+')
{
return $this->set_error('521','socket does not exist',
'The required socked does not exist. The settings for your mail server may be wrong.');
}
if (!$this->write_port($str))
{
if(substr($expected_response,1,1) == '+')
{
return $this->set_error('420','lost connection','Lost connection to pop server.');
}
else
{
return 0;
}
}
$response = $this->read_port();
if (!ereg(strtoupper($expected_response),strtoupper($response)))
{
if(substr($expected_response,1,1) == '+')
{
return $this->set_error('550','','');
}
$pos = strpos(' ',$response);
return $this->set_error(substr($response,0,$pos),
'invalid response('.$expected_response.')',
substr($response,($pos + 1),(strlen($response)-$pos)));
}
else
{
return 1;
}
}
// return contents of a web url as an array or false if timeout
function gethttpsocketfile($file)
{
global $phpgw_info;
$server = str_replace('http://','',$file);
$file = strstr($server,'/');
$server = str_replace($file,'',$server);
if ($phpgw_info['server']['httpproxy_server'])
{
if ($this->open_port($server,80, 15))
{
if (! $this->write_port('GET http://' . $server . $file . ' HTTP/1.0'."\n\n"))
{
return False;
}
$i = 0;
while ($line = $this->read_port())
{
if (feof($this->socket))
{
break;
}
$lines[] = $line;
$i++;
}
$this->close_port();
return $lines;
}
else
{
return False;
}
}
else
{
if ($this->open_port($server, 80, 15))
{
if (!$this->write_port('GET '.$file.' HTTP/1.0'."\n".'Host: '.$server."\n\n"))
{
return 0;
}
while ($line = $this->read_port())
{
$lines[] = $line;
}
$this->close_port();
return $lines;
}
else
{
return 0;
}
}
}
} }