} function encode($what, $errors = false) { if ($what instanceof Unicode) { return new Unicode(mb_convert_encoding((string) $what, $this->target_charset, $what->getEncoding()), $this->target_charset); } } function decode($what, $errors = false) { if ($what instanceof Bytes) { return new Unicode($what, $this->target_charset); } // FIXME: Transcode from unknown source charset? return new Unicode(mb_convert_encoding((string) $what, Unicode::$default_encoding, $this->target_charset)); } static function normalize($charset) { } } if (extension_loaded('mbstring')) { Codec::register(function ($encoding) { static $mb_encodings = false; if (!$mb_encodings) { $mb_encodings = array_map('strtolower', mb_list_encodings()); } // Normalize received encoding if (in_array(strtolower($encoding), $mb_encodings)) { return new MbString($encoding); } }); }
<?php namespace Phlite\Text\Encodings; use Phlite\Text; class Html extends Text\CodecInfo { function encode($obj, $errors = false) { $flags = ENT_COMPAT | ENT_HTML401; return htmlspecialchars((string) $obj, $flags, $obj instanceof Text\Unicode ? $obj->getEncoding() : 'utf-8'); } function decode($obj, $errors = false) { $flags = ENT_COMPAT | ENT_HTML401; return htmlspecialchars_decode((string) $obj, $flags, $errors == 'strict', $obj->encoding); } } Text\Codec::register(function ($encoding) { if ($encoding == 'html') { return new Html(); } });
array_unshift($output, 'N*'); $output = call_user_func_array('pack', $output); // Append the truncated tail to the output if ($tail) { $output .= substr($tail, 0, $size - 1); } return new Bytes($output); } } class Z85 extends Ascii85 { static $alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#'; static $spaces = false; static $zeros = false; } class Rfc1924 extends Ascii85 { static $alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~'; static $spaces = false; static $zeros = false; } Codec::register(function ($encoding) { switch ($encoding) { case 'ascii85': return new Ascii85(); case 'z85': return new Z85(); case 'rfc1924': return new Rfc1924(); } });
{ static $name = 'base64'; function encode($obj, $errors = false) { return new Bytes(base64_encode($obj)); } function decode($obj, $errors = false) { return new Bytes(base64_decode($obj, $errors == 'strict')); } } class Hex extends CodecInfo { static $name = 'hex'; function encode($obj, $errors = false) { return new Bytes(bin2hex($obj)); } function decode($obj, $errors = false) { return new Bytes(hex2bin($obj, $errors == 'strict')); } } Codec::register(function ($encoding) { if ($encoding == 'base64') { return new Base64(); } if ($encoding == 'hex') { return new Hex(); } });
return $rv; } static function register($search_func) { if (!is_callable($search_func)) { throw new InvalidArgumentException('Only callables can be registered'); } static::$registry[] = $search_func; } static function lookup($encoding) { $encoding = strtolower($encoding); foreach (static::$registry as $f) { if ($codec = $f($encoding)) { return $codec; } } throw new DomainException($encoding . ': Unable to find codec'); } /** * Convenience method to load a module from the Encodings/ folder */ static function load($module) { include_once "Encodings/{$module}.php"; } } // Load standard encodings Codec::load('basic'); Codec::load('mbstring');
} elseif ($text[0] == '=' && function_exists('iconv_mime_decode')) { return iconv_mime_decode($text, 0, $encoding); // TODO: Use a pure-PHP version to perform the decoding } elseif (!strcasecmp($encoding, 'utf-8') && function_exists('imap_utf8')) { return imap_utf8($text); } return $text; } function encode($what, $errors = false) { } } class QuotedPrintable extends CodecInfo { function decode($what, $errors = false) { return new Bytes(quoted_printable_decode($what)); } function encode($what, $errors = false) { return new Bytes(quoted_printable_encode($what)); } } Codec::register(function ($encoding) { if (strcasecmp('rfc2047', $encoding) === 0) { return new Rfc2047($encoding); } if (strcasecmp('qp', $encoding) === 0) { return new QuotedPrintable(); } });