| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Libraries\Imap2; |
| 4 |
|
| 5 |
use ZBateson\MailMimeParser\Message; |
| 6 |
use ZBateson\MailMimeParser\Header\HeaderConsts; |
| 7 |
|
| 8 |
class Polyfill |
| 9 |
{ |
| 10 |
public static function convert8bit($string) |
| 11 |
{ |
| 12 |
return quoted_printable_encode($string); |
| 13 |
} |
| 14 |
|
| 15 |
public static function mimeHeaderDecode($string) |
| 16 |
{ |
| 17 |
return $string; |
| 18 |
} |
| 19 |
|
| 20 |
public static function mutf7ToUtf8($string) |
| 21 |
{ |
| 22 |
// MUTF-7 to UTF-7 |
| 23 |
$string = str_replace(['&', ','], ['+', '/'], $string); |
| 24 |
|
| 25 |
return mb_convert_encoding($string, 'UTF-8', 'UTF-7'); |
| 26 |
} |
| 27 |
|
| 28 |
public static function qPrint($string) |
| 29 |
{ |
| 30 |
return quoted_printable_decode($string); |
| 31 |
} |
| 32 |
|
| 33 |
public static function rfc822ParseAdrList($string, $defaultHost) |
| 34 |
{ |
| 35 |
$message = Message::from('To: '.$string, false); |
| 36 |
|
| 37 |
return Functions::getAddressObjectList( |
| 38 |
$message->getHeader(HeaderConsts::TO)->getAddresses(), |
| 39 |
$defaultHost |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
public static function rfc822ParseHeaders($headers, $defaultHost = 'UNKNOWN') |
| 44 |
{ |
| 45 |
$message = Message::from($headers, false); |
| 46 |
|
| 47 |
$date = $message->getHeaderValue(HeaderConsts::DATE); |
| 48 |
$subject = $message->getHeaderValue(HeaderConsts::SUBJECT); |
| 49 |
|
| 50 |
$hasReplyTo = $message->getHeader(HeaderConsts::REPLY_TO) !== null; |
| 51 |
$hasSender = $message->getHeader(HeaderConsts::SENDER) !== null; |
| 52 |
|
| 53 |
return (object)[ |
| 54 |
'date' => $date, |
| 55 |
'Date' => $date, |
| 56 |
'subject' => $subject, |
| 57 |
'Subject' => $subject, |
| 58 |
'message_id' => '<'.$message->getHeaderValue(HeaderConsts::MESSAGE_ID).'>', |
| 59 |
'toaddress' => $message->getHeaderValue(HeaderConsts::TO), |
| 60 |
'to' => Functions::getAddressObjectList($message->getHeader(HeaderConsts::TO)->getAddresses()), |
| 61 |
'fromaddress' => $message->getHeaderValue(HeaderConsts::FROM), |
| 62 |
'from' => Functions::getAddressObjectList($message->getHeader(HeaderConsts::FROM)->getAddresses()), |
| 63 |
'reply_toaddress' => $message->getHeaderValue($hasReplyTo ? HeaderConsts::REPLY_TO : HeaderConsts::FROM), |
| 64 |
'reply_to' => Functions::getAddressObjectList($message->getHeader($hasReplyTo ? HeaderConsts::REPLY_TO : HeaderConsts::FROM)->getAddresses()), |
| 65 |
'senderaddress' => $message->getHeaderValue($hasSender ? HeaderConsts::SENDER : HeaderConsts::FROM), |
| 66 |
'sender' => Functions::getAddressObjectList($message->getHeader($hasSender ? HeaderConsts::SENDER : HeaderConsts::FROM)->getAddresses()), |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
public static function rfc822WriteHeaders($mailbox, $hostname, $personal) |
| 71 |
{ |
| 72 |
$address = $mailbox.'@'.$hostname; |
| 73 |
if (!empty($personal)) { |
| 74 |
$address = $personal.' <'.$address.'>'; |
| 75 |
} |
| 76 |
|
| 77 |
return $address; |
| 78 |
} |
| 79 |
|
| 80 |
public static function utf7Decode($string) |
| 81 |
{ |
| 82 |
// MUTF-7 to UTF-7 |
| 83 |
$string = str_replace(['&', ','], ['+', '/'], $string); |
| 84 |
|
| 85 |
return mb_convert_encoding($string, 'ISO-8859-1', 'UTF-7'); |
| 86 |
} |
| 87 |
|
| 88 |
public static function utf7Encode($string) |
| 89 |
{ |
| 90 |
$utf7 = mb_convert_encoding($string, 'UTF-7', 'ISO-8859-1'); |
| 91 |
|
| 92 |
// Modify it to be Modified UTF-7 (MUTF-7) |
| 93 |
$mutf7 = str_replace(['+', '/', '='], ['&', ',', ''], $utf7); |
| 94 |
|
| 95 |
return $mutf7; |
| 96 |
} |
| 97 |
|
| 98 |
public static function utf8ToMutf7($string) |
| 99 |
{ |
| 100 |
$utf7 = mb_convert_encoding($string, 'UTF-7', 'UTF-8'); |
| 101 |
|
| 102 |
// Modify it to be Modified UTF-7 (MUTF-7) |
| 103 |
$mutf7 = str_replace(['+', '/', '='], ['&', ',', ''], $utf7); |
| 104 |
|
| 105 |
return $mutf7; |
| 106 |
} |
| 107 |
|
| 108 |
public static function utf8($string) |
| 109 |
{ |
| 110 |
return iconv_mime_decode($string, 0, 'UTF-8'); |
| 111 |
} |
| 112 |
|
| 113 |
public static function mailCompose($envelope, $bodies) |
| 114 |
{ |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
public static function base64($string) |
| 119 |
{ |
| 120 |
return base64_decode($string, true); |
| 121 |
} |
| 122 |
|
| 123 |
public static function binary($string) |
| 124 |
{ |
| 125 |
return base64_encode($string); |
| 126 |
} |
| 127 |
} |
| 128 |
|