/wp-includes/class-wp-http-cookie.php

https://github.com/livinglab/openlab · PHP · 261 lines · 99 code · 28 blank · 134 comment · 23 complexity · 603d593db7c83380d6c9702ab31592b8 MD5 · raw file

  1. <?php
  2. /**
  3. * HTTP API: WP_Http_Cookie class
  4. *
  5. * @package WordPress
  6. * @subpackage HTTP
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to encapsulate a single cookie object for internal use.
  11. *
  12. * Returned cookies are represented using this class, and when cookies are set, if they are not
  13. * already a WP_Http_Cookie() object, then they are turned into one.
  14. *
  15. * @todo The WordPress convention is to use underscores instead of camelCase for function and method
  16. * names. Need to switch to use underscores instead for the methods.
  17. *
  18. * @since 2.8.0
  19. */
  20. class WP_Http_Cookie {
  21. /**
  22. * Cookie name.
  23. *
  24. * @since 2.8.0
  25. * @var string
  26. */
  27. public $name;
  28. /**
  29. * Cookie value.
  30. *
  31. * @since 2.8.0
  32. * @var string
  33. */
  34. public $value;
  35. /**
  36. * When the cookie expires. Unix timestamp or formatted date.
  37. *
  38. * @since 2.8.0
  39. * @var string|int|null
  40. */
  41. public $expires;
  42. /**
  43. * Cookie URL path.
  44. *
  45. * @since 2.8.0
  46. * @var string
  47. */
  48. public $path;
  49. /**
  50. * Cookie Domain.
  51. *
  52. * @since 2.8.0
  53. * @var string
  54. */
  55. public $domain;
  56. /**
  57. * host-only flag.
  58. *
  59. * @since 5.2.0
  60. * @var bool
  61. */
  62. public $host_only;
  63. /**
  64. * Sets up this cookie object.
  65. *
  66. * The parameter $data should be either an associative array containing the indices names below
  67. * or a header string detailing it.
  68. *
  69. * @since 2.8.0
  70. * @since 5.2.0 Added `host_only` to the `$data` parameter.
  71. *
  72. * @param string|array $data {
  73. * Raw cookie data as header string or data array.
  74. *
  75. * @type string $name Cookie name.
  76. * @type mixed $value Value. Should NOT already be urlencoded.
  77. * @type string|int|null $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.
  81. * @type bool $host_only Optional. host-only storage flag. Default true.
  82. * }
  83. * @param string $requested_url The URL which the cookie was set on, used for default $domain
  84. * and $port values.
  85. */
  86. public function __construct( $data, $requested_url = '' ) {
  87. if ( $requested_url ) {
  88. $arrURL = parse_url( $requested_url );
  89. }
  90. if ( isset( $arrURL['host'] ) ) {
  91. $this->domain = $arrURL['host'];
  92. }
  93. $this->path = isset( $arrURL['path'] ) ? $arrURL['path'] : '/';
  94. if ( '/' !== substr( $this->path, -1 ) ) {
  95. $this->path = dirname( $this->path ) . '/';
  96. }
  97. if ( is_string( $data ) ) {
  98. // Assume it's a header string direct from a previous request.
  99. $pairs = explode( ';', $data );
  100. // Special handling for first pair; name=value. Also be careful of "=" in value.
  101. $name = trim( substr( $pairs[0], 0, strpos( $pairs[0], '=' ) ) );
  102. $value = substr( $pairs[0], strpos( $pairs[0], '=' ) + 1 );
  103. $this->name = $name;
  104. $this->value = urldecode( $value );
  105. // Removes name=value from items.
  106. array_shift( $pairs );
  107. // Set everything else as a property.
  108. foreach ( $pairs as $pair ) {
  109. $pair = rtrim( $pair );
  110. // Handle the cookie ending in ; which results in a empty final pair.
  111. if ( empty( $pair ) ) {
  112. continue;
  113. }
  114. list( $key, $val ) = strpos( $pair, '=' ) ? explode( '=', $pair ) : array( $pair, '' );
  115. $key = strtolower( trim( $key ) );
  116. if ( 'expires' === $key ) {
  117. $val = strtotime( $val );
  118. }
  119. $this->$key = $val;
  120. }
  121. } else {
  122. if ( ! isset( $data['name'] ) ) {
  123. return;
  124. }
  125. // Set properties based directly on parameters.
  126. foreach ( array( 'name', 'value', 'path', 'domain', 'port', 'host_only' ) as $field ) {
  127. if ( isset( $data[ $field ] ) ) {
  128. $this->$field = $data[ $field ];
  129. }
  130. }
  131. if ( isset( $data['expires'] ) ) {
  132. $this->expires = is_int( $data['expires'] ) ? $data['expires'] : strtotime( $data['expires'] );
  133. } else {
  134. $this->expires = null;
  135. }
  136. }
  137. }
  138. /**
  139. * Confirms that it's OK to send this cookie to the URL checked against.
  140. *
  141. * Decision is based on RFC 2109/2965, so look there for details on validity.
  142. *
  143. * @since 2.8.0
  144. *
  145. * @param string $url URL you intend to send this cookie to
  146. * @return bool true if allowed, false otherwise.
  147. */
  148. public function test( $url ) {
  149. if ( is_null( $this->name ) ) {
  150. return false;
  151. }
  152. // Expires - if expired then nothing else matters.
  153. if ( isset( $this->expires ) && time() > $this->expires ) {
  154. return false;
  155. }
  156. // Get details on the URL we're thinking about sending to.
  157. $url = parse_url( $url );
  158. $url['port'] = isset( $url['port'] ) ? $url['port'] : ( 'https' === $url['scheme'] ? 443 : 80 );
  159. $url['path'] = isset( $url['path'] ) ? $url['path'] : '/';
  160. // Values to use for comparison against the URL.
  161. $path = isset( $this->path ) ? $this->path : '/';
  162. $port = isset( $this->port ) ? $this->port : null;
  163. $domain = isset( $this->domain ) ? strtolower( $this->domain ) : strtolower( $url['host'] );
  164. if ( false === stripos( $domain, '.' ) ) {
  165. $domain .= '.local';
  166. }
  167. // Host - very basic check that the request URL ends with the domain restriction (minus leading dot).
  168. $domain = ( '.' === substr( $domain, 0, 1 ) ) ? substr( $domain, 1 ) : $domain;
  169. if ( substr( $url['host'], -strlen( $domain ) ) != $domain ) {
  170. return false;
  171. }
  172. // Port - supports "port-lists" in the format: "80,8000,8080".
  173. if ( ! empty( $port ) && ! in_array( $url['port'], array_map( 'intval', explode( ',', $port ) ), true ) ) {
  174. return false;
  175. }
  176. // Path - request path must start with path restriction.
  177. if ( substr( $url['path'], 0, strlen( $path ) ) != $path ) {
  178. return false;
  179. }
  180. return true;
  181. }
  182. /**
  183. * Convert cookie name and value back to header string.
  184. *
  185. * @since 2.8.0
  186. *
  187. * @return string Header encoded cookie name and value.
  188. */
  189. public function getHeaderValue() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
  190. if ( ! isset( $this->name ) || ! isset( $this->value ) ) {
  191. return '';
  192. }
  193. /**
  194. * Filters the header-encoded cookie value.
  195. *
  196. * @since 3.4.0
  197. *
  198. * @param string $value The cookie value.
  199. * @param string $name The cookie name.
  200. */
  201. return $this->name . '=' . apply_filters( 'wp_http_cookie_value', $this->value, $this->name );
  202. }
  203. /**
  204. * Retrieve cookie header for usage in the rest of the WordPress HTTP API.
  205. *
  206. * @since 2.8.0
  207. *
  208. * @return string
  209. */
  210. public function getFullHeader() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
  211. return 'Cookie: ' . $this->getHeaderValue();
  212. }
  213. /**
  214. * Retrieves cookie attributes.
  215. *
  216. * @since 4.6.0
  217. *
  218. * @return array {
  219. * List of attributes.
  220. *
  221. * @type string|int|null $expires When the cookie expires. Unix timestamp or formatted date.
  222. * @type string $path Cookie URL path.
  223. * @type string $domain Cookie domain.
  224. * }
  225. */
  226. public function get_attributes() {
  227. return array(
  228. 'expires' => $this->expires,
  229. 'path' => $this->path,
  230. 'domain' => $this->domain,
  231. );
  232. }
  233. }