Make WordPress Core

Ticket #44369: 44369.diff

File 44369.diff, 14.8 KB (added by subrataemfluence, 7 years ago)
  • Cookie.php

    diff --git src/wp-includes/Requests/Cookie.php b/Cookie.php
    index 00fbbc7..c01eb59 100755
    old new class Requests_Cookie { 
    6464         * @param string $value
    6565         * @param array|Requests_Utility_CaseInsensitiveDictionary $attributes Associative array of attribute data
    6666         */
    67         public function __construct($name, $value, $attributes = array(), $flags = array(), $reference_time = null) {
    68                 $this->name = $name;
    69                 $this->value = $value;
     67        public function __construct( $name, $value, $attributes = array(), $flags = array(), $reference_time = null ) {
     68                $this->name       = $name;
     69                $this->value      = $value;
    7070                $this->attributes = $attributes;
    71                 $default_flags = array(
    72                         'creation' => time(),
     71                $default_flags    = array(
     72                        'creation'    => time(),
    7373                        'last-access' => time(),
    74                         'persistent' => false,
    75                         'host-only' => true,
     74                        'persistent'  => false,
     75                        'host-only'   => true,
    7676                );
    77                 $this->flags = array_merge($default_flags, $flags);
     77                $this->flags      = array_merge( $default_flags, $flags );
    7878
    7979                $this->reference_time = time();
    80                 if ($reference_time !== null) {
     80                if ( null !== $reference_time ) {
    8181                        $this->reference_time = $reference_time;
    8282                }
    8383
    class Requests_Cookie { 
    9797                // If a cookie has both the Max-Age and the Expires attribute, the Max-
    9898                // Age attribute has precedence and controls the expiration date of the
    9999                // cookie.
    100                 if (isset($this->attributes['max-age'])) {
     100                if ( isset( $this->attributes['max-age'] ) ) {
    101101                        $max_age = $this->attributes['max-age'];
    102102                        return $max_age < $this->reference_time;
    103103                }
    104104
    105                 if (isset($this->attributes['expires'])) {
     105                if ( isset( $this->attributes['expires'] ) ) {
    106106                        $expires = $this->attributes['expires'];
    107107                        return $expires < $this->reference_time;
    108108                }
    class Requests_Cookie { 
    116116         * @param Requests_IRI $uri URI to check
    117117         * @return boolean Whether the cookie is valid for the given URI
    118118         */
    119         public function uri_matches(Requests_IRI $uri) {
    120                 if (!$this->domain_matches($uri->host)) {
     119        public function uri_matches( Requests_IRI $uri ) {
     120                if ( ! $this->domain_matches( $uri->host ) ) {
    121121                        return false;
    122122                }
    123123
    124                 if (!$this->path_matches($uri->path)) {
     124                if ( ! $this->path_matches( $uri->path ) ) {
    125125                        return false;
    126126                }
    127127
    128                 return empty($this->attributes['secure']) || $uri->scheme === 'https';
     128                return empty( $this->attributes['secure'] ) || 'https' === $uri->scheme;
    129129        }
    130130
    131131        /**
    class Requests_Cookie { 
    134134         * @param string $string Domain to check
    135135         * @return boolean Whether the cookie is valid for the given domain
    136136         */
    137         public function domain_matches($string) {
    138                 if (!isset($this->attributes['domain'])) {
     137        public function domain_matches( $string ) {
     138                if ( ! isset( $this->attributes['domain'] ) ) {
    139139                        // Cookies created manually; cookies created by Requests will set
    140140                        // the domain to the requested domain
    141141                        return true;
    142142                }
    143143
    144144                $domain_string = $this->attributes['domain'];
    145                 if ($domain_string === $string) {
     145                if ( $domain_string === $string ) {
    146146                        // The domain string and the string are identical.
    147147                        return true;
    148148                }
    149149
    150150                // If the cookie is marked as host-only and we don't have an exact
    151151                // match, reject the cookie
    152                 if ($this->flags['host-only'] === true) {
     152                if ( true === $this->flags['host-only'] ) {
    153153                        return false;
    154154                }
    155155
    156                 if (strlen($string) <= strlen($domain_string)) {
     156                if ( strlen( $string ) <= strlen( $domain_string ) ) {
    157157                        // For obvious reasons, the string cannot be a suffix if the domain
    158158                        // is shorter than the domain string
    159159                        return false;
    160160                }
    161161
    162                 if (substr($string, -1 * strlen($domain_string)) !== $domain_string) {
     162                if ( substr( $string, -1 * strlen( $domain_string ) ) !== $domain_string ) {
    163163                        // The domain string should be a suffix of the string.
    164164                        return false;
    165165                }
    166166
    167                 $prefix = substr($string, 0, strlen($string) - strlen($domain_string));
    168                 if (substr($prefix, -1) !== '.') {
     167                $prefix = substr( $string, 0, strlen( $string ) - strlen( $domain_string ) );
     168                if ( substr( $prefix, -1 ) !== '.' ) {
    169169                        // The last character of the string that is not included in the
    170170                        // domain string should be a %x2E (".") character.
    171171                        return false;
    172172                }
    173173
    174174                // The string should be a host name (i.e., not an IP address).
    175                 return !preg_match('#^(.+\.)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $string);
     175                return ! preg_match( '#^(.+\.)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $string );
    176176        }
    177177
    178178        /**
    class Requests_Cookie { 
    183183         * @param string $request_path Path to check
    184184         * @return boolean Whether the cookie is valid for the given path
    185185         */
    186         public function path_matches($request_path) {
    187                 if (empty($request_path)) {
     186        public function path_matches( $request_path ) {
     187                if ( empty( $request_path ) ) {
    188188                        // Normalize empty path to root
    189189                        $request_path = '/';
    190190                }
    191191
    192                 if (!isset($this->attributes['path'])) {
     192                if ( ! isset( $this->attributes['path'] ) ) {
    193193                        // Cookies created manually; cookies created by Requests will set
    194194                        // the path to the requested path
    195195                        return true;
    class Requests_Cookie { 
    197197
    198198                $cookie_path = $this->attributes['path'];
    199199
    200                 if ($cookie_path === $request_path) {
     200                if ( $cookie_path === $request_path ) {
    201201                        // The cookie-path and the request-path are identical.
    202202                        return true;
    203203                }
    204204
    205                 if (strlen($request_path) > strlen($cookie_path) && substr($request_path, 0, strlen($cookie_path)) === $cookie_path) {
    206                         if (substr($cookie_path, -1) === '/') {
     205                if ( strlen( $request_path ) > strlen( $cookie_path ) && substr( $request_path, 0, strlen( $cookie_path ) ) === $cookie_path ) {
     206                        if ( substr( $cookie_path, -1 ) === '/' ) {
    207207                                // The cookie-path is a prefix of the request-path, and the last
    208208                                // character of the cookie-path is %x2F ("/").
    209209                                return true;
    210210                        }
    211211
    212                         if (substr($request_path, strlen($cookie_path), 1) === '/') {
     212                        if ( substr( $request_path, strlen( $cookie_path ), 1 ) === '/' ) {
    213213                                // The cookie-path is a prefix of the request-path, and the
    214214                                // first character of the request-path that is not included in
    215215                                // the cookie-path is a %x2F ("/") character.
    class Requests_Cookie { 
    226226         * @return boolean Whether the cookie was successfully normalized
    227227         */
    228228        public function normalize() {
    229                 foreach ($this->attributes as $key => $value) {
     229                foreach ( $this->attributes as $key => $value ) {
    230230                        $orig_value = $value;
    231                         $value = $this->normalize_attribute($key, $value);
    232                         if ($value === null) {
    233                                 unset($this->attributes[$key]);
     231                        $value      = $this->normalize_attribute( $key, $value );
     232                        if ( null === $value ) {
     233                                unset( $this->attributes[ $key ] );
    234234                                continue;
    235235                        }
    236236
    237                         if ($value !== $orig_value) {
    238                                 $this->attributes[$key] = $value;
     237                        if ( $value !== $orig_value ) {
     238                                $this->attributes[ $key ] = $value;
    239239                        }
    240240                }
    241241
    class Requests_Cookie { 
    251251         * @param string|boolean $value Attribute value (string value, or true if empty/flag)
    252252         * @return mixed Value if available, or null if the attribute value is invalid (and should be skipped)
    253253         */
    254         protected function normalize_attribute($name, $value) {
    255                 switch (strtolower($name)) {
     254        protected function normalize_attribute( $name, $value ) {
     255                switch ( strtolower( $name ) ) {
    256256                        case 'expires':
    257257                                // Expiration parsing, as per RFC 6265 section 5.2.1
    258                                 if (is_int($value)) {
     258                                if ( is_int( $value ) ) {
    259259                                        return $value;
    260260                                }
    261261
    262                                 $expiry_time = strtotime($value);
    263                                 if ($expiry_time === false) {
     262                                $expiry_time = strtotime( $value );
     263                                if ( false === $expiry_time ) {
    264264                                        return null;
    265265                                }
    266266
    class Requests_Cookie { 
    268268
    269269                        case 'max-age':
    270270                                // Expiration parsing, as per RFC 6265 section 5.2.2
    271                                 if (is_int($value)) {
     271                                if ( is_int( $value ) ) {
    272272                                        return $value;
    273273                                }
    274274
    275275                                // Check that we have a valid age
    276                                 if (!preg_match('/^-?\d+$/', $value)) {
     276                                if ( ! preg_match( '/^-?\d+$/', $value ) ) {
    277277                                        return null;
    278278                                }
    279279
    280280                                $delta_seconds = (int) $value;
    281                                 if ($delta_seconds <= 0) {
     281                                if ( $delta_seconds <= 0 ) {
    282282                                        $expiry_time = 0;
    283                                 }
    284                                 else {
     283                                } else {
    285284                                        $expiry_time = $this->reference_time + $delta_seconds;
    286285                                }
    287286
    class Requests_Cookie { 
    289288
    290289                        case 'domain':
    291290                                // Domain normalization, as per RFC 6265 section 5.2.3
    292                                 if ($value[0] === '.') {
    293                                         $value = substr($value, 1);
     291                                if ( '.' === $value[0] ) {
     292                                        $value = substr( $value, 1 );
    294293                                }
    295294
    296295                                return $value;
    class Requests_Cookie { 
    308307         * @return string Cookie formatted for Cookie header
    309308         */
    310309        public function format_for_header() {
    311                 return sprintf('%s=%s', $this->name, $this->value);
     310                /* translators: %s: Formatted cookie. */
     311                return sprintf( '%s=%s', $this->name, $this->value );
    312312        }
    313313
    314314        /**
    class Requests_Cookie { 
    332332         */
    333333        public function format_for_set_cookie() {
    334334                $header_value = $this->format_for_header();
    335                 if (!empty($this->attributes)) {
     335                if ( ! empty( $this->attributes ) ) {
    336336                        $parts = array();
    337                         foreach ($this->attributes as $key => $value) {
     337                        foreach ( $this->attributes as $key => $value ) {
    338338                                // Ignore non-associative attributes
    339                                 if (is_numeric($key)) {
     339                                if ( is_numeric( $key ) ) {
    340340                                        $parts[] = $value;
    341                                 }
    342                                 else {
    343                                         $parts[] = sprintf('%s=%s', $key, $value);
     341                                } else {
     342                                        /* translators: %s: Formatted cookie. */
     343                                        $parts[] = sprintf( '%s=%s', $key, $value );
    344344                                }
    345345                        }
    346346
    347                         $header_value .= '; ' . implode('; ', $parts);
     347                        $header_value .= '; ' . implode( '; ', $parts );
    348348                }
    349349                return $header_value;
    350350        }
    class Requests_Cookie { 
    379379         * @param string Cookie header value (from a Set-Cookie header)
    380380         * @return Requests_Cookie Parsed cookie object
    381381         */
    382         public static function parse($string, $name = '', $reference_time = null) {
    383                 $parts = explode(';', $string);
    384                 $kvparts = array_shift($parts);
     382        public static function parse( $string, $name = '', $reference_time = null ) {
     383                $parts = explode( ';', $string );
     384                $kvparts = array_shift( $parts );
    385385
    386                 if (!empty($name)) {
     386                if ( ! empty( $name ) ) {
    387387                        $value = $string;
    388                 }
    389                 elseif (strpos($kvparts, '=') === false) {
     388                } elseif ( false === strpos( $kvparts, '=' ) ) {
    390389                        // Some sites might only have a value without the equals separator.
    391390                        // Deviate from RFC 6265 and pretend it was actually a blank name
    392391                        // (`=foo`)
    393392                        //
    394393                        // https://bugzilla.mozilla.org/show_bug.cgi?id=169091
    395                         $name = '';
     394                        $name  = '';
    396395                        $value = $kvparts;
     396                } else {
     397                        list( $name, $value ) = explode( '=', $kvparts, 2 );
    397398                }
    398                 else {
    399                         list($name, $value) = explode('=', $kvparts, 2);
    400                 }
    401                 $name = trim($name);
    402                 $value = trim($value);
     399                $name  = trim( $name );
     400                $value = trim( $value );
    403401
    404402                // Attribute key are handled case-insensitively
    405403                $attributes = new Requests_Utility_CaseInsensitiveDictionary();
    406404
    407                 if (!empty($parts)) {
    408                         foreach ($parts as $part) {
    409                                 if (strpos($part, '=') === false) {
    410                                         $part_key = $part;
     405                if ( ! empty( $parts ) ) {
     406                        foreach ( $parts as $part ) {
     407                                if ( false === strpos( $part, '=' ) ) {
     408                                        $part_key   = $part;
    411409                                        $part_value = true;
    412                                 }
    413                                 else {
    414                                         list($part_key, $part_value) = explode('=', $part, 2);
    415                                         $part_value = trim($part_value);
     410                                } else {
     411                                        list ( $part_key, $part_value ) = explode( '=', $part, 2 );
     412                                        $part_value                     = trim( $part_value );
    416413                                }
    417414
    418                                 $part_key = trim($part_key);
    419                                 $attributes[$part_key] = $part_value;
     415                                $part_key                = trim( $part_key );
     416                                $attributes[ $part_key ] = $part_value;
    420417                        }
    421418                }
    422419
    423                 return new Requests_Cookie($name, $value, $attributes, array(), $reference_time);
     420                return new Requests_Cookie( $name, $value, $attributes, array(), $reference_time );
    424421        }
    425422
    426423        /**
    class Requests_Cookie { 
    431428         * @param int|null $time Reference time for expiration calculation
    432429         * @return array
    433430         */
    434         public static function parse_from_headers(Requests_Response_Headers $headers, Requests_IRI $origin = null, $time = null) {
    435                 $cookie_headers = $headers->getValues('Set-Cookie');
    436                 if (empty($cookie_headers)) {
     431        public static function parse_from_headers( Requests_Response_Headers $headers, Requests_IRI $origin = null, $time = null ) {
     432                $cookie_headers = $headers->getValues( 'Set-Cookie' );
     433                if ( empty( $cookie_headers ) ) {
    437434                        return array();
    438435                }
    439436
    440437                $cookies = array();
    441                 foreach ($cookie_headers as $header) {
    442                         $parsed = self::parse($header, '', $time);
     438                foreach ( $cookie_headers as $header ) {
     439                        $parsed = self::parse( $header, '', $time );
    443440
    444441                        // Default domain/path attributes
    445                         if (empty($parsed->attributes['domain']) && !empty($origin)) {
     442                        if ( empty( $parsed->attributes['domain'] ) && ! empty( $origin ) ) {
    446443                                $parsed->attributes['domain'] = $origin->host;
    447                                 $parsed->flags['host-only'] = true;
    448                         }
    449                         else {
     444                                $parsed->flags['host-only']   = true;
     445                        } else {
    450446                                $parsed->flags['host-only'] = false;
    451447                        }
    452448
    453                         $path_is_valid = (!empty($parsed->attributes['path']) && $parsed->attributes['path'][0] === '/');
    454                         if (!$path_is_valid && !empty($origin)) {
     449                        $path_is_valid = ( ! empty( $parsed->attributes['path'] ) && $parsed->attributes['path'][0] === '/' );
     450                        if ( ! $path_is_valid && ! empty( $origin ) ) {
    455451                                $path = $origin->path;
    456452
    457453                                // Default path normalization as per RFC 6265 section 5.1.4
    458                                 if (substr($path, 0, 1) !== '/') {
     454                                if ( '/' !== substr( $path, 0, 1 ) ) {
    459455                                        // If the uri-path is empty or if the first character of
    460456                                        // the uri-path is not a %x2F ("/") character, output
    461457                                        // %x2F ("/") and skip the remaining steps.
    462458                                        $path = '/';
    463                                 }
    464                                 elseif (substr_count($path, '/') === 1) {
     459                                } elseif ( 1 === substr_count( $path, '/' ) ) {
    465460                                        // If the uri-path contains no more than one %x2F ("/")
    466461                                        // character, output %x2F ("/") and skip the remaining
    467462                                        // step.
    468463                                        $path = '/';
    469                                 }
    470                                 else {
     464                                } else {
    471465                                        // Output the characters of the uri-path from the first
    472466                                        // character up to, but not including, the right-most
    473467                                        // %x2F ("/").
    474                                         $path = substr($path, 0, strrpos($path, '/'));
     468                                        $path = substr( $path, 0, strrpos( $path, '/' ) );
    475469                                }
    476470                                $parsed->attributes['path'] = $path;
    477471                        }
    478472
    479473                        // Reject invalid cookie domains
    480                         if (!empty($origin) && !$parsed->domain_matches($origin->host)) {
     474                        if ( ! empty( $origin ) && ! $parsed->domain_matches( $origin->host ) ) {
    481475                                continue;
    482476                        }
    483477
    484                         $cookies[$parsed->name] = $parsed;
     478                        $cookies[ $parsed->name ] = $parsed;
    485479                }
    486480
    487481                return $cookies;
    class Requests_Cookie { 
    494488         * @deprecated Use {@see Requests_Cookie::parse_from_headers}
    495489         * @return string
    496490         */
    497         public static function parseFromHeaders(Requests_Response_Headers $headers) {
    498                 return self::parse_from_headers($headers);
     491        public static function parseFromHeaders( Requests_Response_Headers $headers ) {
     492                return self::parse_from_headers( $headers );
    499493        }
    500494}