| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP\Decoder; |
| 14 |
|
| 15 |
use Webklex\PHPIMAP\EncodingAliases; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
abstract class Decoder implements DecoderInterface { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public function __construct( |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
protected array $options = [], |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
protected string $fallback_encoding = 'UTF-8', |
| 43 |
) { |
| 44 |
$this->options = array_merge([ |
| 45 |
'header' => 'utf-8', |
| 46 |
'message' => 'utf-8', |
| 47 |
'attachment' => 'utf-8', |
| 48 |
], $this->options); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function decode(array|string|null $value, ?string $encoding = null): mixed { |
| 58 |
return $value; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function convertEncoding(string $str, string $from = "ISO-8859-2", string $to = "UTF-8"): mixed { |
| 70 |
$from = EncodingAliases::get($from, $this->fallback_encoding); |
| 71 |
$to = EncodingAliases::get($to, $this->fallback_encoding); |
| 72 |
|
| 73 |
if ($from === $to) { |
| 74 |
return $str; |
| 75 |
} |
| 76 |
|
| 77 |
return EncodingAliases::convert($str, $from, $to); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function mimeHeaderDecode(string $text): array { |
| 88 |
if (extension_loaded('imap')) { |
| 89 |
$result = \imap_mime_header_decode($text); |
| 90 |
return is_array($result) ? $result : []; |
| 91 |
} |
| 92 |
$charset = $this->getEncoding($text); |
| 93 |
return [(object)[ |
| 94 |
"charset" => $charset, |
| 95 |
"text" => $this->convertEncoding($text, $charset) |
| 96 |
]]; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public static function isUTF8($value): bool { |
| 106 |
return str_starts_with(strtolower($value), '=?utf-8?'); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public static function notDecoded($encoded, $decoded): bool { |
| 117 |
return str_starts_with($decoded, '=?') |
| 118 |
&& strlen($decoded) - 2 === strpos($decoded, '?=') |
| 119 |
&& str_contains($encoded, $decoded); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function setOptions(array $config): static { |
| 129 |
$this->options = $config; |
| 130 |
return $this; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public function getOptions(): array { |
| 139 |
return $this->options; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function getFallbackEncoding(): string { |
| 148 |
return $this->fallback_encoding; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function setFallbackEncoding(string $fallback_encoding): static { |
| 158 |
$this->fallback_encoding = $fallback_encoding; |
| 159 |
return $this; |
| 160 |
} |
| 161 |
} |