|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright (c) 2021, Jesse Norell <jesse@kci.net> |
| 5 | +All rights reserved. |
| 6 | +
|
| 7 | +Redistribution and use in source and binary forms, with or without modification, |
| 8 | +are permitted provided that the following conditions are met: |
| 9 | +
|
| 10 | + * Redistributions of source code must retain the above copyright notice, |
| 11 | + this list of conditions and the following disclaimer. |
| 12 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + this list of conditions and the following disclaimer in the documentation |
| 14 | + and/or other materials provided with the distribution. |
| 15 | + * Neither the name of ISPConfig nor the names of its contributors |
| 16 | + may be used to endorse or promote products derived from this software without |
| 17 | + specific prior written permission. |
| 18 | +
|
| 19 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 | +IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 23 | +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 | +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 26 | +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 | +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +*/ |
| 30 | + |
| 31 | +/* random_bytes can be dropped when php 5.6 support is dropped */ |
| 32 | +if (! function_exists('random_bytes')) { |
| 33 | + function random_bytes($length) { |
| 34 | + return openssl_random_pseudo_bytes($length); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/* random_int can be dropped when php 5.6 support is dropped */ |
| 39 | +if (! function_exists('random_int')) { |
| 40 | + function random_int($min=null, $max=null) { |
| 41 | + if (null === $min) { |
| 42 | + $min = PHP_INT_MIN; |
| 43 | + } |
| 44 | + |
| 45 | + if (null === $max) { |
| 46 | + $min = PHP_INT_MAX; |
| 47 | + } |
| 48 | + |
| 49 | + if (!is_int($min) || !is_int($max)) { |
| 50 | + trigger_error('random_int: $min and $max must be integer values', E_USER_NOTICE); |
| 51 | + $min = (int)$min; |
| 52 | + $max = (int)$max; |
| 53 | + } |
| 54 | + |
| 55 | + if ($min > $max) { |
| 56 | + trigger_error('random_int: $max can\'t be lesser than $min', E_USER_WARNING); |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + $range = $counter = $max - $min; |
| 61 | + $bits = 1; |
| 62 | + |
| 63 | + while ($counter >>= 1) { |
| 64 | + ++$bits; |
| 65 | + } |
| 66 | + |
| 67 | + $bytes = (int)max(ceil($bits/8), 1); |
| 68 | + $bitmask = pow(2, $bits) - 1; |
| 69 | + |
| 70 | + if ($bitmask >= PHP_INT_MAX) { |
| 71 | + $bitmask = PHP_INT_MAX; |
| 72 | + } |
| 73 | + |
| 74 | + do { |
| 75 | + $result = hexdec(bin2hex(random_bytes($bytes))) & $bitmask; |
| 76 | + } while ($result > $range); |
| 77 | + |
| 78 | + return $result + $min; |
| 79 | + } |
| 80 | +} |
0 commit comments