wp/wp-includes/random_compat/byte_safe_strings.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 19 3d72ae0968f4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
     3  * Random_* Compatibility Library
     3  * Random_* Compatibility Library
     4  * for using the new PHP 7 random_* API in PHP 5 projects
     4  * for using the new PHP 7 random_* API in PHP 5 projects
     5  *
     5  *
     6  * The MIT License (MIT)
     6  * The MIT License (MIT)
     7  *
     7  *
     8  * Copyright (c) 2015 Paragon Initiative Enterprises
     8  * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises
     9  *
     9  *
    10  * Permission is hereby granted, free of charge, to any person obtaining a copy
    10  * Permission is hereby granted, free of charge, to any person obtaining a copy
    11  * of this software and associated documentation files (the "Software"), to deal
    11  * of this software and associated documentation files (the "Software"), to deal
    12  * in the Software without restriction, including without limitation the rights
    12  * in the Software without restriction, including without limitation the rights
    13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    26  * SOFTWARE.
    26  * SOFTWARE.
    27  */
    27  */
    28 
    28 
    29 if (!function_exists('RandomCompat_strlen')) {
    29 if (!is_callable('RandomCompat_strlen')) {
    30     if (
    30     if (
    31         defined('MB_OVERLOAD_STRING') &&
    31         defined('MB_OVERLOAD_STRING') &&
    32         ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
    32         ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
    33     ) {
    33     ) {
    34         /**
    34         /**
    49                 throw new TypeError(
    49                 throw new TypeError(
    50                     'RandomCompat_strlen() expects a string'
    50                     'RandomCompat_strlen() expects a string'
    51                 );
    51                 );
    52             }
    52             }
    53 
    53 
    54             return mb_strlen($binary_string, '8bit');
    54             return (int) mb_strlen($binary_string, '8bit');
    55         }
    55         }
    56 
    56 
    57     } else {
    57     } else {
    58         /**
    58         /**
    59          * strlen() implementation that isn't brittle to mbstring.func_overload
    59          * strlen() implementation that isn't brittle to mbstring.func_overload
    71             if (!is_string($binary_string)) {
    71             if (!is_string($binary_string)) {
    72                 throw new TypeError(
    72                 throw new TypeError(
    73                     'RandomCompat_strlen() expects a string'
    73                     'RandomCompat_strlen() expects a string'
    74                 );
    74                 );
    75             }
    75             }
    76             return strlen($binary_string);
    76             return (int) strlen($binary_string);
    77         }
    77         }
    78     }
    78     }
    79 }
    79 }
    80 
    80 
    81 if (!function_exists('RandomCompat_substr')) {
    81 if (!is_callable('RandomCompat_substr')) {
    82 
    82 
    83     if (
    83     if (
    84         defined('MB_OVERLOAD_STRING')
    84         defined('MB_OVERLOAD_STRING')
    85         &&
    85         &&
    86         ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
    86         ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
   116             if ($length === null) {
   116             if ($length === null) {
   117                 /**
   117                 /**
   118                  * mb_substr($str, 0, NULL, '8bit') returns an empty string on
   118                  * mb_substr($str, 0, NULL, '8bit') returns an empty string on
   119                  * PHP 5.3, so we have to find the length ourselves.
   119                  * PHP 5.3, so we have to find the length ourselves.
   120                  */
   120                  */
   121                 $length = RandomCompat_strlen($length) - $start;
   121                 $length = RandomCompat_strlen($binary_string) - $start;
   122             } elseif (!is_int($length)) {
   122             } elseif (!is_int($length)) {
   123                 throw new TypeError(
   123                 throw new TypeError(
   124                     'RandomCompat_substr(): Third argument should be an integer, or omitted'
   124                     'RandomCompat_substr(): Third argument should be an integer, or omitted'
   125                 );
   125                 );
   126             }
   126             }
   127 
   127 
   128             return mb_substr($binary_string, $start, $length, '8bit');
   128             // Consistency with PHP's behavior
       
   129             if ($start === RandomCompat_strlen($binary_string) && $length === 0) {
       
   130                 return '';
       
   131             }
       
   132             if ($start > RandomCompat_strlen($binary_string)) {
       
   133                 return '';
       
   134             }
       
   135 
       
   136             return (string) mb_substr($binary_string, $start, $length, '8bit');
   129         }
   137         }
   130 
   138 
   131     } else {
   139     } else {
   132 
   140 
   133         /**
   141         /**
   162                     throw new TypeError(
   170                     throw new TypeError(
   163                         'RandomCompat_substr(): Third argument should be an integer, or omitted'
   171                         'RandomCompat_substr(): Third argument should be an integer, or omitted'
   164                     );
   172                     );
   165                 }
   173                 }
   166 
   174 
   167                 return substr($binary_string, $start, $length);
   175                 return (string) substr($binary_string, $start, $length);
   168             }
   176             }
   169 
   177 
   170             return substr($binary_string, $start);
   178             return (string) substr($binary_string, $start);
   171         }
   179         }
   172     }
   180     }
   173 }
   181 }