Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../vendor/guzzle/guzzle/src/Guzzle/Plugin/Cache
File: DefaultCacheStorage.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Guzzle\Plugin\Cache;
[2] Fix | Delete
[3] Fix | Delete
use Guzzle\Cache\CacheAdapterFactory;
[4] Fix | Delete
use Guzzle\Cache\CacheAdapterInterface;
[5] Fix | Delete
use Guzzle\Http\EntityBodyInterface;
[6] Fix | Delete
use Guzzle\Http\Message\MessageInterface;
[7] Fix | Delete
use Guzzle\Http\Message\Request;
[8] Fix | Delete
use Guzzle\Http\Message\RequestInterface;
[9] Fix | Delete
use Guzzle\Http\Message\Response;
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Default cache storage implementation
[13] Fix | Delete
*/
[14] Fix | Delete
class DefaultCacheStorage implements CacheStorageInterface
[15] Fix | Delete
{
[16] Fix | Delete
/** @var string */
[17] Fix | Delete
protected $keyPrefix;
[18] Fix | Delete
[19] Fix | Delete
/** @var CacheAdapterInterface Cache used to store cache data */
[20] Fix | Delete
protected $cache;
[21] Fix | Delete
[22] Fix | Delete
/** @var int Default cache TTL */
[23] Fix | Delete
protected $defaultTtl;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* @param mixed $cache Cache used to store cache data
[27] Fix | Delete
* @param string $keyPrefix Provide an optional key prefix to prefix on all cache keys
[28] Fix | Delete
* @param int $defaultTtl Default cache TTL
[29] Fix | Delete
*/
[30] Fix | Delete
public function __construct($cache, $keyPrefix = '', $defaultTtl = 3600)
[31] Fix | Delete
{
[32] Fix | Delete
$this->cache = CacheAdapterFactory::fromCache($cache);
[33] Fix | Delete
$this->defaultTtl = $defaultTtl;
[34] Fix | Delete
$this->keyPrefix = $keyPrefix;
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
public function cache(RequestInterface $request, Response $response)
[38] Fix | Delete
{
[39] Fix | Delete
$currentTime = time();
[40] Fix | Delete
[41] Fix | Delete
$overrideTtl = $request->getParams()->get('cache.override_ttl');
[42] Fix | Delete
if ($overrideTtl) {
[43] Fix | Delete
$ttl = $overrideTtl;
[44] Fix | Delete
} else {
[45] Fix | Delete
$maxAge = $response->getMaxAge();
[46] Fix | Delete
if ($maxAge !== null) {
[47] Fix | Delete
$ttl = $maxAge;
[48] Fix | Delete
} else {
[49] Fix | Delete
$ttl = $this->defaultTtl;
[50] Fix | Delete
}
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
if ($cacheControl = $response->getHeader('Cache-Control')) {
[54] Fix | Delete
$stale = $cacheControl->getDirective('stale-if-error');
[55] Fix | Delete
if ($stale === true) {
[56] Fix | Delete
$ttl += $ttl;
[57] Fix | Delete
} else if (is_numeric($stale)) {
[58] Fix | Delete
$ttl += $stale;
[59] Fix | Delete
}
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
// Determine which manifest key should be used
[63] Fix | Delete
$key = $this->getCacheKey($request);
[64] Fix | Delete
$persistedRequest = $this->persistHeaders($request);
[65] Fix | Delete
$entries = array();
[66] Fix | Delete
[67] Fix | Delete
if ($manifest = $this->cache->fetch($key)) {
[68] Fix | Delete
// Determine which cache entries should still be in the cache
[69] Fix | Delete
$vary = $response->getVary();
[70] Fix | Delete
foreach (unserialize($manifest) as $entry) {
[71] Fix | Delete
// Check if the entry is expired
[72] Fix | Delete
if ($entry[4] < $currentTime) {
[73] Fix | Delete
continue;
[74] Fix | Delete
}
[75] Fix | Delete
$entry[1]['vary'] = isset($entry[1]['vary']) ? $entry[1]['vary'] : '';
[76] Fix | Delete
if ($vary != $entry[1]['vary'] || !$this->requestsMatch($vary, $entry[0], $persistedRequest)) {
[77] Fix | Delete
$entries[] = $entry;
[78] Fix | Delete
}
[79] Fix | Delete
}
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
// Persist the response body if needed
[83] Fix | Delete
$bodyDigest = null;
[84] Fix | Delete
if ($response->getBody() && $response->getBody()->getContentLength() > 0) {
[85] Fix | Delete
$bodyDigest = $this->getBodyKey($request->getUrl(), $response->getBody());
[86] Fix | Delete
$this->cache->save($bodyDigest, (string) $response->getBody(), $ttl);
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
array_unshift($entries, array(
[90] Fix | Delete
$persistedRequest,
[91] Fix | Delete
$this->persistHeaders($response),
[92] Fix | Delete
$response->getStatusCode(),
[93] Fix | Delete
$bodyDigest,
[94] Fix | Delete
$currentTime + $ttl
[95] Fix | Delete
));
[96] Fix | Delete
[97] Fix | Delete
$this->cache->save($key, serialize($entries));
[98] Fix | Delete
}
[99] Fix | Delete
[100] Fix | Delete
public function delete(RequestInterface $request)
[101] Fix | Delete
{
[102] Fix | Delete
$key = $this->getCacheKey($request);
[103] Fix | Delete
if ($entries = $this->cache->fetch($key)) {
[104] Fix | Delete
// Delete each cached body
[105] Fix | Delete
foreach (unserialize($entries) as $entry) {
[106] Fix | Delete
if ($entry[3]) {
[107] Fix | Delete
$this->cache->delete($entry[3]);
[108] Fix | Delete
}
[109] Fix | Delete
}
[110] Fix | Delete
$this->cache->delete($key);
[111] Fix | Delete
}
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
public function purge($url)
[115] Fix | Delete
{
[116] Fix | Delete
foreach (array('GET', 'HEAD', 'POST', 'PUT', 'DELETE') as $method) {
[117] Fix | Delete
$this->delete(new Request($method, $url));
[118] Fix | Delete
}
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
public function fetch(RequestInterface $request)
[122] Fix | Delete
{
[123] Fix | Delete
$key = $this->getCacheKey($request);
[124] Fix | Delete
if (!($entries = $this->cache->fetch($key))) {
[125] Fix | Delete
return null;
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
$match = null;
[129] Fix | Delete
$headers = $this->persistHeaders($request);
[130] Fix | Delete
$entries = unserialize($entries);
[131] Fix | Delete
foreach ($entries as $index => $entry) {
[132] Fix | Delete
if ($this->requestsMatch(isset($entry[1]['vary']) ? $entry[1]['vary'] : '', $headers, $entry[0])) {
[133] Fix | Delete
$match = $entry;
[134] Fix | Delete
break;
[135] Fix | Delete
}
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
if (!$match) {
[139] Fix | Delete
return null;
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
// Ensure that the response is not expired
[143] Fix | Delete
$response = null;
[144] Fix | Delete
if ($match[4] < time()) {
[145] Fix | Delete
$response = -1;
[146] Fix | Delete
} else {
[147] Fix | Delete
$response = new Response($match[2], $match[1]);
[148] Fix | Delete
if ($match[3]) {
[149] Fix | Delete
if ($body = $this->cache->fetch($match[3])) {
[150] Fix | Delete
$response->setBody($body);
[151] Fix | Delete
} else {
[152] Fix | Delete
// The response is not valid because the body was somehow deleted
[153] Fix | Delete
$response = -1;
[154] Fix | Delete
}
[155] Fix | Delete
}
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
if ($response === -1) {
[159] Fix | Delete
// Remove the entry from the metadata and update the cache
[160] Fix | Delete
unset($entries[$index]);
[161] Fix | Delete
if ($entries) {
[162] Fix | Delete
$this->cache->save($key, serialize($entries));
[163] Fix | Delete
} else {
[164] Fix | Delete
$this->cache->delete($key);
[165] Fix | Delete
}
[166] Fix | Delete
return null;
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
return $response;
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* Hash a request URL into a string that returns cache metadata
[174] Fix | Delete
*
[175] Fix | Delete
* @param RequestInterface $request
[176] Fix | Delete
*
[177] Fix | Delete
* @return string
[178] Fix | Delete
*/
[179] Fix | Delete
protected function getCacheKey(RequestInterface $request)
[180] Fix | Delete
{
[181] Fix | Delete
// Allow cache.key_filter to trim down the URL cache key by removing generate query string values (e.g. auth)
[182] Fix | Delete
if ($filter = $request->getParams()->get('cache.key_filter')) {
[183] Fix | Delete
$url = $request->getUrl(true);
[184] Fix | Delete
foreach (explode(',', $filter) as $remove) {
[185] Fix | Delete
$url->getQuery()->remove(trim($remove));
[186] Fix | Delete
}
[187] Fix | Delete
} else {
[188] Fix | Delete
$url = $request->getUrl();
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
return $this->keyPrefix . md5($request->getMethod() . ' ' . $url);
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
/**
[195] Fix | Delete
* Create a cache key for a response's body
[196] Fix | Delete
*
[197] Fix | Delete
* @param string $url URL of the entry
[198] Fix | Delete
* @param EntityBodyInterface $body Response body
[199] Fix | Delete
*
[200] Fix | Delete
* @return string
[201] Fix | Delete
*/
[202] Fix | Delete
protected function getBodyKey($url, EntityBodyInterface $body)
[203] Fix | Delete
{
[204] Fix | Delete
return $this->keyPrefix . md5($url) . $body->getContentMd5();
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
/**
[208] Fix | Delete
* Determines whether two Request HTTP header sets are non-varying
[209] Fix | Delete
*
[210] Fix | Delete
* @param string $vary Response vary header
[211] Fix | Delete
* @param array $r1 HTTP header array
[212] Fix | Delete
* @param array $r2 HTTP header array
[213] Fix | Delete
*
[214] Fix | Delete
* @return bool
[215] Fix | Delete
*/
[216] Fix | Delete
private function requestsMatch($vary, $r1, $r2)
[217] Fix | Delete
{
[218] Fix | Delete
if ($vary) {
[219] Fix | Delete
foreach (explode(',', $vary) as $header) {
[220] Fix | Delete
$key = trim(strtolower($header));
[221] Fix | Delete
$v1 = isset($r1[$key]) ? $r1[$key] : null;
[222] Fix | Delete
$v2 = isset($r2[$key]) ? $r2[$key] : null;
[223] Fix | Delete
if ($v1 !== $v2) {
[224] Fix | Delete
return false;
[225] Fix | Delete
}
[226] Fix | Delete
}
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
return true;
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
/**
[233] Fix | Delete
* Creates an array of cacheable and normalized message headers
[234] Fix | Delete
*
[235] Fix | Delete
* @param MessageInterface $message
[236] Fix | Delete
*
[237] Fix | Delete
* @return array
[238] Fix | Delete
*/
[239] Fix | Delete
private function persistHeaders(MessageInterface $message)
[240] Fix | Delete
{
[241] Fix | Delete
// Headers are excluded from the caching (see RFC 2616:13.5.1)
[242] Fix | Delete
static $noCache = array(
[243] Fix | Delete
'age' => true,
[244] Fix | Delete
'connection' => true,
[245] Fix | Delete
'keep-alive' => true,
[246] Fix | Delete
'proxy-authenticate' => true,
[247] Fix | Delete
'proxy-authorization' => true,
[248] Fix | Delete
'te' => true,
[249] Fix | Delete
'trailers' => true,
[250] Fix | Delete
'transfer-encoding' => true,
[251] Fix | Delete
'upgrade' => true,
[252] Fix | Delete
'set-cookie' => true,
[253] Fix | Delete
'set-cookie2' => true
[254] Fix | Delete
);
[255] Fix | Delete
[256] Fix | Delete
// Clone the response to not destroy any necessary headers when caching
[257] Fix | Delete
$headers = $message->getHeaders()->getAll();
[258] Fix | Delete
$headers = array_diff_key($headers, $noCache);
[259] Fix | Delete
// Cast the headers to a string
[260] Fix | Delete
$headers = array_map(function ($h) { return (string) $h; }, $headers);
[261] Fix | Delete
[262] Fix | Delete
return $headers;
[263] Fix | Delete
}
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function