PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/morganestes/wordpress-develop
PHP | 251 lines | 98 code | 27 blank | 126 comment | 27 complexity | 56fc7443861696377a6523ef683e01f1 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.
  37. *
  38. * @since 2.8.0
  39. * @var string
  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. * Sets up this cookie object.
  58. *
  59. * The parameter $data should be either an associative array containing the indices names below
  60. * or a header string detailing it.
  61. *
  62. * @since 2.8.0
  63. *
  64. * @param string|array $data {
  65. * Raw cookie data as header string or data array.
  66. *
  67. * @type string $name Cookie name.
  68. * @type mixed $value Value. Should NOT already be urlencoded.
  69. * @type string|int $expires Optional. Unix timestamp or formatted date. Default null.
  70. * @type string $path Optional. Path. Default '/'.
  71. * @type string $domain Optional. Domain. Default host of parsed $requested_url.
  72. * @type int $port Optional. Port. Default null.
  73. * }
  74. * @param string $requested_url The URL which the cookie was set on, used for default $domain
  75. * and $port values.
  76. */
  77. public function __construct( $data, $requested_url = '' ) {
  78. if ( $requested_url ) {
  79. $arrURL = @parse_url( $requested_url );
  80. }
  81. if ( isset( $arrURL['host'] ) ) {
  82. $this->domain = $arrURL['host'];
  83. }
  84. $this->path = isset( $arrURL['path'] ) ? $arrURL['path'] : '/';
  85. if ( '/' != substr( $this->path, -1 ) ) {
  86. $this->path = dirname( $this->path ) . '/';
  87. }
  88. if ( is_string( $data ) ) {
  89. // Assume it's a header string direct from a previous request.
  90. $pairs = explode( ';', $data );
  91. // Special handling for first pair; name=value. Also be careful of "=" in value.
  92. $name = trim( substr( $pairs[0], 0, strpos( $pairs[0], '=' ) ) );
  93. $value = substr( $pairs[0], strpos( $pairs[0], '=' ) + 1 );
  94. $this->name = $name;
  95. $this->value = urldecode( $value );
  96. // Removes name=value from items.
  97. array_shift( $pairs );
  98. // Set everything else as a property.
  99. foreach ( $pairs as $pair ) {
  100. $pair = rtrim( $pair );
  101. // Handle the cookie ending in ; which results in a empty final pair.
  102. if ( empty( $pair ) ) {
  103. continue;
  104. }
  105. list( $key, $val ) = strpos( $pair, '=' ) ? explode( '=', $pair ) : array( $pair, '' );
  106. $key = strtolower( trim( $key ) );
  107. if ( 'expires' == $key ) {
  108. $val = strtotime( $val );
  109. }
  110. $this->$key = $val;
  111. }
  112. } else {
  113. if ( ! isset( $data['name'] ) ) {
  114. return;
  115. }
  116. // Set properties based directly on parameters.
  117. foreach ( array( 'name', 'value', 'path', 'domain', 'port' ) as $field ) {
  118. if ( isset( $data[ $field ] ) ) {
  119. $this->$field = $data[ $field ];
  120. }
  121. }
  122. if ( isset( $data['expires'] ) ) {
  123. $this->expires = is_int( $data['expires'] ) ? $data['expires'] : strtotime( $data['expires'] );
  124. } else {
  125. $this->expires = null;
  126. }
  127. }
  128. }
  129. /**
  130. * Confirms that it's OK to send this cookie to the URL checked against.
  131. *
  132. * Decision is based on RFC 2109/2965, so look there for details on validity.
  133. *
  134. * @since 2.8.0
  135. *
  136. * @param string $url URL you intend to send this cookie to
  137. * @return bool true if allowed, false otherwise.
  138. */
  139. public function test( $url ) {
  140. if ( is_null( $this->name ) ) {
  141. return false;
  142. }
  143. // Expires - if expired then nothing else matters.
  144. if ( isset( $this->expires ) && time() > $this->expires ) {
  145. return false;
  146. }
  147. // Get details on the URL we're thinking about sending to.
  148. $url = parse_url( $url );
  149. $url['port'] = isset( $url['port'] ) ? $url['port'] : ( 'https' == $url['scheme'] ? 443 : 80 );
  150. $url['path'] = isset( $url['path'] ) ? $url['path'] : '/';
  151. // Values to use for comparison against the URL.
  152. $path = isset( $this->path ) ? $this->path : '/';
  153. $port = isset( $this->port ) ? $this->port : null;
  154. $domain = isset( $this->domain ) ? strtolower( $this->domain ) : strtolower( $url['host'] );
  155. if ( false === stripos( $domain, '.' ) ) {
  156. $domain .= '.local';
  157. }
  158. // Host - very basic check that the request URL ends with the domain restriction (minus leading dot).
  159. $domain = substr( $domain, 0, 1 ) == '.' ? substr( $domain, 1 ) : $domain;
  160. if ( substr( $url['host'], -strlen( $domain ) ) != $domain ) {
  161. return false;
  162. }
  163. // Port - supports "port-lists" in the format: "80,8000,8080".
  164. if ( ! empty( $port ) && ! in_array( $url['port'], explode( ',', $port ) ) ) {
  165. return false;
  166. }
  167. // Path - request path must start with path restriction.
  168. if ( substr( $url['path'], 0, strlen( $path ) ) != $path ) {
  169. return false;
  170. }
  171. return true;
  172. }
  173. /**
  174. * Convert cookie name and value back to header string.
  175. *
  176. * @since 2.8.0
  177. *
  178. * @return string Header encoded cookie name and value.
  179. */
  180. public function getHeaderValue() {
  181. if ( ! isset( $this->name ) || ! isset( $this->value ) ) {
  182. return '';
  183. }
  184. /**
  185. * Filters the header-encoded cookie value.
  186. *
  187. * @since 3.4.0
  188. *
  189. * @param string $value The cookie value.
  190. * @param string $name The cookie name.
  191. */
  192. return $this->name . '=' . apply_filters( 'wp_http_cookie_value', $this->value, $this->name );
  193. }
  194. /**
  195. * Retrieve cookie header for usage in the rest of the WordPress HTTP API.
  196. *
  197. * @since 2.8.0
  198. *
  199. * @return string
  200. */
  201. public function getFullHeader() {
  202. return 'Cookie: ' . $this->getHeaderValue();
  203. }
  204. /**
  205. * Retrieves cookie attributes.
  206. *
  207. * @since 4.6.0
  208. *
  209. * @return array {
  210. * List of attributes.
  211. *
  212. * @type string $expires When the cookie expires.
  213. * @type string $path Cookie URL path.
  214. * @type string $domain Cookie domain.
  215. * }
  216. */
  217. public function get_attributes() {
  218. return array(
  219. 'expires' => $this->expires,
  220. 'path' => $this->path,
  221. 'domain' => $this->domain,
  222. );
  223. }
  224. }