form-presets
2 years ago
pa-pins-handler.php
2 years ago
pa-tiktok-handler.php
2 years ago
pa-weather-handler.php
2 years ago
urlopen.php
2 years ago
urlopen.php
266 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 | } elseif ( 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( |
| 92 | 'http' => array( |
| 93 | 'method' => 'POST', |
| 94 | 'header' => implode( "\r\n", array_merge( array( 'Content-Type: application/x-www-form-urlencoded' ), $headers ) ), |
| 95 | 'content' => rplg_get_query_string( $postdata ), |
| 96 | 'timeout' => RPLG_SOCKET_TIMEOUT, |
| 97 | ), |
| 98 | ); |
| 99 | } else { |
| 100 | $params = array( |
| 101 | 'http' => array( |
| 102 | 'header' => implode( "\r\n", $headers ), |
| 103 | ), |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | ini_set( 'user_agent', RPLG_USER_AGENT ); |
| 108 | $ctx = stream_context_create( $params ); |
| 109 | $fp = fopen( $url, 'rb', false, $ctx ); |
| 110 | if ( ! $fp ) { |
| 111 | return false; } |
| 112 | |
| 113 | $response_header_array = explode( ' ', $http_response_header[0], 3 ); |
| 114 | |
| 115 | $unused = $response_header_array[0]; |
| 116 | |
| 117 | $response['code'] = $response_header_array[0]; |
| 118 | |
| 119 | $unused = $response_header_array[2]; |
| 120 | |
| 121 | $resp_headers = array_slice( $http_response_header, 1 ); |
| 122 | |
| 123 | foreach ( $resp_headers as $unused => $header ) { |
| 124 | $header = explode( ':', $header ); |
| 125 | $header[0] = trim( $header[0] ); |
| 126 | $header[1] = trim( $header[1] ); |
| 127 | $resp_headers[ strtolower( $header[0] ) ] = strtolower( $header[1] ); |
| 128 | } |
| 129 | $response['data'] = stream_get_contents( $fp ); |
| 130 | $response['headers'] = $resp_headers; |
| 131 | } |
| 132 | |
| 133 | /*-------------------------------- fsockpen --------------------------------*/ |
| 134 | function _rplg_fsockopen_urlopen( $url, $postdata, $headers, &$response ) { |
| 135 | $buf = ''; |
| 136 | $req = ''; |
| 137 | $length = 0; |
| 138 | $postdata_str = rplg_get_query_string( $postdata ); |
| 139 | $url_pieces = parse_url( $url ); |
| 140 | $host = $url_pieces['host']; |
| 141 | |
| 142 | if ( ! isset( $url_pieces['port'] ) ) { |
| 143 | switch ( $url_pieces['scheme'] ) { |
| 144 | case 'http': |
| 145 | $url_pieces['port'] = 80; |
| 146 | break; |
| 147 | case 'https': |
| 148 | $url_pieces['port'] = 443; |
| 149 | $host = 'ssl://' . $url_pieces['host']; |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if ( ! isset( $url_pieces['path'] ) ) { |
| 155 | $url_pieces['path'] = '/'; } |
| 156 | |
| 157 | if ( ( $url_pieces['port'] == 80 && $url_pieces['scheme'] == 'http' ) || |
| 158 | ( $url_pieces['port'] == 443 && $url_pieces['scheme'] == 'https' ) ) { |
| 159 | $req_host = $url_pieces['host']; |
| 160 | } else { |
| 161 | $req_host = $url_pieces['host'] . ':' . $url_pieces['port']; |
| 162 | } |
| 163 | |
| 164 | $fp = @fsockopen( $host, $url_pieces['port'], $errno, $errstr, RPLG_SOCKET_TIMEOUT ); |
| 165 | if ( ! $fp ) { |
| 166 | return false; } |
| 167 | |
| 168 | $path = $url_pieces['path']; |
| 169 | if ( isset( $url_pieces['query'] ) ) { |
| 170 | $path .= '?' . $url_pieces['query']; |
| 171 | } |
| 172 | |
| 173 | $req .= ( $postdata_str ? 'POST' : 'GET' ) . ' ' . $path . " HTTP/1.1\r\n"; |
| 174 | $req .= 'Host: ' . $req_host . "\r\n"; |
| 175 | $req .= rplg_get_http_headers_for_request( $postdata_str, $headers ); |
| 176 | if ( $postdata_str ) { |
| 177 | $req .= "\r\n\r\n" . $postdata_str; |
| 178 | } |
| 179 | $req .= "\r\n\r\n"; |
| 180 | |
| 181 | fwrite( $fp, $req ); |
| 182 | while ( ! feof( $fp ) ) { |
| 183 | $buf .= fgets( $fp, 4096 ); |
| 184 | } |
| 185 | |
| 186 | list($headers, $response['data']) = explode( "\r\n\r\n", $buf, 2 ); |
| 187 | |
| 188 | $headers = _rplg_get_response_headers( $headers, $response ); |
| 189 | |
| 190 | if ( isset( $headers['transfer-encoding'] ) && 'chunked' == strtolower( $headers['transfer-encoding'] ) ) { |
| 191 | $chunk_data = $response['data']; |
| 192 | $joined_data = ''; |
| 193 | while ( true ) { |
| 194 | list($chunk_length, $chunk_data) = explode( "\r\n", $chunk_data, 2 ); |
| 195 | $chunk_length = hexdec( $chunk_length ); |
| 196 | if ( ! $chunk_length || ! strlen( $chunk_data ) ) { |
| 197 | break; } |
| 198 | |
| 199 | $joined_data .= substr( $chunk_data, 0, $chunk_length ); |
| 200 | $chunk_data = substr( $chunk_data, $chunk_length + 1 ); |
| 201 | $length += $chunk_length; |
| 202 | } |
| 203 | $response['data'] = $joined_data; |
| 204 | } else { |
| 205 | $length = $headers['content-length']; |
| 206 | } |
| 207 | $response['headers'] = $headers; |
| 208 | } |
| 209 | |
| 210 | /*-------------------------------- helpers --------------------------------*/ |
| 211 | function rplg_get_query_string( $params ) { |
| 212 | $query = ''; |
| 213 | |
| 214 | if ( $params ) { |
| 215 | foreach ( $params as $key => $value ) { |
| 216 | $query .= urlencode( $key ) . '=' . urlencode( $value ) . '&'; |
| 217 | } |
| 218 | } |
| 219 | return $query; |
| 220 | } |
| 221 | |
| 222 | function _rplg_get_response_headers( $headers, &$response ) { |
| 223 | $headers = explode( "\r\n", $headers ); |
| 224 | |
| 225 | $header_array = explode( ' ', $headers[0], 3 ); |
| 226 | |
| 227 | $unused = $header_array[0]; |
| 228 | |
| 229 | $response['code'] = $header_array[1]; |
| 230 | |
| 231 | $unused = $header_array[2]; |
| 232 | |
| 233 | $headers = array_slice( $headers, 1 ); |
| 234 | foreach ( $headers as $unused => $header ) { |
| 235 | $header = explode( ':', $header ); |
| 236 | $header[0] = trim( $header[0] ); |
| 237 | $header[1] = trim( $header[1] ); |
| 238 | $headers[ strtolower( $header[0] ) ] = $header[1]; |
| 239 | } |
| 240 | return $headers; |
| 241 | } |
| 242 | |
| 243 | function rplg_get_http_headers_for_request( $content, $headers ) { |
| 244 | $req_headers = array(); |
| 245 | $req_headers[] = 'User-Agent: ' . RPLG_USER_AGENT; |
| 246 | $req_headers[] = 'Connection: close'; |
| 247 | if ( $content ) { |
| 248 | $req_headers[] = 'Content-Length: ' . strlen( $content ); |
| 249 | $req_headers[] = 'Content-Type: application/x-www-form-urlencoded'; |
| 250 | } |
| 251 | return implode( "\r\n", array_merge( $req_headers, $headers ) ); |
| 252 | } |
| 253 | |
| 254 | function rplg_url_method() { |
| 255 | if ( function_exists( 'curl_init' ) ) { |
| 256 | return 'curl'; |
| 257 | } elseif ( ini_get( 'allow_url_fopen' ) && function_exists( 'stream_get_contents' ) ) { |
| 258 | return 'fopen'; |
| 259 | } else { |
| 260 | return 'fsockopen'; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
| 266 |