25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

242 lines
6.4 KiB

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