<?php
/**
- * @defgroup HTTP HTTP
+ * Cookie for HTTP requests.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup HTTP
*/
class Cookie {
if ( isset( $attr['path'] ) ) {
$this->path = $attr['path'];
} else {
- $this->path = "/";
+ $this->path = '/';
}
if ( isset( $attr['domain'] ) ) {
$this->domain = $attr['domain'];
}
} else {
- throw new MWException( "You must specify a domain." );
+ throw new MWException( 'You must specify a domain.' );
}
}
* A better method might be to use a blacklist like
* http://publicsuffix.org/
*
+ * @fixme fails to detect 3-letter top-level domains
+ * @fixme fails to detect 2-letter top-level domains for single-domain use (probably not a big problem in practice, but there are test cases)
+ *
* @param $domain String: the domain to validate
* @param $originDomain String: (optional) the domain the cookie originates from
* @return Boolean
*/
public static function validateCookieDomain( $domain, $originDomain = null ) {
// Don't allow a trailing dot
- if ( substr( $domain, -1 ) == "." ) {
+ if ( substr( $domain, -1 ) == '.' ) {
return false;
}
|| ( count( $dc ) == 3 && strlen( $dc[0] ) == "" && strlen( $dc[1] ) <= 2 ) ) {
return false;
}
- if ( ( count( $dc ) == 2 || ( count( $dc ) == 3 && $dc[0] == "" ) )
+ if ( ( count( $dc ) == 2 || ( count( $dc ) == 3 && $dc[0] == '' ) )
&& preg_match( '/(com|net|org|gov|edu)\...$/', $domain ) ) {
return false;
}
}
if ( $originDomain != null ) {
- if ( substr( $domain, 0, 1 ) != "." && $domain != $originDomain ) {
+ if ( substr( $domain, 0, 1 ) != '.' && $domain != $originDomain ) {
return false;
}
- if ( substr( $domain, 0, 1 ) == "."
+ if ( substr( $domain, 0, 1 ) == '.'
&& substr_compare( $originDomain, $domain, -strlen( $domain ),
- strlen( $domain ), TRUE ) != 0 ) {
+ strlen( $domain ), true ) != 0 ) {
return false;
}
}
* @return String
*/
public function serializeToHttpRequest( $path, $domain ) {
- $ret = "";
+ $ret = '';
if ( $this->canServeDomain( $domain )
&& $this->canServePath( $path )
&& $this->isUnExpired() ) {
- $ret = $this->name . "=" . $this->value;
+ $ret = $this->name . '=' . $this->value;
}
return $ret;
}
+ /**
+ * @param $domain
+ * @return bool
+ */
protected function canServeDomain( $domain ) {
if ( $domain == $this->domain
|| ( strlen( $domain ) > strlen( $this->domain )
- && substr( $this->domain, 0, 1 ) == "."
+ && substr( $this->domain, 0, 1 ) == '.'
&& substr_compare( $domain, $this->domain, -strlen( $this->domain ),
- strlen( $this->domain ), TRUE ) == 0 ) ) {
+ strlen( $this->domain ), true ) == 0 ) ) {
return true;
}
return false;
}
+ /**
+ * @param $path
+ * @return bool
+ */
protected function canServePath( $path ) {
- if ( $this->path && substr_compare( $this->path, $path, 0, strlen( $this->path ) ) == 0 ) {
- return true;
- }
-
- return false;
+ return ( $this->path && substr_compare( $this->path, $path, 0, strlen( $this->path ) ) == 0 );
}
+ /**
+ * @return bool
+ */
protected function isUnExpired() {
- if ( $this->isSessionKey || $this->expires > time() ) {
- return true;
- }
-
- return false;
+ return $this->isSessionKey || $this->expires > time();
}
}
/**
* @see Cookie::serializeToHttpRequest
+ * @return string
*/
public function serializeToHttpRequest( $path, $domain ) {
$cookies = array();
}
}
- return implode( "; ", $cookies );
+ return implode( '; ', $cookies );
}
/**
*
* @param $cookie String
* @param $domain String: cookie's domain
+ * @return null
*/
public function parseCookieResponseHeader ( $cookie, $domain ) {
- $len = strlen( "Set-Cookie:" );
+ $len = strlen( 'Set-Cookie:' );
- if ( substr_compare( "Set-Cookie:", $cookie, 0, $len, TRUE ) === 0 ) {
+ if ( substr_compare( 'Set-Cookie:', $cookie, 0, $len, true ) === 0 ) {
$cookie = substr( $cookie, $len );
}
- $bit = array_map( 'trim', explode( ";", $cookie ) );
+ $bit = array_map( 'trim', explode( ';', $cookie ) );
if ( count( $bit ) >= 1 ) {
- list( $name, $value ) = explode( "=", array_shift( $bit ), 2 );
+ list( $name, $value ) = explode( '=', array_shift( $bit ), 2 );
$attr = array();
foreach ( $bit as $piece ) {
- $parts = explode( "=", $piece );
+ $parts = explode( '=', $piece );
if ( count( $parts ) > 1 ) {
$attr[strtolower( $parts[0] )] = $parts[1];
} else {