-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathUtil_Http.php
248 lines (213 loc) · 5.87 KB
/
Util_Http.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* File: Util_Http.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class Util_Http
*
* phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
* phpcs:disable WordPress.WP.AlternativeFunctions
*/
class Util_Http {
/**
* Filter handler for use_curl_transport.
* Workaround to not use curl for extra http methods
*
* @param bool $result Result.
* @param array $args Arguments.
*
* @return boolean
*/
public static function use_curl_transport( $result, $args ) {
if ( isset( $args['method'] ) && 'GET' !== $args['method'] && 'POST' !== $args['method'] ) {
return false;
}
return $result;
}
/**
* Sends HTTP request
*
* @param string $url URL.
* @param array $args Arguments.
*
* @return WP_Error|array
*/
public static function request( $url, $args = array() ) {
static $filter_set = false;
if ( ! $filter_set ) {
add_filter( 'use_curl_transport', array( '\W3TC\Util_Http', 'use_curl_transport' ), 10, 2 );
$filter_set = true;
}
$args = array_merge(
array(
'user-agent' => W3TC_POWERED_BY,
),
$args
);
return wp_remote_request( $url, $args );
}
/**
* Sends HTTP GET request
*
* @param string $url URL.
* @param array $args Arguments.
*
* @return array|WP_Error
*/
public static function get( $url, $args = array() ) {
$args = array_merge(
$args,
array(
'method' => 'GET',
)
);
return self::request( $url, $args );
}
/**
* Downloads URL into a file
*
* @param string $url URL.
* @param string $file File.
* @param array $args Arguments.
*
* @return boolean
*/
public static function download( $url, $file, $args = array() ) {
if ( strpos( $url, '//' ) === 0 ) {
$url = ( Util_Environment::is_https() ? 'https:' : 'http:' ) . $url;
}
$response = self::get( $url, $args );
if ( ! is_wp_error( $response ) && 200 === $response['response']['code'] ) {
return @file_put_contents( $file, $response['body'] );
}
return false;
}
/**
* Returns upload info
*
* @return array
*/
public static function upload_info() {
static $upload_info = null;
if ( null === $upload_info ) {
$upload_info = Util_Environment::wp_upload_dir();
if ( empty( $upload_info['error'] ) ) {
$parse_url = @parse_url( $upload_info['baseurl'] );
if ( $parse_url ) {
$baseurlpath = ( ! empty( $parse_url['path'] ) ? trim( $parse_url['path'], '/' ) : '' );
} else {
$baseurlpath = 'wp-content/uploads';
}
$upload_info['baseurlpath'] = '/' . $baseurlpath . '/';
} else {
$upload_info = false;
}
}
return $upload_info;
}
/**
* Test the time to first byte.
*
* @param string $url URL address.
* @param bool $nocache Whether or not to request no cache response, by sending a Cache-Control header.
*
* @return float|false Time in seconds until the first byte is about to be transferred or false on error.
*/
public static function ttfb( $url, $nocache = false ) {
$ch = curl_init( esc_url( $url ) );
$pass = (bool) $ch;
$ttfb = false;
$opts = array(
CURLOPT_FORBID_REUSE => 1,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ),
);
if ( $nocache ) {
$opts[ CURLOPT_HTTPHEADER ] = array(
'Cache-Control: no-cache',
'Pragma: no-cache',
);
$qs_arr = explode( '&', wp_parse_url( $url, PHP_URL_QUERY ) );
array_push( $qs_arr, 'time=' . microtime( true ) );
$opts[ CURLOPT_URL ] = $url . '?' . implode( '&', $qs_arr );
}
if ( $ch ) {
$pass = curl_setopt_array( $ch, $opts ); // phpcs:ignore WordPress.WP.AlternativeFunctions
}
if ( $pass ) {
$pass = (bool) curl_exec( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions
}
if ( $pass ) {
$ttfb = curl_getinfo( $ch, CURLINFO_STARTTRANSFER_TIME ); // phpcs:ignore WordPress.WP.AlternativeFunctions
}
if ( $ch ) {
curl_close( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions
}
return $ttfb;
}
/**
* Retrieve HTTP headers.
*
* @param string $url URL address.
*
* @return array
*/
public static function get_headers( $url ) {
$ch = curl_init( $url ); // phpcs:ignore WordPress.WP.AlternativeFunctions
$pass = (bool) $ch;
$headers = array();
$opts = array(
CURLOPT_FORBID_REUSE => 1,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ),
CURLOPT_HTTPHEADER => array(
'Cache-Control: no-cache',
'Pragma: no-cache',
),
);
if ( $pass ) {
$pass = curl_setopt_array( $ch, $opts ); // phpcs:ignore WordPress.WP.AlternativeFunctions
}
if ( $pass ) {
$response = curl_exec( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions
}
if ( $response ) {
$header_size = curl_getinfo( $ch, CURLINFO_HEADER_SIZE ); // phpcs:ignore WordPress.WP.AlternativeFunctions
$header = substr( $response, 0, $header_size );
foreach ( explode( "\r\n", $header ) as $index => $line ) {
if ( 0 === $index ) {
$headers['http_code'] = $line;
$http_code_arr = explode( ' ', $line );
$headers['protocol'] = $http_code_arr[0];
$headers['status'] = $http_code_arr[1];
} elseif ( ! empty( $line ) && false !== strpos( $line, ':' ) ) {
list ( $key, $value ) = explode( ': ', $line );
$headers[ $key ] = $value;
}
}
}
if ( $ch ) {
curl_close( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions
}
return $headers;
}
/**
* Generate unique md5 value based on domain.
*
* @return string
*/
public static function generate_site_id() {
return md5( network_home_url() );
}
}