WP_Http_Cookie

Core class used to encapsulate a single cookie object for internal use.

Defined (1)

The class is defined in the following location(s).

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