78 * @since 5.2.0 Added `host_only` to the `$data` parameter. |
78 * @since 5.2.0 Added `host_only` to the `$data` parameter. |
79 * |
79 * |
80 * @param string|array $data { |
80 * @param string|array $data { |
81 * Raw cookie data as header string or data array. |
81 * Raw cookie data as header string or data array. |
82 * |
82 * |
83 * @type string $name Cookie name. |
83 * @type string $name Cookie name. |
84 * @type mixed $value Value. Should NOT already be urlencoded. |
84 * @type mixed $value Value. Should NOT already be urlencoded. |
85 * @type string|int $expires Optional. Unix timestamp or formatted date. Default null. |
85 * @type string|int|null $expires Optional. Unix timestamp or formatted date. Default null. |
86 * @type string $path Optional. Path. Default '/'. |
86 * @type string $path Optional. Path. Default '/'. |
87 * @type string $domain Optional. Domain. Default host of parsed $requested_url. |
87 * @type string $domain Optional. Domain. Default host of parsed $requested_url. |
88 * @type int $port Optional. Port. Default null. |
88 * @type int $port Optional. Port. Default null. |
89 * @type bool $host_only Optional. host-only storage flag. Default true. |
89 * @type bool $host_only Optional. host-only storage flag. Default true. |
90 * } |
90 * } |
91 * @param string $requested_url The URL which the cookie was set on, used for default $domain |
91 * @param string $requested_url The URL which the cookie was set on, used for default $domain |
92 * and $port values. |
92 * and $port values. |
93 */ |
93 */ |
94 public function __construct( $data, $requested_url = '' ) { |
94 public function __construct( $data, $requested_url = '' ) { |
95 if ( $requested_url ) { |
95 if ( $requested_url ) { |
96 $arrURL = @parse_url( $requested_url ); |
96 $arrURL = parse_url( $requested_url ); |
97 } |
97 } |
98 if ( isset( $arrURL['host'] ) ) { |
98 if ( isset( $arrURL['host'] ) ) { |
99 $this->domain = $arrURL['host']; |
99 $this->domain = $arrURL['host']; |
100 } |
100 } |
101 $this->path = isset( $arrURL['path'] ) ? $arrURL['path'] : '/'; |
101 $this->path = isset( $arrURL['path'] ) ? $arrURL['path'] : '/'; |
102 if ( '/' != substr( $this->path, -1 ) ) { |
102 if ( '/' !== substr( $this->path, -1 ) ) { |
103 $this->path = dirname( $this->path ) . '/'; |
103 $this->path = dirname( $this->path ) . '/'; |
104 } |
104 } |
105 |
105 |
106 if ( is_string( $data ) ) { |
106 if ( is_string( $data ) ) { |
107 // Assume it's a header string direct from a previous request. |
107 // Assume it's a header string direct from a previous request. |
125 continue; |
125 continue; |
126 } |
126 } |
127 |
127 |
128 list( $key, $val ) = strpos( $pair, '=' ) ? explode( '=', $pair ) : array( $pair, '' ); |
128 list( $key, $val ) = strpos( $pair, '=' ) ? explode( '=', $pair ) : array( $pair, '' ); |
129 $key = strtolower( trim( $key ) ); |
129 $key = strtolower( trim( $key ) ); |
130 if ( 'expires' == $key ) { |
130 if ( 'expires' === $key ) { |
131 $val = strtotime( $val ); |
131 $val = strtotime( $val ); |
132 } |
132 } |
133 $this->$key = $val; |
133 $this->$key = $val; |
134 } |
134 } |
135 } else { |
135 } else { |
172 return false; |
172 return false; |
173 } |
173 } |
174 |
174 |
175 // Get details on the URL we're thinking about sending to. |
175 // Get details on the URL we're thinking about sending to. |
176 $url = parse_url( $url ); |
176 $url = parse_url( $url ); |
177 $url['port'] = isset( $url['port'] ) ? $url['port'] : ( 'https' == $url['scheme'] ? 443 : 80 ); |
177 $url['port'] = isset( $url['port'] ) ? $url['port'] : ( 'https' === $url['scheme'] ? 443 : 80 ); |
178 $url['path'] = isset( $url['path'] ) ? $url['path'] : '/'; |
178 $url['path'] = isset( $url['path'] ) ? $url['path'] : '/'; |
179 |
179 |
180 // Values to use for comparison against the URL. |
180 // Values to use for comparison against the URL. |
181 $path = isset( $this->path ) ? $this->path : '/'; |
181 $path = isset( $this->path ) ? $this->path : '/'; |
182 $port = isset( $this->port ) ? $this->port : null; |
182 $port = isset( $this->port ) ? $this->port : null; |
184 if ( false === stripos( $domain, '.' ) ) { |
184 if ( false === stripos( $domain, '.' ) ) { |
185 $domain .= '.local'; |
185 $domain .= '.local'; |
186 } |
186 } |
187 |
187 |
188 // Host - very basic check that the request URL ends with the domain restriction (minus leading dot). |
188 // Host - very basic check that the request URL ends with the domain restriction (minus leading dot). |
189 $domain = substr( $domain, 0, 1 ) == '.' ? substr( $domain, 1 ) : $domain; |
189 $domain = ( '.' === substr( $domain, 0, 1 ) ) ? substr( $domain, 1 ) : $domain; |
190 if ( substr( $url['host'], -strlen( $domain ) ) != $domain ) { |
190 if ( substr( $url['host'], -strlen( $domain ) ) != $domain ) { |
191 return false; |
191 return false; |
192 } |
192 } |
193 |
193 |
194 // Port - supports "port-lists" in the format: "80,8000,8080". |
194 // Port - supports "port-lists" in the format: "80,8000,8080". |
195 if ( ! empty( $port ) && ! in_array( $url['port'], explode( ',', $port ) ) ) { |
195 if ( ! empty( $port ) && ! in_array( $url['port'], array_map( 'intval', explode( ',', $port ) ), true ) ) { |
196 return false; |
196 return false; |
197 } |
197 } |
198 |
198 |
199 // Path - request path must start with path restriction. |
199 // Path - request path must start with path restriction. |
200 if ( substr( $url['path'], 0, strlen( $path ) ) != $path ) { |
200 if ( substr( $url['path'], 0, strlen( $path ) ) != $path ) { |
232 * |
232 * |
233 * @since 2.8.0 |
233 * @since 2.8.0 |
234 * |
234 * |
235 * @return string |
235 * @return string |
236 */ |
236 */ |
237 public function getFullHeader() { |
237 public function getFullHeader() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
238 return 'Cookie: ' . $this->getHeaderValue(); |
238 return 'Cookie: ' . $this->getHeaderValue(); |
239 } |
239 } |
240 |
240 |
241 /** |
241 /** |
242 * Retrieves cookie attributes. |
242 * Retrieves cookie attributes. |
243 * |
243 * |
244 * @since 4.6.0 |
244 * @since 4.6.0 |
245 * |
245 * |
246 * @return array { |
246 * @return array { |
247 * List of attributes. |
247 * List of attributes. |
248 * |
248 * |
249 * @type string $expires When the cookie expires. |
249 * @type string|int|null $expires When the cookie expires. Unix timestamp or formatted date. |
250 * @type string $path Cookie URL path. |
250 * @type string $path Cookie URL path. |
251 * @type string $domain Cookie domain. |
251 * @type string $domain Cookie domain. |
252 * } |
252 * } |
253 */ |
253 */ |
254 public function get_attributes() { |
254 public function get_attributes() { |
255 return array( |
255 return array( |
256 'expires' => $this->expires, |
256 'expires' => $this->expires, |