summaryrefslogtreecommitdiff
path: root/includes/common.inc
diff options
context:
space:
mode:
Diffstat (limited to 'includes/common.inc')
-rw-r--r--includes/common.inc28
1 files changed, 28 insertions, 0 deletions
diff --git a/includes/common.inc b/includes/common.inc
index a15c0942e..cda083867 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -1795,6 +1795,34 @@ function truncate_utf8($string, $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 <?php ?>
* tags (in other words, we evaluate the code as if it were a stand-alone PHP