|
| 1 | +<?php |
| 2 | + |
| 3 | +if (!function_exists('random_int')) { |
| 4 | + |
| 5 | + /** |
| 6 | + * @param int $min |
| 7 | + * @param int $max |
| 8 | + * @return int |
| 9 | + */ |
| 10 | + function random_int($min, $max) |
| 11 | + { |
| 12 | + return rand((int)$min, (int)$max); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +if (!function_exists('mime_content_type')) { |
| 17 | + |
| 18 | + /** |
| 19 | + * @param string $filename |
| 20 | + * @return string |
| 21 | + */ |
| 22 | + function mime_content_type($filename) |
| 23 | + { |
| 24 | + if (function_exists('finfo_open')) { |
| 25 | + /** @noinspection PhpComposerExtensionStubsInspection */ |
| 26 | + $info = finfo_open(FILEINFO_MIME); |
| 27 | + /** @noinspection PhpComposerExtensionStubsInspection */ |
| 28 | + $mime = finfo_file($info, $filename); |
| 29 | + /** @noinspection PhpComposerExtensionStubsInspection */ |
| 30 | + finfo_close($info); |
| 31 | + return $mime; |
| 32 | + } |
| 33 | + $types = array( |
| 34 | + 'txt' => 'text/plain', |
| 35 | + 'htm' => 'text/html', |
| 36 | + 'html' => 'text/html', |
| 37 | + 'php' => 'text/html', |
| 38 | + 'css' => 'text/css', |
| 39 | + 'js' => 'application/javascript', |
| 40 | + 'json' => 'application/json', |
| 41 | + 'xml' => 'application/xml', |
| 42 | + 'swf' => 'application/x-shockwave-flash', |
| 43 | + 'flv' => 'video/x-flv', |
| 44 | + |
| 45 | + // images |
| 46 | + 'png' => 'image/png', |
| 47 | + 'jpe' => 'image/jpeg', |
| 48 | + 'jpeg' => 'image/jpeg', |
| 49 | + 'jpg' => 'image/jpeg', |
| 50 | + 'gif' => 'image/gif', |
| 51 | + 'bmp' => 'image/bmp', |
| 52 | + 'ico' => 'image/vnd.microsoft.icon', |
| 53 | + 'tiff' => 'image/tiff', |
| 54 | + 'tif' => 'image/tiff', |
| 55 | + 'svg' => 'image/svg+xml', |
| 56 | + 'svgz' => 'image/svg+xml', |
| 57 | + |
| 58 | + // archives |
| 59 | + 'zip' => 'application/zip', |
| 60 | + 'rar' => 'application/x-rar-compressed', |
| 61 | + 'exe' => 'application/x-msdownload', |
| 62 | + 'msi' => 'application/x-msdownload', |
| 63 | + 'cab' => 'application/vnd.ms-cab-compressed', |
| 64 | + |
| 65 | + // audio/video |
| 66 | + 'mp3' => 'audio/mpeg', |
| 67 | + 'qt' => 'video/quicktime', |
| 68 | + 'mov' => 'video/quicktime', |
| 69 | + |
| 70 | + // adobe |
| 71 | + 'pdf' => 'application/pdf', |
| 72 | + 'psd' => 'image/vnd.adobe.photoshop', |
| 73 | + 'ai' => 'application/postscript', |
| 74 | + 'eps' => 'application/postscript', |
| 75 | + 'ps' => 'application/postscript', |
| 76 | + |
| 77 | + // ms office |
| 78 | + 'doc' => 'application/msword', |
| 79 | + 'rtf' => 'application/rtf', |
| 80 | + 'xls' => 'application/vnd.ms-excel', |
| 81 | + 'ppt' => 'application/vnd.ms-powerpoint', |
| 82 | + |
| 83 | + // open office |
| 84 | + 'odt' => 'application/vnd.oasis.opendocument.text', |
| 85 | + 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', |
| 86 | + ); |
| 87 | + $arr = explode('.', $filename); |
| 88 | + $ext = strtolower(array_pop($arr)); |
| 89 | + if (array_key_exists($ext, $types)) { |
| 90 | + return $types[$ext]; |
| 91 | + } |
| 92 | + return 'application/octet-stream'; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * @param string $fileOrContents |
| 98 | + * @return string |
| 99 | + */ |
| 100 | +function ddrv_mailer_define_mime_type($fileOrContents) |
| 101 | +{ |
| 102 | + if (file_exists($fileOrContents)) { |
| 103 | + return mime_content_type($fileOrContents); |
| 104 | + } |
| 105 | + if (function_exists('finfo_open')) { |
| 106 | + /** @noinspection PhpComposerExtensionStubsInspection */ |
| 107 | + $info = finfo_open(FILEINFO_MIME); |
| 108 | + /** @noinspection PhpComposerExtensionStubsInspection */ |
| 109 | + $mime = finfo_buffer($info, $fileOrContents); |
| 110 | + /** @noinspection PhpComposerExtensionStubsInspection */ |
| 111 | + finfo_close($info); |
| 112 | + return $mime; |
| 113 | + } |
| 114 | + return 'application/octet-stream'; |
| 115 | +} |
| 116 | + |
| 117 | +function ddrv_mailer_encode_mime_header($string, $offset = 0) |
| 118 | +{ |
| 119 | + $max = 74; |
| 120 | + $map = array( |
| 121 | + '=00', '=01', '=02', '=03', '=04', '=05', '=06', '=07', '=08', '=09', '=0A', '=0B', '=0C', '=0D', '=0E', '=0F', |
| 122 | + '=10', '=11', '=12', '=13', '=14', '=15', '=16', '=17', '=18', '=19', '=1A', '=1B', '=1C', '=1D', '=1E', '=1F', |
| 123 | + '=20', '=21', '=22', '=23', '=24', '=25', '=26', '=27', '=28', '=29', '=2A', '=2B', '=2C', '=2D', '=2E', '=2F', |
| 124 | + '=30', '=31', '=32', '=33', '=34', '=35', '=36', '=37', '=38', '=39', '=3A', '=3B', '=3C', '=3D', '=3E', '=3F', |
| 125 | + '=40', '=41', '=42', '=43', '=44', '=45', '=46', '=47', '=48', '=49', '=4A', '=4B', '=4C', '=4D', '=4E', '=4F', |
| 126 | + '=50', '=51', '=52', '=53', '=54', '=55', '=56', '=57', '=58', '=59', '=5A', '=5B', '=5C', '=5D', '=5E', '=5F', |
| 127 | + '=60', '=61', '=62', '=63', '=64', '=65', '=66', '=67', '=68', '=69', '=6A', '=6B', '=6C', '=6D', '=6E', '=6F', |
| 128 | + '=70', '=71', '=72', '=73', '=74', '=75', '=76', '=77', '=78', '=79', '=7A', '=7B', '=7C', '=7D', '=7E', '=7F', |
| 129 | + '=80', '=81', '=82', '=83', '=84', '=85', '=86', '=87', '=88', '=89', '=8A', '=8B', '=8C', '=8D', '=8E', '=8F', |
| 130 | + '=90', '=91', '=92', '=93', '=94', '=95', '=96', '=97', '=98', '=99', '=9A', '=9B', '=9C', '=9D', '=9E', '=9F', |
| 131 | + '=A0', '=A1', '=A2', '=A3', '=A4', '=A5', '=A6', '=A7', '=A8', '=A9', '=AA', '=AB', '=AC', '=AD', '=AE', '=AF', |
| 132 | + '=B0', '=B1', '=B2', '=B3', '=B4', '=B5', '=B6', '=B7', '=B8', '=B9', '=BA', '=BB', '=BC', '=BD', '=BE', '=BF', |
| 133 | + '=C0', '=C1', '=C2', '=C3', '=C4', '=C5', '=C6', '=C7', '=C8', '=C9', '=CA', '=CB', '=CC', '=CD', '=CE', '=CF', |
| 134 | + '=D0', '=D1', '=D2', '=D3', '=D4', '=D5', '=D6', '=D7', '=D8', '=D9', '=DA', '=DB', '=DC', '=DD', '=DE', '=DF', |
| 135 | + '=E0', '=E1', '=E2', '=E3', '=E4', '=E5', '=E6', '=E7', '=E8', '=E9', '=EA', '=EB', '=EC', '=ED', '=EE', '=EF', |
| 136 | + '=F0', '=F1', '=F2', '=F3', '=F4', '=F5', '=F6', '=F7', '=F8', '=F9', '=FA', '=FB', '=FC', '=FD', '=FE', '=FF', |
| 137 | + ); |
| 138 | + $symbols = str_split($string); |
| 139 | + unset($string); |
| 140 | + $result = ''; |
| 141 | + $coding = false; |
| 142 | + $all = count($symbols); |
| 143 | + $position = 0; |
| 144 | + foreach ($symbols as $num => $symbol) { |
| 145 | + $line = ''; |
| 146 | + $add = 0; |
| 147 | + $char = ord($symbol); |
| 148 | + $ascii = ($char >= 32 && $char <= 60) || ($char >= 62 && $char <= 126); |
| 149 | + if ($char === 32 && $num + 1 === $all) { |
| 150 | + $ascii = false; |
| 151 | + } |
| 152 | + if ($num < $offset) { |
| 153 | + $ascii = true; |
| 154 | + $coding = false; |
| 155 | + } |
| 156 | + if (!$coding && $char === 61 && preg_match('/;(\s+)?([a-z0-9\-]+)(\s+)?(=(\s+)?\"[^\"]+)?/ui', $result)) { |
| 157 | + $ascii = true; |
| 158 | + } |
| 159 | + if ($ascii) { |
| 160 | + if ($coding) { |
| 161 | + $coding = false; |
| 162 | + $line = '?=' . $symbol; |
| 163 | + $add = 3; |
| 164 | + } else { |
| 165 | + $line = $symbol; |
| 166 | + $add = 1; |
| 167 | + } |
| 168 | + } else { |
| 169 | + if (!$coding) { |
| 170 | + $coding = true; |
| 171 | + $line = '=?utf-8?Q?'; |
| 172 | + $add = 10; |
| 173 | + } |
| 174 | + $line .= $map[$char]; |
| 175 | + $add += 3; |
| 176 | + } |
| 177 | + if ($position + $add >= $max) { |
| 178 | + $line = "=\r\n $line"; |
| 179 | + $position = $add + 1; |
| 180 | + } |
| 181 | + $result .= $line; |
| 182 | + $position += $add; |
| 183 | + } |
| 184 | + if ($coding) { |
| 185 | + $line = '?='; |
| 186 | + if ($position + 3 >= $max) { |
| 187 | + $line = "=\r\n $line"; |
| 188 | + } |
| 189 | + $result .= $line; |
| 190 | + } |
| 191 | + return $result; |
| 192 | +} |
0 commit comments