/** * Mime header value decoder. Usually unicode characters are encoded * according to RFC-2047. This function will decode RFC-2047 encoded * header values as well as detect headers which are not encoded. * * Caveats: * If headers contain non-ascii characters the result of this function * is completely undefined. If osTicket is corrupting your email * headers, your mail software is not encoding the header text * correctly. * * Returns: * Header value, transocded to UTF-8 */ function mime_decode($text, $encoding = 'utf-8') { // Handle poorly or completely un-encoded header values ( if (function_exists('mb_detect_encoding')) { if (($src_enc = mb_detect_encoding($text)) && strcasecmp($src_enc, 'ASCII') !== 0) { return Charset::transcode($text, $src_enc, $encoding); } } // Handle ASCII text and RFC-2047 encoding $str = ''; $parts = imap_mime_header_decode($text); foreach ($parts as $part) { $str .= $this->mime_encode($part->text, $part->charset, $encoding); } return $str ? $str : imap_utf8($text); }
/** * Parse RFC 2397 formatted data strings. Format according to the RFC * should look something like: * * data:[type/subtype][;charset=utf-8][;base64],data * * Parameters: * $data - (string) RFC2397 formatted data string * $output_encoding - (string:optional) Character set the input data * should be encoded to. * $always_convert - (bool|default:true) If the input data string does * not specify an input encding, assume iso-8859-1. If this flag is * set, the output will always be transcoded to the declared * output_encoding, if set. * * Returs: * array (data=>parsed and transcoded data string, type=>MIME type * declared in the data string or text/plain otherwise) * * References: * http://www.ietf.org/rfc/rfc2397.txt */ function parseRfc2397($data, $output_encoding = false, $always_convert = true) { if (substr($data, 0, 5) != "data:") { return array('data' => $data, 'type' => 'text/plain'); } $data = substr($data, 5); list($meta, $contents) = explode(",", $data, 2); if ($meta) { list($type, $extra) = explode(";", $meta, 2); } else { $extra = ''; } if (!isset($type) || !$type) { $type = 'text/plain'; } $parameters = explode(";", $extra); # Handle 'charset' hint in $extra, such as # data:text/plain;charset=iso-8859-1,Blah # Convert to utf-8 since it's the encoding scheme for the database. $charset = $always_convert ? 'iso-8859-1' : false; foreach ($parameters as $p) { list($param, $value) = explode('=', $extra); if ($param == 'charset') { $charset = $value; } elseif ($param == 'base64') { $contents = base64_decode($contents); } } if ($output_encoding && $charset) { $contents = Charset::transcode($contents, $charset, $output_encoding); } return array('data' => $contents, 'type' => $type); }
function getBody($type = 'text/html', $encoding = false) { // First select the body switch ($type) { case 'text/html': $body = $this->BodyHtml; break; default: return false; } // Figure out the source encoding (§5.1.2) $charset = false; if (@$this->OemCodepage) { $charset = 'cp' . $this->OemCodepage; } elseif (@$this->InternetCodepage) { $charset = 'cp' . $this->InternetCodepage; } // Transcode it if ($encoding && $charset) { $body = Charset::transcode($body, $charset, $encoding); } return $body; }
function mime_encode($text, $charset = null, $encoding = 'utf-8') { return Charset::transcode($text, $charset, $encoding); }
function translate($string) { if ($this->short_circuit) { return $string; } // Caching enabled, get translated string from cache if (isset($this->cache_translations[$string])) { $string = $this->cache_translations[$string]; } if (!$this->encode) { return $string; } return Charset::transcode($string, 'utf-8', $this->charset); }