1<?php
2	/**
3	* Handles opening network socket connections, taking proxy into account
4	* @author Mark Peters <skeeter@phpgroupware.org>
5	* @copyright Copyright (C) 2000-2002 Mark Peters
6	* @copyright Portions Copyright (C) 2003,2004 Free Software Foundation, Inc. http://www.fsf.org/
7	* @license http://www.fsf.org/licenses/lgpl.html GNU Lesser General Public License
8	* @package phpgwapi
9	* @subpackage network
10	* @version $Id: class.network.inc.php 14407 2004-02-10 13:51:20Z ceb $
11	*/
12
13	/**
14	* Handles opening network socket connections, taking proxy into account
15	*
16	* @package phpgwapi
17	* @subpackage network
18	*/
19	class network
20	{
21		var $socket;
22		var $addcrlf = TRUE;
23		var $error;
24		var $errorset = 0;
25
26		function network($addcrlf=true)
27		{
28			$this->errorset = 0;
29			$this->set_addcrlf($addcrlf);
30		}
31
32		function set_addcrlf($value)
33		{
34			$this->addcrlf = $value;
35		}
36
37		function add_crlf($str)
38		{
39			if($this->addcrlf)
40			{
41				$str .= "\r\n";
42			}
43			return $str;
44		}
45
46		function set_error($code,$msg,$desc)
47		{
48			$this->error = array('code','msg','desc');
49			$this->error['code'] = $code;
50			$this->error['msg'] = $msg;
51			$this->error['desc'] = $desc;
52	//		$this->close_port();
53			$this->errorset = 1;
54			return 0;
55		}
56
57		function open_port($server,$port,$timeout=15)
58		{
59			switch($port)
60			{
61				case 80:
62				case 443:
63					if((isset($GLOBALS['phpgw_info']['server']['httpproxy_server']) && $GLOBALS['phpgw_info']['server']['httpproxy_server']) &&
64						(isset($GLOBALS['phpgw_info']['server']['httpproxy_port']) && $GLOBALS['phpgw_info']['server']['httpproxy_port']))
65					{
66						$server = $GLOBALS['phpgw_info']['server']['httpproxy_server'];
67						$port   = (int)$GLOBALS['phpgw_info']['server']['httpproxy_port'];
68					}
69					break;
70			}
71			$this->socket = @fsockopen($server,$port,$errcode,$errmsg,$timeout);
72			if($this->socket)
73			{
74				socket_set_timeout($this->socket,$timeout,0);
75			}
76			if (!$this->socket)
77			{
78				return $this->set_error('Error',$errcode.':'.$errmsg,'Connection to '.$server.':'.$port.' failed - could not open socket.');
79			}
80			else
81			{
82				return 1;
83			}
84		}
85
86		function close_port()
87		{
88			return fclose($this->socket);
89		}
90
91		function read_port()
92		{
93			return fgets($this->socket, 1024);
94		}
95
96		function bs_read_port($bytes)
97		{
98			return fread($this->socket, $bytes);
99		}
100
101		function write_port($str)
102		{
103			$ok = fputs($this->socket,$this->add_crlf($str));
104			if (!$ok)
105			{
106				return $this->set_error('Error','Connection Lost','lost connection to server');
107			}
108			else
109			{
110				return 1;
111			}
112		}
113
114		function bs_write_port($str,$bytes=0)
115		{
116			if ($bytes)
117			{
118				$ok = fwrite($this->socket,$this->add_crlf($str),$bytes);
119			}
120			else
121			{
122				$ok = fwrite($this->socket,$this->add_crlf($str));
123			}
124			if (!$ok)
125			{
126				return $this->set_error('Error','Connection Lost','lost connection to server');
127			}
128			else
129			{
130				return 1;
131			}
132		}
133
134		function msg2socket($str,$expected_response,&$response)
135		{
136			if(!$this->socket)
137			{
138				return $this->set_error('521','socket does not exist',
139					'The required socked does not exist.  The settings for your mail server may be wrong.');
140			}
141			if (!$this->write_port($str))
142			{
143				if(substr($expected_response,1,1) == '+')
144				{
145					return $this->set_error('420','lost connection','Lost connection to pop server.');
146				}
147				else
148				{
149					return 0;
150				}
151			}
152			$response = $this->read_port();
153			if (!ereg(strtoupper($expected_response),strtoupper($response)))
154			{
155				if(substr($expected_response,1,1) == '+')
156				{
157					return $this->set_error('550','','');
158				}
159				$pos = strpos(' ',$response);
160				return $this->set_error(substr($response,0,$pos),
161					'invalid response('.$expected_response.')',
162					substr($response,($pos + 1),(strlen($response)-$pos)));
163			}
164			else
165			{
166				return 1;
167			}
168		}
169
170		// return contents of a web url as an array or false if timeout
171		function gethttpsocketfile($file,$user='',$passwd='')
172		{
173			$server = str_replace('http://','',$file);
174			$file = strstr($server,'/');
175			$server = str_replace($file,'',$server);
176
177			//allows for access to http-auth pages - added by Dave Hall <dave.hall@mbox.com.au>
178			if(!((empty($user))&&(empty($passwd))))
179			{
180				$auth = 'Authorization: Basic '.base64_encode("$user:$passwd")."\n";
181			}
182			else
183			{
184				$auth = '';
185			}
186
187			if ($GLOBALS['phpgw_info']['server']['httpproxy_server'])
188			{
189				if ($this->open_port($server,80, 15))
190				{
191					if (! $this->write_port('GET http://' . $server . $file . ' HTTP/1.0'."\n".$auth."\r\n\r\n"))
192					{
193						return False;
194					}
195					$i = 0;
196					while ($line = $this->read_port())
197					{
198						if (feof($this->socket))
199						{
200							break;
201						}
202						$lines[] = $line;
203						$i++;
204					}
205					$this->close_port();
206					return $lines;
207				}
208				else
209				{
210					return False;
211				}
212			}
213			else
214			{
215				if ($this->open_port($server, 80, 15))
216				{
217					if (!$this->write_port('GET '.$file.' HTTP/1.0'."\n".'Host: '.$server."\n".$auth."\r\n\r\n"))
218					{
219						return 0;
220					}
221					while ($line = $this->read_port())
222					{
223						$lines[] = $line;
224					}
225					$this->close_port();
226					return $lines;
227				}
228				else
229				{
230					return 0;
231				}
232			}
233		}
234	}
235?>
236