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@0
|
8 * Copyright (c) 2015 - 2017 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@0
|
31 defined('MB_OVERLOAD_STRING') &&
|
Chris@0
|
32 ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
|
Chris@0
|
33 ) {
|
Chris@0
|
34 /**
|
Chris@0
|
35 * strlen() implementation that isn't brittle to mbstring.func_overload
|
Chris@0
|
36 *
|
Chris@0
|
37 * This version uses mb_strlen() in '8bit' mode to treat strings as raw
|
Chris@0
|
38 * binary rather than UTF-8, ISO-8859-1, etc
|
Chris@0
|
39 *
|
Chris@0
|
40 * @param string $binary_string
|
Chris@0
|
41 *
|
Chris@0
|
42 * @throws TypeError
|
Chris@0
|
43 *
|
Chris@0
|
44 * @return int
|
Chris@0
|
45 */
|
Chris@0
|
46 function RandomCompat_strlen($binary_string)
|
Chris@0
|
47 {
|
Chris@0
|
48 if (!is_string($binary_string)) {
|
Chris@0
|
49 throw new TypeError(
|
Chris@0
|
50 'RandomCompat_strlen() expects a string'
|
Chris@0
|
51 );
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 return (int) mb_strlen($binary_string, '8bit');
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 } else {
|
Chris@0
|
58 /**
|
Chris@0
|
59 * strlen() implementation that isn't brittle to mbstring.func_overload
|
Chris@0
|
60 *
|
Chris@0
|
61 * This version just used the default strlen()
|
Chris@0
|
62 *
|
Chris@0
|
63 * @param string $binary_string
|
Chris@0
|
64 *
|
Chris@0
|
65 * @throws TypeError
|
Chris@0
|
66 *
|
Chris@0
|
67 * @return int
|
Chris@0
|
68 */
|
Chris@0
|
69 function RandomCompat_strlen($binary_string)
|
Chris@0
|
70 {
|
Chris@0
|
71 if (!is_string($binary_string)) {
|
Chris@0
|
72 throw new TypeError(
|
Chris@0
|
73 'RandomCompat_strlen() expects a string'
|
Chris@0
|
74 );
|
Chris@0
|
75 }
|
Chris@0
|
76 return (int) strlen($binary_string);
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 if (!is_callable('RandomCompat_substr')) {
|
Chris@0
|
82
|
Chris@0
|
83 if (
|
Chris@0
|
84 defined('MB_OVERLOAD_STRING')
|
Chris@0
|
85 &&
|
Chris@0
|
86 ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING
|
Chris@0
|
87 ) {
|
Chris@0
|
88 /**
|
Chris@0
|
89 * substr() implementation that isn't brittle to mbstring.func_overload
|
Chris@0
|
90 *
|
Chris@0
|
91 * This version uses mb_substr() in '8bit' mode to treat strings as raw
|
Chris@0
|
92 * binary rather than UTF-8, ISO-8859-1, etc
|
Chris@0
|
93 *
|
Chris@0
|
94 * @param string $binary_string
|
Chris@0
|
95 * @param int $start
|
Chris@0
|
96 * @param int $length (optional)
|
Chris@0
|
97 *
|
Chris@0
|
98 * @throws TypeError
|
Chris@0
|
99 *
|
Chris@0
|
100 * @return string
|
Chris@0
|
101 */
|
Chris@0
|
102 function RandomCompat_substr($binary_string, $start, $length = null)
|
Chris@0
|
103 {
|
Chris@0
|
104 if (!is_string($binary_string)) {
|
Chris@0
|
105 throw new TypeError(
|
Chris@0
|
106 'RandomCompat_substr(): First argument should be a string'
|
Chris@0
|
107 );
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 if (!is_int($start)) {
|
Chris@0
|
111 throw new TypeError(
|
Chris@0
|
112 'RandomCompat_substr(): Second argument should be an integer'
|
Chris@0
|
113 );
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 if ($length === null) {
|
Chris@0
|
117 /**
|
Chris@0
|
118 * mb_substr($str, 0, NULL, '8bit') returns an empty string on
|
Chris@0
|
119 * PHP 5.3, so we have to find the length ourselves.
|
Chris@0
|
120 */
|
Chris@0
|
121 $length = RandomCompat_strlen($binary_string) - $start;
|
Chris@0
|
122 } elseif (!is_int($length)) {
|
Chris@0
|
123 throw new TypeError(
|
Chris@0
|
124 'RandomCompat_substr(): Third argument should be an integer, or omitted'
|
Chris@0
|
125 );
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 // Consistency with PHP's behavior
|
Chris@0
|
129 if ($start === RandomCompat_strlen($binary_string) && $length === 0) {
|
Chris@0
|
130 return '';
|
Chris@0
|
131 }
|
Chris@0
|
132 if ($start > RandomCompat_strlen($binary_string)) {
|
Chris@0
|
133 return '';
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 return (string) mb_substr($binary_string, $start, $length, '8bit');
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 } else {
|
Chris@0
|
140
|
Chris@0
|
141 /**
|
Chris@0
|
142 * substr() implementation that isn't brittle to mbstring.func_overload
|
Chris@0
|
143 *
|
Chris@0
|
144 * This version just uses the default substr()
|
Chris@0
|
145 *
|
Chris@0
|
146 * @param string $binary_string
|
Chris@0
|
147 * @param int $start
|
Chris@0
|
148 * @param int $length (optional)
|
Chris@0
|
149 *
|
Chris@0
|
150 * @throws TypeError
|
Chris@0
|
151 *
|
Chris@0
|
152 * @return string
|
Chris@0
|
153 */
|
Chris@0
|
154 function RandomCompat_substr($binary_string, $start, $length = null)
|
Chris@0
|
155 {
|
Chris@0
|
156 if (!is_string($binary_string)) {
|
Chris@0
|
157 throw new TypeError(
|
Chris@0
|
158 'RandomCompat_substr(): First argument should be a string'
|
Chris@0
|
159 );
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 if (!is_int($start)) {
|
Chris@0
|
163 throw new TypeError(
|
Chris@0
|
164 'RandomCompat_substr(): Second argument should be an integer'
|
Chris@0
|
165 );
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 if ($length !== null) {
|
Chris@0
|
169 if (!is_int($length)) {
|
Chris@0
|
170 throw new TypeError(
|
Chris@0
|
171 'RandomCompat_substr(): Third argument should be an integer, or omitted'
|
Chris@0
|
172 );
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 return (string) substr($binary_string, $start, $length);
|
Chris@0
|
176 }
|
Chris@0
|
177
|
Chris@0
|
178 return (string) substr($binary_string, $start);
|
Chris@0
|
179 }
|
Chris@0
|
180 }
|
Chris@0
|
181 }
|