urlopen.php
256 lines
| 1 | <?php |
| 2 | |
| 3 | if (!function_exists('rplg_urlopen')) { |
| 4 | |
| 5 | define('RPLG_USER_AGENT', 'RPLG-WPPlugin/1.0'); |
| 6 | define('RPLG_SOCKET_TIMEOUT', 10); |
| 7 | |
| 8 | function rplg_json_decode($data) { |
| 9 | return json_decode($data); |
| 10 | } |
| 11 | |
| 12 | function rplg_json_urlopen($url, $postdata=false, $headers=array()) { |
| 13 | if (!($response = rplg_urlopen($url, $postdata, $headers)) || !$response['code']) { |
| 14 | //$this->last_error = 'COULDNT_CONNECT'; |
| 15 | return false; |
| 16 | } |
| 17 | return rplg_json_decode($response['data']); |
| 18 | } |
| 19 | |
| 20 | function rplg_urlopen($url, $postdata=false, $headers=array()) { |
| 21 | $response = array( |
| 22 | 'data' => '', |
| 23 | 'code' => 0 |
| 24 | ); |
| 25 | |
| 26 | $url = preg_replace('/\s+/', '+', $url); |
| 27 | |
| 28 | if(function_exists('curl_init')) { |
| 29 | if (!function_exists('curl_setopt_array')) { |
| 30 | function curl_setopt_array(&$ch, $curl_options) { |
| 31 | foreach ($curl_options as $option => $value) { |
| 32 | if (!curl_setopt($ch, $option, $value)) { |
| 33 | return false; |
| 34 | } |
| 35 | } |
| 36 | return true; |
| 37 | } |
| 38 | } |
| 39 | _rplg_curl_urlopen($url, $postdata, $headers, $response); |
| 40 | } else if(ini_get('allow_url_fopen') && function_exists('stream_get_contents')) { |
| 41 | _rplg_fopen_urlopen($url, $postdata, $headers, $response); |
| 42 | } else { |
| 43 | _rplg_fsockopen_urlopen($url, $postdata, $headers, $response); |
| 44 | } |
| 45 | return $response; |
| 46 | } |
| 47 | |
| 48 | /*-------------------------------- curl --------------------------------*/ |
| 49 | function _rplg_curl_urlopen($url, $postdata, $headers, &$response) { |
| 50 | $c = curl_init($url); |
| 51 | $postdata_str = rplg_get_query_string($postdata); |
| 52 | |
| 53 | $c_options = array( |
| 54 | CURLOPT_USERAGENT => RPLG_USER_AGENT, |
| 55 | CURLOPT_RETURNTRANSFER => true, |
| 56 | CURLOPT_POST => ($postdata_str ? 1 : 0), |
| 57 | CURLOPT_HEADER => true, |
| 58 | CURLOPT_HTTPHEADER => array_merge(array('Expect:'), $headers), |
| 59 | CURLOPT_TIMEOUT => RPLG_SOCKET_TIMEOUT |
| 60 | ); |
| 61 | if($postdata) { |
| 62 | $c_options[CURLOPT_POSTFIELDS] = $postdata_str; |
| 63 | } |
| 64 | curl_setopt_array($c, $c_options); |
| 65 | |
| 66 | $open_basedir = ini_get('open_basedir'); |
| 67 | if( empty( $open_basedir ) ) { |
| 68 | curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); |
| 69 | } |
| 70 | curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); |
| 71 | |
| 72 | $data = curl_exec($c); |
| 73 | |
| 74 | // cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string |
| 75 | if (stripos($data, "HTTP/1.0 200 Connection established\r\n\r\n") !== false) { |
| 76 | $data = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $data); |
| 77 | } |
| 78 | |
| 79 | list($resp_headers, $response['data']) = explode("\r\n\r\n", $data, 2); |
| 80 | |
| 81 | $response['headers'] = _rplg_get_response_headers($resp_headers, $response); |
| 82 | $response['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE); |
| 83 | curl_close($c); |
| 84 | } |
| 85 | |
| 86 | /*-------------------------------- fopen --------------------------------*/ |
| 87 | function _rplg_fopen_urlopen($url, $postdata, $headers, &$response) { |
| 88 | $params = array(); |
| 89 | |
| 90 | if($postdata) { |
| 91 | $params = array('http' => array( |
| 92 | 'method' => 'POST', |
| 93 | 'header' => implode("\r\n", array_merge(array('Content-Type: application/x-www-form-urlencoded'), $headers)), |
| 94 | 'content' => rplg_get_query_string($postdata), |
| 95 | 'timeout' => RPLG_SOCKET_TIMEOUT |
| 96 | )); |
| 97 | } else { |
| 98 | $params = array('http' => array( |
| 99 | 'header' => implode("\r\n", $headers) |
| 100 | )); |
| 101 | } |
| 102 | |
| 103 | ini_set('user_agent', RPLG_USER_AGENT); |
| 104 | $ctx = stream_context_create($params); |
| 105 | $fp = fopen($url, 'rb', false, $ctx); |
| 106 | if(!$fp) { return false; } |
| 107 | |
| 108 | $response_header_array = explode(' ', $http_response_header[0], 3); |
| 109 | |
| 110 | $unused = $response_header_array[0]; |
| 111 | |
| 112 | $response['code'] = $response_header_array[0]; |
| 113 | |
| 114 | $unused = $response_header_array[2]; |
| 115 | |
| 116 | $resp_headers = array_slice($http_response_header, 1); |
| 117 | |
| 118 | foreach($resp_headers as $unused=>$header) { |
| 119 | $header = explode(':', $header); |
| 120 | $header[0] = trim($header[0]); |
| 121 | $header[1] = trim($header[1]); |
| 122 | $resp_headers[strtolower($header[0])] = strtolower($header[1]); |
| 123 | } |
| 124 | $response['data'] = stream_get_contents($fp); |
| 125 | $response['headers'] = $resp_headers; |
| 126 | } |
| 127 | |
| 128 | /*-------------------------------- fsockpen --------------------------------*/ |
| 129 | function _rplg_fsockopen_urlopen($url, $postdata, $headers, &$response) { |
| 130 | $buf = ''; |
| 131 | $req = ''; |
| 132 | $length = 0; |
| 133 | $postdata_str = rplg_get_query_string($postdata); |
| 134 | $url_pieces = parse_url($url); |
| 135 | $host = $url_pieces['host']; |
| 136 | |
| 137 | if(!isset($url_pieces['port'])) { |
| 138 | switch($url_pieces['scheme']) { |
| 139 | case 'http': |
| 140 | $url_pieces['port'] = 80; |
| 141 | break; |
| 142 | case 'https': |
| 143 | $url_pieces['port'] = 443; |
| 144 | $host = 'ssl://' . $url_pieces['host']; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if(!isset($url_pieces['path'])) { $url_pieces['path'] = '/'; } |
| 150 | |
| 151 | if(($url_pieces['port'] == 80 && $url_pieces['scheme'] == 'http') || |
| 152 | ($url_pieces['port'] == 443 && $url_pieces['scheme'] == 'https')) { |
| 153 | $req_host = $url_pieces['host']; |
| 154 | } else { |
| 155 | $req_host = $url_pieces['host'] . ':' . $url_pieces['port']; |
| 156 | } |
| 157 | |
| 158 | $fp = @fsockopen($host, $url_pieces['port'], $errno, $errstr, RPLG_SOCKET_TIMEOUT); |
| 159 | if(!$fp) { return false; } |
| 160 | |
| 161 | $path = $url_pieces['path']; |
| 162 | if (isset($url_pieces['query'])) $path .= '?'.$url_pieces['query']; |
| 163 | |
| 164 | $req .= ($postdata_str ? 'POST' : 'GET') . ' ' . $path . " HTTP/1.1\r\n"; |
| 165 | $req .= 'Host: ' . $req_host . "\r\n"; |
| 166 | $req .= rplg_get_http_headers_for_request($postdata_str, $headers); |
| 167 | if($postdata_str) { |
| 168 | $req .= "\r\n\r\n" . $postdata_str; |
| 169 | } |
| 170 | $req .= "\r\n\r\n"; |
| 171 | |
| 172 | fwrite($fp, $req); |
| 173 | while(!feof($fp)) { |
| 174 | $buf .= fgets($fp, 4096); |
| 175 | } |
| 176 | |
| 177 | list($headers, $response['data']) = explode("\r\n\r\n", $buf, 2); |
| 178 | |
| 179 | $headers = _rplg_get_response_headers($headers, $response); |
| 180 | |
| 181 | if(isset($headers['transfer-encoding']) && 'chunked' == strtolower($headers['transfer-encoding'])) { |
| 182 | $chunk_data = $response['data']; |
| 183 | $joined_data = ''; |
| 184 | while(true) { |
| 185 | list($chunk_length, $chunk_data) = explode("\r\n", $chunk_data, 2); |
| 186 | $chunk_length = hexdec($chunk_length); |
| 187 | if(!$chunk_length || !strlen($chunk_data)) { break; } |
| 188 | |
| 189 | $joined_data .= substr($chunk_data, 0, $chunk_length); |
| 190 | $chunk_data = substr($chunk_data, $chunk_length + 1); |
| 191 | $length += $chunk_length; |
| 192 | } |
| 193 | $response['data'] = $joined_data; |
| 194 | } else { |
| 195 | $length = $headers['content-length']; |
| 196 | } |
| 197 | $response['headers'] = $headers; |
| 198 | } |
| 199 | |
| 200 | /*-------------------------------- helpers --------------------------------*/ |
| 201 | function rplg_get_query_string($params) { |
| 202 | $query = ''; |
| 203 | |
| 204 | if($params) { |
| 205 | foreach($params as $key=>$value) { |
| 206 | $query .= urlencode($key) . '=' . urlencode($value) . '&'; |
| 207 | } |
| 208 | } |
| 209 | return $query; |
| 210 | } |
| 211 | |
| 212 | function _rplg_get_response_headers($headers, &$response) { |
| 213 | $headers = explode("\r\n", $headers); |
| 214 | |
| 215 | $header_array = explode(' ', $headers[0], 3); |
| 216 | |
| 217 | $unused = $header_array[0]; |
| 218 | |
| 219 | $response['code'] = $header_array[1]; |
| 220 | |
| 221 | $unused = $header_array[2]; |
| 222 | |
| 223 | $headers = array_slice($headers, 1); |
| 224 | foreach($headers as $unused=>$header) { |
| 225 | $header = explode(':', $header); |
| 226 | $header[0] = trim($header[0]); |
| 227 | $header[1] = trim($header[1]); |
| 228 | $headers[strtolower($header[0])] = $header[1]; |
| 229 | } |
| 230 | return $headers; |
| 231 | } |
| 232 | |
| 233 | function rplg_get_http_headers_for_request($content, $headers) { |
| 234 | $req_headers = array(); |
| 235 | $req_headers[] = 'User-Agent: ' . RPLG_USER_AGENT; |
| 236 | $req_headers[] = 'Connection: close'; |
| 237 | if($content) { |
| 238 | $req_headers[] = 'Content-Length: ' . strlen($content); |
| 239 | $req_headers[] = 'Content-Type: application/x-www-form-urlencoded'; |
| 240 | } |
| 241 | return implode("\r\n", array_merge($req_headers, $headers)); |
| 242 | } |
| 243 | |
| 244 | function rplg_url_method() { |
| 245 | if(function_exists('curl_init')) { |
| 246 | return 'curl'; |
| 247 | } else if(ini_get('allow_url_fopen') && function_exists('stream_get_contents')) { |
| 248 | return 'fopen'; |
| 249 | } else { |
| 250 | return 'fsockopen'; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | ?> |
| 256 |