Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../vendor/guzzle/guzzle/src/Guzzle/Stream
File: PhpStreamRequestFactory.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Guzzle\Stream;
[2] Fix | Delete
[3] Fix | Delete
use Guzzle\Common\Exception\InvalidArgumentException;
[4] Fix | Delete
use Guzzle\Common\Exception\RuntimeException;
[5] Fix | Delete
use Guzzle\Http\Message\EntityEnclosingRequestInterface;
[6] Fix | Delete
use Guzzle\Http\Message\RequestInterface;
[7] Fix | Delete
use Guzzle\Http\Url;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Factory used to create fopen streams using PHP's http and https stream wrappers
[11] Fix | Delete
*
[12] Fix | Delete
* Note: PHP's http stream wrapper only supports streaming downloads. It does not support streaming uploads.
[13] Fix | Delete
*/
[14] Fix | Delete
class PhpStreamRequestFactory implements StreamRequestFactoryInterface
[15] Fix | Delete
{
[16] Fix | Delete
/** @var resource Stream context options */
[17] Fix | Delete
protected $context;
[18] Fix | Delete
[19] Fix | Delete
/** @var array Stream context */
[20] Fix | Delete
protected $contextOptions;
[21] Fix | Delete
[22] Fix | Delete
/** @var Url Stream URL */
[23] Fix | Delete
protected $url;
[24] Fix | Delete
[25] Fix | Delete
/** @var array Last response headers received by the HTTP request */
[26] Fix | Delete
protected $lastResponseHeaders;
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* {@inheritdoc}
[30] Fix | Delete
*
[31] Fix | Delete
* The $params array can contain the following custom keys specific to the PhpStreamRequestFactory:
[32] Fix | Delete
* - stream_class: The name of a class to create instead of a Guzzle\Stream\Stream object
[33] Fix | Delete
*/
[34] Fix | Delete
public function fromRequest(RequestInterface $request, $context = array(), array $params = array())
[35] Fix | Delete
{
[36] Fix | Delete
if (is_resource($context)) {
[37] Fix | Delete
$this->contextOptions = stream_context_get_options($context);
[38] Fix | Delete
$this->context = $context;
[39] Fix | Delete
} elseif (is_array($context) || !$context) {
[40] Fix | Delete
$this->contextOptions = $context;
[41] Fix | Delete
$this->createContext($params);
[42] Fix | Delete
} elseif ($context) {
[43] Fix | Delete
throw new InvalidArgumentException('$context must be an array or resource');
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
// Dispatch the before send event
[47] Fix | Delete
$request->dispatch('request.before_send', array(
[48] Fix | Delete
'request' => $request,
[49] Fix | Delete
'context' => $this->context,
[50] Fix | Delete
'context_options' => $this->contextOptions
[51] Fix | Delete
));
[52] Fix | Delete
[53] Fix | Delete
$this->setUrl($request);
[54] Fix | Delete
$this->addDefaultContextOptions($request);
[55] Fix | Delete
$this->addSslOptions($request);
[56] Fix | Delete
$this->addBodyOptions($request);
[57] Fix | Delete
$this->addProxyOptions($request);
[58] Fix | Delete
[59] Fix | Delete
// Create the file handle but silence errors
[60] Fix | Delete
return $this->createStream($params)
[61] Fix | Delete
->setCustomData('request', $request)
[62] Fix | Delete
->setCustomData('response_headers', $this->getLastResponseHeaders());
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
/**
[66] Fix | Delete
* Set an option on the context and the internal options array
[67] Fix | Delete
*
[68] Fix | Delete
* @param string $wrapper Stream wrapper name of http
[69] Fix | Delete
* @param string $name Context name
[70] Fix | Delete
* @param mixed $value Context value
[71] Fix | Delete
* @param bool $overwrite Set to true to overwrite an existing value
[72] Fix | Delete
*/
[73] Fix | Delete
protected function setContextValue($wrapper, $name, $value, $overwrite = false)
[74] Fix | Delete
{
[75] Fix | Delete
if (!isset($this->contextOptions[$wrapper])) {
[76] Fix | Delete
$this->contextOptions[$wrapper] = array($name => $value);
[77] Fix | Delete
} elseif (!$overwrite && isset($this->contextOptions[$wrapper][$name])) {
[78] Fix | Delete
return;
[79] Fix | Delete
}
[80] Fix | Delete
$this->contextOptions[$wrapper][$name] = $value;
[81] Fix | Delete
stream_context_set_option($this->context, $wrapper, $name, $value);
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
/**
[85] Fix | Delete
* Create a stream context
[86] Fix | Delete
*
[87] Fix | Delete
* @param array $params Parameter array
[88] Fix | Delete
*/
[89] Fix | Delete
protected function createContext(array $params)
[90] Fix | Delete
{
[91] Fix | Delete
$options = $this->contextOptions;
[92] Fix | Delete
$this->context = $this->createResource(function () use ($params, $options) {
[93] Fix | Delete
return stream_context_create($options, $params);
[94] Fix | Delete
});
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* Get the last response headers received by the HTTP request
[99] Fix | Delete
*
[100] Fix | Delete
* @return array
[101] Fix | Delete
*/
[102] Fix | Delete
public function getLastResponseHeaders()
[103] Fix | Delete
{
[104] Fix | Delete
return $this->lastResponseHeaders;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* Adds the default context options to the stream context options
[109] Fix | Delete
*
[110] Fix | Delete
* @param RequestInterface $request Request
[111] Fix | Delete
*/
[112] Fix | Delete
protected function addDefaultContextOptions(RequestInterface $request)
[113] Fix | Delete
{
[114] Fix | Delete
$this->setContextValue('http', 'method', $request->getMethod());
[115] Fix | Delete
$headers = $request->getHeaderLines();
[116] Fix | Delete
[117] Fix | Delete
// "Connection: close" is required to get streams to work in HTTP 1.1
[118] Fix | Delete
if (!$request->hasHeader('Connection')) {
[119] Fix | Delete
$headers[] = 'Connection: close';
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
$this->setContextValue('http', 'header', $headers);
[123] Fix | Delete
$this->setContextValue('http', 'protocol_version', $request->getProtocolVersion());
[124] Fix | Delete
$this->setContextValue('http', 'ignore_errors', true);
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
/**
[128] Fix | Delete
* Set the URL to use with the factory
[129] Fix | Delete
*
[130] Fix | Delete
* @param RequestInterface $request Request that owns the URL
[131] Fix | Delete
*/
[132] Fix | Delete
protected function setUrl(RequestInterface $request)
[133] Fix | Delete
{
[134] Fix | Delete
$this->url = $request->getUrl(true);
[135] Fix | Delete
[136] Fix | Delete
// Check for basic Auth username
[137] Fix | Delete
if ($request->getUsername()) {
[138] Fix | Delete
$this->url->setUsername($request->getUsername());
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
// Check for basic Auth password
[142] Fix | Delete
if ($request->getPassword()) {
[143] Fix | Delete
$this->url->setPassword($request->getPassword());
[144] Fix | Delete
}
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
/**
[148] Fix | Delete
* Add SSL options to the stream context
[149] Fix | Delete
*
[150] Fix | Delete
* @param RequestInterface $request Request
[151] Fix | Delete
*/
[152] Fix | Delete
protected function addSslOptions(RequestInterface $request)
[153] Fix | Delete
{
[154] Fix | Delete
if ($request->getCurlOptions()->get(CURLOPT_SSL_VERIFYPEER)) {
[155] Fix | Delete
$this->setContextValue('ssl', 'verify_peer', true, true);
[156] Fix | Delete
if ($cafile = $request->getCurlOptions()->get(CURLOPT_CAINFO)) {
[157] Fix | Delete
$this->setContextValue('ssl', 'cafile', $cafile, true);
[158] Fix | Delete
}
[159] Fix | Delete
} else {
[160] Fix | Delete
$this->setContextValue('ssl', 'verify_peer', false, true);
[161] Fix | Delete
}
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
/**
[165] Fix | Delete
* Add body (content) specific options to the context options
[166] Fix | Delete
*
[167] Fix | Delete
* @param RequestInterface $request
[168] Fix | Delete
*/
[169] Fix | Delete
protected function addBodyOptions(RequestInterface $request)
[170] Fix | Delete
{
[171] Fix | Delete
// Add the content for the request if needed
[172] Fix | Delete
if (!($request instanceof EntityEnclosingRequestInterface)) {
[173] Fix | Delete
return;
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
if (count($request->getPostFields())) {
[177] Fix | Delete
$this->setContextValue('http', 'content', (string) $request->getPostFields(), true);
[178] Fix | Delete
} elseif ($request->getBody()) {
[179] Fix | Delete
$this->setContextValue('http', 'content', (string) $request->getBody(), true);
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
// Always ensure a content-length header is sent
[183] Fix | Delete
if (isset($this->contextOptions['http']['content'])) {
[184] Fix | Delete
$headers = isset($this->contextOptions['http']['header']) ? $this->contextOptions['http']['header'] : array();
[185] Fix | Delete
$headers[] = 'Content-Length: ' . strlen($this->contextOptions['http']['content']);
[186] Fix | Delete
$this->setContextValue('http', 'header', $headers, true);
[187] Fix | Delete
}
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
/**
[191] Fix | Delete
* Add proxy parameters to the context if needed
[192] Fix | Delete
*
[193] Fix | Delete
* @param RequestInterface $request Request
[194] Fix | Delete
*/
[195] Fix | Delete
protected function addProxyOptions(RequestInterface $request)
[196] Fix | Delete
{
[197] Fix | Delete
if ($proxy = $request->getCurlOptions()->get(CURLOPT_PROXY)) {
[198] Fix | Delete
$this->setContextValue('http', 'proxy', $proxy);
[199] Fix | Delete
}
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
/**
[203] Fix | Delete
* Create the stream for the request with the context options
[204] Fix | Delete
*
[205] Fix | Delete
* @param array $params Parameters of the stream
[206] Fix | Delete
*
[207] Fix | Delete
* @return StreamInterface
[208] Fix | Delete
*/
[209] Fix | Delete
protected function createStream(array $params)
[210] Fix | Delete
{
[211] Fix | Delete
$http_response_header = null;
[212] Fix | Delete
$url = $this->url;
[213] Fix | Delete
$context = $this->context;
[214] Fix | Delete
$fp = $this->createResource(function () use ($context, $url, &$http_response_header) {
[215] Fix | Delete
return fopen((string) $url, 'r', false, $context);
[216] Fix | Delete
});
[217] Fix | Delete
[218] Fix | Delete
// Determine the class to instantiate
[219] Fix | Delete
$className = isset($params['stream_class']) ? $params['stream_class'] : __NAMESPACE__ . '\\Stream';
[220] Fix | Delete
[221] Fix | Delete
/** @var $stream StreamInterface */
[222] Fix | Delete
$stream = new $className($fp);
[223] Fix | Delete
[224] Fix | Delete
// Track the response headers of the request
[225] Fix | Delete
if (isset($http_response_header)) {
[226] Fix | Delete
$this->lastResponseHeaders = $http_response_header;
[227] Fix | Delete
$this->processResponseHeaders($stream);
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
return $stream;
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
/**
[234] Fix | Delete
* Process response headers
[235] Fix | Delete
*
[236] Fix | Delete
* @param StreamInterface $stream
[237] Fix | Delete
*/
[238] Fix | Delete
protected function processResponseHeaders(StreamInterface $stream)
[239] Fix | Delete
{
[240] Fix | Delete
// Set the size on the stream if it was returned in the response
[241] Fix | Delete
foreach ($this->lastResponseHeaders as $header) {
[242] Fix | Delete
if ((stripos($header, 'Content-Length:')) === 0) {
[243] Fix | Delete
$stream->setSize(trim(substr($header, 15)));
[244] Fix | Delete
}
[245] Fix | Delete
}
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
/**
[249] Fix | Delete
* Create a resource and check to ensure it was created successfully
[250] Fix | Delete
*
[251] Fix | Delete
* @param callable $callback Closure to invoke that must return a valid resource
[252] Fix | Delete
*
[253] Fix | Delete
* @return resource
[254] Fix | Delete
* @throws RuntimeException on error
[255] Fix | Delete
*/
[256] Fix | Delete
protected function createResource($callback)
[257] Fix | Delete
{
[258] Fix | Delete
$errors = null;
[259] Fix | Delete
set_error_handler(function ($_, $msg, $file, $line) use (&$errors) {
[260] Fix | Delete
$errors[] = array(
[261] Fix | Delete
'message' => $msg,
[262] Fix | Delete
'file' => $file,
[263] Fix | Delete
'line' => $line
[264] Fix | Delete
);
[265] Fix | Delete
return true;
[266] Fix | Delete
});
[267] Fix | Delete
$resource = call_user_func($callback);
[268] Fix | Delete
restore_error_handler();
[269] Fix | Delete
[270] Fix | Delete
if (!$resource) {
[271] Fix | Delete
$message = 'Error creating resource. ';
[272] Fix | Delete
foreach ($errors as $err) {
[273] Fix | Delete
foreach ($err as $key => $value) {
[274] Fix | Delete
$message .= "[$key] $value" . PHP_EOL;
[275] Fix | Delete
}
[276] Fix | Delete
}
[277] Fix | Delete
throw new RuntimeException(trim($message));
[278] Fix | Delete
}
[279] Fix | Delete
[280] Fix | Delete
return $resource;
[281] Fix | Delete
}
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function