Make WordPress Core

Changeset 36886


Ignore:
Timestamp:
03/08/2016 05:14:52 PM (8 years ago)
Author:
ocean90
Message:

Update Random_Compat from 1.1.6 to 1.2.1.

Changes: https://github.com/paragonie/random_compat/compare/1.1.6...v1.2.1

See #35665.

Location:
trunk/src/wp-includes/random_compat
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/random_compat/byte_safe_strings.php

    r35365 r36886  
    5151                );
    5252            }
     53
    5354            return mb_strlen($binary_string, '8bit');
    5455        }
     56
    5557    } else {
    5658        /**
     
    7880
    7981if (!function_exists('RandomCompat_substr')) {
     82
    8083    if (
    81         defined('MB_OVERLOAD_STRING') &&
     84        defined('MB_OVERLOAD_STRING')
     85        &&
    8286        ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
    8387    ) {
     
    103107                );
    104108            }
     109
    105110            if (!is_int($start)) {
    106111                throw new TypeError(
     
    108113                );
    109114            }
     115
    110116            if ($length === null) {
    111117                /**
     
    119125                );
    120126            }
     127
    121128            return mb_substr($binary_string, $start, $length, '8bit');
    122129        }
     130
    123131    } else {
     132
    124133        /**
    125134         * substr() implementation that isn't brittle to mbstring.func_overload
     
    142151                );
    143152            }
     153
    144154            if (!is_int($start)) {
    145155                throw new TypeError(
     
    147157                );
    148158            }
     159
    149160            if ($length !== null) {
    150161                if (!is_int($length)) {
     
    153164                    );
    154165                }
     166
    155167                return substr($binary_string, $start, $length);
    156168            }
     169
    157170            return substr($binary_string, $start);
    158171        }
  • trunk/src/wp-includes/random_compat/cast_to_int.php

    r35365 r36886  
    3838     * through.
    3939     *
    40      * @param numeric $number The number we want to convert to an int
    41      * @param boolean $fail_open Set to true to not throw an exception
     40     * @param int|float $number    The number we want to convert to an int
     41     * @param boolean   $fail_open Set to true to not throw an exception
    4242     *
    4343     * @return int (or float if $fail_open)
     44     *
     45     * @throws TypeError
    4446     */
    4547    function RandomCompat_intval($number, $fail_open = false)
     
    4850            $number += 0;
    4951        }
     52
    5053        if (
    51             is_float($number) &&
    52             $number > ~PHP_INT_MAX &&
     54            is_float($number)
     55            &&
     56            $number > ~PHP_INT_MAX
     57            &&
    5358            $number < PHP_INT_MAX
    5459        ) {
    5560            $number = (int) $number;
    5661        }
     62
    5763        if (is_int($number) || $fail_open) {
    5864            return $number;
    5965        }
     66
    6067        throw new TypeError(
    6168            'Expected an integer.'
  • trunk/src/wp-includes/random_compat/random.php

    r36421 r36886  
    11<?php
    22/**
    3  * Random_* Compatibility Library 
     3 * Random_* Compatibility Library
    44 * for using the new PHP 7 random_* API in PHP 5 projects
    5  *
     5 *
     6 * @version 1.2.1
     7 * @released 2016-02-29
     8 *
    69 * The MIT License (MIT)
    7  * 
     10 *
    811 * Copyright (c) 2015 Paragon Initiative Enterprises
    9  * 
     12 *
    1013 * Permission is hereby granted, free of charge, to any person obtaining a copy
    1114 * of this software and associated documentation files (the "Software"), to deal
     
    1417 * copies of the Software, and to permit persons to whom the Software is
    1518 * furnished to do so, subject to the following conditions:
    16  * 
     19 *
    1720 * The above copyright notice and this permission notice shall be included in
    1821 * all copies or substantial portions of the Software.
    19  * 
     22 *
    2023 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    2124 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     
    3033    // This constant was introduced in PHP 5.2.7
    3134    $RandomCompatversion = explode('.', PHP_VERSION);
    32     define('PHP_VERSION_ID', ($RandomCompatversion[0] * 10000 + $RandomCompatversion[1] * 100 + $RandomCompatversion[2]));
     35    define(
     36        'PHP_VERSION_ID',
     37        $RandomCompatversion[0] * 10000
     38        + $RandomCompatversion[1] * 100
     39        + $RandomCompatversion[2]
     40    );
    3341    $RandomCompatversion = null;
    3442}
     43
    3544if (PHP_VERSION_ID < 70000) {
     45
    3646    if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
    3747        define('RANDOM_COMPAT_READ_BUFFER', 8);
    3848    }
     49
    3950    $RandomCompatDIR = dirname(__FILE__);
     51
    4052    require_once $RandomCompatDIR.'/byte_safe_strings.php';
    4153    require_once $RandomCompatDIR.'/cast_to_int.php';
    4254    require_once $RandomCompatDIR.'/error_polyfill.php';
     55
    4356    if (!function_exists('random_bytes')) {
    4457        /**
    4558         * PHP 5.2.0 - 5.6.x way to implement random_bytes()
    46          * 
     59         *
    4760         * We use conditional statements here to define the function in accordance
    4861         * to the operating environment. It's a micro-optimization.
    49          * 
     62         *
    5063         * In order of preference:
    5164         *   1. Use libsodium if available.
     
    5467         *   4. COM('CAPICOM.Utilities.1')->GetRandom()
    5568         *   5. openssl_random_pseudo_bytes() (absolute last resort)
    56          * 
     69         *
    5770         * See ERRATA.md for our reasoning behind this particular order
    5871         */
     
    6578            }
    6679        }
     80
    6781        /**
    6882         * Reading directly from /dev/urandom:
     
    7387            $RandomCompatUrandom = true;
    7488            $RandomCompat_basedir = ini_get('open_basedir');
     89
    7590            if (!empty($RandomCompat_basedir)) {
    7691                $RandomCompat_open_basedir = explode(
     
    8499                $RandomCompat_open_basedir = null;
    85100            }
     101
    86102            if (
    87                 !function_exists('random_bytes') &&
    88                 $RandomCompatUrandom &&
     103                !function_exists('random_bytes')
     104                &&
     105                $RandomCompatUrandom
     106                &&
    89107                @is_readable('/dev/urandom')
    90108            ) {
    91109                // Error suppression on is_readable() in case of an open_basedir
    92110                // or safe_mode failure. All we care about is whether or not we
    93                 // can read it at this point. If the PHP environment is going to 
    94                 // panic over trying to see if the file can be read in the first 
     111                // can read it at this point. If the PHP environment is going to
     112                // panic over trying to see if the file can be read in the first
    95113                // place, that is not helpful to us here.
    96114
     
    99117            }
    100118            // Unset variables after use
     119            $RandomCompat_basedir = null;
    101120            $RandomCompatUrandom = null;
    102             $RandomCompat_basedir = null;
    103         }
    104        
     121        }
     122
    105123        /**
    106124         * mcrypt_create_iv()
    107125         */
    108126        if (
    109             !function_exists('random_bytes') &&
    110             PHP_VERSION_ID >= 50307 &&
     127            !function_exists('random_bytes')
     128            &&
     129            PHP_VERSION_ID >= 50307
     130            &&
    111131            extension_loaded('mcrypt')
    112132        ) {
    113             // See random_bytes_mcrypt.php
    114             require_once $RandomCompatDIR.'/random_bytes_mcrypt.php';
    115         }
     133            // Prevent this code from hanging indefinitely on non-Windows;
     134            // see https://bugs.php.net/bug.php?id=69833
     135            if (
     136                DIRECTORY_SEPARATOR !== '/' ||
     137                (PHP_VERSION_ID <= 50609 || PHP_VERSION_ID >= 50613)
     138            ) {
     139                // See random_bytes_mcrypt.php
     140                require_once $RandomCompatDIR.'/random_bytes_mcrypt.php';
     141            }
     142        }
     143
    116144        if (
    117             !function_exists('random_bytes') &&
    118             extension_loaded('com_dotnet') &&
     145            !function_exists('random_bytes')
     146            &&
     147            extension_loaded('com_dotnet')
     148            &&
    119149            class_exists('COM')
    120150        ) {
    121151            $RandomCompat_disabled_classes = preg_split(
    122                 '#\s*,\s*#', 
     152                '#\s*,\s*#',
    123153                strtolower(ini_get('disable_classes'))
    124154            );
    125            
     155
    126156            if (!in_array('com', $RandomCompat_disabled_classes)) {
    127157                try {
     
    138168            $RandomCompatCOMtest = null;
    139169        }
    140        
     170
    141171        /**
    142172         * openssl_random_pseudo_bytes()
    143173         */
    144174        if (
    145             !function_exists('random_bytes') &&
    146             extension_loaded('openssl') &&
    147175            (
    148176                // Unix-like with PHP >= 5.3.0 or
    149177                (
    150                     DIRECTORY_SEPARATOR === '/' &&
     178                    DIRECTORY_SEPARATOR === '/'
     179                    &&
    151180                    PHP_VERSION_ID >= 50300
    152                 ) ||
     181                )
     182                ||
    153183                // Windows with PHP >= 5.4.1
    154184                PHP_VERSION_ID >= 50401
    155185            )
     186            &&
     187            !function_exists('random_bytes')
     188            &&
     189            extension_loaded('openssl')
    156190        ) {
    157191            // See random_bytes_openssl.php
    158192            require_once $RandomCompatDIR.'/random_bytes_openssl.php';
    159193        }
    160        
     194
    161195        /**
    162196         * throw new Exception
     
    175209        }
    176210    }
     211
    177212    if (!function_exists('random_int')) {
    178213        require_once $RandomCompatDIR.'/random_int.php';
    179214    }
     215
    180216    $RandomCompatDIR = null;
    181217}
  • trunk/src/wp-includes/random_compat/random_bytes_com_dotnet.php

    r35600 r36886  
    4747        );
    4848    }
     49
    4950    if ($bytes < 1) {
    5051        throw new Error(
     
    5253        );
    5354    }
     55
    5456    $buf = '';
    5557    $util = new COM('CAPICOM.Utilities.1');
    5658    $execCount = 0;
     59
    5760    /**
    5861     * Let's not let it loop forever. If we run N times and fail to
     
    6972        ++$execCount;
    7073    } while ($execCount < $bytes);
     74
    7175    /**
    7276     * If we reach here, PHP has failed us.
  • trunk/src/wp-includes/random_compat/random_bytes_dev_urandom.php

    r35922 r36886  
    6363            }
    6464        }
     65
    6566        if (!empty($fp)) {
    6667            /**
     
    8081        }
    8182    }
     83
    8284    try {
    8385        $bytes = RandomCompat_intval($bytes);
     
    8789        );
    8890    }
     91
    8992    if ($bytes < 1) {
    9093        throw new Error(
     
    9295        );
    9396    }
     97
    9498    /**
    9599     * This if() block only runs if we managed to open a file handle
     
    102106        $remaining = $bytes;
    103107        $buf = '';
     108
    104109        /**
    105110         * We use fread() in a loop to protect against partial reads
     
    134139        }
    135140    }
     141
    136142    /**
    137143     * If we reach here, PHP has failed us.
  • trunk/src/wp-includes/random_compat/random_bytes_libsodium.php

    r35600 r36886  
    4949        );
    5050    }
     51
    5152    if ($bytes < 1) {
    5253        throw new Error(
     
    5455        );
    5556    }
     57
    5658    /**
    5759     * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be
  • trunk/src/wp-includes/random_compat/random_bytes_libsodium_legacy.php

    r36220 r36886  
    4949        );
    5050    }
     51
    5152    if ($bytes < 1) {
    5253        throw new Error(
     
    5455        );
    5556    }
     57
    5658    /**
    5759     * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be
  • trunk/src/wp-includes/random_compat/random_bytes_mcrypt.php

    r35600 r36886  
    4949        );
    5050    }
     51
    5152    if ($bytes < 1) {
    5253        throw new Error(
     
    5657
    5758    $buf = @mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
    58     if ($buf !== false) {
    59         if (RandomCompat_strlen($buf) === $bytes) {
    60             /**
    61              * Return our random entropy buffer here:
    62              */
    63             return $buf;
    64         }
     59    if (
     60        $buf !== false
     61        &&
     62        RandomCompat_strlen($buf) === $bytes
     63    ) {
     64        /**
     65         * Return our random entropy buffer here:
     66         */
     67        return $buf;
    6568    }
     69
    6670    /**
    6771     * If we reach here, PHP has failed us.
  • trunk/src/wp-includes/random_compat/random_bytes_openssl.php

    r35600 r36886  
    4949        );
    5050    }
     51
    5152    if ($bytes < 1) {
    5253        throw new Error(
     
    5455        );
    5556    }
    56     $secure = true;
     57
    5758    /**
    5859     * $secure is passed by reference. If it's set to false, fail. Note
     
    6263     * @ref https://github.com/paragonie/random_compat/issues/6#issuecomment-119564973
    6364     */
     65    $secure = true;
    6466    $buf = openssl_random_pseudo_bytes($bytes, $secure);
    65     if ($buf !== false && $secure) {
    66         if (RandomCompat_strlen($buf) === $bytes) {
    67             return $buf;
    68         }
     67    if (
     68        $buf !== false
     69        &&
     70        $secure
     71        &&
     72        RandomCompat_strlen($buf) === $bytes
     73    ) {
     74        return $buf;
    6975    }
     76
    7077    /**
    7178     * If we reach here, PHP has failed us.
  • trunk/src/wp-includes/random_compat/random_int.php

    r35365 r36886  
    5656        );
    5757    }
     58
    5859    try {
    5960        $max = RandomCompat_intval($max);
     
    7475        );
    7576    }
     77
    7678    if ($max === $min) {
    7779        return $min;
     
    99101     */
    100102    if (!is_int($range)) {
     103
    101104        /**
    102105         * Still safely calculate wider ranges.
     
    112115        $bytes = PHP_INT_SIZE;
    113116        $mask = ~0;
     117
    114118    } else {
     119
    115120        /**
    116121         * $bits is effectively ceil(log($range, 2)) without dealing with
     
    182187         */
    183188    } while (!is_int($val) || $val > $max || $val < $min);
     189
    184190    return (int) $val;
    185191}
Note: See TracChangeset for help on using the changeset viewer.