| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\MbstringException; |
| 6 |
|
| 7 |
/** |
| 8 |
* |
| 9 |
* |
| 10 |
* @param int $cp |
| 11 |
* @param string $encoding |
| 12 |
* @return string Returns a specific character. |
| 13 |
* @throws MbstringException |
| 14 |
* |
| 15 |
*/ |
| 16 |
function mb_chr(int $cp, string $encoding = null): string |
| 17 |
{ |
| 18 |
error_clear_last(); |
| 19 |
if ($encoding !== null) { |
| 20 |
$result = \mb_chr($cp, $encoding); |
| 21 |
} else { |
| 22 |
$result = \mb_chr($cp); |
| 23 |
} |
| 24 |
if ($result === false) { |
| 25 |
throw MbstringException::createFromPhpError(); |
| 26 |
} |
| 27 |
return $result; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Sets the automatic character |
| 33 |
* encoding detection order to encoding_list. |
| 34 |
* |
| 35 |
* @param mixed $encoding_list encoding_list is an array or |
| 36 |
* comma separated list of character encoding. See supported encodings. |
| 37 |
* |
| 38 |
* If encoding_list is omitted, it returns |
| 39 |
* the current character encoding detection order as array. |
| 40 |
* |
| 41 |
* This setting affects mb_detect_encoding and |
| 42 |
* mb_send_mail. |
| 43 |
* |
| 44 |
* mbstring currently implements the following |
| 45 |
* encoding detection filters. If there is an invalid byte sequence |
| 46 |
* for the following encodings, encoding detection will fail. |
| 47 |
* |
| 48 |
* For ISO-8859-*, mbstring |
| 49 |
* always detects as ISO-8859-*. |
| 50 |
* |
| 51 |
* For UTF-16, UTF-32, |
| 52 |
* UCS2 and UCS4, encoding |
| 53 |
* detection will fail always. |
| 54 |
* @return bool|array When setting the encoding detection order, TRUE is returned on success. |
| 55 |
* |
| 56 |
* When getting the encoding detection order, an ordered array of the encodings is returned. |
| 57 |
* @throws MbstringException |
| 58 |
* |
| 59 |
*/ |
| 60 |
function mb_detect_order($encoding_list = null) |
| 61 |
{ |
| 62 |
error_clear_last(); |
| 63 |
if ($encoding_list !== null) { |
| 64 |
$result = \mb_detect_order($encoding_list); |
| 65 |
} else { |
| 66 |
$result = \mb_detect_order(); |
| 67 |
} |
| 68 |
if ($result === false) { |
| 69 |
throw MbstringException::createFromPhpError(); |
| 70 |
} |
| 71 |
return $result; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Returns an array of aliases for a known encoding type. |
| 77 |
* |
| 78 |
* @param string $encoding The encoding type being checked, for aliases. |
| 79 |
* @return array Returns a numerically indexed array of encoding aliases on success |
| 80 |
* @throws MbstringException |
| 81 |
* |
| 82 |
*/ |
| 83 |
function mb_encoding_aliases(string $encoding): array |
| 84 |
{ |
| 85 |
error_clear_last(); |
| 86 |
$result = \mb_encoding_aliases($encoding); |
| 87 |
if ($result === false) { |
| 88 |
throw MbstringException::createFromPhpError(); |
| 89 |
} |
| 90 |
return $result; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Scans string for matches to |
| 96 |
* pattern, then replaces the matched text |
| 97 |
* with the output of callback function. |
| 98 |
* |
| 99 |
* The behavior of this function is almost identical to mb_ereg_replace, |
| 100 |
* except for the fact that instead of |
| 101 |
* replacement parameter, one should specify a |
| 102 |
* callback. |
| 103 |
* |
| 104 |
* @param string $pattern The regular expression pattern. |
| 105 |
* |
| 106 |
* Multibyte characters may be used in pattern. |
| 107 |
* @param callable $callback A callback that will be called and passed an array of matched elements |
| 108 |
* in the subject string. The callback should |
| 109 |
* return the replacement string. |
| 110 |
* |
| 111 |
* You'll often need the callback function |
| 112 |
* for a mb_ereg_replace_callback in just one place. |
| 113 |
* In this case you can use an |
| 114 |
* anonymous function to |
| 115 |
* declare the callback within the call to |
| 116 |
* mb_ereg_replace_callback. By doing it this way |
| 117 |
* you have all information for the call in one place and do not |
| 118 |
* clutter the function namespace with a callback function's name |
| 119 |
* not used anywhere else. |
| 120 |
* @param string $string The string being checked. |
| 121 |
* @param string $option The search option. See mb_regex_set_options for explanation. |
| 122 |
* @return string The resultant string on success. |
| 123 |
* @throws MbstringException |
| 124 |
* |
| 125 |
*/ |
| 126 |
function mb_ereg_replace_callback(string $pattern, callable $callback, string $string, string $option = "msr"): string |
| 127 |
{ |
| 128 |
error_clear_last(); |
| 129 |
$result = \mb_ereg_replace_callback($pattern, $callback, $string, $option); |
| 130 |
if ($result === false) { |
| 131 |
throw MbstringException::createFromPhpError(); |
| 132 |
} |
| 133 |
return $result; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* |
| 139 |
* |
| 140 |
* @param string $pattern The regular expression pattern. |
| 141 |
* |
| 142 |
* Multibyte characters may be used in pattern. |
| 143 |
* @param string $replacement The replacement text. |
| 144 |
* @param string $string The string being checked. |
| 145 |
* @param string $option |
| 146 |
* @return string The resultant string on success. |
| 147 |
* @throws MbstringException |
| 148 |
* |
| 149 |
*/ |
| 150 |
function mb_ereg_replace(string $pattern, string $replacement, string $string, string $option = "msr"): string |
| 151 |
{ |
| 152 |
error_clear_last(); |
| 153 |
$result = \mb_ereg_replace($pattern, $replacement, $string, $option); |
| 154 |
if ($result === false) { |
| 155 |
throw MbstringException::createFromPhpError(); |
| 156 |
} |
| 157 |
return $result; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
/** |
| 162 |
* |
| 163 |
* |
| 164 |
* @return array |
| 165 |
* @throws MbstringException |
| 166 |
* |
| 167 |
*/ |
| 168 |
function mb_ereg_search_getregs(): array |
| 169 |
{ |
| 170 |
error_clear_last(); |
| 171 |
$result = \mb_ereg_search_getregs(); |
| 172 |
if ($result === false) { |
| 173 |
throw MbstringException::createFromPhpError(); |
| 174 |
} |
| 175 |
return $result; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* mb_ereg_search_init sets |
| 181 |
* string and pattern |
| 182 |
* for a multibyte regular expression. These values are used for |
| 183 |
* mb_ereg_search, |
| 184 |
* mb_ereg_search_pos, and |
| 185 |
* mb_ereg_search_regs. |
| 186 |
* |
| 187 |
* @param string $string The search string. |
| 188 |
* @param string $pattern The search pattern. |
| 189 |
* @param string $option The search option. See mb_regex_set_options for explanation. |
| 190 |
* @throws MbstringException |
| 191 |
* |
| 192 |
*/ |
| 193 |
function mb_ereg_search_init(string $string, string $pattern = null, string $option = "msr"): void |
| 194 |
{ |
| 195 |
error_clear_last(); |
| 196 |
if ($option !== "msr") { |
| 197 |
$result = \mb_ereg_search_init($string, $pattern, $option); |
| 198 |
} elseif ($pattern !== null) { |
| 199 |
$result = \mb_ereg_search_init($string, $pattern); |
| 200 |
} else { |
| 201 |
$result = \mb_ereg_search_init($string); |
| 202 |
} |
| 203 |
if ($result === false) { |
| 204 |
throw MbstringException::createFromPhpError(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
/** |
| 210 |
* Returns the matched part of a multibyte regular expression. |
| 211 |
* |
| 212 |
* @param string $pattern The search pattern. |
| 213 |
* @param string $option The search option. See mb_regex_set_options for explanation. |
| 214 |
* @return array |
| 215 |
* @throws MbstringException |
| 216 |
* |
| 217 |
*/ |
| 218 |
function mb_ereg_search_regs(string $pattern = null, string $option = "ms"): array |
| 219 |
{ |
| 220 |
error_clear_last(); |
| 221 |
if ($option !== "ms") { |
| 222 |
$result = \mb_ereg_search_regs($pattern, $option); |
| 223 |
} elseif ($pattern !== null) { |
| 224 |
$result = \mb_ereg_search_regs($pattern); |
| 225 |
} else { |
| 226 |
$result = \mb_ereg_search_regs(); |
| 227 |
} |
| 228 |
if ($result === false) { |
| 229 |
throw MbstringException::createFromPhpError(); |
| 230 |
} |
| 231 |
return $result; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
/** |
| 236 |
* |
| 237 |
* |
| 238 |
* @param int $position The position to set. If it is negative, it counts from the end of the string. |
| 239 |
* @throws MbstringException |
| 240 |
* |
| 241 |
*/ |
| 242 |
function mb_ereg_search_setpos(int $position): void |
| 243 |
{ |
| 244 |
error_clear_last(); |
| 245 |
$result = \mb_ereg_search_setpos($position); |
| 246 |
if ($result === false) { |
| 247 |
throw MbstringException::createFromPhpError(); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
/** |
| 253 |
* |
| 254 |
* |
| 255 |
* @param string $pattern The regular expression pattern. Multibyte characters may be used. The case will be ignored. |
| 256 |
* @param string $replace The replacement text. |
| 257 |
* @param string $string The searched string. |
| 258 |
* @param string $option |
| 259 |
* @return string The resultant string. |
| 260 |
* @throws MbstringException |
| 261 |
* |
| 262 |
*/ |
| 263 |
function mb_eregi_replace(string $pattern, string $replace, string $string, string $option = "msri"): string |
| 264 |
{ |
| 265 |
error_clear_last(); |
| 266 |
$result = \mb_eregi_replace($pattern, $replace, $string, $option); |
| 267 |
if ($result === false) { |
| 268 |
throw MbstringException::createFromPhpError(); |
| 269 |
} |
| 270 |
return $result; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
/** |
| 275 |
* Set/Get the HTTP output character encoding. |
| 276 |
* Output after this function is called will be converted from the set internal encoding to encoding. |
| 277 |
* |
| 278 |
* @param string $encoding If encoding is set, |
| 279 |
* mb_http_output sets the HTTP output character |
| 280 |
* encoding to encoding. |
| 281 |
* |
| 282 |
* If encoding is omitted, |
| 283 |
* mb_http_output returns the current HTTP output |
| 284 |
* character encoding. |
| 285 |
* @return string|bool If encoding is omitted, |
| 286 |
* mb_http_output returns the current HTTP output |
| 287 |
* character encoding. Otherwise, |
| 288 |
* Returns TRUE on success. |
| 289 |
* @throws MbstringException |
| 290 |
* |
| 291 |
*/ |
| 292 |
function mb_http_output(string $encoding = null) |
| 293 |
{ |
| 294 |
error_clear_last(); |
| 295 |
if ($encoding !== null) { |
| 296 |
$result = \mb_http_output($encoding); |
| 297 |
} else { |
| 298 |
$result = \mb_http_output(); |
| 299 |
} |
| 300 |
if ($result === false) { |
| 301 |
throw MbstringException::createFromPhpError(); |
| 302 |
} |
| 303 |
return $result; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
/** |
| 308 |
* Set/Get the internal character encoding |
| 309 |
* |
| 310 |
* @param string $encoding encoding is the character encoding name |
| 311 |
* used for the HTTP input character encoding conversion, HTTP output |
| 312 |
* character encoding conversion, and the default character encoding |
| 313 |
* for string functions defined by the mbstring module. |
| 314 |
* You should notice that the internal encoding is totally different from the one for multibyte regex. |
| 315 |
* @return string|bool If encoding is set, then |
| 316 |
* Returns TRUE on success. |
| 317 |
* In this case, the character encoding for multibyte regex is NOT changed. |
| 318 |
* If encoding is omitted, then |
| 319 |
* the current character encoding name is returned. |
| 320 |
* @throws MbstringException |
| 321 |
* |
| 322 |
*/ |
| 323 |
function mb_internal_encoding(string $encoding = null) |
| 324 |
{ |
| 325 |
error_clear_last(); |
| 326 |
if ($encoding !== null) { |
| 327 |
$result = \mb_internal_encoding($encoding); |
| 328 |
} else { |
| 329 |
$result = \mb_internal_encoding(); |
| 330 |
} |
| 331 |
if ($result === false) { |
| 332 |
throw MbstringException::createFromPhpError(); |
| 333 |
} |
| 334 |
return $result; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
/** |
| 339 |
* |
| 340 |
* |
| 341 |
* @param string $str |
| 342 |
* @param string $encoding |
| 343 |
* @return int Returns a code point of character. |
| 344 |
* @throws MbstringException |
| 345 |
* |
| 346 |
*/ |
| 347 |
function mb_ord(string $str, string $encoding = null): int |
| 348 |
{ |
| 349 |
error_clear_last(); |
| 350 |
if ($encoding !== null) { |
| 351 |
$result = \mb_ord($str, $encoding); |
| 352 |
} else { |
| 353 |
$result = \mb_ord($str); |
| 354 |
} |
| 355 |
if ($result === false) { |
| 356 |
throw MbstringException::createFromPhpError(); |
| 357 |
} |
| 358 |
return $result; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
/** |
| 363 |
* Parses GET/POST/COOKIE data and |
| 364 |
* sets global variables. Since PHP does not provide raw POST/COOKIE |
| 365 |
* data, it can only be used for GET data for now. It parses URL |
| 366 |
* encoded data, detects encoding, converts coding to internal |
| 367 |
* encoding and set values to the result array or |
| 368 |
* global variables. |
| 369 |
* |
| 370 |
* @param string $encoded_string The URL encoded data. |
| 371 |
* @param array|null $result An array containing decoded and character encoded converted values. |
| 372 |
* @throws MbstringException |
| 373 |
* |
| 374 |
*/ |
| 375 |
function mb_parse_str(string $encoded_string, ?array &$result): void |
| 376 |
{ |
| 377 |
error_clear_last(); |
| 378 |
$result = \mb_parse_str($encoded_string, $result); |
| 379 |
if ($result === false) { |
| 380 |
throw MbstringException::createFromPhpError(); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
/** |
| 386 |
* Set/Get character encoding for a multibyte regex. |
| 387 |
* |
| 388 |
* @param string $encoding The encoding |
| 389 |
* parameter is the character encoding. If it is omitted, the internal character |
| 390 |
* encoding value will be used. |
| 391 |
* @return string|bool |
| 392 |
* @throws MbstringException |
| 393 |
* |
| 394 |
*/ |
| 395 |
function mb_regex_encoding(string $encoding = null) |
| 396 |
{ |
| 397 |
error_clear_last(); |
| 398 |
if ($encoding !== null) { |
| 399 |
$result = \mb_regex_encoding($encoding); |
| 400 |
} else { |
| 401 |
$result = \mb_regex_encoding(); |
| 402 |
} |
| 403 |
if ($result === false) { |
| 404 |
throw MbstringException::createFromPhpError(); |
| 405 |
} |
| 406 |
return $result; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
/** |
| 411 |
* Sends email. Headers and messages are converted and encoded according |
| 412 |
* to the mb_language setting. It's a wrapper function |
| 413 |
* for mail, so see also mail for details. |
| 414 |
* |
| 415 |
* @param string $to The mail addresses being sent to. Multiple |
| 416 |
* recipients may be specified by putting a comma between each |
| 417 |
* address in to. |
| 418 |
* This parameter is not automatically encoded. |
| 419 |
* @param string $subject The subject of the mail. |
| 420 |
* @param string $message The message of the mail. |
| 421 |
* @param string|array|null $additional_headers String or array to be inserted at the end of the email header. |
| 422 |
* |
| 423 |
* This is typically used to add extra headers (From, Cc, and Bcc). |
| 424 |
* Multiple extra headers should be separated with a CRLF (\r\n). |
| 425 |
* Validate parameter not to be injected unwanted headers by attackers. |
| 426 |
* |
| 427 |
* If an array is passed, its keys are the header names and its |
| 428 |
* values are the respective header values. |
| 429 |
* |
| 430 |
* When sending mail, the mail must contain |
| 431 |
* a From header. This can be set with the |
| 432 |
* additional_headers parameter, or a default |
| 433 |
* can be set in php.ini. |
| 434 |
* |
| 435 |
* Failing to do this will result in an error |
| 436 |
* message similar to Warning: mail(): "sendmail_from" not |
| 437 |
* set in php.ini or custom "From:" header missing. |
| 438 |
* The From header sets also |
| 439 |
* Return-Path under Windows. |
| 440 |
* |
| 441 |
* If messages are not received, try using a LF (\n) only. |
| 442 |
* Some Unix mail transfer agents (most notably |
| 443 |
* qmail) replace LF by CRLF |
| 444 |
* automatically (which leads to doubling CR if CRLF is used). |
| 445 |
* This should be a last resort, as it does not comply with |
| 446 |
* RFC 2822. |
| 447 |
* @param string $additional_parameter additional_parameter is a MTA command line |
| 448 |
* parameter. It is useful when setting the correct Return-Path |
| 449 |
* header when using sendmail. |
| 450 |
* |
| 451 |
* This parameter is escaped by escapeshellcmd internally |
| 452 |
* to prevent command execution. escapeshellcmd prevents |
| 453 |
* command execution, but allows to add additional parameters. For security reason, |
| 454 |
* this parameter should be validated. |
| 455 |
* |
| 456 |
* Since escapeshellcmd is applied automatically, some characters |
| 457 |
* that are allowed as email addresses by internet RFCs cannot be used. Programs |
| 458 |
* that are required to use these characters mail cannot be used. |
| 459 |
* |
| 460 |
* The user that the webserver runs as should be added as a trusted user to the |
| 461 |
* sendmail configuration to prevent a 'X-Warning' header from being added |
| 462 |
* to the message when the envelope sender (-f) is set using this method. |
| 463 |
* For sendmail users, this file is /etc/mail/trusted-users. |
| 464 |
* @throws MbstringException |
| 465 |
* |
| 466 |
*/ |
| 467 |
function mb_send_mail(string $to, string $subject, string $message, $additional_headers = null, string $additional_parameter = null): void |
| 468 |
{ |
| 469 |
error_clear_last(); |
| 470 |
$result = \mb_send_mail($to, $subject, $message, $additional_headers, $additional_parameter); |
| 471 |
if ($result === false) { |
| 472 |
throw MbstringException::createFromPhpError(); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
/** |
| 478 |
* |
| 479 |
* |
| 480 |
* @param string $pattern The regular expression pattern. |
| 481 |
* @param string $string The string being split. |
| 482 |
* @param int $limit |
| 483 |
* @return array The result as an array. |
| 484 |
* @throws MbstringException |
| 485 |
* |
| 486 |
*/ |
| 487 |
function mb_split(string $pattern, string $string, int $limit = -1): array |
| 488 |
{ |
| 489 |
error_clear_last(); |
| 490 |
$result = \mb_split($pattern, $string, $limit); |
| 491 |
if ($result === false) { |
| 492 |
throw MbstringException::createFromPhpError(); |
| 493 |
} |
| 494 |
return $result; |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
/** |
| 499 |
* This function will return an array of strings, it is a version of str_split with support for encodings of variable character size as well as fixed-size encodings of 1,2 or 4 byte characters. |
| 500 |
* If the split_length parameter is specified, the string is broken down into chunks of the specified length in characters (not bytes). |
| 501 |
* The encoding parameter can be optionally specified and it is good practice to do so. |
| 502 |
* |
| 503 |
* @param string $string The string to split into characters or chunks. |
| 504 |
* @param int $split_length If specified, each element of the returned array will be composed of multiple characters instead of a single character. |
| 505 |
* @param string $encoding The encoding |
| 506 |
* parameter is the character encoding. If it is omitted, the internal character |
| 507 |
* encoding value will be used. |
| 508 |
* |
| 509 |
* A string specifying one of the supported encodings. |
| 510 |
* @return array mb_str_split returns an array of strings. |
| 511 |
* @throws MbstringException |
| 512 |
* |
| 513 |
*/ |
| 514 |
function mb_str_split(string $string, int $split_length = 1, string $encoding = null): array |
| 515 |
{ |
| 516 |
error_clear_last(); |
| 517 |
if ($encoding !== null) { |
| 518 |
$result = \mb_str_split($string, $split_length, $encoding); |
| 519 |
} else { |
| 520 |
$result = \mb_str_split($string, $split_length); |
| 521 |
} |
| 522 |
if ($result === false) { |
| 523 |
throw MbstringException::createFromPhpError(); |
| 524 |
} |
| 525 |
return $result; |
| 526 |
} |
| 527 |
|