Edit File by line
/home/zeestwma/richards.../wp-inclu.../Requests/src/Transpor...
File: Fsockopen.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* fsockopen HTTP transport
[2] Fix | Delete
*
[3] Fix | Delete
* @package Requests\Transport
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
namespace WpOrg\Requests\Transport;
[7] Fix | Delete
[8] Fix | Delete
use WpOrg\Requests\Capability;
[9] Fix | Delete
use WpOrg\Requests\Exception;
[10] Fix | Delete
use WpOrg\Requests\Exception\InvalidArgument;
[11] Fix | Delete
use WpOrg\Requests\Port;
[12] Fix | Delete
use WpOrg\Requests\Requests;
[13] Fix | Delete
use WpOrg\Requests\Ssl;
[14] Fix | Delete
use WpOrg\Requests\Transport;
[15] Fix | Delete
use WpOrg\Requests\Utility\CaseInsensitiveDictionary;
[16] Fix | Delete
use WpOrg\Requests\Utility\InputValidator;
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* fsockopen HTTP transport
[20] Fix | Delete
*
[21] Fix | Delete
* @package Requests\Transport
[22] Fix | Delete
*/
[23] Fix | Delete
final class Fsockopen implements Transport {
[24] Fix | Delete
/**
[25] Fix | Delete
* Second to microsecond conversion
[26] Fix | Delete
*
[27] Fix | Delete
* @var integer
[28] Fix | Delete
*/
[29] Fix | Delete
const SECOND_IN_MICROSECONDS = 1000000;
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Raw HTTP data
[33] Fix | Delete
*
[34] Fix | Delete
* @var string
[35] Fix | Delete
*/
[36] Fix | Delete
public $headers = '';
[37] Fix | Delete
[38] Fix | Delete
/**
[39] Fix | Delete
* Stream metadata
[40] Fix | Delete
*
[41] Fix | Delete
* @var array Associative array of properties, see {@link https://www.php.net/stream_get_meta_data}
[42] Fix | Delete
*/
[43] Fix | Delete
public $info;
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* What's the maximum number of bytes we should keep?
[47] Fix | Delete
*
[48] Fix | Delete
* @var int|bool Byte count, or false if no limit.
[49] Fix | Delete
*/
[50] Fix | Delete
private $max_bytes = false;
[51] Fix | Delete
[52] Fix | Delete
/**
[53] Fix | Delete
* Cache for received connection errors.
[54] Fix | Delete
*
[55] Fix | Delete
* @var string
[56] Fix | Delete
*/
[57] Fix | Delete
private $connect_error = '';
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* Perform a request
[61] Fix | Delete
*
[62] Fix | Delete
* @param string|Stringable $url URL to request
[63] Fix | Delete
* @param array $headers Associative array of request headers
[64] Fix | Delete
* @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
[65] Fix | Delete
* @param array $options Request options, see {@see \WpOrg\Requests\Requests::response()} for documentation
[66] Fix | Delete
* @return string Raw HTTP result
[67] Fix | Delete
*
[68] Fix | Delete
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $url argument is not a string or Stringable.
[69] Fix | Delete
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $headers argument is not an array.
[70] Fix | Delete
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $data parameter is not an array or string.
[71] Fix | Delete
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array.
[72] Fix | Delete
* @throws \WpOrg\Requests\Exception On failure to connect to socket (`fsockopenerror`)
[73] Fix | Delete
* @throws \WpOrg\Requests\Exception On socket timeout (`timeout`)
[74] Fix | Delete
*/
[75] Fix | Delete
public function request($url, $headers = [], $data = [], $options = []) {
[76] Fix | Delete
if (InputValidator::is_string_or_stringable($url) === false) {
[77] Fix | Delete
throw InvalidArgument::create(1, '$url', 'string|Stringable', gettype($url));
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
if (is_array($headers) === false) {
[81] Fix | Delete
throw InvalidArgument::create(2, '$headers', 'array', gettype($headers));
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
if (!is_array($data) && !is_string($data)) {
[85] Fix | Delete
if ($data === null) {
[86] Fix | Delete
$data = '';
[87] Fix | Delete
} else {
[88] Fix | Delete
throw InvalidArgument::create(3, '$data', 'array|string', gettype($data));
[89] Fix | Delete
}
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
if (is_array($options) === false) {
[93] Fix | Delete
throw InvalidArgument::create(4, '$options', 'array', gettype($options));
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
$options['hooks']->dispatch('fsockopen.before_request');
[97] Fix | Delete
[98] Fix | Delete
$url_parts = parse_url($url);
[99] Fix | Delete
if (empty($url_parts)) {
[100] Fix | Delete
throw new Exception('Invalid URL.', 'invalidurl', $url);
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
$host = $url_parts['host'];
[104] Fix | Delete
$context = stream_context_create();
[105] Fix | Delete
$verifyname = false;
[106] Fix | Delete
$case_insensitive_headers = new CaseInsensitiveDictionary($headers);
[107] Fix | Delete
[108] Fix | Delete
// HTTPS support
[109] Fix | Delete
if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https') {
[110] Fix | Delete
$remote_socket = 'ssl://' . $host;
[111] Fix | Delete
if (!isset($url_parts['port'])) {
[112] Fix | Delete
$url_parts['port'] = Port::HTTPS;
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
$context_options = [
[116] Fix | Delete
'verify_peer' => true,
[117] Fix | Delete
'capture_peer_cert' => true,
[118] Fix | Delete
];
[119] Fix | Delete
$verifyname = true;
[120] Fix | Delete
[121] Fix | Delete
// SNI, if enabled (OpenSSL >=0.9.8j)
[122] Fix | Delete
// phpcs:ignore PHPCompatibility.Constants.NewConstants.openssl_tlsext_server_nameFound
[123] Fix | Delete
if (defined('OPENSSL_TLSEXT_SERVER_NAME') && OPENSSL_TLSEXT_SERVER_NAME) {
[124] Fix | Delete
$context_options['SNI_enabled'] = true;
[125] Fix | Delete
if (isset($options['verifyname']) && $options['verifyname'] === false) {
[126] Fix | Delete
$context_options['SNI_enabled'] = false;
[127] Fix | Delete
}
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
if (isset($options['verify'])) {
[131] Fix | Delete
if ($options['verify'] === false) {
[132] Fix | Delete
$context_options['verify_peer'] = false;
[133] Fix | Delete
$context_options['verify_peer_name'] = false;
[134] Fix | Delete
$verifyname = false;
[135] Fix | Delete
} elseif (is_string($options['verify'])) {
[136] Fix | Delete
$context_options['cafile'] = $options['verify'];
[137] Fix | Delete
}
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
if (isset($options['verifyname']) && $options['verifyname'] === false) {
[141] Fix | Delete
$context_options['verify_peer_name'] = false;
[142] Fix | Delete
$verifyname = false;
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
// Handle the PHP 8.4 deprecation (PHP 9.0 removal) of the function signature we use for stream_context_set_option().
[146] Fix | Delete
// Ref: https://wiki.php.net/rfc/deprecate_functions_with_overloaded_signatures#stream_context_set_option
[147] Fix | Delete
if (function_exists('stream_context_set_options')) {
[148] Fix | Delete
// PHP 8.3+.
[149] Fix | Delete
stream_context_set_options($context, ['ssl' => $context_options]);
[150] Fix | Delete
} else {
[151] Fix | Delete
// PHP < 8.3.
[152] Fix | Delete
stream_context_set_option($context, ['ssl' => $context_options]);
[153] Fix | Delete
}
[154] Fix | Delete
} else {
[155] Fix | Delete
$remote_socket = 'tcp://' . $host;
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
$this->max_bytes = $options['max_bytes'];
[159] Fix | Delete
[160] Fix | Delete
if (!isset($url_parts['port'])) {
[161] Fix | Delete
$url_parts['port'] = Port::HTTP;
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
$remote_socket .= ':' . $url_parts['port'];
[165] Fix | Delete
[166] Fix | Delete
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
[167] Fix | Delete
set_error_handler([$this, 'connect_error_handler'], E_WARNING | E_NOTICE);
[168] Fix | Delete
[169] Fix | Delete
$options['hooks']->dispatch('fsockopen.remote_socket', [&$remote_socket]);
[170] Fix | Delete
[171] Fix | Delete
$socket = stream_socket_client($remote_socket, $errno, $errstr, ceil($options['connect_timeout']), STREAM_CLIENT_CONNECT, $context);
[172] Fix | Delete
[173] Fix | Delete
restore_error_handler();
[174] Fix | Delete
[175] Fix | Delete
if ($verifyname && !$this->verify_certificate_from_context($host, $context)) {
[176] Fix | Delete
throw new Exception('SSL certificate did not match the requested domain name', 'ssl.no_match');
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
if (!$socket) {
[180] Fix | Delete
if ($errno === 0) {
[181] Fix | Delete
// Connection issue
[182] Fix | Delete
throw new Exception(rtrim($this->connect_error), 'fsockopen.connect_error');
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
throw new Exception($errstr, 'fsockopenerror', null, $errno);
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
$data_format = $options['data_format'];
[189] Fix | Delete
[190] Fix | Delete
if ($data_format === 'query') {
[191] Fix | Delete
$path = self::format_get($url_parts, $data);
[192] Fix | Delete
$data = '';
[193] Fix | Delete
} else {
[194] Fix | Delete
$path = self::format_get($url_parts, []);
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
$options['hooks']->dispatch('fsockopen.remote_host_path', [&$path, $url]);
[198] Fix | Delete
[199] Fix | Delete
$request_body = '';
[200] Fix | Delete
$out = sprintf("%s %s HTTP/%.1F\r\n", $options['type'], $path, $options['protocol_version']);
[201] Fix | Delete
[202] Fix | Delete
if ($options['type'] !== Requests::TRACE) {
[203] Fix | Delete
if (is_array($data)) {
[204] Fix | Delete
$request_body = http_build_query($data, '', '&');
[205] Fix | Delete
} else {
[206] Fix | Delete
$request_body = $data;
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
// Always include Content-length on POST requests to prevent
[210] Fix | Delete
// 411 errors from some servers when the body is empty.
[211] Fix | Delete
if (!empty($data) || $options['type'] === Requests::POST) {
[212] Fix | Delete
if (!isset($case_insensitive_headers['Content-Length'])) {
[213] Fix | Delete
$headers['Content-Length'] = strlen($request_body);
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
if (!isset($case_insensitive_headers['Content-Type'])) {
[217] Fix | Delete
$headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
[218] Fix | Delete
}
[219] Fix | Delete
}
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
if (!isset($case_insensitive_headers['Host'])) {
[223] Fix | Delete
$out .= sprintf('Host: %s', $url_parts['host']);
[224] Fix | Delete
$scheme_lower = strtolower($url_parts['scheme']);
[225] Fix | Delete
[226] Fix | Delete
if (($scheme_lower === 'http' && $url_parts['port'] !== Port::HTTP) || ($scheme_lower === 'https' && $url_parts['port'] !== Port::HTTPS)) {
[227] Fix | Delete
$out .= ':' . $url_parts['port'];
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
$out .= "\r\n";
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
if (!isset($case_insensitive_headers['User-Agent'])) {
[234] Fix | Delete
$out .= sprintf("User-Agent: %s\r\n", $options['useragent']);
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
$accept_encoding = $this->accept_encoding();
[238] Fix | Delete
if (!isset($case_insensitive_headers['Accept-Encoding']) && !empty($accept_encoding)) {
[239] Fix | Delete
$out .= sprintf("Accept-Encoding: %s\r\n", $accept_encoding);
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
$headers = Requests::flatten($headers);
[243] Fix | Delete
[244] Fix | Delete
if (!empty($headers)) {
[245] Fix | Delete
$out .= implode("\r\n", $headers) . "\r\n";
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
$options['hooks']->dispatch('fsockopen.after_headers', [&$out]);
[249] Fix | Delete
[250] Fix | Delete
if (substr($out, -2) !== "\r\n") {
[251] Fix | Delete
$out .= "\r\n";
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
if (!isset($case_insensitive_headers['Connection'])) {
[255] Fix | Delete
$out .= "Connection: Close\r\n";
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
$out .= "\r\n" . $request_body;
[259] Fix | Delete
[260] Fix | Delete
$options['hooks']->dispatch('fsockopen.before_send', [&$out]);
[261] Fix | Delete
[262] Fix | Delete
fwrite($socket, $out);
[263] Fix | Delete
$options['hooks']->dispatch('fsockopen.after_send', [$out]);
[264] Fix | Delete
[265] Fix | Delete
if (!$options['blocking']) {
[266] Fix | Delete
fclose($socket);
[267] Fix | Delete
$fake_headers = '';
[268] Fix | Delete
$options['hooks']->dispatch('fsockopen.after_request', [&$fake_headers]);
[269] Fix | Delete
return '';
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
$timeout_sec = (int) floor($options['timeout']);
[273] Fix | Delete
if ($timeout_sec === $options['timeout']) {
[274] Fix | Delete
$timeout_msec = 0;
[275] Fix | Delete
} else {
[276] Fix | Delete
$timeout_msec = self::SECOND_IN_MICROSECONDS * $options['timeout'] % self::SECOND_IN_MICROSECONDS;
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
stream_set_timeout($socket, $timeout_sec, $timeout_msec);
[280] Fix | Delete
[281] Fix | Delete
$response = '';
[282] Fix | Delete
$body = '';
[283] Fix | Delete
$headers = '';
[284] Fix | Delete
$this->info = stream_get_meta_data($socket);
[285] Fix | Delete
$size = 0;
[286] Fix | Delete
$doingbody = false;
[287] Fix | Delete
$download = false;
[288] Fix | Delete
if ($options['filename']) {
[289] Fix | Delete
// phpcs:ignore WordPress.PHP.NoSilencedErrors -- Silenced the PHP native warning in favour of throwing an exception.
[290] Fix | Delete
$download = @fopen($options['filename'], 'wb');
[291] Fix | Delete
if ($download === false) {
[292] Fix | Delete
$error = error_get_last();
[293] Fix | Delete
throw new Exception($error['message'], 'fopen');
[294] Fix | Delete
}
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
while (!feof($socket)) {
[298] Fix | Delete
$this->info = stream_get_meta_data($socket);
[299] Fix | Delete
if ($this->info['timed_out']) {
[300] Fix | Delete
throw new Exception('fsocket timed out', 'timeout');
[301] Fix | Delete
}
[302] Fix | Delete
[303] Fix | Delete
$block = fread($socket, Requests::BUFFER_SIZE);
[304] Fix | Delete
if (!$doingbody) {
[305] Fix | Delete
$response .= $block;
[306] Fix | Delete
if (strpos($response, "\r\n\r\n")) {
[307] Fix | Delete
list($headers, $block) = explode("\r\n\r\n", $response, 2);
[308] Fix | Delete
$doingbody = true;
[309] Fix | Delete
}
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
// Are we in body mode now?
[313] Fix | Delete
if ($doingbody) {
[314] Fix | Delete
$options['hooks']->dispatch('request.progress', [$block, $size, $this->max_bytes]);
[315] Fix | Delete
$data_length = strlen($block);
[316] Fix | Delete
if ($this->max_bytes) {
[317] Fix | Delete
// Have we already hit a limit?
[318] Fix | Delete
if ($size === $this->max_bytes) {
[319] Fix | Delete
continue;
[320] Fix | Delete
}
[321] Fix | Delete
[322] Fix | Delete
if (($size + $data_length) > $this->max_bytes) {
[323] Fix | Delete
// Limit the length
[324] Fix | Delete
$limited_length = ($this->max_bytes - $size);
[325] Fix | Delete
$block = substr($block, 0, $limited_length);
[326] Fix | Delete
}
[327] Fix | Delete
}
[328] Fix | Delete
[329] Fix | Delete
$size += strlen($block);
[330] Fix | Delete
if ($download) {
[331] Fix | Delete
fwrite($download, $block);
[332] Fix | Delete
} else {
[333] Fix | Delete
$body .= $block;
[334] Fix | Delete
}
[335] Fix | Delete
}
[336] Fix | Delete
}
[337] Fix | Delete
[338] Fix | Delete
$this->headers = $headers;
[339] Fix | Delete
[340] Fix | Delete
if ($download) {
[341] Fix | Delete
fclose($download);
[342] Fix | Delete
} else {
[343] Fix | Delete
$this->headers .= "\r\n\r\n" . $body;
[344] Fix | Delete
}
[345] Fix | Delete
[346] Fix | Delete
fclose($socket);
[347] Fix | Delete
[348] Fix | Delete
$options['hooks']->dispatch('fsockopen.after_request', [&$this->headers, &$this->info]);
[349] Fix | Delete
return $this->headers;
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
/**
[353] Fix | Delete
* Send multiple requests simultaneously
[354] Fix | Delete
*
[355] Fix | Delete
* @param array $requests Request data (array of 'url', 'headers', 'data', 'options') as per {@see \WpOrg\Requests\Transport::request()}
[356] Fix | Delete
* @param array $options Global options, see {@see \WpOrg\Requests\Requests::response()} for documentation
[357] Fix | Delete
* @return array Array of \WpOrg\Requests\Response objects (may contain \WpOrg\Requests\Exception or string responses as well)
[358] Fix | Delete
*
[359] Fix | Delete
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $requests argument is not an array or iterable object with array access.
[360] Fix | Delete
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $options argument is not an array.
[361] Fix | Delete
*/
[362] Fix | Delete
public function request_multiple($requests, $options) {
[363] Fix | Delete
// If you're not requesting, we can't get any responses ¯\_(ツ)_/¯
[364] Fix | Delete
if (empty($requests)) {
[365] Fix | Delete
return [];
[366] Fix | Delete
}
[367] Fix | Delete
[368] Fix | Delete
if (InputValidator::has_array_access($requests) === false || InputValidator::is_iterable($requests) === false) {
[369] Fix | Delete
throw InvalidArgument::create(1, '$requests', 'array|ArrayAccess&Traversable', gettype($requests));
[370] Fix | Delete
}
[371] Fix | Delete
[372] Fix | Delete
if (is_array($options) === false) {
[373] Fix | Delete
throw InvalidArgument::create(2, '$options', 'array', gettype($options));
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
$responses = [];
[377] Fix | Delete
$class = get_class($this);
[378] Fix | Delete
foreach ($requests as $id => $request) {
[379] Fix | Delete
try {
[380] Fix | Delete
$handler = new $class();
[381] Fix | Delete
$responses[$id] = $handler->request($request['url'], $request['headers'], $request['data'], $request['options']);
[382] Fix | Delete
[383] Fix | Delete
$request['options']['hooks']->dispatch('transport.internal.parse_response', [&$responses[$id], $request]);
[384] Fix | Delete
} catch (Exception $e) {
[385] Fix | Delete
$responses[$id] = $e;
[386] Fix | Delete
}
[387] Fix | Delete
[388] Fix | Delete
if (!is_string($responses[$id])) {
[389] Fix | Delete
$request['options']['hooks']->dispatch('multiple.request.complete', [&$responses[$id], $id]);
[390] Fix | Delete
}
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
return $responses;
[394] Fix | Delete
}
[395] Fix | Delete
[396] Fix | Delete
/**
[397] Fix | Delete
* Retrieve the encodings we can accept
[398] Fix | Delete
*
[399] Fix | Delete
* @return string Accept-Encoding header value
[400] Fix | Delete
*/
[401] Fix | Delete
private static function accept_encoding() {
[402] Fix | Delete
$type = [];
[403] Fix | Delete
if (function_exists('gzinflate')) {
[404] Fix | Delete
$type[] = 'deflate;q=1.0';
[405] Fix | Delete
}
[406] Fix | Delete
[407] Fix | Delete
if (function_exists('gzuncompress')) {
[408] Fix | Delete
$type[] = 'compress;q=0.5';
[409] Fix | Delete
}
[410] Fix | Delete
[411] Fix | Delete
$type[] = 'gzip;q=0.5';
[412] Fix | Delete
[413] Fix | Delete
return implode(', ', $type);
[414] Fix | Delete
}
[415] Fix | Delete
[416] Fix | Delete
/**
[417] Fix | Delete
* Format a URL given GET data
[418] Fix | Delete
*
[419] Fix | Delete
* @param array $url_parts Array of URL parts as received from {@link https://www.php.net/parse_url}
[420] Fix | Delete
* @param array|object $data Data to build query using, see {@link https://www.php.net/http_build_query}
[421] Fix | Delete
* @return string URL with data
[422] Fix | Delete
*/
[423] Fix | Delete
private static function format_get($url_parts, $data) {
[424] Fix | Delete
if (!empty($data)) {
[425] Fix | Delete
if (empty($url_parts['query'])) {
[426] Fix | Delete
$url_parts['query'] = '';
[427] Fix | Delete
}
[428] Fix | Delete
[429] Fix | Delete
$url_parts['query'] .= '&' . http_build_query($data, '', '&');
[430] Fix | Delete
$url_parts['query'] = trim($url_parts['query'], '&');
[431] Fix | Delete
}
[432] Fix | Delete
[433] Fix | Delete
if (isset($url_parts['path'])) {
[434] Fix | Delete
if (isset($url_parts['query'])) {
[435] Fix | Delete
$get = $url_parts['path'] . '?' . $url_parts['query'];
[436] Fix | Delete
} else {
[437] Fix | Delete
$get = $url_parts['path'];
[438] Fix | Delete
}
[439] Fix | Delete
} else {
[440] Fix | Delete
$get = '/';
[441] Fix | Delete
}
[442] Fix | Delete
[443] Fix | Delete
return $get;
[444] Fix | Delete
}
[445] Fix | Delete
[446] Fix | Delete
/**
[447] Fix | Delete
* Error handler for stream_socket_client()
[448] Fix | Delete
*
[449] Fix | Delete
* @param int $errno Error number (e.g. E_WARNING)
[450] Fix | Delete
* @param string $errstr Error message
[451] Fix | Delete
*/
[452] Fix | Delete
public function connect_error_handler($errno, $errstr) {
[453] Fix | Delete
// Double-check we can handle it
[454] Fix | Delete
if (($errno & E_WARNING) === 0 && ($errno & E_NOTICE) === 0) {
[455] Fix | Delete
// Return false to indicate the default error handler should engage
[456] Fix | Delete
return false;
[457] Fix | Delete
}
[458] Fix | Delete
[459] Fix | Delete
$this->connect_error .= $errstr . "\n";
[460] Fix | Delete
return true;
[461] Fix | Delete
}
[462] Fix | Delete
[463] Fix | Delete
/**
[464] Fix | Delete
* Verify the certificate against common name and subject alternative names
[465] Fix | Delete
*
[466] Fix | Delete
* Unfortunately, PHP doesn't check the certificate against the alternative
[467] Fix | Delete
* names, leading things like 'https://www.github.com/' to be invalid.
[468] Fix | Delete
* Instead
[469] Fix | Delete
*
[470] Fix | Delete
* @link https://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.1
[471] Fix | Delete
*
[472] Fix | Delete
* @param string $host Host name to verify against
[473] Fix | Delete
* @param resource $context Stream context
[474] Fix | Delete
* @return bool
[475] Fix | Delete
*
[476] Fix | Delete
* @throws \WpOrg\Requests\Exception On failure to connect via TLS (`fsockopen.ssl.connect_error`)
[477] Fix | Delete
* @throws \WpOrg\Requests\Exception On not obtaining a match for the host (`fsockopen.ssl.no_match`)
[478] Fix | Delete
*/
[479] Fix | Delete
public function verify_certificate_from_context($host, $context) {
[480] Fix | Delete
$meta = stream_context_get_options($context);
[481] Fix | Delete
[482] Fix | Delete
// If we don't have SSL options, then we couldn't make the connection at
[483] Fix | Delete
// all
[484] Fix | Delete
if (empty($meta) || empty($meta['ssl']) || empty($meta['ssl']['peer_certificate'])) {
[485] Fix | Delete
throw new Exception(rtrim($this->connect_error), 'ssl.connect_error');
[486] Fix | Delete
}
[487] Fix | Delete
[488] Fix | Delete
$cert = openssl_x509_parse($meta['ssl']['peer_certificate']);
[489] Fix | Delete
[490] Fix | Delete
return Ssl::verify_certificate($host, $cert);
[491] Fix | Delete
}
[492] Fix | Delete
[493] Fix | Delete
/**
[494] Fix | Delete
* Self-test whether the transport can be used.
[495] Fix | Delete
*
[496] Fix | Delete
* The available capabilities to test for can be found in {@see \WpOrg\Requests\Capability}.
[497] Fix | Delete
*
[498] Fix | Delete
* @codeCoverageIgnore
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function