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).

/bp-forums/bbpress/bb-includes/backpress/class.wp-http.php  
  1. class WP_Http_Cookie { 
  2.  
  3. /** 
  4. * Cookie name. 
  5. * @since 2.8.0 
  6. * @var string 
  7. */ 
  8. var $name; 
  9.  
  10. /** 
  11. * Cookie value. 
  12. * @since 2.8.0 
  13. * @var string 
  14. */ 
  15. var $value; 
  16.  
  17. /** 
  18. * When the cookie expires. 
  19. * @since 2.8.0 
  20. * @var string 
  21. */ 
  22. var $expires; 
  23.  
  24. /** 
  25. * Cookie URL path. 
  26. * @since 2.8.0 
  27. * @var string 
  28. */ 
  29. var $path; 
  30.  
  31. /** 
  32. * Cookie Domain. 
  33. * @since 2.8.0 
  34. * @var string 
  35. */ 
  36. var $domain; 
  37.  
  38. /** 
  39. * PHP4 style Constructor - Calls PHP5 Style Constructor. 
  40. * @access public 
  41. * @since 2.8.0 
  42. * @param string|array $data Raw cookie data. 
  43. */ 
  44. function WP_Http_Cookie( $data ) { 
  45. $this->__construct( $data ); 
  46.  
  47. /** 
  48. * Sets up this cookie object. 
  49. * The parameter $data should be either an associative array containing the indices names below 
  50. * or a header string detailing it. 
  51. * If it's an array, it should include the following elements: 
  52. * <ol> 
  53. * <li>Name</li> 
  54. * <li>Value - should NOT be urlencoded already.</li> 
  55. * <li>Expires - (optional) String or int (UNIX timestamp).</li> 
  56. * <li>Path (optional)</li> 
  57. * <li>Domain (optional)</li> 
  58. * </ol> 
  59. * @access public 
  60. * @since 2.8.0 
  61. * @param string|array $data Raw cookie data. 
  62. */ 
  63. function __construct( $data ) { 
  64. if ( is_string( $data ) ) { 
  65. // Assume it's a header string direct from a previous request 
  66. $pairs = explode( ';', $data ); 
  67.  
  68. // Special handling for first pair; name=value. Also be careful of "=" in value 
  69. $name = trim( substr( $pairs[0], 0, strpos( $pairs[0], '=' ) ) ); 
  70. $value = substr( $pairs[0], strpos( $pairs[0], '=' ) + 1 ); 
  71. $this->name = $name; 
  72. $this->value = urldecode( $value ); 
  73. array_shift( $pairs ); //Removes name=value from items. 
  74.  
  75. // Set everything else as a property 
  76. foreach ( $pairs as $pair ) { 
  77. if ( empty($pair) ) //Handles the cookie ending in ; which results in a empty final pair 
  78. continue; 
  79.  
  80. list( $key, $val ) = explode( '=', $pair ); 
  81. $key = strtolower( trim( $key ) ); 
  82. if ( 'expires' == $key ) 
  83. $val = strtotime( $val ); 
  84. $this->$key = $val; 
  85. } else { 
  86. if ( !isset( $data['name'] ) ) 
  87. return false; 
  88.  
  89. // Set properties based directly on parameters 
  90. $this->name = $data['name']; 
  91. $this->value = isset( $data['value'] ) ? $data['value'] : ''; 
  92. $this->path = isset( $data['path'] ) ? $data['path'] : ''; 
  93. $this->domain = isset( $data['domain'] ) ? $data['domain'] : ''; 
  94.  
  95. if ( isset( $data['expires'] ) ) 
  96. $this->expires = is_int( $data['expires'] ) ? $data['expires'] : strtotime( $data['expires'] ); 
  97. else 
  98. $this->expires = null; 
  99.  
  100. /** 
  101. * Confirms that it's OK to send this cookie to the URL checked against. 
  102. * Decision is based on RFC 2109/2965, so look there for details on validity. 
  103. * @access public 
  104. * @since 2.8.0 
  105. * @param string $url URL you intend to send this cookie to 
  106. * @return boolean TRUE if allowed, FALSE otherwise. 
  107. */ 
  108. function test( $url ) { 
  109. // Expires - if expired then nothing else matters 
  110. if ( time() > $this->expires ) 
  111. return false; 
  112.  
  113. // Get details on the URL we're thinking about sending to 
  114. $url = parse_url( $url ); 
  115. $url['port'] = isset( $url['port'] ) ? $url['port'] : 80; 
  116. $url['path'] = isset( $url['path'] ) ? $url['path'] : '/'; 
  117.  
  118. // Values to use for comparison against the URL 
  119. $path = isset( $this->path ) ? $this->path : '/'; 
  120. $port = isset( $this->port ) ? $this->port : 80; 
  121. $domain = isset( $this->domain ) ? strtolower( $this->domain ) : strtolower( $url['host'] ); 
  122. if ( false === stripos( $domain, '.' ) ) 
  123. $domain .= '.local'; 
  124.  
  125. // Host - very basic check that the request URL ends with the domain restriction (minus leading dot) 
  126. $domain = substr( $domain, 0, 1 ) == '.' ? substr( $domain, 1 ) : $domain; 
  127. if ( substr( $url['host'], -strlen( $domain ) ) != $domain ) 
  128. return false; 
  129.  
  130. // Port - supports "port-lists" in the format: "80, 8000, 8080" 
  131. if ( !in_array( $url['port'], explode( ', ', $port) ) ) 
  132. return false; 
  133.  
  134. // Path - request path must start with path restriction 
  135. if ( substr( $url['path'], 0, strlen( $path ) ) != $path ) 
  136. return false; 
  137.  
  138. return true; 
  139.  
  140. /** 
  141. * Convert cookie name and value back to header string. 
  142. * @access public 
  143. * @since 2.8.0 
  144. * @return string Header encoded cookie name and value. 
  145. */ 
  146. function getHeaderValue() { 
  147. if ( empty( $this->name ) || empty( $this->value ) ) 
  148. return ''; 
  149.  
  150. return $this->name . '=' . urlencode( $this->value ); 
  151.  
  152. /** 
  153. * Retrieve cookie header for usage in the rest of the WordPress HTTP API. 
  154. * @access public 
  155. * @since 2.8.0 
  156. * @return string 
  157. */ 
  158. function getFullHeader() { 
  159. return 'Cookie: ' . $this->getHeaderValue();