From 78b052a6af5fc7c87c807821bfc9267f8007ed7b Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Tue, 17 Aug 2004 21:35:26 +0000 Subject: - The upload (filehandler) module has landed! --- includes/common.inc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'includes/common.inc') diff --git a/includes/common.inc b/includes/common.inc index a15c0942e..cda083867 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -1794,6 +1794,34 @@ function truncate_utf8($string, $len) { return substr($string, 0, $len); } +/** + * Encodes MIME/HTTP header values that contain non US-ASCII + * characters. + * + * Example: mime_header_encode('tést.txt') returns '=?UTF-8?B?dMOpc3QudHh0?=' + * + * For more info: http://www.rfc-editor.org/rfc/rfc2047.txt + * + */ +function mime_header_encode($string, $charset = 'UTF-8') { + // Notes: + // - Only encode strings that contain non-ASCII characters. + // - The chunks come in groupings of 4 bytes when using base64 + // encoded. + // - trim() is used to ensure that no extra spacing is added by + // chunk_split() or preg_replace(). + // - Using \n as the chunk separator may cause problems on some + // systems and may have to be changed to \r\n or \r. + + if (!preg_match('/^[\x20-\x7E]*$/', $string)) { + $chunk_size = 75 - 7 - strlen($charset); + $chunk_size -= $chunk_size % 4; + $string = trim(chunk_split(base64_encode($string), $chunk_size, "\n")); + $string = trim(preg_replace('/^(.*)$/m', " =?$charset?B?\\1?=", $string)); + } + return $string; +} + /** * Wrapper around PHP's eval(). Uses output buffering to capture both returned * and printed text. Unlike eval(), we require code to be surrounded by -- cgit v1.2.3