MediaWiki
REL1_24
|
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 $dc = explode( ".", $domain ); 00094 00095 // Don't allow a trailing dot or addresses without a or just a leading dot 00096 if ( substr( $domain, -1 ) == '.' || 00097 count( $dc ) <= 1 || 00098 count( $dc ) == 2 && $dc[0] === '' 00099 ) { 00100 return false; 00101 } 00102 00103 // Only allow full, valid IP addresses 00104 if ( preg_match( '/^[0-9.]+$/', $domain ) ) { 00105 if ( count( $dc ) != 4 ) { 00106 return false; 00107 } 00108 00109 if ( ip2long( $domain ) === false ) { 00110 return false; 00111 } 00112 00113 if ( $originDomain == null || $originDomain == $domain ) { 00114 return true; 00115 } 00116 00117 } 00118 00119 // Don't allow cookies for "co.uk" or "gov.uk", etc, but allow "supermarket.uk" 00120 if ( strrpos( $domain, "." ) - strlen( $domain ) == -3 ) { 00121 if ( ( count( $dc ) == 2 && strlen( $dc[0] ) <= 2 ) 00122 || ( count( $dc ) == 3 && strlen( $dc[0] ) == "" && strlen( $dc[1] ) <= 2 ) ) { 00123 return false; 00124 } 00125 if ( ( count( $dc ) == 2 || ( count( $dc ) == 3 && $dc[0] == '' ) ) 00126 && preg_match( '/(com|net|org|gov|edu)\...$/', $domain ) ) { 00127 return false; 00128 } 00129 } 00130 00131 if ( $originDomain != null ) { 00132 if ( substr( $domain, 0, 1 ) != '.' && $domain != $originDomain ) { 00133 return false; 00134 } 00135 00136 if ( substr( $domain, 0, 1 ) == '.' 00137 && substr_compare( 00138 $originDomain, 00139 $domain, 00140 -strlen( $domain ), 00141 strlen( $domain ), 00142 true 00143 ) != 0 00144 ) { 00145 return false; 00146 } 00147 } 00148 00149 return true; 00150 } 00151 00159 public function serializeToHttpRequest( $path, $domain ) { 00160 $ret = ''; 00161 00162 if ( $this->canServeDomain( $domain ) 00163 && $this->canServePath( $path ) 00164 && $this->isUnExpired() ) { 00165 $ret = $this->name . '=' . $this->value; 00166 } 00167 00168 return $ret; 00169 } 00170 00175 protected function canServeDomain( $domain ) { 00176 if ( $domain == $this->domain 00177 || ( strlen( $domain ) > strlen( $this->domain ) 00178 && substr( $this->domain, 0, 1 ) == '.' 00179 && substr_compare( 00180 $domain, 00181 $this->domain, 00182 -strlen( $this->domain ), 00183 strlen( $this->domain ), 00184 true 00185 ) == 0 00186 ) 00187 ) { 00188 return true; 00189 } 00190 00191 return false; 00192 } 00193 00198 protected function canServePath( $path ) { 00199 return ( $this->path && substr_compare( $this->path, $path, 0, strlen( $this->path ) ) == 0 ); 00200 } 00201 00205 protected function isUnExpired() { 00206 return $this->isSessionKey || $this->expires > time(); 00207 } 00208 } 00209 00210 class CookieJar { 00211 private $cookie = array(); 00212 00220 public function setCookie( $name, $value, $attr ) { 00221 /* cookies: case insensitive, so this should work. 00222 * We'll still send the cookies back in the same case we got them, though. 00223 */ 00224 $index = strtoupper( $name ); 00225 00226 if ( isset( $this->cookie[$index] ) ) { 00227 $this->cookie[$index]->set( $value, $attr ); 00228 } else { 00229 $this->cookie[$index] = new Cookie( $name, $value, $attr ); 00230 } 00231 } 00232 00239 public function serializeToHttpRequest( $path, $domain ) { 00240 $cookies = array(); 00241 00242 foreach ( $this->cookie as $c ) { 00243 $serialized = $c->serializeToHttpRequest( $path, $domain ); 00244 00245 if ( $serialized ) { 00246 $cookies[] = $serialized; 00247 } 00248 } 00249 00250 return implode( '; ', $cookies ); 00251 } 00252 00260 public function parseCookieResponseHeader( $cookie, $domain ) { 00261 $len = strlen( 'Set-Cookie:' ); 00262 00263 if ( substr_compare( 'Set-Cookie:', $cookie, 0, $len, true ) === 0 ) { 00264 $cookie = substr( $cookie, $len ); 00265 } 00266 00267 $bit = array_map( 'trim', explode( ';', $cookie ) ); 00268 00269 if ( count( $bit ) >= 1 ) { 00270 list( $name, $value ) = explode( '=', array_shift( $bit ), 2 ); 00271 $attr = array(); 00272 00273 foreach ( $bit as $piece ) { 00274 $parts = explode( '=', $piece ); 00275 if ( count( $parts ) > 1 ) { 00276 $attr[strtolower( $parts[0] )] = $parts[1]; 00277 } else { 00278 $attr[strtolower( $parts[0] )] = true; 00279 } 00280 } 00281 00282 if ( !isset( $attr['domain'] ) ) { 00283 $attr['domain'] = $domain; 00284 } elseif ( !Cookie::validateCookieDomain( $attr['domain'], $domain ) ) { 00285 return null; 00286 } 00287 00288 $this->setCookie( $name, $value, $attr ); 00289 } 00290 } 00291 }