3 * Random_* Compatibility Library
4 * for using the new PHP 7 random_* API in PHP 5 projects
6 * The MIT License (MIT)
8 * Copyright (c) 2015 Paragon Initiative Enterprises
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
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
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
29 if (!function_exists('RandomCompat_strlen')) {
31 defined('MB_OVERLOAD_STRING') &&
32 ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
35 * strlen() implementation that isn't brittle to mbstring.func_overload
37 * This version uses mb_strlen() in '8bit' mode to treat strings as raw
38 * binary rather than UTF-8, ISO-8859-1, etc
40 * @param string $binary_string
46 function RandomCompat_strlen($binary_string)
48 if (!is_string($binary_string)) {
50 'RandomCompat_strlen() expects a string'
53 return mb_strlen($binary_string, '8bit');
57 * strlen() implementation that isn't brittle to mbstring.func_overload
59 * This version just used the default strlen()
61 * @param string $binary_string
67 function RandomCompat_strlen($binary_string)
69 if (!is_string($binary_string)) {
71 'RandomCompat_strlen() expects a string'
74 return strlen($binary_string);
79 if (!function_exists('RandomCompat_substr')) {
81 defined('MB_OVERLOAD_STRING') &&
82 ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
85 * substr() implementation that isn't brittle to mbstring.func_overload
87 * This version uses mb_substr() in '8bit' mode to treat strings as raw
88 * binary rather than UTF-8, ISO-8859-1, etc
90 * @param string $binary_string
92 * @param int $length (optional)
98 function RandomCompat_substr($binary_string, $start, $length = null)
100 if (!is_string($binary_string)) {
102 'RandomCompat_substr(): First argument should be a string'
105 if (!is_int($start)) {
107 'RandomCompat_substr(): Second argument should be an integer'
110 if ($length === null) {
112 * mb_substr($str, 0, NULL, '8bit') returns an empty string on
113 * PHP 5.3, so we have to find the length ourselves.
115 $length = RandomCompat_strlen($length) - $start;
116 } elseif (!is_int($length)) {
118 'RandomCompat_substr(): Third argument should be an integer, or omitted'
121 return mb_substr($binary_string, $start, $length, '8bit');
125 * substr() implementation that isn't brittle to mbstring.func_overload
127 * This version just uses the default substr()
129 * @param string $binary_string
131 * @param int $length (optional)
137 function RandomCompat_substr($binary_string, $start, $length = null)
139 if (!is_string($binary_string)) {
141 'RandomCompat_substr(): First argument should be a string'
144 if (!is_int($start)) {
146 'RandomCompat_substr(): Second argument should be an integer'
149 if ($length !== null) {
150 if (!is_int($length)) {
152 'RandomCompat_substr(): Third argument should be an integer, or omitted'
155 return substr($binary_string, $start, $length);
157 return substr($binary_string, $start);