MediaWiki
REL1_23
|
00001 <?php 00024 class Cookie { 00025 protected $name; 00026 protected $value; 00027 protected $expires; 00028 protected $path; 00029 protected $domain; 00030 protected $isSessionKey = true; 00031 // TO IMPLEMENT protected $secure 00032 // TO IMPLEMENT? protected $maxAge (add onto expires) 00033 // TO IMPLEMENT? protected $version 00034 // TO IMPLEMENT? protected $comment 00035 00036 function __construct( $name, $value, $attr ) { 00037 $this->name = $name; 00038 $this->set( $value, $attr ); 00039 } 00040 00053 public function set( $value, $attr ) { 00054 $this->value = $value; 00055 00056 if ( isset( $attr['expires'] ) ) { 00057 $this->isSessionKey = false; 00058 $this->expires = strtotime( $attr['expires'] ); 00059 } 00060 00061 if ( isset( $attr['path'] ) ) { 00062 $this->path = $attr['path']; 00063 } else { 00064 $this->path = '/'; 00065 } 00066 00067 if ( isset( $attr['domain'] ) ) { 00068 if ( self::validateCookieDomain( $attr['domain'] ) ) { 00069 $this->domain = $attr['domain']; 00070 } 00071 } else { 00072 throw new MWException( 'You must specify a domain.' ); 00073 } 00074 } 00075 00092 public static function validateCookieDomain( $domain, $originDomain = null ) { 00093 // Don't allow a trailing dot 00094 if ( substr( $domain, -1 ) == '.' ) { 00095 return false; 00096 } 00097 00098 $dc = explode( ".", $domain ); 00099 00100 // Only allow full, valid IP addresses 00101 if ( preg_match( '/^[0-9.]+$/', $domain ) ) { 00102 if ( count( $dc ) != 4 ) { 00103 return false; 00104 } 00105 00106 if ( ip2long( $domain ) === false ) { 00107 return false; 00108 } 00109 00110 if ( $originDomain == null || $originDomain == $domain ) { 00111 return true; 00112 } 00113 00114 } 00115 00116 // Don't allow cookies for "co.uk" or "gov.uk", etc, but allow "supermarket.uk" 00117 if ( strrpos( $domain, "." ) - strlen( $domain ) == -3 ) { 00118 if ( ( count( $dc ) == 2 && strlen( $dc[0] ) <= 2 ) 00119 || ( count( $dc ) == 3 && strlen( $dc[0] ) == "" && strlen( $dc[1] ) <= 2 ) ) { 00120 return false; 00121 } 00122 if ( ( count( $dc ) == 2 || ( count( $dc ) == 3 && $dc[0] == '' ) ) 00123 && preg_match( '/(com|net|org|gov|edu)\...$/', $domain ) ) { 00124 return false; 00125 } 00126 } 00127 00128 if ( $originDomain != null ) { 00129 if ( substr( $domain, 0, 1 ) != '.' && $domain != $originDomain ) { 00130 return false; 00131 } 00132 00133 if ( substr( $domain, 0, 1 ) == '.' 00134 && substr_compare( 00135 $originDomain, 00136 $domain, 00137 -strlen( $domain ), 00138 strlen( $domain ), 00139 true 00140 ) != 0 00141 ) { 00142 return false; 00143 } 00144 } 00145 00146 return true; 00147 } 00148 00156 public function serializeToHttpRequest( $path, $domain ) { 00157 $ret = ''; 00158 00159 if ( $this->canServeDomain( $domain ) 00160 && $this->canServePath( $path ) 00161 && $this->isUnExpired() ) { 00162 $ret = $this->name . '=' . $this->value; 00163 } 00164 00165 return $ret; 00166 } 00167 00172 protected function canServeDomain( $domain ) { 00173 if ( $domain == $this->domain 00174 || ( strlen( $domain ) > strlen( $this->domain ) 00175 && substr( $this->domain, 0, 1 ) == '.' 00176 && substr_compare( 00177 $domain, 00178 $this->domain, 00179 -strlen( $this->domain ), 00180 strlen( $this->domain ), 00181 true 00182 ) == 0 00183 ) 00184 ) { 00185 return true; 00186 } 00187 00188 return false; 00189 } 00190 00195 protected function canServePath( $path ) { 00196 return ( $this->path && substr_compare( $this->path, $path, 0, strlen( $this->path ) ) == 0 ); 00197 } 00198 00202 protected function isUnExpired() { 00203 return $this->isSessionKey || $this->expires > time(); 00204 } 00205 } 00206 00207 class CookieJar { 00208 private $cookie = array(); 00209 00214 public function setCookie( $name, $value, $attr ) { 00215 /* cookies: case insensitive, so this should work. 00216 * We'll still send the cookies back in the same case we got them, though. 00217 */ 00218 $index = strtoupper( $name ); 00219 00220 if ( isset( $this->cookie[$index] ) ) { 00221 $this->cookie[$index]->set( $value, $attr ); 00222 } else { 00223 $this->cookie[$index] = new Cookie( $name, $value, $attr ); 00224 } 00225 } 00226 00231 public function serializeToHttpRequest( $path, $domain ) { 00232 $cookies = array(); 00233 00234 foreach ( $this->cookie as $c ) { 00235 $serialized = $c->serializeToHttpRequest( $path, $domain ); 00236 00237 if ( $serialized ) { 00238 $cookies[] = $serialized; 00239 } 00240 } 00241 00242 return implode( '; ', $cookies ); 00243 } 00244 00252 public function parseCookieResponseHeader( $cookie, $domain ) { 00253 $len = strlen( 'Set-Cookie:' ); 00254 00255 if ( substr_compare( 'Set-Cookie:', $cookie, 0, $len, true ) === 0 ) { 00256 $cookie = substr( $cookie, $len ); 00257 } 00258 00259 $bit = array_map( 'trim', explode( ';', $cookie ) ); 00260 00261 if ( count( $bit ) >= 1 ) { 00262 list( $name, $value ) = explode( '=', array_shift( $bit ), 2 ); 00263 $attr = array(); 00264 00265 foreach ( $bit as $piece ) { 00266 $parts = explode( '=', $piece ); 00267 if ( count( $parts ) > 1 ) { 00268 $attr[strtolower( $parts[0] )] = $parts[1]; 00269 } else { 00270 $attr[strtolower( $parts[0] )] = true; 00271 } 00272 } 00273 00274 if ( !isset( $attr['domain'] ) ) { 00275 $attr['domain'] = $domain; 00276 } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) { 00277 return null; 00278 } 00279 00280 $this->setCookie( $name, $value, $attr ); 00281 } 00282 } 00283 }