90 lines
2.6 KiB
PHP
90 lines
2.6 KiB
PHP
<?php
|
|
// File name: mimeenc.inc.php
|
|
// Description: PHP subroutine to handle MIME encoding/decoding (Base-64 and Quoted-Printable)
|
|
// Date: 2004-04-27
|
|
// Author: imacat <imacat@pristine.com.tw>
|
|
// Copyright: Copyright (C) 2004-2007 Pristine Communications
|
|
|
|
// b64encode_header: Encode a piece of header text with Base-64
|
|
// Refer to RFC-1522 4.1
|
|
function b64encode_header($text, $charset)
|
|
{
|
|
return "=?$charset?B?" . base64_encode($text) . "?=";
|
|
}
|
|
|
|
// qpencode: Encode a piece of text with Quoted-Printable
|
|
// Refer to RFC-1521 5.1
|
|
function qpencode($source)
|
|
{
|
|
// Convert to RFC-822 CRLF (Rule #4)
|
|
$source = str_replace("\r\n", "\n", $source);
|
|
$source = str_replace("\n", "\r\n", $source);
|
|
$result = "";
|
|
// Convert each character
|
|
for ($i = 0, $linelen = 0; $i < strlen($source); $i++) {
|
|
// Rule #4 (Line Breaks)
|
|
if (substr($source, $i, 2) == "\r\n") {
|
|
$result .= "\r\n";
|
|
$linelen = 0;
|
|
$i++;
|
|
continue;
|
|
}
|
|
$c = substr($source, $i, 1);
|
|
$o = ord($c);
|
|
// Rule #2 (Literal representation)
|
|
if (($o >= 33 && $o <= 60) || ($o >= 62 && $o <= 126)) {
|
|
$char = $c;
|
|
// Rule #4 (Line Breaks)
|
|
} elseif ($c == "\r" || $c == "\n") {
|
|
$char = $c;
|
|
// Rule #3 (White Space)
|
|
} elseif ($o == 9 || $o == 32) {
|
|
// At the end of line
|
|
if ($i+1 == strlen($source) || substr($source, $i+1) == "\r") {
|
|
$char = sprintf("=%02X", $o);
|
|
// Not at the end of line
|
|
} else {
|
|
$char = $c;
|
|
}
|
|
// Rule #1 (General 8-bit representation)
|
|
} else {
|
|
$char = sprintf("=%02X", $o);
|
|
}
|
|
// Rule #5 (Soft Line Breaks)
|
|
if ($linelen + strlen($char) > 75) {
|
|
$result .= "=\r\n";
|
|
$linelen = 0;
|
|
}
|
|
$result .= $char;
|
|
$linelen += strlen($char);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
// qpencode_header: Encode a piece of header text with Quoted-Printable
|
|
// Refer to RFC-1522 4.2
|
|
function qpencode_header($source, $charset)
|
|
{
|
|
$result = "";
|
|
// Convert each character
|
|
for ($i = 0; $i < strlen($source); $i++) {
|
|
$c = substr($source, $i, 1);
|
|
$o = ord($c);
|
|
// 4.2.3
|
|
if ($o >= 33 && $o <= 126 && $c != "=" && $c != "?" && $c != "_" && $o != 32) {
|
|
$char = $c;
|
|
// 4.2.2
|
|
} elseif ($o == 32) {
|
|
$char = "_";
|
|
// 4.2.1
|
|
} else {
|
|
$char = sprintf("=%02X", $o);
|
|
}
|
|
$result .= $char;
|
|
}
|
|
$result = "=?$charset?Q?$result?=";
|
|
return $result;
|
|
}
|
|
|
|
?>
|