Make WordPress Core

Changeset 50842


Ignore:
Timestamp:
05/11/2021 07:40:41 PM (5 years ago)
Author:
desrosj
Message:

External Libraries: Update the Requests library to version 1.8.0.

While some of the changes in the 1.8.0 release have already been copied to WordPress Core in earlier releases (see [38727], [46258], [47902] and [49382]), this release contains additional improvements, including:

  • A significant performance fix when using cURL.
  • Improved compliance with RFC2616.

The library has also been moved under the WordPress project’s GitHub organization and can now be found at https://github.com/WordPress/Requests.

Props jrf, dd32, rmccue, justinahinon, netweb, schlessera, TimothyBJacobs, soulseekah, ozh, skithund, carlalexander, travisnorthcutt, desrosj.
Fixes #53101.

Location:
trunk/src/wp-includes
Files:
19 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/Requests/Auth.php

    r46586 r50842  
    3030         * @param Requests_Hooks $hooks Hook system
    3131         */
    32         public function register(Requests_Hooks &$hooks);
     32        public function register(Requests_Hooks $hooks);
    3333}
  • trunk/src/wp-includes/Requests/Auth/Basic.php

    r46586 r50842  
    5454         * @param Requests_Hooks $hooks Hook system
    5555         */
    56         public function register(Requests_Hooks &$hooks) {
    57                 $hooks->register('curl.before_send', array(&$this, 'curl_before_send'));
    58                 $hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header'));
     56        public function register(Requests_Hooks $hooks) {
     57                $hooks->register('curl.before_send', array($this, 'curl_before_send'));
     58                $hooks->register('fsockopen.after_headers', array($this, 'fsockopen_header'));
    5959        }
    6060
  • trunk/src/wp-includes/Requests/Cookie.php

    r46586 r50842  
    6666         */
    6767        public function __construct($name, $value, $attributes = array(), $flags = array(), $reference_time = null) {
    68                 $this->name = $name;
    69                 $this->value = $value;
     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();
     
    229229                foreach ($this->attributes as $key => $value) {
    230230                        $orig_value = $value;
    231                         $value = $this->normalize_attribute($key, $value);
     231                        $value      = $this->normalize_attribute($key, $value);
    232232                        if ($value === null) {
    233233                                unset($this->attributes[$key]);
     
    386386         */
    387387        public static function parse($string, $name = '', $reference_time = null) {
    388                 $parts = explode(';', $string);
     388                $parts   = explode(';', $string);
    389389                $kvparts = array_shift($parts);
    390390
     
    398398                        //
    399399                        // https://bugzilla.mozilla.org/show_bug.cgi?id=169091
    400                         $name = '';
     400                        $name  = '';
    401401                        $value = $kvparts;
    402402                }
     
    404404                        list($name, $value) = explode('=', $kvparts, 2);
    405405                }
    406                 $name = trim($name);
     406                $name  = trim($name);
    407407                $value = trim($value);
    408408
     
    413413                        foreach ($parts as $part) {
    414414                                if (strpos($part, '=') === false) {
    415                                         $part_key = $part;
     415                                        $part_key   = $part;
    416416                                        $part_value = true;
    417417                                }
    418418                                else {
    419419                                        list($part_key, $part_value) = explode('=', $part, 2);
    420                                         $part_value = trim($part_value);
    421                                 }
    422 
    423                                 $part_key = trim($part_key);
     420                                        $part_value                  = trim($part_value);
     421                                }
     422
     423                                $part_key              = trim($part_key);
    424424                                $attributes[$part_key] = $part_value;
    425425                        }
     
    450450                        if (empty($parsed->attributes['domain']) && !empty($origin)) {
    451451                                $parsed->attributes['domain'] = $origin->host;
    452                                 $parsed->flags['host-only'] = true;
     452                                $parsed->flags['host-only']   = true;
    453453                        }
    454454                        else {
     
    498498         * @codeCoverageIgnore
    499499         * @deprecated Use {@see Requests_Cookie::parse_from_headers}
    500          * @return string
     500         * @return array
    501501         */
    502502        public static function parseFromHeaders(Requests_Response_Headers $headers) {
  • trunk/src/wp-includes/Requests/Cookie/Jar.php

    r46586 r50842  
    6969         *
    7070         * @param string $key Item key
    71          * @return string Item value
     71         * @return string|null Item value (null if offsetExists is false)
    7272         */
    7373        public function offsetGet($key) {
     
    163163         * @var Requests_Response $response
    164164         */
    165         public function before_redirect_check(Requests_Response &$return) {
     165        public function before_redirect_check(Requests_Response $return) {
    166166                $url = $return->url;
    167167                if (!$url instanceof Requests_IRI) {
     
    169169                }
    170170
    171                 $cookies = Requests_Cookie::parse_from_headers($return->headers, $url);
    172                 $this->cookies = array_merge($this->cookies, $cookies);
     171                $cookies         = Requests_Cookie::parse_from_headers($return->headers, $url);
     172                $this->cookies   = array_merge($this->cookies, $cookies);
    173173                $return->cookies = $this;
    174174        }
  • trunk/src/wp-includes/Requests/Exception/Transport/cURL.php

    r46586 r50842  
    33class Requests_Exception_Transport_cURL extends Requests_Exception_Transport {
    44
    5         const EASY = 'cURLEasy';
     5        const EASY  = 'cURLEasy';
    66        const MULTI = 'cURLMulti';
    77        const SHARE = 'cURLShare';
  • trunk/src/wp-includes/Requests/IDNAEncoder.php

    r46586 r50842  
    142142                $strlen = strlen($input);
    143143
     144                // phpcs:ignore Generic.CodeAnalysis.JumbledIncrementer -- This is a deliberate choice.
    144145                for ($position = 0; $position < $strlen; $position++) {
    145146                        $value = ord($input[$position]);
     
    148149                        if ((~$value & 0x80) === 0x80) {
    149150                                $character = $value;
    150                                 $length = 1;
     151                                $length    = 1;
    151152                                $remaining = 0;
    152153                        }
     
    154155                        elseif (($value & 0xE0) === 0xC0) {
    155156                                $character = ($value & 0x1F) << 6;
    156                                 $length = 2;
     157                                $length    = 2;
    157158                                $remaining = 1;
    158159                        }
     
    160161                        elseif (($value & 0xF0) === 0xE0) {
    161162                                $character = ($value & 0x0F) << 12;
    162                                 $length = 3;
     163                                $length    = 3;
    163164                                $remaining = 2;
    164165                        }
     
    166167                        elseif (($value & 0xF8) === 0xF0) {
    167168                                $character = ($value & 0x07) << 18;
    168                                 $length = 4;
     169                                $length    = 4;
    169170                                $remaining = 3;
    170171                        }
     
    186187                                        }
    187188
    188                                         $character |= ($value & 0x3F) << (--$remaining * 6);
     189                                        --$remaining;
     190                                        $character |= ($value & 0x3F) << ($remaining * 6);
    189191                                }
    190192                                $position--;
    191193                        }
    192194
    193                         if (
    194                                 // Non-shortest form sequences are invalid
    195                                    $length > 1 && $character <= 0x7F
     195                        if (// Non-shortest form sequences are invalid
     196                                $length > 1 && $character <= 0x7F
    196197                                || $length > 2 && $character <= 0x7FF
    197198                                || $length > 3 && $character <= 0xFFFF
     
    202203                                || (
    203204                                        // Everything else not in ucschar
    204                                            $character > 0xD7FF && $character < 0xF900
     205                                        $character > 0xD7FF && $character < 0xF900
    205206                                        || $character < 0x20
    206207                                        || $character > 0x7E && $character < 0xA0
     
    228229        public static function punycode_encode($input) {
    229230                $output = '';
    230 #               let n = initial_n
     231                // let n = initial_n
    231232                $n = self::BOOTSTRAP_INITIAL_N;
    232 #               let delta = 0
     233                // let delta = 0
    233234                $delta = 0;
    234 #               let bias = initial_bias
     235                // let bias = initial_bias
    235236                $bias = self::BOOTSTRAP_INITIAL_BIAS;
    236 #               let h = b = the number of basic code points in the input
    237                 $h = $b = 0; // see loop
    238 #               copy them to the output in order
     237                // let h = b = the number of basic code points in the input
     238                $h = 0;
     239                $b = 0; // see loop
     240                // copy them to the output in order
    239241                $codepoints = self::utf8_to_codepoints($input);
    240                 $extended = array();
     242                $extended   = array();
    241243
    242244                foreach ($codepoints as $char) {
     
    261263                sort($extended);
    262264                $b = $h;
    263 #               [copy them] followed by a delimiter if b > 0
     265                // [copy them] followed by a delimiter if b > 0
    264266                if (strlen($output) > 0) {
    265267                        $output .= '-';
    266268                }
    267 #               {if the input contains a non-basic code point < n then fail}
    268 #               while h < length(input) do begin
    269                 while ($h < count($codepoints)) {
    270 #                       let m = the minimum code point >= n in the input
     269                // {if the input contains a non-basic code point < n then fail}
     270                // while h < length(input) do begin
     271                $codepointcount = count($codepoints);
     272                while ($h < $codepointcount) {
     273                        // let m = the minimum code point >= n in the input
    271274                        $m = array_shift($extended);
    272275                        //printf('next code point to insert is %s' . PHP_EOL, dechex($m));
    273 #                       let delta = delta + (m - n) * (h + 1), fail on overflow
     276                        // let delta = delta + (m - n) * (h + 1), fail on overflow
    274277                        $delta += ($m - $n) * ($h + 1);
    275 #                       let n = m
     278                        // let n = m
    276279                        $n = $m;
    277 #                       for each code point c in the input (in order) do begin
    278                         for ($num = 0; $num < count($codepoints); $num++) {
     280                        // for each code point c in the input (in order) do begin
     281                        for ($num = 0; $num < $codepointcount; $num++) {
    279282                                $c = $codepoints[$num];
    280 #                               if c < n then increment delta, fail on overflow
     283                                // if c < n then increment delta, fail on overflow
    281284                                if ($c < $n) {
    282285                                        $delta++;
    283286                                }
    284 #                               if c == n then begin
     287                                // if c == n then begin
    285288                                elseif ($c === $n) {
    286 #                                       let q = delta
     289                                        // let q = delta
    287290                                        $q = $delta;
    288 #                                       for k = base to infinity in steps of base do begin
     291                                        // for k = base to infinity in steps of base do begin
    289292                                        for ($k = self::BOOTSTRAP_BASE; ; $k += self::BOOTSTRAP_BASE) {
    290 #                                               let t = tmin if k <= bias {+ tmin}, or
    291 #                                                               tmax if k >= bias + tmax, or k - bias otherwise
     293                                                // let t = tmin if k <= bias {+ tmin}, or
     294                                                //     tmax if k >= bias + tmax, or k - bias otherwise
    292295                                                if ($k <= ($bias + self::BOOTSTRAP_TMIN)) {
    293296                                                        $t = self::BOOTSTRAP_TMIN;
     
    299302                                                        $t = $k - $bias;
    300303                                                }
    301 #                                               if q < t then break
     304                                                // if q < t then break
    302305                                                if ($q < $t) {
    303306                                                        break;
    304307                                                }
    305 #                                               output the code point for digit t + ((q - t) mod (base - t))
    306                                                 $digit = $t + (($q - $t) % (self::BOOTSTRAP_BASE - $t));
     308                                                // output the code point for digit t + ((q - t) mod (base - t))
     309                                                $digit   = $t + (($q - $t) % (self::BOOTSTRAP_BASE - $t));
    307310                                                $output .= self::digit_to_char($digit);
    308 #                                               let q = (q - t) div (base - t)
     311                                                // let q = (q - t) div (base - t)
    309312                                                $q = floor(($q - $t) / (self::BOOTSTRAP_BASE - $t));
    310 #                                       end
    311                                         }
    312 #                                       output the code point for digit q
     313                                        } // end
     314                                        // output the code point for digit q
    313315                                        $output .= self::digit_to_char($q);
    314 #                                       let bias = adapt(delta, h + 1, test h equals b?)
     316                                        // let bias = adapt(delta, h + 1, test h equals b?)
    315317                                        $bias = self::adapt($delta, $h + 1, $h === $b);
    316 #                                       let delta = 0
     318                                        // let delta = 0
    317319                                        $delta = 0;
    318 #                                       increment h
     320                                        // increment h
    319321                                        $h++;
    320 #                               end
    321                                 }
    322 #                       end
    323                         }
    324 #                       increment delta and n
     322                                } // end
     323                        } // end
     324                        // increment delta and n
    325325                        $delta++;
    326326                        $n++;
    327 #               end
    328                 }
     327                } // end
    329328
    330329                return $output;
     
    359358         * @param bool $firsttime
    360359         * @return int New bias
     360         *
     361         * function adapt(delta,numpoints,firsttime):
    361362         */
    362363        protected static function adapt($delta, $numpoints, $firsttime) {
    363 #       function adapt(delta,numpoints,firsttime):
    364 #               if firsttime then let delta = delta div damp
     364                // if firsttime then let delta = delta div damp
    365365                if ($firsttime) {
    366366                        $delta = floor($delta / self::BOOTSTRAP_DAMP);
    367367                }
    368 #               else let delta = delta div 2
     368                // else let delta = delta div 2
    369369                else {
    370370                        $delta = floor($delta / 2);
    371371                }
    372 #               let delta = delta + (delta div numpoints)
     372                // let delta = delta + (delta div numpoints)
    373373                $delta += floor($delta / $numpoints);
    374 #               let k = 0
     374                // let k = 0
    375375                $k = 0;
    376 #               while delta > ((base - tmin) * tmax) div 2 do begin
     376                // while delta > ((base - tmin) * tmax) div 2 do begin
    377377                $max = floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN) * self::BOOTSTRAP_TMAX) / 2);
    378378                while ($delta > $max) {
    379 #                       let delta = delta div (base - tmin)
     379                        // let delta = delta div (base - tmin)
    380380                        $delta = floor($delta / (self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN));
    381 #                       let k = k + base
     381                        // let k = k + base
    382382                        $k += self::BOOTSTRAP_BASE;
    383 #               end
    384                 }
    385 #               return k + (((base - tmin + 1) * delta) div (delta + skew))
     383                } // end
     384                // return k + (((base - tmin + 1) * delta) div (delta + skew))
    386385                return $k + floor(((self::BOOTSTRAP_BASE - self::BOOTSTRAP_TMIN + 1) * $delta) / ($delta + self::BOOTSTRAP_SKEW));
    387386        }
  • trunk/src/wp-includes/Requests/IPv6.php

    r46586 r50842  
    4141
    4242                list($ip1, $ip2) = explode('::', $ip);
    43                 $c1 = ($ip1 === '') ? -1 : substr_count($ip1, ':');
    44                 $c2 = ($ip2 === '') ? -1 : substr_count($ip2, ':');
     43                $c1              = ($ip1 === '') ? -1 : substr_count($ip1, ':');
     44                $c2              = ($ip2 === '') ? -1 : substr_count($ip2, ':');
    4545
    4646                if (strpos($ip2, '.') !== false) {
     
    5252                }
    5353                // ::xxx
    54                 else if ($c1 === -1) {
     54                elseif ($c1 === -1) {
    5555                        $fill = str_repeat('0:', 7 - $c2);
    56                         $ip = str_replace('::', $fill, $ip);
     56                        $ip   = str_replace('::', $fill, $ip);
    5757                }
    5858                // xxx::
    59                 else if ($c2 === -1) {
     59                elseif ($c2 === -1) {
    6060                        $fill = str_repeat(':0', 7 - $c1);
    61                         $ip = str_replace('::', $fill, $ip);
     61                        $ip   = str_replace('::', $fill, $ip);
    6262                }
    6363                // xxx::xxx
    6464                else {
    6565                        $fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
    66                         $ip = str_replace('::', $fill, $ip);
     66                        $ip   = str_replace('::', $fill, $ip);
    6767                }
    6868                return $ip;
     
    8585        public static function compress($ip) {
    8686                // Prepare the IP to be compressed
    87                 $ip = self::uncompress($ip);
     87                $ip       = self::uncompress($ip);
    8888                $ip_parts = self::split_v6_v4($ip);
    8989
     
    127127        protected static function split_v6_v4($ip) {
    128128                if (strpos($ip, '.') !== false) {
    129                         $pos = strrpos($ip, ':');
     129                        $pos       = strrpos($ip, ':');
    130130                        $ipv6_part = substr($ip, 0, $pos);
    131131                        $ipv4_part = substr($ip, $pos + 1);
     
    146146         */
    147147        public static function check_ipv6($ip) {
    148                 $ip = self::uncompress($ip);
     148                $ip                = self::uncompress($ip);
    149149                list($ipv6, $ipv4) = self::split_v6_v4($ip);
    150                 $ipv6 = explode(':', $ipv6);
    151                 $ipv4 = explode('.', $ipv4);
     150                $ipv6              = explode(':', $ipv6);
     151                $ipv4              = explode('.', $ipv4);
    152152                if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) {
    153153                        foreach ($ipv6 as $ipv6_part) {
  • trunk/src/wp-includes/Requests/IRI.php

    r46586 r50842  
    6868         * Scheme
    6969         *
     70         * @var string|null
     71         */
     72        protected $scheme = null;
     73
     74        /**
     75         * User Information
     76         *
     77         * @var string|null
     78         */
     79        protected $iuserinfo = null;
     80
     81        /**
     82         * ihost
     83         *
     84         * @var string|null
     85         */
     86        protected $ihost = null;
     87
     88        /**
     89         * Port
     90         *
     91         * @var string|null
     92         */
     93        protected $port = null;
     94
     95        /**
     96         * ipath
     97         *
    7098         * @var string
    7199         */
    72         protected $scheme = null;
    73 
    74         /**
    75          * User Information
    76          *
    77          * @var string
    78          */
    79         protected $iuserinfo = null;
    80 
    81         /**
    82          * ihost
    83          *
    84          * @var string
    85          */
    86         protected $ihost = null;
    87 
    88         /**
    89          * Port
    90          *
    91          * @var string
    92          */
    93         protected $port = null;
    94 
    95         /**
    96          * ipath
    97          *
    98          * @var string
    99          */
    100100        protected $ipath = '';
    101101
     
    103103         * iquery
    104104         *
    105          * @var string
     105         * @var string|null
    106106         */
    107107        protected $iquery = null;
    108108
    109109        /**
    110          * ifragment
     110         * ifragment|null
    111111         *
    112112         * @var string
     
    119119         * Each key is the scheme, each value is an array with each key as the IRI
    120120         * part and value as the default value for that part.
     121         *
     122         * @var array
    121123         */
    122124        protected $normalization = array(
     
    250252         * Returns false if $base is not absolute, otherwise an IRI.
    251253         *
    252          * @param IRI|string $base (Absolute) Base IRI
    253          * @param IRI|string $relative Relative IRI
    254          * @return IRI|false
     254         * @param Requests_IRI|string $base (Absolute) Base IRI
     255         * @param Requests_IRI|string $relative Relative IRI
     256         * @return Requests_IRI|false
    255257         */
    256258        public static function absolutize($base, $relative) {
     
    420422        protected function replace_invalid_with_pct_encoding($string, $extra_chars, $iprivate = false) {
    421423                // Normalize as many pct-encoded sections as possible
    422                 $string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array(&$this, 'remove_iunreserved_percent_encoded'), $string);
     424                $string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array($this, 'remove_iunreserved_percent_encoded'), $string);
    423425
    424426                // Replace invalid percent characters
     
    10111013         * Get the complete IRI
    10121014         *
    1013          * @return string
     1015         * @return string|false
    10141016         */
    10151017        protected function get_iri() {
     
    10481050         * Get the complete iauthority
    10491051         *
    1050          * @return string
     1052         * @return string|null
    10511053         */
    10521054        protected function get_iauthority() {
  • trunk/src/wp-includes/Requests/Proxy.php

    r46586 r50842  
    3232         * @param Requests_Hooks $hooks Hook system
    3333         */
    34         public function register(Requests_Hooks &$hooks);
     34        public function register(Requests_Hooks $hooks);
    3535}
  • trunk/src/wp-includes/Requests/Proxy/HTTP.php

    r46586 r50842  
    6060                }
    6161                elseif (is_array($args)) {
    62                         if (count($args) == 1) {
     62                        if (count($args) === 1) {
    6363                                list($this->proxy) = $args;
    6464                        }
    65                         elseif (count($args) == 3) {
     65                        elseif (count($args) === 3) {
    6666                                list($this->proxy, $this->user, $this->pass) = $args;
    67                                 $this->use_authentication = true;
     67                                $this->use_authentication                    = true;
    6868                        }
    6969                        else {
     
    8383         * @param Requests_Hooks $hooks Hook system
    8484         */
    85         public function register(Requests_Hooks &$hooks) {
    86                 $hooks->register('curl.before_send', array(&$this, 'curl_before_send'));
     85        public function register(Requests_Hooks $hooks) {
     86                $hooks->register('curl.before_send', array($this, 'curl_before_send'));
    8787
    88                 $hooks->register('fsockopen.remote_socket', array(&$this, 'fsockopen_remote_socket'));
    89                 $hooks->register('fsockopen.remote_host_path', array(&$this, 'fsockopen_remote_host_path'));
     88                $hooks->register('fsockopen.remote_socket', array($this, 'fsockopen_remote_socket'));
     89                $hooks->register('fsockopen.remote_host_path', array($this, 'fsockopen_remote_host_path'));
    9090                if ($this->use_authentication) {
    91                         $hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header'));
     91                        $hooks->register('fsockopen.after_headers', array($this, 'fsockopen_header'));
    9292                }
    9393        }
  • trunk/src/wp-includes/Requests/Response.php

    r46586 r50842  
    5252        /**
    5353         * Protocol version, false if non-blocking
     54         *
    5455         * @var float|boolean
    5556         */
     
    9899        public function is_redirect() {
    99100                $code = $this->status_code;
    100                 return in_array($code, array(300, 301, 302, 303, 307)) || $code > 307 && $code < 400;
     101                return in_array($code, array(300, 301, 302, 303, 307), true) || $code > 307 && $code < 400;
    101102        }
    102103
  • trunk/src/wp-includes/Requests/Response/Headers.php

    r46586 r50842  
    2222         *
    2323         * @param string $key
    24          * @return string Header value
     24         * @return string|null Header value
    2525         */
    2626        public function offsetGet($key) {
     
    5959         *
    6060         * @param string $key
    61          * @return array Header values
     61         * @return array|null Header values
    6262         */
    6363        public function getValues($key) {
  • trunk/src/wp-includes/Requests/SSL.php

    r48121 r50842  
    2121         * Unfortunately, PHP doesn't check the certificate against the alternative
    2222         * names, leading things like 'https://www.github.com/' to be invalid.
    23          * Instead
    2423         *
    2524         * @see https://tools.ietf.org/html/rfc2818#section-3.1 RFC2818, Section 3.1
     
    3130         */
    3231        public static function verify_certificate($host, $cert) {
    33                 // Calculate the valid wildcard match if the host is not an IP address
    34                 $parts = explode('.', $host);
    35                 if (ip2long($host) === false) {
    36                         $parts[0] = '*';
    37                 }
    38                 $wildcard = implode('.', $parts);
    39 
    4032                $has_dns_alt = false;
    4133
     
    140132                // ruleset.
    141133                if (ip2long($host) === false) {
    142                         $parts = explode('.', $host);
     134                        $parts    = explode('.', $host);
    143135                        $parts[0] = '*';
    144136                        $wildcard = implode('.', $parts);
  • trunk/src/wp-includes/Requests/Session.php

    r46586 r50842  
    2323         *
    2424         * URLs will be made absolute using this as the base
     25         *
    2526         * @var string|null
    2627         */
     
    2930        /**
    3031         * Base headers for requests
     32         *
    3133         * @var array
    3234         */
     
    6567         */
    6668        public function __construct($url = null, $headers = array(), $data = array(), $options = array()) {
    67                 $this->url = $url;
     69                $this->url     = $url;
    6870                $this->headers = $headers;
    69                 $this->data = $data;
     71                $this->data    = $data;
    7072                $this->options = $options;
    7173
  • trunk/src/wp-includes/Requests/Transport/cURL.php

    r46586 r50842  
    3939
    4040        /**
    41          * Version string
    42          *
    43          * @var long
     41         * cURL version number
     42         *
     43         * @var int
    4444         */
    4545        public $version;
     
    9191         */
    9292        public function __construct() {
    93                 $curl = curl_version();
     93                $curl          = curl_version();
    9494                $this->version = $curl['version_number'];
    95                 $this->handle = curl_init();
     95                $this->handle  = curl_init();
    9696
    9797                curl_setopt($this->handle, CURLOPT_HEADER, false);
     
    101101                }
    102102                if (defined('CURLOPT_PROTOCOLS')) {
     103                        // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_protocolsFound
    103104                        curl_setopt($this->handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
    104105                }
    105106                if (defined('CURLOPT_REDIR_PROTOCOLS')) {
     107                        // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_redir_protocolsFound
    106108                        curl_setopt($this->handle, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
    107109                }
     
    139141                }
    140142
    141                 $this->response_data = '';
    142                 $this->response_bytes = 0;
     143                $this->response_data       = '';
     144                $this->response_bytes      = 0;
    143145                $this->response_byte_limit = false;
    144146                if ($options['max_bytes'] !== false) {
     
    169171                        curl_setopt($this->handle, CURLOPT_ENCODING, 'none');
    170172
    171                         $this->response_data = '';
     173                        $this->response_data  = '';
    172174                        $this->response_bytes = 0;
    173175                        curl_exec($this->handle);
     
    200202                $multihandle = curl_multi_init();
    201203                $subrequests = array();
    202                 $subhandles = array();
     204                $subhandles  = array();
    203205
    204206                $class = get_class($this);
    205207                foreach ($requests as $id => $request) {
    206208                        $subrequests[$id] = new $class();
    207                         $subhandles[$id] = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']);
     209                        $subhandles[$id]  = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']);
    208210                        $request['options']['hooks']->dispatch('curl.before_multi_add', array(&$subhandles[$id]));
    209211                        curl_multi_add_handle($multihandle, $subhandles[$id]);
    210212                }
    211213
    212                 $completed = 0;
    213                 $responses = array();
     214                $completed       = 0;
     215                $responses       = array();
     216                $subrequestcount = count($subrequests);
    214217
    215218                $request['options']['hooks']->dispatch('curl.before_multi_exec', array(&$multihandle));
    216219
    217220                do {
    218                         $active = false;
     221                        $active = 0;
    219222
    220223                        do {
     
    236239                        foreach ($to_process as $key => $done) {
    237240                                $options = $requests[$key]['options'];
    238                                 if (CURLE_OK !== $done['result']) {
     241                                if ($done['result'] !== CURLE_OK) {
    239242                                        //get error string for handle.
    240                                         $reason = curl_error($done['handle']);
    241                                         $exception = new Requests_Exception_Transport_cURL(
    242                                                                         $reason,
    243                                                                         Requests_Exception_Transport_cURL::EASY,
    244                                                                         $done['handle'],
    245                                                                         $done['result']
    246                                                                 );
     243                                        $reason          = curl_error($done['handle']);
     244                                        $exception       = new Requests_Exception_Transport_cURL(
     245                                                $reason,
     246                                                Requests_Exception_Transport_cURL::EASY,
     247                                                $done['handle'],
     248                                                $done['result']
     249                                        );
    247250                                        $responses[$key] = $exception;
    248251                                        $options['hooks']->dispatch('transport.internal.parse_error', array(&$responses[$key], $requests[$key]));
     
    263266                        }
    264267                }
    265                 while ($active || $completed < count($subrequests));
     268                while ($active || $completed < $subrequestcount);
    266269
    267270                $request['options']['hooks']->dispatch('curl.after_multi_exec', array(&$multihandle));
     
    288291                }
    289292
    290                 $this->response_data = '';
    291                 $this->response_bytes = 0;
     293                $this->response_data       = '';
     294                $this->response_bytes      = 0;
    292295                $this->response_byte_limit = false;
    293296                if ($options['max_bytes'] !== false) {
     
    311314
    312315                // Force closing the connection for old versions of cURL (<7.22).
    313                 if ( ! isset( $headers['Connection'] ) ) {
     316                if (!isset($headers['Connection'])) {
    314317                        $headers['Connection'] = 'close';
     318                }
     319
     320                /**
     321                 * Add "Expect" header.
     322                 *
     323                 * By default, cURL adds a "Expect: 100-Continue" to most requests. This header can
     324                 * add as much as a second to the time it takes for cURL to perform a request. To
     325                 * prevent this, we need to set an empty "Expect" header. To match the behaviour of
     326                 * Guzzle, we'll add the empty header to requests that are smaller than 1 MB and use
     327                 * HTTP/1.1.
     328                 *
     329                 * https://curl.se/mail/lib-2017-07/0013.html
     330                 */
     331                if (!isset($headers['Expect']) && $options['protocol_version'] === 1.1) {
     332                        $headers['Expect'] = $this->get_expect_header($data);
    315333                }
    316334
     
    321339
    322340                        if ($data_format === 'query') {
    323                                 $url = self::format_get($url, $data);
     341                                $url  = self::format_get($url, $data);
    324342                                $data = '';
    325343                        }
     
    364382                }
    365383                else {
     384                        // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_timeout_msFound
    366385                        curl_setopt($this->handle, CURLOPT_TIMEOUT_MS, round($timeout * 1000));
    367386                }
     
    371390                }
    372391                else {
     392                        // phpcs:ignore PHPCompatibility.Constants.NewConstants.curlopt_connecttimeout_msFound
    373393                        curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000));
    374394                }
     
    386406                }
    387407
    388                 if (true === $options['blocking']) {
    389                         curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
    390                         curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, array(&$this, 'stream_body'));
     408                if ($options['blocking'] === true) {
     409                        curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, array($this, 'stream_headers'));
     410                        curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, array($this, 'stream_body'));
    391411                        curl_setopt($this->handle, CURLOPT_BUFFERSIZE, Requests::BUFFER_SIZE);
    392412                }
     
    398418         * @param string $response Response data from the body
    399419         * @param array $options Request options
    400          * @return string HTTP response data including headers
     420         * @return string|false HTTP response data including headers. False if non-blocking.
     421         * @throws Requests_Exception
    401422         */
    402423        public function process_response($response, $options) {
     
    406427                        return false;
    407428                }
    408                 if ($options['filename'] !== false) {
     429                if ($options['filename'] !== false && $this->stream_handle) {
    409430                        fclose($this->stream_handle);
    410431                        $this->headers = trim($this->headers);
     
    440461                // (We may want to keep this somewhere just in case)
    441462                if ($this->done_headers) {
    442                         $this->headers = '';
     463                        $this->headers      = '';
    443464                        $this->done_headers = false;
    444465                }
     
    474495                                // Limit the length
    475496                                $limited_length = ($this->response_byte_limit - $this->response_bytes);
    476                                 $data = substr($data, 0, $limited_length);
     497                                $data           = substr($data, 0, $limited_length);
    477498                        }
    478499                }
     
    498519        protected static function format_get($url, $data) {
    499520                if (!empty($data)) {
     521                        $query     = '';
    500522                        $url_parts = parse_url($url);
    501523                        if (empty($url_parts['query'])) {
    502                                 $query = $url_parts['query'] = '';
     524                                $url_parts['query'] = '';
    503525                        }
    504526                        else {
     
    507529
    508530                        $query .= '&' . http_build_query($data, null, '&');
    509                         $query = trim($query, '&');
     531                        $query  = trim($query, '&');
    510532
    511533                        if (empty($url_parts['query'])) {
     
    540562                return true;
    541563        }
     564
     565        /**
     566         * Get the correct "Expect" header for the given request data.
     567         *
     568         * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD.
     569         * @return string The "Expect" header.
     570         */
     571        protected function get_expect_header($data) {
     572                if (!is_array($data)) {
     573                        return strlen((string) $data) >= 1048576 ? '100-Continue' : '';
     574                }
     575
     576                $bytesize = 0;
     577                $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
     578
     579                foreach ($iterator as $datum) {
     580                        $bytesize += strlen((string) $datum);
     581
     582                        if ($bytesize >= 1048576) {
     583                                return '100-Continue';
     584                        }
     585                }
     586
     587                return '';
     588        }
    542589}
  • trunk/src/wp-includes/Requests/Transport/fsockopen.php

    r46586 r50842  
    6363                        throw new Requests_Exception('Invalid URL.', 'invalidurl', $url);
    6464                }
    65                 $host = $url_parts['host'];
    66                 $context = stream_context_create();
    67                 $verifyname = false;
     65                $host                     = $url_parts['host'];
     66                $context                  = stream_context_create();
     67                $verifyname               = false;
    6868                $case_insensitive_headers = new Requests_Utility_CaseInsensitiveDictionary($headers);
    6969
     
    7676
    7777                        $context_options = array(
    78                                 'verify_peer' => true,
    79                                 // 'CN_match' => $host,
    80                                 'capture_peer_cert' => true
     78                                'verify_peer'       => true,
     79                                'capture_peer_cert' => true,
    8180                        );
    82                         $verifyname = true;
     81                        $verifyname      = true;
    8382
    8483                        // SNI, if enabled (OpenSSL >=0.9.8j)
     84                        // phpcs:ignore PHPCompatibility.Constants.NewConstants.openssl_tlsext_server_nameFound
    8585                        if (defined('OPENSSL_TLSEXT_SERVER_NAME') && OPENSSL_TLSEXT_SERVER_NAME) {
    8686                                $context_options['SNI_enabled'] = true;
     
    9292                        if (isset($options['verify'])) {
    9393                                if ($options['verify'] === false) {
    94                                         $context_options['verify_peer'] = false;
     94                                        $context_options['verify_peer']      = false;
     95                                        $context_options['verify_peer_name'] = false;
     96                                        $verifyname                          = false;
    9597                                }
    9698                                elseif (is_string($options['verify'])) {
     
    101103                        if (isset($options['verifyname']) && $options['verifyname'] === false) {
    102104                                $context_options['verify_peer_name'] = false;
    103                                 $verifyname = false;
     105                                $verifyname                          = false;
    104106                        }
    105107
     
    117119                $remote_socket .= ':' . $url_parts['port'];
    118120
     121                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
    119122                set_error_handler(array($this, 'connect_error_handler'), E_WARNING | E_NOTICE);
    120123
     
    151154
    152155                $request_body = '';
    153                 $out = sprintf("%s %s HTTP/%.1F\r\n", $options['type'], $path, $options['protocol_version']);
     156                $out          = sprintf("%s %s HTTP/%.1F\r\n", $options['type'], $path, $options['protocol_version']);
    154157
    155158                if ($options['type'] !== Requests::TRACE) {
    156159                        if (is_array($data)) {
    157                                 $request_body = http_build_query($data, null, '&');
     160                                $request_body = http_build_query($data, '', '&');
    158161                        }
    159162                        else {
     
    161164                        }
    162165
    163                         if (!empty($data)) {
     166                        // Always include Content-length on POST requests to prevent
     167                        // 411 errors from some servers when the body is empty.
     168                        if (!empty($data) || $options['type'] === Requests::POST) {
    164169                                if (!isset($case_insensitive_headers['Content-Length'])) {
    165170                                        $headers['Content-Length'] = strlen($request_body);
     
    175180                        $out .= sprintf('Host: %s', $url_parts['host']);
    176181
    177                         if (( 'http' === strtolower($url_parts['scheme']) && $url_parts['port'] !== 80 ) || ( 'https' === strtolower($url_parts['scheme']) && $url_parts['port'] !== 443 )) {
     182                        if ((strtolower($url_parts['scheme']) === 'http' && $url_parts['port'] !== 80) || (strtolower($url_parts['scheme']) === 'https' && $url_parts['port'] !== 443)) {
    178183                                $out .= ':' . $url_parts['port'];
    179184                        }
     
    221226
    222227                $timeout_sec = (int) floor($options['timeout']);
    223                 if ($timeout_sec == $options['timeout']) {
     228                if ($timeout_sec === $options['timeout']) {
    224229                        $timeout_msec = 0;
    225230                }
     
    229234                stream_set_timeout($socket, $timeout_sec, $timeout_msec);
    230235
    231                 $response = $body = $headers = '';
     236                $response   = '';
     237                $body       = '';
     238                $headers    = '';
    232239                $this->info = stream_get_meta_data($socket);
    233                 $size = 0;
    234                 $doingbody = false;
    235                 $download = false;
     240                $size       = 0;
     241                $doingbody  = false;
     242                $download   = false;
    236243                if ($options['filename']) {
    237244                        $download = fopen($options['filename'], 'wb');
     
    249256                                if (strpos($response, "\r\n\r\n")) {
    250257                                        list($headers, $block) = explode("\r\n\r\n", $response, 2);
    251                                         $doingbody = true;
     258                                        $doingbody             = true;
    252259                                }
    253260                        }
     
    265272                                                // Limit the length
    266273                                                $limited_length = ($this->max_bytes - $size);
    267                                                 $block = substr($block, 0, $limited_length);
     274                                                $block          = substr($block, 0, $limited_length);
    268275                                        }
    269276                                }
     
    301308        public function request_multiple($requests, $options) {
    302309                $responses = array();
    303                 $class = get_class($this);
     310                $class     = get_class($this);
    304311                foreach ($requests as $id => $request) {
    305312                        try {
    306                                 $handler = new $class();
     313                                $handler        = new $class();
    307314                                $responses[$id] = $handler->request($request['url'], $request['headers'], $request['data'], $request['options']);
    308315
     
    354361                        }
    355362
    356                         $url_parts['query'] .= '&' . http_build_query($data, null, '&');
    357                         $url_parts['query'] = trim($url_parts['query'], '&');
     363                        $url_parts['query'] .= '&' . http_build_query($data, '', '&');
     364                        $url_parts['query']  = trim($url_parts['query'], '&');
    358365                }
    359366                if (isset($url_parts['path'])) {
  • trunk/src/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php

    r46586 r50842  
    4747         *
    4848         * @param string $key Item key
    49          * @return string Item value
     49         * @return string|null Item value (null if offsetExists is false)
    5050         */
    5151        public function offsetGet($key) {
     
    7171                }
    7272
    73                 $key = strtolower($key);
     73                $key              = strtolower($key);
    7474                $this->data[$key] = $value;
    7575        }
  • trunk/src/wp-includes/Requests/Utility/FilteredIterator.php

    r49382 r50842  
    4040        public function current() {
    4141                $value = parent::current();
    42                 $value = call_user_func($this->callback, $value);
     42
     43                if (is_callable($this->callback)) {
     44                        $value = call_user_func($this->callback, $value);
     45                }
     46
    4347                return $value;
    4448        }
     
    4751         * @inheritdoc
    4852         */
    49         public function unserialize( $serialized ) {
    50         }
     53        public function unserialize($serialized) {}
    5154
    5255        /**
    5356         * @inheritdoc
     57         *
     58         * @phpcs:disable PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.MethodDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
    5459         */
    55         public function __unserialize( $serialized ) { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.MethodDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__unserializeFound
    56         }
     60        public function __unserialize($serialized) {}
    5761
    58         public function __wakeup() { // phpcs:ignore PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.MethodDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.NewMagicMethods.__wakeupFound
    59                 unset( $this->callback );
     62        public function __wakeup() {
     63                unset($this->callback);
    6064        }
    6165}
  • trunk/src/wp-includes/class-requests.php

    r47902 r50842  
    8989         * @var string
    9090         */
    91         const VERSION = '1.7-3470169';
     91        const VERSION = '1.7';
    9292
    9393        /**
     
    144144                $file = str_replace('_', '/', $class);
    145145                if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
    146                         require_once(dirname(__FILE__) . '/' . $file . '.php');
     146                        require_once dirname(__FILE__) . '/' . $file . '.php';
    147147                }
    148148        }
     
    343343         *    (string|boolean, default: library/Requests/Transport/cacert.pem)
    344344         * - `verifyname`: Should we verify the common name in the SSL certificate?
    345          *    (boolean: default, true)
     345         *    (boolean, default: true)
    346346         * - `data_format`: How should we send the `$data` parameter?
    347347         *    (string, one of 'query' or 'body', default: 'query' for
     
    375375                }
    376376                else {
    377                         $need_ssl = (0 === stripos($url, 'https://'));
     377                        $need_ssl     = (stripos($url, 'https://') === 0);
    378378                        $capabilities = array('ssl' => $need_ssl);
    379                         $transport = self::get_transport($capabilities);
     379                        $transport    = self::get_transport($capabilities);
    380380                }
    381381                $response = $transport->request($url, $headers, $data, $options);
     
    448448                        }
    449449                        if (!isset($request['options'])) {
    450                                 $request['options'] = $options;
     450                                $request['options']         = $options;
    451451                                $request['options']['type'] = $request['type'];
    452452                        }
     
    504504        protected static function get_default_options($multirequest = false) {
    505505                $defaults = array(
    506                         'timeout' => 10,
    507                         'connect_timeout' => 10,
    508                         'useragent' => 'php-requests/' . self::VERSION,
     506                        'timeout'          => 10,
     507                        'connect_timeout'  => 10,
     508                        'useragent'        => 'php-requests/' . self::VERSION,
    509509                        'protocol_version' => 1.1,
    510                         'redirected' => 0,
    511                         'redirects' => 10,
     510                        'redirected'       => 0,
     511                        'redirects'        => 10,
    512512                        'follow_redirects' => true,
    513                         'blocking' => true,
    514                         'type' => self::GET,
    515                         'filename' => false,
    516                         'auth' => false,
    517                         'proxy' => false,
    518                         'cookies' => false,
    519                         'max_bytes' => false,
    520                         'idn' => true,
    521                         'hooks' => null,
    522                         'transport' => null,
    523                         'verify' => Requests::get_certificate_path(),
    524                         'verifyname' => true,
     513                        'blocking'         => true,
     514                        'type'             => self::GET,
     515                        'filename'         => false,
     516                        'auth'             => false,
     517                        'proxy'            => false,
     518                        'cookies'          => false,
     519                        'max_bytes'        => false,
     520                        'idn'              => true,
     521                        'hooks'            => null,
     522                        'transport'        => null,
     523                        'verify'           => self::get_certificate_path(),
     524                        'verifyname'       => true,
    525525                );
    526526                if ($multirequest !== false) {
     
    536536         */
    537537        public static function get_certificate_path() {
    538                 if ( ! empty( Requests::$certificate_path ) ) {
    539                         return Requests::$certificate_path;
     538                if (!empty(self::$certificate_path)) {
     539                        return self::$certificate_path;
    540540                }
    541541
     
    548548         * @param string $path Certificate path, pointing to a PEM file.
    549549         */
    550         public static function set_certificate_path( $path ) {
    551                 Requests::$certificate_path = $path;
     550        public static function set_certificate_path($path) {
     551                self::$certificate_path = $path;
    552552        }
    553553
     
    596596
    597597                if ($options['idn'] !== false) {
    598                         $iri = new Requests_IRI($url);
     598                        $iri       = new Requests_IRI($url);
    599599                        $iri->host = Requests_IDNAEncoder::encode($iri->ihost);
    600                         $url = $iri->uri;
     600                        $url       = $iri->uri;
    601601                }
    602602
     
    605605
    606606                if (!isset($options['data_format'])) {
    607                         if (in_array($type, array(self::HEAD, self::GET, self::DELETE))) {
     607                        if (in_array($type, array(self::HEAD, self::GET, self::DELETE), true)) {
    608608                                $options['data_format'] = 'query';
    609609                        }
     
    634634                }
    635635
    636                 $return->raw = $headers;
    637                 $return->url = $url;
     636                $return->raw  = $headers;
     637                $return->url  = (string) $url;
     638                $return->body = '';
    638639
    639640                if (!$options['filename']) {
    640                         if (($pos = strpos($headers, "\r\n\r\n")) === false) {
     641                        $pos = strpos($headers, "\r\n\r\n");
     642                        if ($pos === false) {
    641643                                // Crap!
    642644                                throw new Requests_Exception('Missing header/body separator', 'requests.no_crlf_separator');
     
    644646
    645647                        $headers = substr($return->raw, 0, $pos);
    646                         $return->body = substr($return->raw, $pos + strlen("\n\r\n\r"));
    647                 }
    648                 else {
    649                         $return->body = '';
     648                        // Headers will always be separated from the body by two new lines - `\n\r\n\r`.
     649                        $body = substr($return->raw, $pos + 4);
     650                        if (!empty($body)) {
     651                                $return->body = $body;
     652                        }
    650653                }
    651654                // Pretend CRLF = LF for compatibility (RFC 2616, section 19.3)
     
    659662                }
    660663                $return->protocol_version = (float) $matches[1];
    661                 $return->status_code = (int) $matches[2];
     664                $return->status_code      = (int) $matches[2];
    662665                if ($return->status_code >= 200 && $return->status_code < 300) {
    663666                        $return->success = true;
     
    666669                foreach ($headers as $header) {
    667670                        list($key, $value) = explode(':', $header, 2);
    668                         $value = trim($value);
     671                        $value             = trim($value);
    669672                        preg_replace('#(\s+)#i', ' ', $value);
    670673                        $return->headers[$key] = $value;
     
    703706                                        &$req_data,
    704707                                        &$options,
    705                                         $return
     708                                        $return,
    706709                                );
    707710                                $options['hooks']->dispatch('requests.before_redirect', $hook_args);
    708                                 $redirected = self::request($location, $req_headers, $req_data, $options['type'], $options);
     711                                $redirected            = self::request($location, $req_headers, $req_data, $options['type'], $options);
    709712                                $redirected->history[] = $return;
    710713                                return $redirected;
     
    733736        public static function parse_multiple(&$response, $request) {
    734737                try {
    735                         $url = $request['url'];
    736                         $headers = $request['headers'];
    737                         $data = $request['data'];
    738                         $options = $request['options'];
     738                        $url      = $request['url'];
     739                        $headers  = $request['headers'];
     740                        $data     = $request['data'];
     741                        $options  = $request['options'];
    739742                        $response = self::parse_response($response, $url, $headers, $data, $options);
    740743                }
     
    755758                        return $data;
    756759                }
    757 
    758 
    759760
    760761                $decoded = '';
     
    775776
    776777                        $chunk_length = strlen($matches[0]);
    777                         $decoded .= substr($encoded, $chunk_length, $length);
    778                         $encoded = substr($encoded, $chunk_length + $length + 2);
     778                        $decoded     .= substr($encoded, $chunk_length, $length);
     779                        $encoded      = substr($encoded, $chunk_length + $length + 2);
    779780
    780781                        if (trim($encoded) === '0' || empty($encoded)) {
     
    792793         *
    793794         * @param array $array Dictionary of header values
    794          * @return string[] List of headers
     795         * @return array List of headers
    795796         */
    796797        public static function flatten($array) {
     
    808809         * @deprecated Misspelling of {@see Requests::flatten}
    809810         * @param array $array Dictionary of header values
    810          * @return string[] List of headers
     811         * @return array List of headers
    811812         */
    812813        public static function flattern($array) {
     
    829830                }
    830831
    831                 if (function_exists('gzdecode') && ($decoded = @gzdecode($data)) !== false) {
     832                if (function_exists('gzdecode')) {
     833                        // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.gzdecodeFound -- Wrapped in function_exists() for PHP 5.2.
     834                        $decoded = @gzdecode($data);
     835                        if ($decoded !== false) {
     836                                return $decoded;
     837                        }
     838                }
     839
     840                if (function_exists('gzinflate')) {
     841                        $decoded = @gzinflate($data);
     842                        if ($decoded !== false) {
     843                                return $decoded;
     844                        }
     845                }
     846
     847                $decoded = self::compatible_gzinflate($data);
     848                if ($decoded !== false) {
    832849                        return $decoded;
    833850                }
    834                 elseif (function_exists('gzinflate') && ($decoded = @gzinflate($data)) !== false) {
    835                         return $decoded;
    836                 }
    837                 elseif (($decoded = self::compatible_gzinflate($data)) !== false) {
    838                         return $decoded;
    839                 }
    840                 elseif (function_exists('gzuncompress') && ($decoded = @gzuncompress($data)) !== false) {
    841                         return $decoded;
     851
     852                if (function_exists('gzuncompress')) {
     853                        $decoded = @gzuncompress($data);
     854                        if ($decoded !== false) {
     855                                return $decoded;
     856                        }
    842857                }
    843858
     
    862877         * @link https://secure.php.net/manual/en/function.gzinflate.php#77336
    863878         *
    864          * @param string $gzData String to decompress.
     879         * @param string $gz_data String to decompress.
    865880         * @return string|bool False on failure.
    866881         */
    867         public static function compatible_gzinflate($gzData) {
     882        public static function compatible_gzinflate($gz_data) {
    868883                // Compressed data might contain a full zlib header, if so strip it for
    869884                // gzinflate()
    870                 if (substr($gzData, 0, 3) == "\x1f\x8b\x08") {
    871                         $i = 10;
    872                         $flg = ord(substr($gzData, 3, 1));
     885                if (substr($gz_data, 0, 3) === "\x1f\x8b\x08") {
     886                        $i   = 10;
     887                        $flg = ord(substr($gz_data, 3, 1));
    873888                        if ($flg > 0) {
    874889                                if ($flg & 4) {
    875                                         list($xlen) = unpack('v', substr($gzData, $i, 2));
    876                                         $i = $i + 2 + $xlen;
     890                                        list($xlen) = unpack('v', substr($gz_data, $i, 2));
     891                                        $i         += 2 + $xlen;
    877892                                }
    878893                                if ($flg & 8) {
    879                                         $i = strpos($gzData, "\0", $i) + 1;
     894                                        $i = strpos($gz_data, "\0", $i) + 1;
    880895                                }
    881896                                if ($flg & 16) {
    882                                         $i = strpos($gzData, "\0", $i) + 1;
     897                                        $i = strpos($gz_data, "\0", $i) + 1;
    883898                                }
    884899                                if ($flg & 2) {
    885                                         $i = $i + 2;
     900                                        $i += 2;
    886901                                }
    887902                        }
    888                         $decompressed = self::compatible_gzinflate(substr($gzData, $i));
    889                         if (false !== $decompressed) {
     903                        $decompressed = self::compatible_gzinflate(substr($gz_data, $i));
     904                        if ($decompressed !== false) {
    890905                                return $decompressed;
    891906                        }
     
    903918
    904919                // low nibble of first byte should be 0x08
    905                 list(, $first_nibble)    = unpack('h', $gzData);
     920                list(, $first_nibble) = unpack('h', $gz_data);
    906921
    907922                // First 2 bytes should be divisible by 0x1F
    908                 list(, $first_two_bytes) = unpack('n', $gzData);
    909 
    910                 if (0x08 == $first_nibble && 0 == ($first_two_bytes % 0x1F)) {
     923                list(, $first_two_bytes) = unpack('n', $gz_data);
     924
     925                if ($first_nibble === 0x08 && ($first_two_bytes % 0x1F) === 0) {
    911926                        $huffman_encoded = true;
    912927                }
    913928
    914929                if ($huffman_encoded) {
    915                         if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) {
     930                        $decompressed = @gzinflate(substr($gz_data, 2));
     931                        if ($decompressed !== false) {
    916932                                return $decompressed;
    917933                        }
    918934                }
    919935
    920                 if ("\x50\x4b\x03\x04" == substr($gzData, 0, 4)) {
     936                if (substr($gz_data, 0, 4) === "\x50\x4b\x03\x04") {
    921937                        // ZIP file format header
    922938                        // Offset 6: 2 bytes, General-purpose field
     
    925941                        // Offset 30: Filename field, followed by optional field, followed
    926942                        // immediately by data
    927                         list(, $general_purpose_flag) = unpack('v', substr($gzData, 6, 2));
     943                        list(, $general_purpose_flag) = unpack('v', substr($gz_data, 6, 2));
    928944
    929945                        // If the file has been compressed on the fly, 0x08 bit is set of
    930946                        // the general purpose field. We can use this to differentiate
    931947                        // between a compressed document, and a ZIP file
    932                         $zip_compressed_on_the_fly = (0x08 == (0x08 & $general_purpose_flag));
     948                        $zip_compressed_on_the_fly = ((0x08 & $general_purpose_flag) === 0x08);
    933949
    934950                        if (!$zip_compressed_on_the_fly) {
    935951                                // Don't attempt to decode a compressed zip file
    936                                 return $gzData;
     952                                return $gz_data;
    937953                        }
    938954
    939955                        // Determine the first byte of data, based on the above ZIP header
    940956                        // offsets:
    941                         $first_file_start = array_sum(unpack('v2', substr($gzData, 26, 4)));
    942                         if (false !== ($decompressed = @gzinflate(substr($gzData, 30 + $first_file_start)))) {
     957                        $first_file_start = array_sum(unpack('v2', substr($gz_data, 26, 4)));
     958                        $decompressed     = @gzinflate(substr($gz_data, 30 + $first_file_start));
     959                        if ($decompressed !== false) {
    943960                                return $decompressed;
    944961                        }
     
    947964
    948965                // Finally fall back to straight gzinflate
    949                 if (false !== ($decompressed = @gzinflate($gzData))) {
     966                $decompressed = @gzinflate($gz_data);
     967                if ($decompressed !== false) {
    950968                        return $decompressed;
    951969                }
     
    953971                // Fallback for all above failing, not expected, but included for
    954972                // debugging and preventing regressions and to track stats
    955                 if (false !== ($decompressed = @gzinflate(substr($gzData, 2)))) {
     973                $decompressed = @gzinflate(substr($gz_data, 2));
     974                if ($decompressed !== false) {
    956975                        return $decompressed;
    957976                }
Note: See TracChangeset for help on using the changeset viewer.