3 * HTTP API: WP_Http_Cookie class
11 * Core class used to encapsulate a single cookie object for internal use.
13 * Returned cookies are represented using this class, and when cookies are set, if they are not
14 * already a WP_Http_Cookie() object, then they are turned into one.
16 * @todo The WordPress convention is to use underscores instead of camelCase for function and method
17 * names. Need to switch to use underscores instead for the methods.
21 class WP_Http_Cookie {
40 * When the cookie expires.
64 * Sets up this cookie object.
66 * The parameter $data should be either an associative array containing the indices names below
67 * or a header string detailing it.
72 * @param string|array $data {
73 * Raw cookie data as header string or data array.
75 * @type string $name Cookie name.
76 * @type mixed $value Value. Should NOT already be urlencoded.
77 * @type string|int $expires Optional. Unix timestamp or formatted date. Default null.
78 * @type string $path Optional. Path. Default '/'.
79 * @type string $domain Optional. Domain. Default host of parsed $requested_url.
80 * @type int $port Optional. Port. Default null.
82 * @param string $requested_url The URL which the cookie was set on, used for default $domain
85 public function __construct( $data, $requested_url = '' ) {
87 $arrURL = @parse_url( $requested_url );
88 if ( isset( $arrURL['host'] ) )
89 $this->domain = $arrURL['host'];
90 $this->path = isset( $arrURL['path'] ) ? $arrURL['path'] : '/';
91 if ( '/' != substr( $this->path, -1 ) )
92 $this->path = dirname( $this->path ) . '/';
94 if ( is_string( $data ) ) {
95 // Assume it's a header string direct from a previous request.
96 $pairs = explode( ';', $data );
98 // Special handling for first pair; name=value. Also be careful of "=" in value.
99 $name = trim( substr( $pairs[0], 0, strpos( $pairs[0], '=' ) ) );
100 $value = substr( $pairs[0], strpos( $pairs[0], '=' ) + 1 );
102 $this->value = urldecode( $value );
104 // Removes name=value from items.
105 array_shift( $pairs );
107 // Set everything else as a property.
108 foreach ( $pairs as $pair ) {
109 $pair = rtrim($pair);
111 // Handle the cookie ending in ; which results in a empty final pair.
115 list( $key, $val ) = strpos( $pair, '=' ) ? explode( '=', $pair ) : array( $pair, '' );
116 $key = strtolower( trim( $key ) );
117 if ( 'expires' == $key )
118 $val = strtotime( $val );
122 if ( !isset( $data['name'] ) )
125 // Set properties based directly on parameters.
126 foreach ( array( 'name', 'value', 'path', 'domain', 'port' ) as $field ) {
127 if ( isset( $data[ $field ] ) )
128 $this->$field = $data[ $field ];
131 if ( isset( $data['expires'] ) )
132 $this->expires = is_int( $data['expires'] ) ? $data['expires'] : strtotime( $data['expires'] );
134 $this->expires = null;
139 * Confirms that it's OK to send this cookie to the URL checked against.
141 * Decision is based on RFC 2109/2965, so look there for details on validity.
146 * @param string $url URL you intend to send this cookie to
147 * @return bool true if allowed, false otherwise.
149 public function test( $url ) {
150 if ( is_null( $this->name ) )
153 // Expires - if expired then nothing else matters.
154 if ( isset( $this->expires ) && time() > $this->expires )
157 // Get details on the URL we're thinking about sending to.
158 $url = parse_url( $url );
159 $url['port'] = isset( $url['port'] ) ? $url['port'] : ( 'https' == $url['scheme'] ? 443 : 80 );
160 $url['path'] = isset( $url['path'] ) ? $url['path'] : '/';
162 // Values to use for comparison against the URL.
163 $path = isset( $this->path ) ? $this->path : '/';
164 $port = isset( $this->port ) ? $this->port : null;
165 $domain = isset( $this->domain ) ? strtolower( $this->domain ) : strtolower( $url['host'] );
166 if ( false === stripos( $domain, '.' ) )
169 // Host - very basic check that the request URL ends with the domain restriction (minus leading dot).
170 $domain = substr( $domain, 0, 1 ) == '.' ? substr( $domain, 1 ) : $domain;
171 if ( substr( $url['host'], -strlen( $domain ) ) != $domain )
174 // Port - supports "port-lists" in the format: "80,8000,8080".
175 if ( !empty( $port ) && !in_array( $url['port'], explode( ',', $port) ) )
178 // Path - request path must start with path restriction.
179 if ( substr( $url['path'], 0, strlen( $path ) ) != $path )
186 * Convert cookie name and value back to header string.
191 * @return string Header encoded cookie name and value.
193 public function getHeaderValue() {
194 if ( ! isset( $this->name ) || ! isset( $this->value ) )
198 * Filters the header-encoded cookie value.
202 * @param string $value The cookie value.
203 * @param string $name The cookie name.
205 return $this->name . '=' . apply_filters( 'wp_http_cookie_value', $this->value, $this->name );
209 * Retrieve cookie header for usage in the rest of the WordPress HTTP API.
216 public function getFullHeader() {
217 return 'Cookie: ' . $this->getHeaderValue();
221 * Retrieves cookie attributes.
227 * List of attributes.
229 * @type string $expires When the cookie expires.
230 * @type string $path Cookie URL path.
231 * @type string $domain Cookie domain.
234 public function get_attributes() {
236 'expires' => $this->expires,
237 'path' => $this->path,
238 'domain' => $this->domain,