Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Decoder/MessageDecoder.php

raw symbols SCIP
1 <?php
2 /*
3 * File: MessageDecoder.php
4 * Category: -
5 * Author: M.Goldenbaum
6 * Created: 12.04.24 20:14
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Webklex\PHPIMAP\Decoder;
14
15 use Exception;
16 use Webklex\PHPIMAP\EncodingAliases;
17 use Webklex\PHPIMAP\IMAP;
18
19 /**
20 * Class MessageDecoder
21 *
22 * @package Webklex\PHPIMAP
23 */
24 class MessageDecoder extends Decoder {
25
26 public function decode(array|string|null $value, ?string $encoding = null): mixed {
27 if(is_array($value)) {
28 return array_map(function($item){
29 return $this->decode($item);
30 }, $value);
31 }
32
33 switch ($encoding) {
34 case IMAP::MESSAGE_ENC_BINARY:
35 if (extension_loaded('imap')) {
36 return base64_decode(\imap_binary($value));
37 }
38 return base64_decode($value);
39 case IMAP::MESSAGE_ENC_BASE64:
40 return base64_decode($value);
41 case IMAP::MESSAGE_ENC_QUOTED_PRINTABLE:
42 return quoted_printable_decode($value);
43 case IMAP::MESSAGE_ENC_8BIT:
44 case IMAP::MESSAGE_ENC_7BIT:
45 case IMAP::MESSAGE_ENC_OTHER:
46 default:
47 return $value;
48 }
49 }
50
51 /**
52 * Get the encoding of a given abject
53 * @param object|string $structure
54 *
55 * @return string
56 */
57 public function getEncoding(object|string $structure): string {
58 if (property_exists($structure, 'parameters')) {
59 foreach ($structure->parameters as $parameter) {
60 if (strtolower($parameter->attribute) == "charset") {
61 return EncodingAliases::get($parameter->value, "ISO-8859-2");
62 }
63 }
64 } elseif (property_exists($structure, 'charset')) {
65 return EncodingAliases::get($structure->charset, "ISO-8859-2");
66 } elseif (is_string($structure) === true) {
67 return EncodingAliases::detectEncoding($structure);
68 }
69
70 return $this->fallback_encoding;
71 }
72
73
74 /**
75 * Convert the encoding
76 * @param $str
77 * @param string $from
78 * @param string $to
79 *
80 * @return mixed|string
81 */
82 public function convertEncoding($str, string $from = "ISO-8859-2", string $to = "UTF-8"): mixed {
83 $from = EncodingAliases::get($from);
84 $to = EncodingAliases::get($to);
85
86 if ($from === $to) {
87 return $str;
88 }
89
90 // We don't need to do convertEncoding() if charset is ASCII (us-ascii):
91 // ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded
92 // https://stackoverflow.com/a/11303410
93 //
94 // us-ascii is the same as ASCII:
95 // ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA)
96 // prefers the updated name US-ASCII, which clarifies that this system was developed in the US and
97 // based on the typographical symbols predominantly in use there.
98 // https://en.wikipedia.org/wiki/ASCII
99 //
100 // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken.
101 if (strtolower($from ?? '') == 'us-ascii' && $to == 'UTF-8') {
102 return $str;
103 }
104
105 if (function_exists('iconv') && !EncodingAliases::isUtf7($from) && !EncodingAliases::isUtf7($to)) {
106 try {
107 return iconv($from, $to.'//IGNORE', $str);
108 } catch (Exception) {
109 return @iconv($from, $to, $str);
110 }
111 } else {
112 if (!$from) {
113 return mb_convert_encoding($str, $to);
114 }
115 return mb_convert_encoding($str, $to, $from);
116 }
117 }
118
119 }