[ Index ] |
|
Code source de bblocked 0.6.5 |
1 <?php 2 /*********************************************************\ 3 ****** bblocked HTTP Class ****** 4 ***** ***** 5 **** Copyleft (C) 2007 bblocked **** 6 *** *** 7 ** This program is free software; you can redistribute it and/or ** 8 ** modify it under the terms of the GNU General Public License ** 9 ** as published by the Free Software Foundation; either version 2 ** 10 ** of the License, or (at your option) any later version. ** 11 ** ** 12 ** This program is distributed in the hope that it will be useful, ** 13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 15 ** GNU General Public License for more details. ** 16 *** *** 17 **** **** 18 **** http://www.bblocked.org/ ***** 19 ****** ****** 20 \*********************************************************/ 21 22 require ('base.php'); 23 24 25 // Extend bblocked Proxy class for HTTP support 26 27 class HTTP extends Proxy { 28 29 function HTTP($url=false) { 30 31 $this->_method = strtoupper($_SERVER['REQUEST_METHOD'] ? $_SERVER['REQUEST_METHOD'] : 'GET'); 32 33 if(!($url ? $this->url_parse($url, $this->_url) : is_array($this->_url))) 34 die('Could not parse URL.'); 35 36 if((bool)$GLOBALS['_config']['enable_cache'] === true) { 37 38 if($this->read_cache($data) == true) 39 $this->parse_response("HTTP/1.1 200 OK\r\n" . $data, false); 40 41 else if($this->send_http_request($data)) 42 $this->parse_response($data, true); 43 44 else 45 report_errors($this->_url, 'connection', 'general'); 46 47 $this->output_page(); 48 } 49 50 else { 51 52 if($this->send_http_request($data)) 53 $this->parse_response($data, false) !== false ? $this->output_page() : ''; 54 } 55 } 56 57 function send_http_request(&$output) { 58 59 if($GLOBALS['_config']['proxy_enable']) { 60 61 $host = $GLOBALS['_config']['proxy_host']; 62 $port = $GLOBALS['_config']['proxy_port']; 63 } 64 65 else { 66 67 $host = $this->_url['host']; 68 $port = $this->_url['port']; 69 } 70 71 if(false === ($this->_socket = @fsockopen(($this->_url['scheme'] === 'https' && $GLOBALS['_config']['ssl'] ? 'ssl://' : 'tcp://') . $this->_url['host'], $port, $errno, $errstr, 30))) { 72 73 if($GLOBALS['_config']['search_keywords'] && gethostbyname($this->_url['host']) == $this->_url['host']) 74 $this->HTTP(str_replace('*', urlencode(rtrim(str_replace('http://', '', $this->_url['full']), '/')), $GLOBALS['_config']['keyword_url'])); 75 76 else 77 report_errors($this->_url, 'connection', 'http_notfound'); 78 79 return false; 80 } 81 82 $headers = $this->_method . ' ' . $this->_url['path']; 83 84 if(isset($this->_url['query'])) { 85 86 $headers .= '?'; 87 $query = preg_split("'([&;])'", $this->_url['query'], -1, PREG_SPLIT_DELIM_CAPTURE); 88 for($i = 0, $count = count($query); $i < $count; $headers .= implode('=', array_map('urlencode', array_map('urldecode', explode('=', $query[$i])))) . (isset($query[++$i]) ? $query[$i] : ''), $i++); 89 } 90 91 $headers .= " HTTP/1.0\r\n"; 92 $headers .= "Host: {$this->_url['host']}{$this->_url['port_ext']}\r\n"; 93 $headers .= 'User-Agent: ' . ($GLOBALS['_config']['user_agent'] ? $GLOBALS['_config']['user_agent'] : ($_SERVER['HTTP_USER_AGENT'] ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (' . PHP_OS . '; en-US; +http://www.bblocked.org/) ' . $GLOBALS['_config']['bblocked_ver_full'])) . "\r\n"; 94 $headers .= 'Accept: ' . (isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '*/*;q=0.1') . "\r\n"; 95 96 if(isset($_SERVER['HTTP_ACCEPT_CHARSET'])) 97 $headers .= "Accept-Charset: {$_SERVER['HTTP_ACCEPT_CHARSET']}\r\n"; 98 99 if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) 100 $headers .= "Accept-Language: {$_SERVER['HTTP_ACCEPT_LANGUAGE']}\r\n"; 101 102 if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) 103 $headers .= "If-Modified-Since: {$_SERVER['HTTP_IF_MODIFIED_SINCE']}\r\n"; 104 105 if(!$GLOBALS['_config']['remove_referer'] && isset($_SERVER['HTTP_REFERER']) && preg_match("'\Q" . $GLOBALS['_config']['script_url'] . "?\E.*?\Q" . $GLOBALS['_config']['arg_url'] . "=\E([^&]*)'", $_SERVER['HTTP_REFERER'], $matches)) 106 $headers .= 'Referer: ' . decode_url($matches[1]) . "\r\n"; 107 108 if(!empty($_COOKIE)) { 109 110 $cookie = ''; 111 $auth = array(); 112 113 foreach($_COOKIE as $cookie_id=>$cookie_content) { 114 115 $cookie_id = explode(';', rawurldecode($cookie_id)); 116 $cookie_content = explode(';', rawurldecode($cookie_content[0])); 117 118 if($cookie_id[0] === 'COOKIE') { 119 120 $cookie_id[3] = str_replace('_', '.', $cookie_id[3]); 121 122 if(count($cookie_id) < 4 || ($cookie_content[1] == 'secure' && $this->_url['scheme'] != 'https')) 123 continue; 124 125 if((preg_match("'\Q" . $cookie_id[3] . "\E$'i", $this->_url['host']) || strtolower($cookie_id[3]) == strtolower('.' . $this->_url['host'])) && preg_match("'^\Q" . $cookie_id[2] . "\E'", $this->_url['path'])) 126 $cookie .= ($cookie != '' ? '; ' : '') . (empty($cookie_id[1]) ? '' : $cookie_id[1] . '=') . $cookie_content[0]; 127 } 128 } 129 } 130 131 if(!empty($_SESSION['data']['cookies'])) { 132 133 foreach($_SESSION['data']['cookies'] as $cookie_id=>$cookie_content) { 134 135 $cookie_id = explode(';', unserialize($cookie_id)); 136 $cookie_content = explode(';', unserialize($cookie_content)); 137 138 $cookie_id[2] = str_replace('_', '.', $cookie_id[2]); 139 140 if(count($cookie_id) < 3 || ($cookie_content[1] == 'secure' && $this->_url['scheme'] != 'https')) 141 continue; 142 143 if((preg_match("'\Q" . $cookie_id[2] . "\E$'i", $this->_url['host']) || strtolower($cookie_id[2]) == strtolower('.' . $this->_url['host'])) && preg_match("'^\Q" . $cookie_id[1] . "\E'", $this->_url['path'])) 144 $cookie .= ($cookie != '' ? '; ' : '') . (empty($cookie_id[0]) ? '' : $cookie_id[0] . '=') . $cookie_content[0]; 145 } 146 } 147 148 if(!empty($_SESSION['data']['auth'])) { 149 150 foreach($_SESSION['data']['auth'] as $auth_id=>$auth_content) { 151 152 $auth_id = explode(';', unserialize($auth_id)); 153 $auth_content = explode(';', unserialize($auth_content)); 154 155 $auth_id[1] = str_replace('_', '.', $auth_id[1]); 156 157 if($this->_url['host'] . ':' . $this->_url['port'] === $auth_id[1]) 158 $this->_auth[$auth_id[0]] = $v; 159 } 160 } 161 162 if($cookie != '') { 163 164 $headers .= "Cookie: $cookie\r\n"; 165 $this->_no_cache = true; 166 } 167 168 if(isset($this->_realm)) 169 $basic_auth_realm = $this->_realm; 170 171 if(isset($_POST[$GLOBALS['_config']['arg_user']], $_POST[$GLOBALS['_config']['arg_pass']])) { 172 173 $basic_auth_realm = $_POST[$GLOBALS['_config']['arg_realm']]; 174 $basic_auth_header = base64_encode($_POST[$GLOBALS['_config']['arg_user']] . ':' . $_POST[$GLOBALS['_config']['arg_pass']]); 175 } 176 177 if(isset($this->_url['user'], $this->_url['pass'])) 178 $basic_auth_header = base64_encode($this->_url['user'] . ':' . $this->_url['pass']); 179 180 if(!empty($basic_auth_header)) { 181 182 $_SESSION['data']['auth']["{$basic_auth_realm};{$this->_url['host']}:{$this->_url['port']}"] = $basic_auth_header; 183 $headers .= "Authorization: Basic {$basic_auth_header}\r\n"; 184 } 185 186 else if(!empty($basic_auth_realm) && isset($this->_auth[$basic_auth_realm])) 187 $headers .= "Authorization: Basic {$this->_auth[$basic_auth_realm]}\r\n"; 188 189 else if(list($basic_auth_realm, $basic_auth_header) = each($this->_auth)) 190 $headers .= "Authorization: Basic {$basic_auth_header}\r\n"; 191 192 if($this->_method == 'POST') { 193 194 if(!empty($_FILES) && $GLOBALS['_config']['file_uploads']) { 195 196 $boundary = '----' . md5(uniqid(rand(), true)); 197 198 foreach(encode_post($_POST) as $k=>$v) { 199 200 $post_data .= "--{$boundary}\r\n"; 201 $post_data .= "Content-Disposition: form-data; name=\"{$k}\"\r\n\r\n"; 202 $post_data .= urldecode($v) . "\r\n"; 203 } 204 205 foreach(get_post_files($_FILES) as $k=>$info) { 206 207 $post_data .= "--{$boundary}\r\n"; 208 $post_data .= "Content-Disposition: form-data; name=\"{$k}\"; filename=\"{$info['name']}\"\r\n"; 209 $post_data .= 'content-type: ' . (empty($info['type']) ? 'application/octet-stream' : $info['type']) . "\r\n\r\n"; 210 211 if(is_readable($info['tmp_name'])) 212 $post_data .= file_get_contents($info['tmp_name']); 213 214 215 $post_data .= "\r\n"; 216 } 217 218 $post_data .= "--{$boundary}--"; 219 $headers .= "content-type: multipart/form-data; boundary={$boundary}\r\n"; 220 $headers .= "Content-Length: " . strlen($post_data) . "\r\n\r\n" . $post_data; 221 } 222 223 224 else { 225 226 foreach(encode_post($_POST) as $k=>$v) 227 $post_data .= (!empty($post_data) ? '&' : '') . $k . '=' . $v; 228 229 230 $headers .= "content-type: application/x-www-form-urlencoded\r\n"; 231 $headers .= "Content-Length: " . strlen($post_data) . "\r\n\r\n" . $post_data; 232 } 233 234 $post_data = ''; 235 } 236 237 if(fwrite($this->_socket, $headers . "\r\n") !== false) { 238 239 while(!feof($this->_socket)) 240 $output .= @fgets($this->_socket, 8192); 241 242 return true; 243 } 244 245 return false; 246 } 247 } 248 249 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Tue Nov 20 20:31:26 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |