CookieJar.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup HTTP
  20. */
  21. /**
  22. * Cookie jar to use with MWHttpRequest. Does not handle cookie unsetting.
  23. */
  24. class CookieJar {
  25. /** @var Cookie[] */
  26. private $cookie = [];
  27. /**
  28. * Set a cookie in the cookie jar. Make sure only one cookie per-name exists.
  29. * @see Cookie::set()
  30. * @param string $name
  31. * @param string $value
  32. * @param array $attr
  33. */
  34. public function setCookie( $name, $value, $attr ) {
  35. /* cookies: case insensitive, so this should work.
  36. * We'll still send the cookies back in the same case we got them, though.
  37. */
  38. $index = strtoupper( $name );
  39. if ( isset( $this->cookie[$index] ) ) {
  40. $this->cookie[$index]->set( $value, $attr );
  41. } else {
  42. $this->cookie[$index] = new Cookie( $name, $value, $attr );
  43. }
  44. }
  45. /**
  46. * @see Cookie::serializeToHttpRequest
  47. * @param string $path
  48. * @param string $domain
  49. * @return string
  50. */
  51. public function serializeToHttpRequest( $path, $domain ) {
  52. $cookies = [];
  53. foreach ( $this->cookie as $c ) {
  54. $serialized = $c->serializeToHttpRequest( $path, $domain );
  55. if ( $serialized ) {
  56. $cookies[] = $serialized;
  57. }
  58. }
  59. return implode( '; ', $cookies );
  60. }
  61. /**
  62. * Parse the content of an Set-Cookie HTTP Response header.
  63. *
  64. * @param string $cookie
  65. * @param string $domain Cookie's domain
  66. * @return null
  67. */
  68. public function parseCookieResponseHeader( $cookie, $domain ) {
  69. $len = strlen( 'Set-Cookie:' );
  70. if ( substr_compare( 'Set-Cookie:', $cookie, 0, $len, true ) === 0 ) {
  71. $cookie = substr( $cookie, $len );
  72. }
  73. $bit = array_map( 'trim', explode( ';', $cookie ) );
  74. if ( count( $bit ) >= 1 ) {
  75. list( $name, $value ) = explode( '=', array_shift( $bit ), 2 );
  76. $attr = [];
  77. foreach ( $bit as $piece ) {
  78. $parts = explode( '=', $piece );
  79. if ( count( $parts ) > 1 ) {
  80. $attr[strtolower( $parts[0] )] = $parts[1];
  81. } else {
  82. $attr[strtolower( $parts[0] )] = true;
  83. }
  84. }
  85. if ( !isset( $attr['domain'] ) ) {
  86. $attr['domain'] = $domain;
  87. } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) {
  88. return null;
  89. }
  90. $this->setCookie( $name, $value, $attr );
  91. }
  92. }
  93. }