[2] | 1 | <?php |
---|
| 2 | /**************************************************************************\ |
---|
| 3 | * eGroupWare API - Network * |
---|
| 4 | * This file written by Mark Peters <skeeter@phpgroupware.org> * |
---|
| 5 | * Handles opening network socket connections, taking proxy into account * |
---|
| 6 | * Copyright (C) 2000, 2001 Mark Peters * |
---|
| 7 | * -------------------------------------------------------------------------* |
---|
| 8 | * This library is part of the eGroupWare API * |
---|
| 9 | * http://www.egroupware.org/api * |
---|
| 10 | * ------------------------------------------------------------------------ * |
---|
| 11 | * This library is free software; you can redistribute it and/or modify it * |
---|
| 12 | * under the terms of the GNU Lesser General Public License as published by * |
---|
| 13 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
| 14 | * or any later version. * |
---|
| 15 | * This library is distributed in the hope that it will be useful, but * |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
| 18 | * See the GNU Lesser General Public License for more details. * |
---|
| 19 | * You should have received a copy of the GNU Lesser General Public License * |
---|
| 20 | * along with this library; if not, write to the Free Software Foundation, * |
---|
| 21 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
| 22 | \**************************************************************************/ |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | class network |
---|
| 26 | { |
---|
| 27 | var $socket; |
---|
| 28 | var $addcrlf = TRUE; |
---|
| 29 | var $error; |
---|
| 30 | var $errorset = 0; |
---|
| 31 | |
---|
| 32 | function network($addcrlf=true) |
---|
| 33 | { |
---|
| 34 | $this->errorset = 0; |
---|
| 35 | $this->set_addcrlf($addcrlf); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | function set_addcrlf($value) |
---|
| 39 | { |
---|
| 40 | $this->addcrlf = $value; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | function add_crlf($str) |
---|
| 44 | { |
---|
| 45 | if($this->addcrlf) |
---|
| 46 | { |
---|
| 47 | $str .= "\r\n"; |
---|
| 48 | } |
---|
| 49 | return $str; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | function set_error($code,$msg,$desc) |
---|
| 53 | { |
---|
| 54 | $this->error = array('code','msg','desc'); |
---|
| 55 | $this->error['code'] = $code; |
---|
| 56 | $this->error['msg'] = $msg; |
---|
| 57 | $this->error['desc'] = $desc; |
---|
| 58 | // $this->close_port(); |
---|
| 59 | $this->errorset = 1; |
---|
| 60 | return 0; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | function open_port($server,$port,$timeout=15) |
---|
| 64 | { |
---|
| 65 | switch($port) |
---|
| 66 | { |
---|
| 67 | case 80: |
---|
| 68 | case 443: |
---|
| 69 | if((isset($GLOBALS['phpgw_info']['server']['httpproxy_server']) && $GLOBALS['phpgw_info']['server']['httpproxy_server']) && |
---|
| 70 | (isset($GLOBALS['phpgw_info']['server']['httpproxy_port']) && $GLOBALS['phpgw_info']['server']['httpproxy_port'])) |
---|
| 71 | { |
---|
| 72 | $server = $GLOBALS['phpgw_info']['server']['httpproxy_server']; |
---|
| 73 | $port = (int)$GLOBALS['phpgw_info']['server']['httpproxy_port']; |
---|
| 74 | } |
---|
| 75 | break; |
---|
| 76 | } |
---|
| 77 | if(version_compare(phpversion(),'4.3.0') >= 0) |
---|
| 78 | { |
---|
| 79 | $this->socket = @fsockopen($server,$port,$errcode,$errmsg,$timeout); |
---|
| 80 | if($this->socket) |
---|
| 81 | { |
---|
| 82 | stream_set_timeout($this->socket,$timeout,0); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | else |
---|
| 86 | { |
---|
| 87 | $this->socket = @fsockopen($server,$port,$errcode,$errmsg); |
---|
| 88 | } |
---|
| 89 | if(!$this->socket) |
---|
| 90 | { |
---|
| 91 | return $this->set_error('Error',$errcode.':'.$errmsg,'Connection to '.$server.':'.$port.' failed - could not open socket.'); |
---|
| 92 | } |
---|
| 93 | else |
---|
| 94 | { |
---|
| 95 | return 1; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | function close_port() |
---|
| 100 | { |
---|
| 101 | return fclose($this->socket); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | function read_port() |
---|
| 105 | { |
---|
| 106 | return fgets($this->socket, 1024); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | function bs_read_port($bytes) |
---|
| 110 | { |
---|
| 111 | return fread($this->socket, $bytes); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | function write_port($str) |
---|
| 115 | { |
---|
| 116 | $ok = fputs($this->socket,$this->add_crlf($str)); |
---|
| 117 | if(!$ok) |
---|
| 118 | { |
---|
| 119 | return $this->set_error('Error','Connection Lost','lost connection to server'); |
---|
| 120 | } |
---|
| 121 | else |
---|
| 122 | { |
---|
| 123 | return 1; |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | function bs_write_port($str,$bytes=0) |
---|
| 128 | { |
---|
| 129 | if($bytes) |
---|
| 130 | { |
---|
| 131 | $ok = fwrite($this->socket,$this->add_crlf($str),$bytes); |
---|
| 132 | } |
---|
| 133 | else |
---|
| 134 | { |
---|
| 135 | $ok = fwrite($this->socket,$this->add_crlf($str)); |
---|
| 136 | } |
---|
| 137 | if(!$ok) |
---|
| 138 | { |
---|
| 139 | return $this->set_error('Error','Connection Lost','lost connection to server'); |
---|
| 140 | } |
---|
| 141 | else |
---|
| 142 | { |
---|
| 143 | return 1; |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | function msg2socket($str,$expected_response,&$response) |
---|
| 148 | { |
---|
| 149 | if(!$this->socket) |
---|
| 150 | { |
---|
| 151 | return $this->set_error('521','socket does not exist', |
---|
| 152 | 'The required socket does not exist. The settings for your mail server may be wrong.'); |
---|
| 153 | } |
---|
| 154 | if(!$this->write_port($str)) |
---|
| 155 | { |
---|
| 156 | if(substr($expected_response,1,1) == '+') |
---|
| 157 | { |
---|
| 158 | return $this->set_error('420','lost connection','Lost connection to pop server.'); |
---|
| 159 | } |
---|
| 160 | else |
---|
| 161 | { |
---|
| 162 | return 0; |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | $response = $this->read_port(); |
---|
[5915] | 166 | if(!preg_match(strtoupper("/$expected_response/"),strtoupper($response))) |
---|
[2] | 167 | { |
---|
| 168 | if(substr($expected_response,1,1) == '+') |
---|
| 169 | { |
---|
| 170 | return $this->set_error('550','',''); |
---|
| 171 | } |
---|
| 172 | $pos = strpos(' ',$response); |
---|
| 173 | return $this->set_error(substr($response,0,$pos), |
---|
| 174 | 'invalid response('.$expected_response.')', |
---|
| 175 | substr($response,($pos + 1),(strlen($response)-$pos))); |
---|
| 176 | } |
---|
| 177 | else |
---|
| 178 | { |
---|
| 179 | return 1; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | // return contents of a web url as an array (or string) or false if timeout |
---|
| 184 | function gethttpsocketfile($file,$user='',$passwd='',$string=False) |
---|
| 185 | { |
---|
| 186 | $server = str_replace('http://','',$file); |
---|
| 187 | $file = strstr($server,'/'); |
---|
| 188 | $server = str_replace($file,'',$server); |
---|
| 189 | |
---|
| 190 | //allows for access to http-auth pages - added by Dave Hall <dave.hall@mbox.com.au> |
---|
| 191 | if(!((empty($user))&&(empty($passwd)))) |
---|
| 192 | { |
---|
| 193 | $auth = 'Authorization: Basic '.base64_encode("$user:$passwd")."\n"; |
---|
| 194 | } |
---|
| 195 | else |
---|
| 196 | { |
---|
| 197 | $auth = ''; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | if($GLOBALS['phpgw_info']['server']['httpproxy_server']) |
---|
| 201 | { |
---|
| 202 | $proxyAuth = ''; |
---|
| 203 | if(!empty($GLOBALS['phpgw_info']['server']['httpproxy_server_username'])) |
---|
| 204 | { |
---|
| 205 | $proxyUsername = $GLOBALS['phpgw_info']['server']['httpproxy_server_username']; |
---|
| 206 | $proxyPassword = $GLOBALS['phpgw_info']['server']['httpproxy_server_password']; |
---|
| 207 | $proxyAuth = 'Proxy-Authorization: Basic '.base64_encode("$proxyUsername:$proxyPassword")."\n"; |
---|
| 208 | } |
---|
| 209 | if($this->open_port($server,80, 15)) |
---|
| 210 | { |
---|
| 211 | if(!$this->write_port('GET http://' . $server . $file . ' HTTP/1.0'."\n".$proxyAuth.$auth."\r\n\r\n")) |
---|
| 212 | { |
---|
| 213 | return False; |
---|
| 214 | } |
---|
| 215 | $i = 0; |
---|
| 216 | while($line = $this->read_port()) |
---|
| 217 | { |
---|
| 218 | if(feof($this->socket)) |
---|
| 219 | { |
---|
| 220 | break; |
---|
| 221 | } |
---|
| 222 | $lines[] = $line; |
---|
[7655] | 223 | ++$i; |
---|
[2] | 224 | } |
---|
| 225 | $this->close_port(); |
---|
| 226 | if($string) |
---|
| 227 | { |
---|
| 228 | return implode("\n",$lines); |
---|
| 229 | } |
---|
| 230 | return $lines; |
---|
| 231 | } |
---|
| 232 | else |
---|
| 233 | { |
---|
| 234 | return False; |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | else |
---|
| 238 | { |
---|
| 239 | if($this->open_port($server, 80, 15)) |
---|
| 240 | { |
---|
| 241 | $lines = array(); |
---|
| 242 | if(!$this->write_port('GET '.$file.' HTTP/1.0'."\n".'Host: '.$server."\n".$auth."\r\n\r\n")) |
---|
| 243 | { |
---|
| 244 | return 0; |
---|
| 245 | } |
---|
| 246 | while($line = $this->read_port()) |
---|
| 247 | { |
---|
| 248 | $lines[] = $line; |
---|
| 249 | } |
---|
| 250 | $this->close_port(); |
---|
| 251 | if($string) |
---|
| 252 | { |
---|
| 253 | return implode("\n",$lines); |
---|
| 254 | } |
---|
| 255 | return $lines; |
---|
| 256 | } |
---|
| 257 | else |
---|
| 258 | { |
---|
| 259 | return 0; |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | ?> |
---|