X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/libs/CookieJar.php diff --git a/includes/libs/CookieJar.php b/includes/libs/CookieJar.php new file mode 100644 index 00000000..8f5700ab --- /dev/null +++ b/includes/libs/CookieJar.php @@ -0,0 +1,107 @@ +cookie[$index] ) ) { + $this->cookie[$index]->set( $value, $attr ); + } else { + $this->cookie[$index] = new Cookie( $name, $value, $attr ); + } + } + + /** + * @see Cookie::serializeToHttpRequest + * @param string $path + * @param string $domain + * @return string + */ + public function serializeToHttpRequest( $path, $domain ) { + $cookies = []; + + foreach ( $this->cookie as $c ) { + $serialized = $c->serializeToHttpRequest( $path, $domain ); + + if ( $serialized ) { + $cookies[] = $serialized; + } + } + + return implode( '; ', $cookies ); + } + + /** + * Parse the content of an Set-Cookie HTTP Response header. + * + * @param string $cookie + * @param string $domain Cookie's domain + * @return null + */ + public function parseCookieResponseHeader( $cookie, $domain ) { + $len = strlen( 'Set-Cookie:' ); + + if ( substr_compare( 'Set-Cookie:', $cookie, 0, $len, true ) === 0 ) { + $cookie = substr( $cookie, $len ); + } + + $bit = array_map( 'trim', explode( ';', $cookie ) ); + + if ( count( $bit ) >= 1 ) { + list( $name, $value ) = explode( '=', array_shift( $bit ), 2 ); + $attr = []; + + foreach ( $bit as $piece ) { + $parts = explode( '=', $piece ); + if ( count( $parts ) > 1 ) { + $attr[strtolower( $parts[0] )] = $parts[1]; + } else { + $attr[strtolower( $parts[0] )] = true; + } + } + + if ( !isset( $attr['domain'] ) ) { + $attr['domain'] = $domain; + } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) { + return null; + } + + $this->setCookie( $name, $value, $attr ); + } + } +}