private function _convertUTF8($attr) { if (is_array($attr)) { $new = array(); foreach ($attr as $key => $val) { $new[$key] = $this->_convertUTF8($val); } } else { $new = GO\Base\Util\String::clean_utf8($attr); } return $new; }
public static function mimeHeaderDecode($string, $defaultCharset = 'UTF-8') { /* * (=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=) (ab) * White space between adjacent 'encoded-word's is not displayed. * * http://www.faqs.org/rfcs/rfc2047.html */ $string = preg_replace("/\\?=[\\s]*=\\?/", "?==?", $string); if (preg_match_all("/(=\\?[^\\?]+\\?(q|b)\\?[^\\?]+\\?=)/i", $string, $matches)) { foreach ($matches[1] as $v) { $fld = substr($v, 2, -2); $charset = strtolower(substr($fld, 0, strpos($fld, '?'))); $fld = substr($fld, strlen($charset) + 1); $encoding = $fld[0]; $fld = substr($fld, strpos($fld, '?') + 1); $fld = str_replace('_', '=20', $fld); if (strtoupper($encoding) == 'B') { $fld = base64_decode($fld); } elseif (strtoupper($encoding) == 'Q') { $fld = quoted_printable_decode($fld); } $fld = \GO\Base\Util\String::clean_utf8($fld, $charset); $string = str_replace($v, $fld, $string); } } elseif (($pos = strpos($string, "''")) && $pos < 64) { //check pos for not being to great //eg. iso-8859-1''%66%6F%73%73%2D%69%74%2D%73%6D%61%6C%6C%2E%67%69%66 $charset = substr($string, 0, $pos); // throw new \Exception($charset.' : '.substr($string, $pos+2)); $string = rawurldecode(substr($string, $pos + 2)); $string = \GO\Base\Util\String::clean_utf8($string, $charset); } else { $string = \GO\Base\Util\String::clean_utf8($string, $defaultCharset); } // $string=\GO\Base\Util\String::clean_utf8($string); //\GO::debug($string); return str_replace(array('\\\\', '\\(', '\\)'), array('\\', '(', ')'), $string); }
/** * Convert and clean the file to ensure it has valid UTF-8 data. * * @return boolean */ public function convertToUtf8() { if (!$this->isWritable()) { return false; } $str = $this->getContents(); if (!$str) { return false; } $enc = $this->detectEncoding($str); if (!$enc) { $enc = 'UTF-8'; } $bom = pack("CCC", 0xef, 0xbb, 0xbf); if (0 == strncmp($str, $bom, 3)) { //echo "BOM detected - file is UTF-8\n"; $str = substr($str, 3); } return $this->putContents(\GO\Base\Util\String::clean_utf8($str, $enc)); }
private function _getParts($structure, $part_number_prefix = '') { if (isset($structure->parts)) { $structure->ctype_primary = strtolower($structure->ctype_primary); $structure->ctype_secondary = strtolower($structure->ctype_secondary); //$part_number=0; foreach ($structure->parts as $part_number => $part) { $part->ctype_primary = strtolower($part->ctype_primary); $part->ctype_secondary = strtolower($part->ctype_secondary); //text part and no attachment so it must be the body if ($structure->ctype_primary == 'multipart' && $structure->ctype_secondary == 'alternative' && $part->ctype_primary == 'text' && $part->ctype_secondary == 'plain') { //check if html part is there if ($this->_hasHtmlPart($structure)) { continue; } } if ($part->ctype_primary == 'text' && ($part->ctype_secondary == 'plain' || $part->ctype_secondary == 'html') && (!isset($part->disposition) || $part->disposition != 'attachment') && empty($part->d_parameters['filename'])) { $charset = isset($part->ctype_parameters['charset']) ? $part->ctype_parameters['charset'] : 'UTF-8'; $body = \GO\Base\Util\String::clean_utf8($part->body, $charset); if (stripos($part->ctype_secondary, 'plain') !== false) { $body = nl2br($body); } else { $body = \GO\Base\Util\String::convertLinks($body); $body = \GO\Base\Util\String::sanitizeHtml($body); $body = $body; } $this->_loadedBody .= $body; } elseif ($part->ctype_primary == 'multipart') { } else { //attachment if (!empty($part->ctype_parameters['name'])) { $filename = $part->ctype_parameters['name']; } elseif (!empty($part->d_parameters['filename'])) { $filename = $part->d_parameters['filename']; } elseif (!empty($part->d_parameters['filename*'])) { $filename = $part->d_parameters['filename*']; } else { $filename = uniqid(time()); } $mime_type = $part->ctype_primary . '/' . $part->ctype_secondary; if (isset($part->headers['content-id'])) { $content_id = trim($part->headers['content-id']); if (strpos($content_id, '>')) { $content_id = substr($part->headers['content-id'], 1, strlen($part->headers['content-id']) - 2); } } else { $content_id = ''; } $f = new \GO\Base\Fs\File($filename); $a = new MessageAttachment(); $a->name = $filename; $a->number = $part_number_prefix . $part_number; $a->content_id = $content_id; $a->mime = $mime_type; $tmp_file = new \GO\Base\Fs\File($this->_getTempDir() . $filename); if (!empty($part->body)) { $tmp_file = new \GO\Base\Fs\File($this->_getTempDir() . $filename); if (!$tmp_file->exists()) { $tmp_file->putContents($part->body); } $a->setTempFile($tmp_file); } $a->index = count($this->attachments); $a->size = isset($part->body) ? strlen($part->body) : 0; $a->encoding = isset($part->headers['content-transfer-encoding']) ? $part->headers['content-transfer-encoding'] : ''; $a->disposition = isset($part->disposition) ? $part->disposition : ''; $this->addAttachment($a); } //$part_number++; if (isset($part->parts)) { $this->_getParts($part, $part_number_prefix . $part_number . '.'); } } } elseif (isset($structure->body)) { $charset = isset($structure->ctype_parameters['charset']) ? $structure->ctype_parameters['charset'] : 'UTF-8'; $text_part = \GO\Base\Util\String::clean_utf8($structure->body, $charset); //convert text to html if (stripos($structure->ctype_secondary, 'plain') !== false) { $this->extractUuencodedAttachments($text_part); $text_part = nl2br($text_part); } else { $text_part = \GO\Base\Util\String::convertLinks($text_part); $text_part = \GO\Base\Util\String::sanitizeHtml($text_part); } $this->_loadedBody .= $text_part; } }
/** * Get's a message part and returned in binary form or UTF-8 charset. * * @param int $uid * @param string $part_no * @param stirng $encoding * @param string $charset * @param boolean $peek * @return string */ public function get_message_part_decoded($uid, $part_no, $encoding, $charset = false, $peek = false, $cutofflength = false, $fp = false) { \GO::debug("get_message_part_decoded({$uid}, {$part_no}, {$encoding}, {$charset})"); if ($encoding == 'uuencode') { return $this->_uudecode($uid, $part_no, $peek, $fp); } $str = ''; $this->get_message_part_start($uid, $part_no, $peek); $leftOver = ''; while ($line = $this->get_message_part_line()) { switch (strtolower($encoding)) { case 'base64': $line = trim($leftOver . $line); $leftOver = ""; if (strlen($line) % 4 == 0) { if (!$fp) { $str .= base64_decode($line); } else { fputs($fp, base64_decode($line)); } } else { $buffer = ""; while (strlen($line) > 4) { $buffer .= substr($line, 0, 4); $line = substr($line, 4); } if (!$fp) { $str .= base64_decode($buffer); } else { fputs($fp, base64_decode($buffer)); } if (strlen($line)) { $leftOver = $line; } } break; case 'quoted-printable': if (!$fp) { $str .= quoted_printable_decode($line); } else { fputs($fp, quoted_printable_decode($line)); } break; default: if (!$fp) { $str .= $line; } else { fputs($fp, $line); } break; } if ($cutofflength && strlen($line) > $cutofflength) { break; } } if (!empty($leftOver)) { \GO::debug($leftOver); if (!$fp) { $str .= base64_decode($leftOver); } else { fputs($fp, base64_decode($leftOver)); } } if ($charset) { //some clients don't send the charset. if ($charset == 'us-ascii') { $charset = 'windows-1252'; } $str = \GO\Base\Util\String::clean_utf8($str, $charset); if ($charset != 'utf-8') { $str = str_replace($charset, 'utf-8', $str); } } return $fp ? true : $str; // return $this->decode_message_part( // $this->get_message_part($uid, $part_no, $peek, $cutofflength), // $encoding, // $charset // ); }
private function _convertZipEncoding(\GO\Base\Fs\Folder $folder, $charset = 'CP850') { $items = $folder->ls(); foreach ($items as $item) { if (!\GO\Base\Util\String::isUtf8($item->name())) { $item->rename(\GO\Base\Util\String::clean_utf8($item->name(), $charset)); } if ($item->isFolder()) { $this->_convertZipEncoding($item, $charset); } } }