-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinString.php
More file actions
369 lines (342 loc) · 13.2 KB
/
BinString.php
File metadata and controls
369 lines (342 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
* If you use mbstring.func_overload, or the server you are running on has it enabled, you are in trouble.
* This class will help overcome that, by provide alternative functions to work around it.
*
* see readme.markdown for further details.
*
* @author A. Grandt <php@grandt.com>
* @copyright 2014 A. Grandt
* @license GNU LGPL 2.1
*/
namespace com\grandt;
class BinString {
const VERSION = 1.00;
private $has_mbstring = FALSE;
private $has_mb_shadow = FALSE;
private $has_mb_mail_overload = FALSE;
private $has_mb_string_overload = FALSE;
private $has_mb_regex_overload = FALSE;
/**
* mbstring.func_overload has an undocumented feature, to retain access to the original function.
* As it is undocumented, it is uncertain if it'll remain, therefore it's being made an optional.
*
* @var boolean
*/
public $USE_MB_ORIG = false;
function __construct() {
$this->has_mbstring = extension_loaded('mbstring');
$this->has_mb_shadow = (int) ini_get('mbstring.func_overload');
$this->has_mb_mail_overload = $this->has_mbstring && ($this->has_mb_shadow & 1);
$this->has_mb_string_overload = $this->has_mbstring && ($this->has_mb_shadow & 2);
$this->has_mb_regex_overload = $this->has_mbstring && ($this->has_mb_shadow & 4);
}
public function getPHPVersionId() {
if (!defined('PHP_VERSION_ID')) {
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}
return PHP_VERSION_ID;
}
/**
* @link http://php.net/manual/en/function.mail.php
*/
public function _mail($to, $subject, $message, $additional_headers = null, $additional_parameters = null) {
if ($this->has_mb_mail_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_mail($to, $subject, $message, $additional_headers, $additional_parameters);
}
$lang = mb_language(); // get current language
mb_language("en"); // Force encoding to iso8859-1
$rv = mb_send_mail($to, $subject, $message, $additional_headers, $additional_parameters);
mb_language($lang);
return $rv;
} else {
return mail($to, $subject, $message, $additional_headers, $additional_parameters);
}
}
/**
* @link http://php.net/manual/en/function.strlen.php
*/
public function _strlen($string) {
if ($this->has_mb_string_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_strlen($string);
}
return mb_strlen($string, 'latin1');
} else {
return strlen($string);
}
}
/**
* @link http://php.net/manual/en/function.strpos.php
*/
public function _strpos($haystack, $needle, $offset = 0) {
if ($this->has_mb_string_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_strpos($haystack, $needle, $offset);
}
return mb_strpos($haystack, $needle, $offset, 'latin1');
} else {
return strpos($haystack, $needle, $offset);
}
}
/**
* @link http://php.net/manual/en/function.strrpos.php
*/
public function _strrpos($haystack, $needle, $offset = 0) {
if ($this->has_mb_string_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_strrpos($haystack, $needle, $offset);
}
return mb_strrpos($haystack, $needle, $offset, 'latin1');
} else {
return strrpos($haystack, $needle, $offset);
}
}
/**
* @link http://php.net/manual/en/function.stripos.php
*/
public function _stripos($haystack, $needle, $offset = 0) {
if ($this->getPHPVersionId() >= 50200 && $this->has_mb_string_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_stripos($haystack, $needle, $offset);
}
return mb_stripos($haystack, $needle, $offset, 'latin1');
} else {
return stripos($haystack, $needle, $offset);
}
}
/**
* @link http://php.net/manual/en/function.strripos.php
*/
public function _strripos($haystack, $needle, $offset = 0) {
if ($this->getPHPVersionId() >= 50200 && $this->has_mb_string_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_strripos($haystack, $needle, $offset);
}
return mb_strripos($haystack, $needle, $offset, 'latin1');
} else {
return strripos($haystack, $needle, $offset);
}
}
/**
* @link http://php.net/manual/en/function.strstr.php
*/
public function _strstr($haystack, $needle, $before_needle = false) {
if ($this->getPHPVersionId() >= 50200 && $this->has_mb_string_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_strstr($haystack, $needle, $before_needle);
}
return mb_strstr($haystack, $needle, $before_needle, 'latin1');
} else {
return strstr($haystack, $needle, $before_needle);
}
}
/**
* @link http://php.net/manual/en/function.stristr.php
*/
public function _stristr($haystack, $needle, $before_needle = false) {
if ($this->getPHPVersionId() >= 50200 && $this->has_mb_string_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_stristr($haystack, $needle, $before_needle);
}
return mb_stristr($haystack, $needle, $before_needle, 'latin1');
} else {
return stristr($haystack, $needle, $before_needle);
}
}
/**
* @link http://php.net/manual/en/function.strrchr.php
*/
public function _strrchr($haystack, $needle) {
if ($this->getPHPVersionId() >= 50200 && $this->has_mb_string_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_strrchr($haystack, $needle);
}
return mb_strrchr($haystack, $needle, 'latin1');
} else {
return strrchr($haystack, $needle);
}
}
/**
* @link http://php.net/manual/en/function.substr.php
*/
public function _substr($string, $start, $length = null) {
if ($this->has_mb_string_overload) {
if ($this->USE_MB_ORIG) {
if (func_num_args() == 2) { // Kludgry hack, as PHP substr is lobotomized.
return mb_orig_substr($string, $start);
}
return mb_orig_substr($string, $start, $length);
}
if (func_num_args() == 2) { // Kludgry hack, as mb_substr is lobotomized, AND broken.
return mb_substr($string, $start, mb_strlen($string, 'latin1'), 'latin1');
}
return mb_substr($string, $start, $length, 'latin1');
} else {
if (func_num_args() == 2) { // Kludgry hack, as PHP substr is lobotomized.
return substr($string, $start);
}
return substr($string, $start, $length);
}
}
/**
* @link http://php.net/manual/en/function.strtolower.php
*/
public function _strtolower($string) {
if ($this->has_mb_string_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_strtolower($string);
}
return mb_strtolower($string, 'latin1');
} else {
return strtolower($string);
}
}
/**
* @link http://php.net/manual/en/function.strtoupper.php
*/
public function _strtoupper($string) {
if ($this->has_mb_string_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_strtoupper($string);
}
return mb_strtoupper($string, 'latin1');
} else {
return strtoupper($string);
}
}
/**
* @link http://php.net/manual/en/function.substr_count.php
*/
public function _substr_count($haystack, $needle, $offset = 0, $length = null) {
if ($this->has_mb_string_overload) {
if ($this->USE_MB_ORIG) {
if (func_num_args() > 3) { // Kludgry hack, as PHP substr_count is lobotomized.
return mb_orig_substr_count($haystack, $needle, $offset, $length);
}
return mb_orig_substr_count($haystack, $needle, $offset);
}
return mb_substr_count($haystack, $needle, 'latin1');
} else {
if (func_num_args() > 3) { // Kludgry hack, as PHP substr_count is lobotomized.
return substr_count($haystack, $needle, $offset, $length);
}
return substr_count($haystack, $needle, $offset);
}
}
/**
* @link http://php.net/manual/en/function.ereg.php
* @deprecated ereg_* functions are deprecated in PHP 5.3 onwards, use the PCRE preg_* instead.
*/
public function _ereg($pattern, $string, array &$regs) {
if ($this->has_mb_regex_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_ereg($pattern, $string, $regs);
}
$enc = mb_regex_encoding(); // get current encoding
mb_regex_encoding("latin1"); // Force encoding to iso8859-1
$rv = mb_ereg($pattern, $string, $regs);
mb_regex_encoding($enc);
return $rv;
} else {
return ereg($pattern, $string, $regs);
}
}
/**
* @link http://php.net/manual/en/function.eregi.php
* @deprecated ereg_* functions are deprecated in PHP 5.3 onwards, use the PCRE preg_* instead.
*/
public function _eregi($pattern, $string, array &$regs) {
if ($this->has_mb_regex_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_eregi($pattern, $string, $regs);
}
$enc = mb_regex_encoding(); // get current encoding
mb_regex_encoding("latin1"); // Force encoding to iso8859-1
$rv = mb_eregi($pattern, $string, $regs);
mb_regex_encoding($enc);
return $rv;
} else {
return eregi($pattern, $string, $regs);
}
}
/**
* @link http://php.net/manual/en/function.ereg_replace.php
* @deprecated ereg_* functions are deprecated in PHP 5.3 onwards, use the PCRE preg_* instead.
*/
public function _ereg_replace($pattern, $replacement, $string, $mb_specific_option = "msr") {
if ($this->has_mb_regex_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_ereg_replace($pattern, $replacement, $string);
}
$enc = mb_regex_encoding(); // get current encoding
mb_regex_encoding("latin1"); // Force encoding to iso8859-1
$rv = mb_ereg_replace($pattern, $replacement, $string, $mb_specific_option);
mb_regex_encoding($enc);
return $rv;
} else {
return ereg_replace($pattern, $replacement, $string);
}
}
/**
* @link http://php.net/manual/en/function.eregi_replace.php
* @deprecated ereg_* functions are deprecated in PHP 5.3 onwards, use the PCRE preg_* instead.
*/
public function _eregi_replace($pattern, $replacement, $string, $mb_specific_option = "msri") {
if ($this->has_mb_regex_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_eregi_replace($pattern, $replacement, $string);
}
$enc = mb_regex_encoding(); // get current encoding
mb_regex_encoding("latin1"); // Force encoding to iso8859-1
$rv = mb_eregi_replace($pattern, $replacement, $string, $mb_specific_option);
mb_regex_encoding($enc);
return $rv;
} else {
return eregi_replace($pattern, $replacement, $string);
}
}
/**
* @link http://php.net/manual/en/function.split.php
* @deprecated Split is deprecated in PHP 5.3 onwards, use preg_split instead. It'll bypass mb_* anyway.
*/
public function _split($pattern, $string, $limit = -1) {
if ($this->has_mb_regex_overload) {
if ($this->USE_MB_ORIG) {
return mb_orig_split($pattern, $string, $limit);
}
$enc = mb_regex_encoding(); // get current encoding
mb_regex_encoding("latin1"); // Force encoding to iso8859-1
$rv = mb_split($pattern, $string, $limit);
mb_regex_encoding($enc);
return $rv;
} else {
return split($pattern, $string, $limit);
}
}
/**
* Checks if the $haystack starts with the text in the $needle.
* Case sensitive.
* @param string $haystack
* @param string $needle
*
* @return bool
*/
public function startsWith($haystack, $needle) {
return $needle === '' || $this->_strpos($haystack, $needle) === 0;
}
/**
* Checks if the $haystack ends with the text in the $needle.
* Case sensitive.
*
* @param string $haystack
* @param string $needle
*
* @return bool
*/
public function endsWith($haystack, $needle) {
return $needle === '' || $this->_substr($haystack, -$this->_strlen($needle)) === $needle;
}
}