Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/updraftp.../vendor/guzzleht.../guzzle/src/Cookie
File: SetCookie.php
<?php
[0] Fix | Delete
namespace GuzzleHttp\Cookie;
[1] Fix | Delete
[2] Fix | Delete
/**
[3] Fix | Delete
* Set-Cookie object
[4] Fix | Delete
*/
[5] Fix | Delete
class SetCookie
[6] Fix | Delete
{
[7] Fix | Delete
/** @var array */
[8] Fix | Delete
private static $defaults = [
[9] Fix | Delete
'Name' => null,
[10] Fix | Delete
'Value' => null,
[11] Fix | Delete
'Domain' => null,
[12] Fix | Delete
'Path' => '/',
[13] Fix | Delete
'Max-Age' => null,
[14] Fix | Delete
'Expires' => null,
[15] Fix | Delete
'Secure' => false,
[16] Fix | Delete
'Discard' => false,
[17] Fix | Delete
'HttpOnly' => false
[18] Fix | Delete
];
[19] Fix | Delete
[20] Fix | Delete
/** @var array Cookie data */
[21] Fix | Delete
private $data;
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Create a new SetCookie object from a string
[25] Fix | Delete
*
[26] Fix | Delete
* @param string $cookie Set-Cookie header string
[27] Fix | Delete
*
[28] Fix | Delete
* @return self
[29] Fix | Delete
*/
[30] Fix | Delete
public static function fromString($cookie)
[31] Fix | Delete
{
[32] Fix | Delete
// Create the default return array
[33] Fix | Delete
$data = self::$defaults;
[34] Fix | Delete
// Explode the cookie string using a series of semicolons
[35] Fix | Delete
$pieces = array_filter(array_map('trim', explode(';', $cookie)));
[36] Fix | Delete
// The name of the cookie (first kvp) must exist and include an equal sign.
[37] Fix | Delete
if (empty($pieces[0]) || !strpos($pieces[0], '=')) {
[38] Fix | Delete
return new self($data);
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
// Add the cookie pieces into the parsed data array
[42] Fix | Delete
foreach ($pieces as $part) {
[43] Fix | Delete
$cookieParts = explode('=', $part, 2);
[44] Fix | Delete
$key = trim($cookieParts[0]);
[45] Fix | Delete
$value = isset($cookieParts[1])
[46] Fix | Delete
? trim($cookieParts[1], " \n\r\t\0\x0B")
[47] Fix | Delete
: true;
[48] Fix | Delete
[49] Fix | Delete
// Only check for non-cookies when cookies have been found
[50] Fix | Delete
if (empty($data['Name'])) {
[51] Fix | Delete
$data['Name'] = $key;
[52] Fix | Delete
$data['Value'] = $value;
[53] Fix | Delete
} else {
[54] Fix | Delete
foreach (array_keys(self::$defaults) as $search) {
[55] Fix | Delete
if (!strcasecmp($search, $key)) {
[56] Fix | Delete
$data[$search] = $value;
[57] Fix | Delete
continue 2;
[58] Fix | Delete
}
[59] Fix | Delete
}
[60] Fix | Delete
$data[$key] = $value;
[61] Fix | Delete
}
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
return new self($data);
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
/**
[68] Fix | Delete
* @param array $data Array of cookie data provided by a Cookie parser
[69] Fix | Delete
*/
[70] Fix | Delete
public function __construct(array $data = [])
[71] Fix | Delete
{
[72] Fix | Delete
$this->data = array_replace(self::$defaults, $data);
[73] Fix | Delete
// Extract the Expires value and turn it into a UNIX timestamp if needed
[74] Fix | Delete
if (!$this->getExpires() && $this->getMaxAge()) {
[75] Fix | Delete
// Calculate the Expires date
[76] Fix | Delete
$this->setExpires(time() + $this->getMaxAge());
[77] Fix | Delete
} elseif ($this->getExpires() && !is_numeric($this->getExpires())) {
[78] Fix | Delete
$this->setExpires($this->getExpires());
[79] Fix | Delete
}
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
public function __toString()
[83] Fix | Delete
{
[84] Fix | Delete
$str = $this->data['Name'] . '=' . $this->data['Value'] . '; ';
[85] Fix | Delete
foreach ($this->data as $k => $v) {
[86] Fix | Delete
if ($k !== 'Name' && $k !== 'Value' && $v !== null && $v !== false) {
[87] Fix | Delete
if ($k === 'Expires') {
[88] Fix | Delete
$str .= 'Expires=' . gmdate('D, d M Y H:i:s \G\M\T', $v) . '; ';
[89] Fix | Delete
} else {
[90] Fix | Delete
$str .= ($v === true ? $k : "{$k}={$v}") . '; ';
[91] Fix | Delete
}
[92] Fix | Delete
}
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
return rtrim($str, '; ');
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
public function toArray()
[99] Fix | Delete
{
[100] Fix | Delete
return $this->data;
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
/**
[104] Fix | Delete
* Get the cookie name
[105] Fix | Delete
*
[106] Fix | Delete
* @return string
[107] Fix | Delete
*/
[108] Fix | Delete
public function getName()
[109] Fix | Delete
{
[110] Fix | Delete
return $this->data['Name'];
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
/**
[114] Fix | Delete
* Set the cookie name
[115] Fix | Delete
*
[116] Fix | Delete
* @param string $name Cookie name
[117] Fix | Delete
*/
[118] Fix | Delete
public function setName($name)
[119] Fix | Delete
{
[120] Fix | Delete
$this->data['Name'] = $name;
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
/**
[124] Fix | Delete
* Get the cookie value
[125] Fix | Delete
*
[126] Fix | Delete
* @return string
[127] Fix | Delete
*/
[128] Fix | Delete
public function getValue()
[129] Fix | Delete
{
[130] Fix | Delete
return $this->data['Value'];
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
/**
[134] Fix | Delete
* Set the cookie value
[135] Fix | Delete
*
[136] Fix | Delete
* @param string $value Cookie value
[137] Fix | Delete
*/
[138] Fix | Delete
public function setValue($value)
[139] Fix | Delete
{
[140] Fix | Delete
$this->data['Value'] = $value;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* Get the domain
[145] Fix | Delete
*
[146] Fix | Delete
* @return string|null
[147] Fix | Delete
*/
[148] Fix | Delete
public function getDomain()
[149] Fix | Delete
{
[150] Fix | Delete
return $this->data['Domain'];
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
/**
[154] Fix | Delete
* Set the domain of the cookie
[155] Fix | Delete
*
[156] Fix | Delete
* @param string $domain
[157] Fix | Delete
*/
[158] Fix | Delete
public function setDomain($domain)
[159] Fix | Delete
{
[160] Fix | Delete
$this->data['Domain'] = $domain;
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
/**
[164] Fix | Delete
* Get the path
[165] Fix | Delete
*
[166] Fix | Delete
* @return string
[167] Fix | Delete
*/
[168] Fix | Delete
public function getPath()
[169] Fix | Delete
{
[170] Fix | Delete
return $this->data['Path'];
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* Set the path of the cookie
[175] Fix | Delete
*
[176] Fix | Delete
* @param string $path Path of the cookie
[177] Fix | Delete
*/
[178] Fix | Delete
public function setPath($path)
[179] Fix | Delete
{
[180] Fix | Delete
$this->data['Path'] = $path;
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
/**
[184] Fix | Delete
* Maximum lifetime of the cookie in seconds
[185] Fix | Delete
*
[186] Fix | Delete
* @return int|null
[187] Fix | Delete
*/
[188] Fix | Delete
public function getMaxAge()
[189] Fix | Delete
{
[190] Fix | Delete
return $this->data['Max-Age'];
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
/**
[194] Fix | Delete
* Set the max-age of the cookie
[195] Fix | Delete
*
[196] Fix | Delete
* @param int $maxAge Max age of the cookie in seconds
[197] Fix | Delete
*/
[198] Fix | Delete
public function setMaxAge($maxAge)
[199] Fix | Delete
{
[200] Fix | Delete
$this->data['Max-Age'] = $maxAge;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* The UNIX timestamp when the cookie Expires
[205] Fix | Delete
*
[206] Fix | Delete
* @return mixed
[207] Fix | Delete
*/
[208] Fix | Delete
public function getExpires()
[209] Fix | Delete
{
[210] Fix | Delete
return $this->data['Expires'];
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
/**
[214] Fix | Delete
* Set the unix timestamp for which the cookie will expire
[215] Fix | Delete
*
[216] Fix | Delete
* @param int $timestamp Unix timestamp
[217] Fix | Delete
*/
[218] Fix | Delete
public function setExpires($timestamp)
[219] Fix | Delete
{
[220] Fix | Delete
$this->data['Expires'] = is_numeric($timestamp)
[221] Fix | Delete
? (int) $timestamp
[222] Fix | Delete
: strtotime($timestamp);
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
/**
[226] Fix | Delete
* Get whether or not this is a secure cookie
[227] Fix | Delete
*
[228] Fix | Delete
* @return bool|null
[229] Fix | Delete
*/
[230] Fix | Delete
public function getSecure()
[231] Fix | Delete
{
[232] Fix | Delete
return $this->data['Secure'];
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
/**
[236] Fix | Delete
* Set whether or not the cookie is secure
[237] Fix | Delete
*
[238] Fix | Delete
* @param bool $secure Set to true or false if secure
[239] Fix | Delete
*/
[240] Fix | Delete
public function setSecure($secure)
[241] Fix | Delete
{
[242] Fix | Delete
$this->data['Secure'] = $secure;
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
/**
[246] Fix | Delete
* Get whether or not this is a session cookie
[247] Fix | Delete
*
[248] Fix | Delete
* @return bool|null
[249] Fix | Delete
*/
[250] Fix | Delete
public function getDiscard()
[251] Fix | Delete
{
[252] Fix | Delete
return $this->data['Discard'];
[253] Fix | Delete
}
[254] Fix | Delete
[255] Fix | Delete
/**
[256] Fix | Delete
* Set whether or not this is a session cookie
[257] Fix | Delete
*
[258] Fix | Delete
* @param bool $discard Set to true or false if this is a session cookie
[259] Fix | Delete
*/
[260] Fix | Delete
public function setDiscard($discard)
[261] Fix | Delete
{
[262] Fix | Delete
$this->data['Discard'] = $discard;
[263] Fix | Delete
}
[264] Fix | Delete
[265] Fix | Delete
/**
[266] Fix | Delete
* Get whether or not this is an HTTP only cookie
[267] Fix | Delete
*
[268] Fix | Delete
* @return bool
[269] Fix | Delete
*/
[270] Fix | Delete
public function getHttpOnly()
[271] Fix | Delete
{
[272] Fix | Delete
return $this->data['HttpOnly'];
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
/**
[276] Fix | Delete
* Set whether or not this is an HTTP only cookie
[277] Fix | Delete
*
[278] Fix | Delete
* @param bool $httpOnly Set to true or false if this is HTTP only
[279] Fix | Delete
*/
[280] Fix | Delete
public function setHttpOnly($httpOnly)
[281] Fix | Delete
{
[282] Fix | Delete
$this->data['HttpOnly'] = $httpOnly;
[283] Fix | Delete
}
[284] Fix | Delete
[285] Fix | Delete
/**
[286] Fix | Delete
* Check if the cookie matches a path value.
[287] Fix | Delete
*
[288] Fix | Delete
* A request-path path-matches a given cookie-path if at least one of
[289] Fix | Delete
* the following conditions holds:
[290] Fix | Delete
*
[291] Fix | Delete
* - The cookie-path and the request-path are identical.
[292] Fix | Delete
* - The cookie-path is a prefix of the request-path, and the last
[293] Fix | Delete
* character of the cookie-path is %x2F ("/").
[294] Fix | Delete
* - The cookie-path is a prefix of the request-path, and the first
[295] Fix | Delete
* character of the request-path that is not included in the cookie-
[296] Fix | Delete
* path is a %x2F ("/") character.
[297] Fix | Delete
*
[298] Fix | Delete
* @param string $requestPath Path to check against
[299] Fix | Delete
*
[300] Fix | Delete
* @return bool
[301] Fix | Delete
*/
[302] Fix | Delete
public function matchesPath($requestPath)
[303] Fix | Delete
{
[304] Fix | Delete
$cookiePath = $this->getPath();
[305] Fix | Delete
[306] Fix | Delete
// Match on exact matches or when path is the default empty "/"
[307] Fix | Delete
if ($cookiePath === '/' || $cookiePath == $requestPath) {
[308] Fix | Delete
return true;
[309] Fix | Delete
}
[310] Fix | Delete
[311] Fix | Delete
// Ensure that the cookie-path is a prefix of the request path.
[312] Fix | Delete
if (0 !== strpos($requestPath, $cookiePath)) {
[313] Fix | Delete
return false;
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
// Match if the last character of the cookie-path is "/"
[317] Fix | Delete
if (substr($cookiePath, -1, 1) === '/') {
[318] Fix | Delete
return true;
[319] Fix | Delete
}
[320] Fix | Delete
[321] Fix | Delete
// Match if the first character not included in cookie path is "/"
[322] Fix | Delete
return substr($requestPath, strlen($cookiePath), 1) === '/';
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
/**
[326] Fix | Delete
* Check if the cookie matches a domain value
[327] Fix | Delete
*
[328] Fix | Delete
* @param string $domain Domain to check against
[329] Fix | Delete
*
[330] Fix | Delete
* @return bool
[331] Fix | Delete
*/
[332] Fix | Delete
public function matchesDomain($domain)
[333] Fix | Delete
{
[334] Fix | Delete
// Remove the leading '.' as per spec in RFC 6265.
[335] Fix | Delete
// http://tools.ietf.org/html/rfc6265#section-5.2.3
[336] Fix | Delete
$cookieDomain = ltrim($this->getDomain(), '.');
[337] Fix | Delete
[338] Fix | Delete
// Domain not set or exact match.
[339] Fix | Delete
if (!$cookieDomain || !strcasecmp($domain, $cookieDomain)) {
[340] Fix | Delete
return true;
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
// Matching the subdomain according to RFC 6265.
[344] Fix | Delete
// http://tools.ietf.org/html/rfc6265#section-5.1.3
[345] Fix | Delete
if (filter_var($domain, FILTER_VALIDATE_IP)) {
[346] Fix | Delete
return false;
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
return (bool) preg_match('/\.' . preg_quote($cookieDomain, '/') . '$/', $domain);
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
/**
[353] Fix | Delete
* Check if the cookie is expired
[354] Fix | Delete
*
[355] Fix | Delete
* @return bool
[356] Fix | Delete
*/
[357] Fix | Delete
public function isExpired()
[358] Fix | Delete
{
[359] Fix | Delete
return $this->getExpires() !== null && time() > $this->getExpires();
[360] Fix | Delete
}
[361] Fix | Delete
[362] Fix | Delete
/**
[363] Fix | Delete
* Check if the cookie is valid according to RFC 6265
[364] Fix | Delete
*
[365] Fix | Delete
* @return bool|string Returns true if valid or an error message if invalid
[366] Fix | Delete
*/
[367] Fix | Delete
public function validate()
[368] Fix | Delete
{
[369] Fix | Delete
// Names must not be empty, but can be 0
[370] Fix | Delete
$name = $this->getName();
[371] Fix | Delete
if (empty($name) && !is_numeric($name)) {
[372] Fix | Delete
return 'The cookie name must not be empty';
[373] Fix | Delete
}
[374] Fix | Delete
[375] Fix | Delete
// Check if any of the invalid characters are present in the cookie name
[376] Fix | Delete
if (preg_match(
[377] Fix | Delete
'/[\x00-\x20\x22\x28-\x29\x2c\x2f\x3a-\x40\x5c\x7b\x7d\x7f]/',
[378] Fix | Delete
$name
[379] Fix | Delete
)) {
[380] Fix | Delete
return 'Cookie name must not contain invalid characters: ASCII '
[381] Fix | Delete
. 'Control characters (0-31;127), space, tab and the '
[382] Fix | Delete
. 'following characters: ()<>@,;:\"/?={}';
[383] Fix | Delete
}
[384] Fix | Delete
[385] Fix | Delete
// Value must not be empty, but can be 0
[386] Fix | Delete
$value = $this->getValue();
[387] Fix | Delete
if (empty($value) && !is_numeric($value)) {
[388] Fix | Delete
return 'The cookie value must not be empty';
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
// Domains must not be empty, but can be 0
[392] Fix | Delete
// A "0" is not a valid internet domain, but may be used as server name
[393] Fix | Delete
// in a private network.
[394] Fix | Delete
$domain = $this->getDomain();
[395] Fix | Delete
if (empty($domain) && !is_numeric($domain)) {
[396] Fix | Delete
return 'The cookie domain must not be empty';
[397] Fix | Delete
}
[398] Fix | Delete
[399] Fix | Delete
return true;
[400] Fix | Delete
}
[401] Fix | Delete
}
[402] Fix | Delete
[403] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function