Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Random_* Compatibility Library
|
Chris@0
|
4 * for using the new PHP 7 random_* API in PHP 5 projects
|
Chris@0
|
5 *
|
Chris@0
|
6 * The MIT License (MIT)
|
Chris@0
|
7 *
|
Chris@16
|
8 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
|
Chris@0
|
9 *
|
Chris@0
|
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
Chris@0
|
11 * of this software and associated documentation files (the "Software"), to deal
|
Chris@0
|
12 * in the Software without restriction, including without limitation the rights
|
Chris@0
|
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
Chris@0
|
14 * copies of the Software, and to permit persons to whom the Software is
|
Chris@0
|
15 * furnished to do so, subject to the following conditions:
|
Chris@0
|
16 *
|
Chris@0
|
17 * The above copyright notice and this permission notice shall be included in
|
Chris@0
|
18 * all copies or substantial portions of the Software.
|
Chris@0
|
19 *
|
Chris@0
|
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
Chris@0
|
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
Chris@0
|
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
Chris@0
|
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
Chris@0
|
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
Chris@0
|
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
Chris@0
|
26 * SOFTWARE.
|
Chris@0
|
27 */
|
Chris@0
|
28
|
Chris@0
|
29 if (!is_callable('RandomCompat_strlen')) {
|
Chris@0
|
30 if (
|
Chris@16
|
31 defined('MB_OVERLOAD_STRING')
|
Chris@16
|
32 &&
|
Chris@16
|
33 ((int) ini_get('mbstring.func_overload')) & MB_OVERLOAD_STRING
|
Chris@0
|
34 ) {
|
Chris@0
|
35 /**
|
Chris@0
|
36 * strlen() implementation that isn't brittle to mbstring.func_overload
|
Chris@0
|
37 *
|
Chris@0
|
38 * This version uses mb_strlen() in '8bit' mode to treat strings as raw
|
Chris@0
|
39 * binary rather than UTF-8, ISO-8859-1, etc
|
Chris@0
|
40 *
|
Chris@0
|
41 * @param string $binary_string
|
Chris@0
|
42 *
|
Chris@0
|
43 * @throws TypeError
|
Chris@0
|
44 *
|
Chris@0
|
45 * @return int
|
Chris@0
|
46 */
|
Chris@0
|
47 function RandomCompat_strlen($binary_string)
|
Chris@0
|
48 {
|
Chris@0
|
49 if (!is_string($binary_string)) {
|
Chris@0
|
50 throw new TypeError(
|
Chris@0
|
51 'RandomCompat_strlen() expects a string'
|
Chris@0
|
52 );
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 return (int) mb_strlen($binary_string, '8bit');
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 } else {
|
Chris@0
|
59 /**
|
Chris@0
|
60 * strlen() implementation that isn't brittle to mbstring.func_overload
|
Chris@0
|
61 *
|
Chris@0
|
62 * This version just used the default strlen()
|
Chris@0
|
63 *
|
Chris@0
|
64 * @param string $binary_string
|
Chris@0
|
65 *
|
Chris@0
|
66 * @throws TypeError
|
Chris@0
|
67 *
|
Chris@0
|
68 * @return int
|
Chris@0
|
69 */
|
Chris@0
|
70 function RandomCompat_strlen($binary_string)
|
Chris@0
|
71 {
|
Chris@0
|
72 if (!is_string($binary_string)) {
|
Chris@0
|
73 throw new TypeError(
|
Chris@0
|
74 'RandomCompat_strlen() expects a string'
|
Chris@0
|
75 );
|
Chris@0
|
76 }
|
Chris@0
|
77 return (int) strlen($binary_string);
|
Chris@0
|
78 }
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 if (!is_callable('RandomCompat_substr')) {
|
Chris@0
|
83
|
Chris@0
|
84 if (
|
Chris@0
|
85 defined('MB_OVERLOAD_STRING')
|
Chris@16
|
86 &&
|
Chris@16
|
87 ((int) ini_get('mbstring.func_overload')) & MB_OVERLOAD_STRING
|
Chris@0
|
88 ) {
|
Chris@0
|
89 /**
|
Chris@0
|
90 * substr() implementation that isn't brittle to mbstring.func_overload
|
Chris@0
|
91 *
|
Chris@0
|
92 * This version uses mb_substr() in '8bit' mode to treat strings as raw
|
Chris@0
|
93 * binary rather than UTF-8, ISO-8859-1, etc
|
Chris@0
|
94 *
|
Chris@0
|
95 * @param string $binary_string
|
Chris@0
|
96 * @param int $start
|
Chris@16
|
97 * @param int|null $length (optional)
|
Chris@0
|
98 *
|
Chris@0
|
99 * @throws TypeError
|
Chris@0
|
100 *
|
Chris@0
|
101 * @return string
|
Chris@0
|
102 */
|
Chris@0
|
103 function RandomCompat_substr($binary_string, $start, $length = null)
|
Chris@0
|
104 {
|
Chris@0
|
105 if (!is_string($binary_string)) {
|
Chris@0
|
106 throw new TypeError(
|
Chris@0
|
107 'RandomCompat_substr(): First argument should be a string'
|
Chris@0
|
108 );
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 if (!is_int($start)) {
|
Chris@0
|
112 throw new TypeError(
|
Chris@0
|
113 'RandomCompat_substr(): Second argument should be an integer'
|
Chris@0
|
114 );
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 if ($length === null) {
|
Chris@0
|
118 /**
|
Chris@0
|
119 * mb_substr($str, 0, NULL, '8bit') returns an empty string on
|
Chris@0
|
120 * PHP 5.3, so we have to find the length ourselves.
|
Chris@0
|
121 */
|
Chris@16
|
122 /** @var int $length */
|
Chris@0
|
123 $length = RandomCompat_strlen($binary_string) - $start;
|
Chris@0
|
124 } elseif (!is_int($length)) {
|
Chris@0
|
125 throw new TypeError(
|
Chris@0
|
126 'RandomCompat_substr(): Third argument should be an integer, or omitted'
|
Chris@0
|
127 );
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 // Consistency with PHP's behavior
|
Chris@0
|
131 if ($start === RandomCompat_strlen($binary_string) && $length === 0) {
|
Chris@0
|
132 return '';
|
Chris@0
|
133 }
|
Chris@0
|
134 if ($start > RandomCompat_strlen($binary_string)) {
|
Chris@0
|
135 return '';
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@16
|
138 return (string) mb_substr(
|
Chris@16
|
139 (string) $binary_string,
|
Chris@16
|
140 (int) $start,
|
Chris@16
|
141 (int) $length,
|
Chris@16
|
142 '8bit'
|
Chris@16
|
143 );
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 } else {
|
Chris@0
|
147
|
Chris@0
|
148 /**
|
Chris@0
|
149 * substr() implementation that isn't brittle to mbstring.func_overload
|
Chris@0
|
150 *
|
Chris@0
|
151 * This version just uses the default substr()
|
Chris@0
|
152 *
|
Chris@0
|
153 * @param string $binary_string
|
Chris@0
|
154 * @param int $start
|
Chris@16
|
155 * @param int|null $length (optional)
|
Chris@0
|
156 *
|
Chris@0
|
157 * @throws TypeError
|
Chris@0
|
158 *
|
Chris@0
|
159 * @return string
|
Chris@0
|
160 */
|
Chris@0
|
161 function RandomCompat_substr($binary_string, $start, $length = null)
|
Chris@0
|
162 {
|
Chris@0
|
163 if (!is_string($binary_string)) {
|
Chris@0
|
164 throw new TypeError(
|
Chris@0
|
165 'RandomCompat_substr(): First argument should be a string'
|
Chris@0
|
166 );
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 if (!is_int($start)) {
|
Chris@0
|
170 throw new TypeError(
|
Chris@0
|
171 'RandomCompat_substr(): Second argument should be an integer'
|
Chris@0
|
172 );
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 if ($length !== null) {
|
Chris@0
|
176 if (!is_int($length)) {
|
Chris@0
|
177 throw new TypeError(
|
Chris@0
|
178 'RandomCompat_substr(): Third argument should be an integer, or omitted'
|
Chris@0
|
179 );
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@16
|
182 return (string) substr(
|
Chris@16
|
183 (string )$binary_string,
|
Chris@16
|
184 (int) $start,
|
Chris@16
|
185 (int) $length
|
Chris@16
|
186 );
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@16
|
189 return (string) substr(
|
Chris@16
|
190 (string) $binary_string,
|
Chris@16
|
191 (int) $start
|
Chris@16
|
192 );
|
Chris@0
|
193 }
|
Chris@0
|
194 }
|
Chris@0
|
195 }
|