← All changes
|
jetpack_vendor/automattic/jetpack-waf/src/class-waf-transforms.php
+47
-5
13.2.4
→
16.3
View file →
| @@ -349,13 +349,55 @@ | ||
| 349 | 349 | return trim( $value, self::TRIM_CHARS ); |
| 350 | 350 | } |
| 351 | 351 | |
| 352 | 352 | /** |
| 353 | - * Convert utf-8 characters to unicode characters | |
| 353 | + * Convert UTF-8 characters to Unicode characters. | |
| 354 | 354 | * |
| 355 | - * @param string $value value to be encoded. | |
| 356 | - * @return string | |
| 355 | + * This function iterates through each character of the input string, checks the ASCII value, | |
| 356 | + * and converts it to its corresponding Unicode representation. It handles characters that are | |
| 357 | + * represented with 1 to 4 bytes in UTF-8. | |
| 358 | + * | |
| 359 | + * @param string $str The string value to be encoded from UTF-8 to Unicode. | |
| 360 | + * @return string The converted string with Unicode representation. | |
| 357 | 361 | */ |
| 358 | - public function utf8_to_unicode( $value ) { | |
| 359 | - return preg_replace( '/\\\u(?=[a-f0-9]{4})/', '%u', substr( json_encode( $value ), 1, -1 ) ); | |
| 362 | + public function utf8_to_unicode( $str ) { | |
| 363 | + $unicodeStr = ''; | |
| 364 | + $strLen = strlen( $str ); | |
| 365 | + $i = 0; | |
| 366 | + | |
| 367 | + // Iterate through each character of the input string. | |
| 368 | + while ( $i < $strLen ) { | |
| 369 | + // Get the ASCII value of the current character. | |
| 370 | + $value = ord( $str[ $i ] ); | |
| 371 | + | |
| 372 | + if ( $value < 128 ) { | |
| 373 | + // If the character is in the ASCII range (0-127), directly add it to the Unicode string. | |
| 374 | + $unicodeStr .= chr( $value ); | |
| 375 | + ++$i; | |
| 376 | + } else { | |
| 377 | + // For characters outside the ASCII range, determine the number of bytes in the UTF-8 representation. | |
| 378 | + $unicodeValue = ''; | |
| 379 | + if ( $value >= 192 && $value <= 223 ) { | |
| 380 | + // For characters represented with 2 bytes in UTF-8. | |
| 381 | + $unicodeValue = ( ord( $str[ $i ] ) & 0x1F ) << 6 | ( ord( $str[ $i + 1 ] ) & 0x3F ); | |
| 382 | + $i += 2; | |
| 383 | + } elseif ( $value >= 224 && $value <= 239 ) { | |
| 384 | + // For characters represented with 3 bytes in UTF-8. | |
| 385 | + $unicodeValue = ( ord( $str[ $i ] ) & 0x0F ) << 12 | ( ord( $str[ $i + 1 ] ) & 0x3F ) << 6 | ( ord( $str[ $i + 2 ] ) & 0x3F ); | |
| 386 | + $i += 3; | |
| 387 | + } elseif ( $value >= 240 && $value <= 247 ) { | |
| 388 | + // For characters represented with 4 bytes in UTF-8. | |
| 389 | + $unicodeValue = ( ord( $str[ $i ] ) & 0x07 ) << 18 | ( ord( $str[ $i + 1 ] ) & 0x3F ) << 12 | ( ord( $str[ $i + 2 ] ) & 0x3F ) << 6 | ( ord( $str[ $i + 3 ] ) & 0x3F ); | |
| 390 | + $i += 4; | |
| 391 | + } else { | |
| 392 | + // If the sequence does not match any known UTF-8 pattern, skip to the next character. | |
| 393 | + ++$i; | |
| 394 | + continue; | |
| 395 | + } | |
| 396 | + // Convert the Unicode value to a formatted string and append it to the Unicode string. | |
| 397 | + $unicodeStr .= sprintf( '%%u%04X', $unicodeValue ); | |
| 398 | + } | |
| 399 | + } | |
| 400 | + | |
| 401 | + return strtolower( $unicodeStr ); | |
| 360 | 402 | } |
| 361 | 403 | } |