2015-08-26 05:55:21 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2015-09-26 09:04:28 +02:00
|
|
|
* HTTP API: WP_Http_Cookie class
|
2015-09-03 05:19:21 +02:00
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage HTTP
|
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core class used to encapsulate a single cookie object for internal use.
|
2015-08-26 05:55:21 +02:00
|
|
|
*
|
|
|
|
* Returned cookies are represented using this class, and when cookies are set, if they are not
|
|
|
|
* already a WP_Http_Cookie() object, then they are turned into one.
|
|
|
|
*
|
|
|
|
* @todo The WordPress convention is to use underscores instead of camelCase for function and method
|
|
|
|
* names. Need to switch to use underscores instead for the methods.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*/
|
|
|
|
class WP_Http_Cookie {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cookie name.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cookie value.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When the cookie expires.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $expires;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cookie URL path.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $path;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cookie Domain.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $domain;
|
|
|
|
|
2019-04-08 07:32:51 +02:00
|
|
|
/**
|
|
|
|
* host-only flag.
|
|
|
|
*
|
|
|
|
* @since 5.2.0
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $host_only;
|
|
|
|
|
2015-08-26 05:55:21 +02:00
|
|
|
/**
|
|
|
|
* Sets up this cookie object.
|
|
|
|
*
|
|
|
|
* The parameter $data should be either an associative array containing the indices names below
|
|
|
|
* or a header string detailing it.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
2019-04-08 07:32:51 +02:00
|
|
|
* @since 5.2.0 Added `host_only` to the `$data` parameter.
|
2015-08-26 05:55:21 +02:00
|
|
|
*
|
|
|
|
* @param string|array $data {
|
|
|
|
* Raw cookie data as header string or data array.
|
|
|
|
*
|
2019-04-08 07:32:51 +02:00
|
|
|
* @type string $name Cookie name.
|
|
|
|
* @type mixed $value Value. Should NOT already be urlencoded.
|
|
|
|
* @type string|int $expires Optional. Unix timestamp or formatted date. Default null.
|
|
|
|
* @type string $path Optional. Path. Default '/'.
|
|
|
|
* @type string $domain Optional. Domain. Default host of parsed $requested_url.
|
|
|
|
* @type int $port Optional. Port. Default null.
|
|
|
|
* @type bool $host_only Optional. host-only storage flag. Default true.
|
2015-08-26 05:55:21 +02:00
|
|
|
* }
|
|
|
|
* @param string $requested_url The URL which the cookie was set on, used for default $domain
|
|
|
|
* and $port values.
|
|
|
|
*/
|
|
|
|
public function __construct( $data, $requested_url = '' ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $requested_url ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
$arrURL = @parse_url( $requested_url );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
|
|
|
if ( isset( $arrURL['host'] ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
$this->domain = $arrURL['host'];
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
$this->path = isset( $arrURL['path'] ) ? $arrURL['path'] : '/';
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( '/' != substr( $this->path, -1 ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
$this->path = dirname( $this->path ) . '/';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
if ( is_string( $data ) ) {
|
|
|
|
// Assume it's a header string direct from a previous request.
|
|
|
|
$pairs = explode( ';', $data );
|
|
|
|
|
|
|
|
// Special handling for first pair; name=value. Also be careful of "=" in value.
|
2017-12-01 00:11:00 +01:00
|
|
|
$name = trim( substr( $pairs[0], 0, strpos( $pairs[0], '=' ) ) );
|
|
|
|
$value = substr( $pairs[0], strpos( $pairs[0], '=' ) + 1 );
|
2015-08-26 05:55:21 +02:00
|
|
|
$this->name = $name;
|
|
|
|
$this->value = urldecode( $value );
|
|
|
|
|
|
|
|
// Removes name=value from items.
|
|
|
|
array_shift( $pairs );
|
|
|
|
|
|
|
|
// Set everything else as a property.
|
|
|
|
foreach ( $pairs as $pair ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
$pair = rtrim( $pair );
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
// Handle the cookie ending in ; which results in a empty final pair.
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( empty( $pair ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
continue;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
list( $key, $val ) = strpos( $pair, '=' ) ? explode( '=', $pair ) : array( $pair, '' );
|
2017-12-01 00:11:00 +01:00
|
|
|
$key = strtolower( trim( $key ) );
|
|
|
|
if ( 'expires' == $key ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
$val = strtotime( $val );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
$this->$key = $val;
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! isset( $data['name'] ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
// Set properties based directly on parameters.
|
2019-04-08 07:32:51 +02:00
|
|
|
foreach ( array( 'name', 'value', 'path', 'domain', 'port', 'host_only' ) as $field ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( isset( $data[ $field ] ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
$this->$field = $data[ $field ];
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( isset( $data['expires'] ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
$this->expires = is_int( $data['expires'] ) ? $data['expires'] : strtotime( $data['expires'] );
|
2017-12-01 00:11:00 +01:00
|
|
|
} else {
|
2015-08-26 05:55:21 +02:00
|
|
|
$this->expires = null;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Confirms that it's OK to send this cookie to the URL checked against.
|
|
|
|
*
|
|
|
|
* Decision is based on RFC 2109/2965, so look there for details on validity.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
|
|
|
* @param string $url URL you intend to send this cookie to
|
|
|
|
* @return bool true if allowed, false otherwise.
|
|
|
|
*/
|
|
|
|
public function test( $url ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( is_null( $this->name ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
// Expires - if expired then nothing else matters.
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( isset( $this->expires ) && time() > $this->expires ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
// Get details on the URL we're thinking about sending to.
|
2017-12-01 00:11:00 +01:00
|
|
|
$url = parse_url( $url );
|
2015-08-26 05:55:21 +02:00
|
|
|
$url['port'] = isset( $url['port'] ) ? $url['port'] : ( 'https' == $url['scheme'] ? 443 : 80 );
|
|
|
|
$url['path'] = isset( $url['path'] ) ? $url['path'] : '/';
|
|
|
|
|
|
|
|
// Values to use for comparison against the URL.
|
2017-12-01 00:11:00 +01:00
|
|
|
$path = isset( $this->path ) ? $this->path : '/';
|
|
|
|
$port = isset( $this->port ) ? $this->port : null;
|
2015-08-26 05:55:21 +02:00
|
|
|
$domain = isset( $this->domain ) ? strtolower( $this->domain ) : strtolower( $url['host'] );
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( false === stripos( $domain, '.' ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
$domain .= '.local';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
// Host - very basic check that the request URL ends with the domain restriction (minus leading dot).
|
|
|
|
$domain = substr( $domain, 0, 1 ) == '.' ? substr( $domain, 1 ) : $domain;
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( substr( $url['host'], -strlen( $domain ) ) != $domain ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
// Port - supports "port-lists" in the format: "80,8000,8080".
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! empty( $port ) && ! in_array( $url['port'], explode( ',', $port ) ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
// Path - request path must start with path restriction.
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( substr( $url['path'], 0, strlen( $path ) ) != $path ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert cookie name and value back to header string.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
|
|
|
* @return string Header encoded cookie name and value.
|
|
|
|
*/
|
2019-07-01 10:01:57 +02:00
|
|
|
public function getHeaderValue() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! isset( $this->name ) || ! isset( $this->value ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return '';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
/**
|
2016-05-22 20:15:28 +02:00
|
|
|
* Filters the header-encoded cookie value.
|
2015-08-26 05:55:21 +02:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param string $value The cookie value.
|
|
|
|
* @param string $name The cookie name.
|
|
|
|
*/
|
|
|
|
return $this->name . '=' . apply_filters( 'wp_http_cookie_value', $this->value, $this->name );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve cookie header for usage in the rest of the WordPress HTTP API.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-07-01 10:01:57 +02:00
|
|
|
public function getFullHeader() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
|
2015-08-26 05:55:21 +02:00
|
|
|
return 'Cookie: ' . $this->getHeaderValue();
|
|
|
|
}
|
2016-07-27 17:32:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves cookie attributes.
|
|
|
|
*
|
|
|
|
* @since 4.6.0
|
|
|
|
*
|
|
|
|
* @return array {
|
|
|
|
* List of attributes.
|
|
|
|
*
|
|
|
|
* @type string $expires When the cookie expires.
|
|
|
|
* @type string $path Cookie URL path.
|
|
|
|
* @type string $domain Cookie domain.
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
public function get_attributes() {
|
|
|
|
return array(
|
|
|
|
'expires' => $this->expires,
|
|
|
|
'path' => $this->path,
|
|
|
|
'domain' => $this->domain,
|
|
|
|
);
|
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
}
|