Make WordPress Core


Ignore:
Timestamp:
09/30/2024 10:48:16 PM (7 months ago)
Author:
SergeyBiryukov
Message:

External Libraries: Update the SimplePie library to version 1.8.0.

The most notable change in this update is that all code is now namespaced and uses PSR-4 classes, though there is a compatibility layer available for extenders using the older class names, so plugin or theme authors directly using SimplePie can decide for themselves when they want to change to using the namespaced names for SimplePie classes.

Note: This commit includes additional fixes for PHP 8.4 compatibility (PR 875, PR 888) from the one-dot-eight branch of SimplePie, which is expected to be released as SimplePie 1.8.1 soon.

References:

Follow-up to [47733], [49176], [52393], [52413].

Props jrf, peterwilsoncc, chaion07, cu121, markparnell, audrasjb, costdev, Presskopp, desrosj, faisal03, mukesh27, SergeyBiryukov.
See #55604.

Location:
trunk/src/wp-includes/SimplePie/src
Files:
1 added
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/SimplePie/src/Net/IPv6.php

    r47733 r59141  
    11<?php
     2
    23/**
    34 * SimplePie
     
    67 * Takes the hard work out of managing a complete RSS/Atom solution.
    78 *
    8  * Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
     9 * Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
    910 * All rights reserved.
    1011 *
     
    4243 */
    4344
     45namespace SimplePie\Net;
    4446
    4547/**
     
    5658 * @author Sam Sneddon <geoffers@gmail.com>
    5759 */
    58 class SimplePie_Net_IPv6
     60class IPv6
    5961{
    60     /**
    61      * Uncompresses an IPv6 address
    62      *
    63      * RFC 4291 allows you to compress concecutive zero pieces in an address to
    64      * '::'. This method expects a valid IPv6 address and expands the '::' to
    65      * the required number of zero pieces.
    66      *
    67      * Example:  FF01::101   ->  FF01:0:0:0:0:0:0:101
    68      *           ::1         ->  0:0:0:0:0:0:0:1
    69      *
    70      * @author Alexander Merz <alexander.merz@web.de>
    71      * @author elfrink at introweb dot nl
    72      * @author Josh Peck <jmp at joshpeck dot org>
    73      * @copyright 2003-2005 The PHP Group
    74      * @license http://www.opensource.org/licenses/bsd-license.php
    75      * @param string $ip An IPv6 address
    76      * @return string The uncompressed IPv6 address
    77      */
    78     public static function uncompress($ip)
    79     {
    80         $c1 = -1;
    81         $c2 = -1;
    82         if (substr_count($ip, '::') === 1)
    83         {
    84             list($ip1, $ip2) = explode('::', $ip);
    85             if ($ip1 === '')
    86             {
    87                 $c1 = -1;
    88             }
    89             else
    90             {
    91                 $c1 = substr_count($ip1, ':');
    92             }
    93             if ($ip2 === '')
    94             {
    95                 $c2 = -1;
    96             }
    97             else
    98             {
    99                 $c2 = substr_count($ip2, ':');
    100             }
    101             if (strpos($ip2, '.') !== false)
    102             {
    103                 $c2++;
    104             }
    105             // ::
    106             if ($c1 === -1 && $c2 === -1)
    107             {
    108                 $ip = '0:0:0:0:0:0:0:0';
    109             }
    110             // ::xxx
    111             else if ($c1 === -1)
    112             {
    113                 $fill = str_repeat('0:', 7 - $c2);
    114                 $ip = str_replace('::', $fill, $ip);
    115             }
    116             // xxx::
    117             else if ($c2 === -1)
    118             {
    119                 $fill = str_repeat(':0', 7 - $c1);
    120                 $ip = str_replace('::', $fill, $ip);
    121             }
    122             // xxx::xxx
    123             else
    124             {
    125                 $fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
    126                 $ip = str_replace('::', $fill, $ip);
    127             }
    128         }
    129         return $ip;
    130     }
    131 
    132     /**
    133      * Compresses an IPv6 address
    134      *
    135      * RFC 4291 allows you to compress concecutive zero pieces in an address to
    136      * '::'. This method expects a valid IPv6 address and compresses consecutive
    137      * zero pieces to '::'.
    138      *
    139      * Example:  FF01:0:0:0:0:0:0:101   ->  FF01::101
    140      *           0:0:0:0:0:0:0:1        ->  ::1
    141      *
    142      * @see uncompress()
    143      * @param string $ip An IPv6 address
    144      * @return string The compressed IPv6 address
    145      */
    146     public static function compress($ip)
    147     {
    148         // Prepare the IP to be compressed
    149         $ip = self::uncompress($ip);
    150         $ip_parts = self::split_v6_v4($ip);
    151 
    152         // Replace all leading zeros
    153         $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
    154 
    155         // Find bunches of zeros
    156         if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE))
    157         {
    158             $max = 0;
    159             $pos = null;
    160             foreach ($matches[0] as $match)
    161             {
    162                 if (strlen($match[0]) > $max)
    163                 {
    164                     $max = strlen($match[0]);
    165                     $pos = $match[1];
    166                 }
    167             }
    168 
    169             $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
    170         }
    171 
    172         if ($ip_parts[1] !== '')
    173         {
    174             return implode(':', $ip_parts);
    175         }
    176 
    177         return $ip_parts[0];
    178     }
    179 
    180     /**
    181      * Splits an IPv6 address into the IPv6 and IPv4 representation parts
    182      *
    183      * RFC 4291 allows you to represent the last two parts of an IPv6 address
    184      * using the standard IPv4 representation
    185      *
    186      * Example:  0:0:0:0:0:0:13.1.68.3
    187      *           0:0:0:0:0:FFFF:129.144.52.38
    188      *
    189      * @param string $ip An IPv6 address
    190      * @return array [0] contains the IPv6 represented part, and [1] the IPv4 represented part
    191      */
    192     private static function split_v6_v4($ip)
    193     {
    194         if (strpos($ip, '.') !== false)
    195         {
    196             $pos = strrpos($ip, ':');
    197             $ipv6_part = substr($ip, 0, $pos);
    198             $ipv4_part = substr($ip, $pos + 1);
    199             return array($ipv6_part, $ipv4_part);
    200         }
    201 
    202         return array($ip, '');
    203     }
    204 
    205     /**
    206      * Checks an IPv6 address
    207      *
    208      * Checks if the given IP is a valid IPv6 address
    209      *
    210      * @param string $ip An IPv6 address
    211      * @return bool true if $ip is a valid IPv6 address
    212      */
    213     public static function check_ipv6($ip)
    214     {
    215         $ip = self::uncompress($ip);
    216         list($ipv6, $ipv4) = self::split_v6_v4($ip);
    217         $ipv6 = explode(':', $ipv6);
    218         $ipv4 = explode('.', $ipv4);
    219         if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4)
    220         {
    221             foreach ($ipv6 as $ipv6_part)
    222             {
    223                 // The section can't be empty
    224                 if ($ipv6_part === '')
    225                     return false;
    226 
    227                 // Nor can it be over four characters
    228                 if (strlen($ipv6_part) > 4)
    229                     return false;
    230 
    231                 // Remove leading zeros (this is safe because of the above)
    232                 $ipv6_part = ltrim($ipv6_part, '0');
    233                 if ($ipv6_part === '')
    234                     $ipv6_part = '0';
    235 
    236                 // Check the value is valid
    237                 $value = hexdec($ipv6_part);
    238                 if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF)
    239                     return false;
    240             }
    241             if (count($ipv4) === 4)
    242             {
    243                 foreach ($ipv4 as $ipv4_part)
    244                 {
    245                     $value = (int) $ipv4_part;
    246                     if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF)
    247                         return false;
    248                 }
    249             }
    250             return true;
    251         }
    252 
    253         return false;
    254     }
    255 
    256     /**
    257      * Checks if the given IP is a valid IPv6 address
    258      *
    259      * @codeCoverageIgnore
    260      * @deprecated Use {@see SimplePie_Net_IPv6::check_ipv6()} instead
    261      * @see check_ipv6
    262      * @param string $ip An IPv6 address
    263      * @return bool true if $ip is a valid IPv6 address
    264      */
    265     public static function checkIPv6($ip)
    266     {
    267         return self::check_ipv6($ip);
    268     }
     62    /**
     63     * Uncompresses an IPv6 address
     64     *
     65     * RFC 4291 allows you to compress concecutive zero pieces in an address to
     66     * '::'. This method expects a valid IPv6 address and expands the '::' to
     67     * the required number of zero pieces.
     68     *
     69     * Example:  FF01::101   ->  FF01:0:0:0:0:0:0:101
     70     *           ::1         ->  0:0:0:0:0:0:0:1
     71     *
     72     * @author Alexander Merz <alexander.merz@web.de>
     73     * @author elfrink at introweb dot nl
     74     * @author Josh Peck <jmp at joshpeck dot org>
     75     * @copyright 2003-2005 The PHP Group
     76     * @license http://www.opensource.org/licenses/bsd-license.php
     77     * @param string $ip An IPv6 address
     78     * @return string The uncompressed IPv6 address
     79     */
     80    public static function uncompress($ip)
     81    {
     82        $c1 = -1;
     83        $c2 = -1;
     84        if (substr_count($ip, '::') === 1) {
     85            [$ip1, $ip2] = explode('::', $ip);
     86            if ($ip1 === '') {
     87                $c1 = -1;
     88            } else {
     89                $c1 = substr_count($ip1, ':');
     90            }
     91            if ($ip2 === '') {
     92                $c2 = -1;
     93            } else {
     94                $c2 = substr_count($ip2, ':');
     95            }
     96            if (strpos($ip2, '.') !== false) {
     97                $c2++;
     98            }
     99            // ::
     100            if ($c1 === -1 && $c2 === -1) {
     101                $ip = '0:0:0:0:0:0:0:0';
     102            }
     103            // ::xxx
     104            elseif ($c1 === -1) {
     105                $fill = str_repeat('0:', 7 - $c2);
     106                $ip = str_replace('::', $fill, $ip);
     107            }
     108            // xxx::
     109            elseif ($c2 === -1) {
     110                $fill = str_repeat(':0', 7 - $c1);
     111                $ip = str_replace('::', $fill, $ip);
     112            }
     113            // xxx::xxx
     114            else {
     115                $fill = ':' . str_repeat('0:', 6 - $c2 - $c1);
     116                $ip = str_replace('::', $fill, $ip);
     117            }
     118        }
     119        return $ip;
     120    }
     121
     122    /**
     123     * Compresses an IPv6 address
     124     *
     125     * RFC 4291 allows you to compress concecutive zero pieces in an address to
     126     * '::'. This method expects a valid IPv6 address and compresses consecutive
     127     * zero pieces to '::'.
     128     *
     129     * Example:  FF01:0:0:0:0:0:0:101   ->  FF01::101
     130     *           0:0:0:0:0:0:0:1        ->  ::1
     131     *
     132     * @see uncompress()
     133     * @param string $ip An IPv6 address
     134     * @return string The compressed IPv6 address
     135     */
     136    public static function compress($ip)
     137    {
     138        // Prepare the IP to be compressed
     139        $ip = self::uncompress($ip);
     140        $ip_parts = self::split_v6_v4($ip);
     141
     142        // Replace all leading zeros
     143        $ip_parts[0] = preg_replace('/(^|:)0+([0-9])/', '\1\2', $ip_parts[0]);
     144
     145        // Find bunches of zeros
     146        if (preg_match_all('/(?:^|:)(?:0(?::|$))+/', $ip_parts[0], $matches, PREG_OFFSET_CAPTURE)) {
     147            $max = 0;
     148            $pos = null;
     149            foreach ($matches[0] as $match) {
     150                if (strlen($match[0]) > $max) {
     151                    $max = strlen($match[0]);
     152                    $pos = $match[1];
     153                }
     154            }
     155
     156            $ip_parts[0] = substr_replace($ip_parts[0], '::', $pos, $max);
     157        }
     158
     159        if ($ip_parts[1] !== '') {
     160            return implode(':', $ip_parts);
     161        }
     162
     163        return $ip_parts[0];
     164    }
     165
     166    /**
     167     * Splits an IPv6 address into the IPv6 and IPv4 representation parts
     168     *
     169     * RFC 4291 allows you to represent the last two parts of an IPv6 address
     170     * using the standard IPv4 representation
     171     *
     172     * Example:  0:0:0:0:0:0:13.1.68.3
     173     *           0:0:0:0:0:FFFF:129.144.52.38
     174     *
     175     * @param string $ip An IPv6 address
     176     * @return array [0] contains the IPv6 represented part, and [1] the IPv4 represented part
     177     */
     178    private static function split_v6_v4($ip)
     179    {
     180        if (strpos($ip, '.') !== false) {
     181            $pos = strrpos($ip, ':');
     182            $ipv6_part = substr($ip, 0, $pos);
     183            $ipv4_part = substr($ip, $pos + 1);
     184            return [$ipv6_part, $ipv4_part];
     185        }
     186
     187        return [$ip, ''];
     188    }
     189
     190    /**
     191     * Checks an IPv6 address
     192     *
     193     * Checks if the given IP is a valid IPv6 address
     194     *
     195     * @param string $ip An IPv6 address
     196     * @return bool true if $ip is a valid IPv6 address
     197     */
     198    public static function check_ipv6($ip)
     199    {
     200        $ip = self::uncompress($ip);
     201        [$ipv6, $ipv4] = self::split_v6_v4($ip);
     202        $ipv6 = explode(':', $ipv6);
     203        $ipv4 = explode('.', $ipv4);
     204        if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4) {
     205            foreach ($ipv6 as $ipv6_part) {
     206                // The section can't be empty
     207                if ($ipv6_part === '') {
     208                    return false;
     209                }
     210
     211                // Nor can it be over four characters
     212                if (strlen($ipv6_part) > 4) {
     213                    return false;
     214                }
     215
     216                // Remove leading zeros (this is safe because of the above)
     217                $ipv6_part = ltrim($ipv6_part, '0');
     218                if ($ipv6_part === '') {
     219                    $ipv6_part = '0';
     220                }
     221
     222                // Check the value is valid
     223                $value = hexdec($ipv6_part);
     224                if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF) {
     225                    return false;
     226                }
     227            }
     228            if (count($ipv4) === 4) {
     229                foreach ($ipv4 as $ipv4_part) {
     230                    $value = (int) $ipv4_part;
     231                    if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF) {
     232                        return false;
     233                    }
     234                }
     235            }
     236            return true;
     237        }
     238
     239        return false;
     240    }
     241
     242    /**
     243     * Checks if the given IP is a valid IPv6 address
     244     *
     245     * @codeCoverageIgnore
     246     * @deprecated Use {@see IPv6::check_ipv6()} instead
     247     * @see check_ipv6
     248     * @param string $ip An IPv6 address
     249     * @return bool true if $ip is a valid IPv6 address
     250     */
     251    public static function checkIPv6($ip)
     252    {
     253        return self::check_ipv6($ip);
     254    }
    269255}
     256
     257class_alias('SimplePie\Net\IPv6', 'SimplePie_Net_IPv6');
Note: See TracChangeset for help on using the changeset viewer.