| 1 |
<?php |
| 2 |
/** |
| 3 |
* Friends: SimplePie_Misc class |
| 4 |
* |
| 5 |
* @package Friends |
| 6 |
* @since 1.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Friends; |
| 10 |
|
| 11 |
/** |
| 12 |
* Overrides SimplePie_Misc functions as necessary. |
| 13 |
* |
| 14 |
* @see SimplePie_Misc |
| 15 |
*/ |
| 16 |
class SimplePie_Misc extends \SimplePie_Misc { |
| 17 |
/** |
| 18 |
* Change a string from one encoding to another |
| 19 |
* |
| 20 |
* @param string $data Raw data in $input encoding. |
| 21 |
* @param string $input Encoding of $data. |
| 22 |
* @param string $output Encoding you want. |
| 23 |
* @return string|boolean False if we can't convert it. |
| 24 |
*/ |
| 25 |
public static function change_encoding( $data, $input, $output ) { |
| 26 |
if ( 'UTF-8' === $input && 'UTF-8' === $output ) { |
| 27 |
$clean_utf8_regex = <<<'END' |
| 28 |
! # Thanks, https://stackoverflow.com/a/1401716/578588 |
| 29 |
( # modified to clean for valid XML ASCII, see https://www.w3.org/TR/REC-xml/#NT-Char |
| 30 |
(?: [\x9\xD] # valid XML single-bytes |
| 31 |
| [\x20-\x7F] # single-byte sequences 0xxxxxxx |
| 32 |
| [\xC0-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx |
| 33 |
| [\xE0-\xEF][\x80-\xBF]{2} # triple-byte sequences 1110xxxx 10xxxxxx * 2 |
| 34 |
| [\xF0-\xF7][\x80-\xBF]{3} # quadruple-byte sequence 11110xxx 10xxxxxx * 3 |
| 35 |
){1,100} # ...one or more times |
| 36 |
) |
| 37 |
| ( [\x80-\xBF] ) # invalid byte in range 10000000 - 10111111 |
| 38 |
| ( [\xC0-\xFF] ) # invalid byte in range 11000000 - 11111111 |
| 39 |
| (.) |
| 40 |
!x |
| 41 |
END; |
| 42 |
$data = preg_replace_callback( |
| 43 |
$clean_utf8_regex, |
| 44 |
function ( $captures ) { |
| 45 |
if ( $captures[1] ) { |
| 46 |
// Valid byte sequence. Return unmodified. |
| 47 |
return $captures[1]; |
| 48 |
} elseif ( $captures[2] ) { |
| 49 |
// Invalid byte of the form 10xxxxxx. |
| 50 |
// Encode as 11000010 10xxxxxx. |
| 51 |
return "\xC2" . $captures[2]; |
| 52 |
} elseif ( $captures[3] ) { |
| 53 |
// Invalid byte of the form 11xxxxxx. |
| 54 |
// Encode as 11000011 10xxxxxx. |
| 55 |
return "\xC3" . chr( ord( $captures[3] ) - 64 ); |
| 56 |
} else { |
| 57 |
// Single-byte characters invalid for XML. Ignore. |
| 58 |
} |
| 59 |
}, |
| 60 |
$data |
| 61 |
); |
| 62 |
} |
| 63 |
return parent::change_encoding( $data, $input, $output ); |
| 64 |
} |
| 65 |
|
| 66 |
public static function error( $message, $level, $file, $line ) { |
| 67 |
if ( apply_filters( 'friends_debug', false ) ) { |
| 68 |
return parent::error( $message, $level, $file, $line ); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|