2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
|
|
|
/**************************************************************************\
|
2004-05-05 14:06:13 +02:00
|
|
|
* eGroupWare API - Network *
|
2001-01-13 11:18:50 +01:00
|
|
|
* This file written by Mark Peters <skeeter@phpgroupware.org> *
|
|
|
|
* Handles opening network socket connections, taking proxy into account *
|
|
|
|
* Copyright (C) 2000, 2001 Mark Peters *
|
2005-11-25 01:13:29 +01:00
|
|
|
* ------------------------------------------------------------------------ *
|
2004-05-05 14:06:13 +02:00
|
|
|
* This library is part of the eGroupWare API *
|
2005-11-25 01:13:29 +01:00
|
|
|
* http://www.egroupware.org/api *
|
2001-01-16 14:52:32 +01:00
|
|
|
* ------------------------------------------------------------------------ *
|
2001-01-13 11:18:50 +01:00
|
|
|
* This library is free software; you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2.1 of the License, *
|
|
|
|
* or any later version. *
|
|
|
|
* This library is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
|
|
* See the GNU Lesser General Public License for more details. *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this library; if not, write to the Free Software Foundation, *
|
|
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
|
2001-01-11 10:52:33 +01:00
|
|
|
\**************************************************************************/
|
2003-12-13 19:29:53 +01:00
|
|
|
|
2001-05-21 11:00:41 +02:00
|
|
|
/* $Id$ */
|
2001-01-11 10:52:33 +01:00
|
|
|
|
2001-05-21 11:00:41 +02:00
|
|
|
class network
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
var $socket;
|
|
|
|
var $addcrlf = TRUE;
|
|
|
|
var $error;
|
|
|
|
var $errorset = 0;
|
2001-03-30 02:58:38 +02:00
|
|
|
|
2001-05-21 11:00:41 +02:00
|
|
|
function network($addcrlf=true)
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
$this->errorset = 0;
|
|
|
|
$this->set_addcrlf($addcrlf);
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
|
|
|
|
2001-05-21 11:00:41 +02:00
|
|
|
function set_addcrlf($value)
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
$this->addcrlf = $value;
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
|
|
|
|
function add_crlf($str)
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
if($this->addcrlf)
|
|
|
|
{
|
|
|
|
$str .= "\r\n";
|
|
|
|
}
|
|
|
|
return $str;
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
|
|
|
|
2001-05-21 11:00:41 +02:00
|
|
|
function set_error($code,$msg,$desc)
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
$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;
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
|
|
|
|
2001-05-21 11:00:41 +02:00
|
|
|
function open_port($server,$port,$timeout=15)
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
switch($port)
|
|
|
|
{
|
|
|
|
case 80:
|
|
|
|
case 443:
|
2005-11-25 01:13:29 +01:00
|
|
|
if((isset($GLOBALS['egw_info']['server']['httpproxy_server']) && $GLOBALS['egw_info']['server']['httpproxy_server']) &&
|
|
|
|
(isset($GLOBALS['egw_info']['server']['httpproxy_port']) && $GLOBALS['egw_info']['server']['httpproxy_port']))
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
2005-11-25 01:13:29 +01:00
|
|
|
$server = $GLOBALS['egw_info']['server']['httpproxy_server'];
|
|
|
|
$port = (int)$GLOBALS['egw_info']['server']['httpproxy_port'];
|
2001-05-21 11:00:41 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2003-09-28 12:32:12 +02:00
|
|
|
if(version_compare(phpversion(),'4.3.0') >= 0)
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
2004-01-26 15:00:52 +01:00
|
|
|
$this->socket = @fsockopen($server,$port,$errcode,$errmsg,$timeout);
|
2002-07-20 03:32:00 +02:00
|
|
|
if($this->socket)
|
|
|
|
{
|
2003-09-28 12:32:12 +02:00
|
|
|
stream_set_timeout($this->socket,$timeout,0);
|
2002-07-20 03:32:00 +02:00
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-01-26 15:00:52 +01:00
|
|
|
$this->socket = @fsockopen($server,$port,$errcode,$errmsg);
|
2001-05-21 11:00:41 +02:00
|
|
|
}
|
2003-12-13 19:29:53 +01:00
|
|
|
if(!$this->socket)
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
|
|
|
return $this->set_error('Error',$errcode.':'.$errmsg,'Connection to '.$server.':'.$port.' failed - could not open socket.');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
|
|
|
|
function close_port()
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
return fclose($this->socket);
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
|
|
|
|
function read_port()
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
return fgets($this->socket, 1024);
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
|
|
|
|
2001-05-21 11:00:41 +02:00
|
|
|
function bs_read_port($bytes)
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
return fread($this->socket, $bytes);
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
|
|
|
|
function write_port($str)
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
$ok = fputs($this->socket,$this->add_crlf($str));
|
2003-12-13 19:29:53 +01:00
|
|
|
if(!$ok)
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
return $this->set_error('Error','Connection Lost','lost connection to server');
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
return 1;
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
|
|
|
|
function bs_write_port($str,$bytes=0)
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2003-12-13 19:29:53 +01:00
|
|
|
if($bytes)
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
|
|
|
$ok = fwrite($this->socket,$this->add_crlf($str),$bytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$ok = fwrite($this->socket,$this->add_crlf($str));
|
|
|
|
}
|
2003-12-13 19:29:53 +01:00
|
|
|
if(!$ok)
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
|
|
|
return $this->set_error('Error','Connection Lost','lost connection to server');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
|
|
|
|
2001-05-21 11:00:41 +02:00
|
|
|
function msg2socket($str,$expected_response,&$response)
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-08-01 13:10:23 +02:00
|
|
|
if(!$this->socket)
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
|
|
|
return $this->set_error('521','socket does not exist',
|
2004-01-07 10:17:15 +01:00
|
|
|
'The required socket does not exist. The settings for your mail server may be wrong.');
|
2001-05-21 11:00:41 +02:00
|
|
|
}
|
2003-12-13 19:29:53 +01:00
|
|
|
if(!$this->write_port($str))
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
if(substr($expected_response,1,1) == '+')
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
return $this->set_error('420','lost connection','Lost connection to pop server.');
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
else
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
return 0;
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
}
|
|
|
|
$response = $this->read_port();
|
2003-12-13 19:29:53 +01:00
|
|
|
if(!ereg(strtoupper($expected_response),strtoupper($response)))
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
|
|
|
if(substr($expected_response,1,1) == '+')
|
|
|
|
{
|
|
|
|
return $this->set_error('550','','');
|
2003-12-13 19:29:53 +01:00
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
$pos = strpos(' ',$response);
|
|
|
|
return $this->set_error(substr($response,0,$pos),
|
|
|
|
'invalid response('.$expected_response.')',
|
|
|
|
substr($response,($pos + 1),(strlen($response)-$pos)));
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
return 1;
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
|
2004-01-07 10:17:15 +01:00
|
|
|
// return contents of a web url as an array (or string) or false if timeout
|
2003-12-13 19:29:53 +01:00
|
|
|
function gethttpsocketfile($file,$user='',$passwd='',$string=False)
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
$server = str_replace('http://','',$file);
|
2007-04-30 07:42:05 +02:00
|
|
|
$file = @strstr($server,'/');
|
2001-05-21 11:00:41 +02:00
|
|
|
$server = str_replace($file,'',$server);
|
2002-09-01 15:37:10 +02:00
|
|
|
|
|
|
|
//allows for access to http-auth pages - added by Dave Hall <dave.hall@mbox.com.au>
|
|
|
|
if(!((empty($user))&&(empty($passwd))))
|
|
|
|
{
|
2011-08-03 15:40:51 +02:00
|
|
|
$auth = 'Authorization: Basic '.base64_encode("$user:$passwd")."\r\n";
|
2002-09-01 15:37:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$auth = '';
|
|
|
|
}
|
|
|
|
|
2005-11-25 01:13:29 +01:00
|
|
|
if($GLOBALS['egw_info']['server']['httpproxy_server'])
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2003-09-24 07:23:29 +02:00
|
|
|
$proxyAuth = '';
|
2005-11-25 01:13:29 +01:00
|
|
|
if(!empty($GLOBALS['egw_info']['server']['httpproxy_server_username']))
|
2003-09-24 07:23:29 +02:00
|
|
|
{
|
2005-11-25 01:13:29 +01:00
|
|
|
$proxyUsername = $GLOBALS['egw_info']['server']['httpproxy_server_username'];
|
|
|
|
$proxyPassword = $GLOBALS['egw_info']['server']['httpproxy_server_password'];
|
2011-08-03 15:40:51 +02:00
|
|
|
$proxyAuth = 'Proxy-Authorization: Basic '.base64_encode("$proxyUsername:$proxyPassword")."\r\n";
|
2003-09-24 07:23:29 +02:00
|
|
|
}
|
2003-12-13 19:29:53 +01:00
|
|
|
if($this->open_port($server,80, 15))
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2011-08-03 15:40:51 +02:00
|
|
|
if(!$this->write_port('GET http://' . $server . $file . ' HTTP/1.0'."\r\n".$proxyAuth.$auth."\r\n\r\n"))
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
$i = 0;
|
2003-12-13 19:29:53 +01:00
|
|
|
while($line = $this->read_port())
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
2003-12-13 19:29:53 +01:00
|
|
|
if(feof($this->socket))
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2003-08-28 16:31:11 +02:00
|
|
|
$lines[] = $line;
|
2001-05-21 11:00:41 +02:00
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
$this->close_port();
|
2003-12-13 19:29:53 +01:00
|
|
|
if($string)
|
|
|
|
{
|
|
|
|
return implode("\n",$lines);
|
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
return $lines;
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
else
|
2001-03-30 02:58:38 +02:00
|
|
|
{
|
2001-05-21 11:00:41 +02:00
|
|
|
return False;
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-03 15:40:51 +02:00
|
|
|
if($this->open_port($server, 80, 30))
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
2004-02-26 02:38:51 +01:00
|
|
|
$lines = array();
|
2011-08-03 15:40:51 +02:00
|
|
|
if(!$this->write_port('GET '.$file.' HTTP/1.0'."\r\n".'Host: '.$server."\r\nAccept: */*".($auth?"\r\n".$auth:'')."\r\n\r\n"))
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2003-12-13 19:29:53 +01:00
|
|
|
while($line = $this->read_port())
|
2001-05-21 11:00:41 +02:00
|
|
|
{
|
2003-08-28 16:31:11 +02:00
|
|
|
$lines[] = $line;
|
2001-05-21 11:00:41 +02:00
|
|
|
}
|
|
|
|
$this->close_port();
|
2003-12-13 19:29:53 +01:00
|
|
|
if($string)
|
|
|
|
{
|
|
|
|
return implode("\n",$lines);
|
|
|
|
}
|
2001-05-21 11:00:41 +02:00
|
|
|
return $lines;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2001-03-30 02:58:38 +02:00
|
|
|
}
|
|
|
|
}
|
2001-01-11 10:52:33 +01:00
|
|
|
}
|
2003-12-13 19:29:53 +01:00
|
|
|
?>
|