| @@ -1,0 +1,194 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +/* | |
| 4 | + * This file is part of the PHP IMAP2 package. | |
| 5 | + * | |
| 6 | + * (c) Francesco Bianco <[email protected]> | |
| 7 | + * | |
| 8 | + * For the full copyright and license information, please view the LICENSE | |
| 9 | + * file that was distributed with this source code. | |
| 10 | + */ | |
| 11 | + | |
| 12 | +namespace AcyMailing\Libraries\Imap2; | |
| 13 | + | |
| 14 | +class Functions | |
| 15 | +{ | |
| 16 | + public static function parseMailboxString($mailbox) | |
| 17 | + { | |
| 18 | + $mailboxParts = explode('}', $mailbox); | |
| 19 | + $mailboxParts[0] = substr($mailboxParts[0], 1); | |
| 20 | + | |
| 21 | + $values = parse_url($mailboxParts[0]); | |
| 22 | + | |
| 23 | + $values['mailbox'] = $mailboxParts[1] ?? ''; | |
| 24 | + $values['path'] = explode('/', $values['path']); | |
| 25 | + | |
| 26 | + return $values; | |
| 27 | + } | |
| 28 | + | |
| 29 | + public static function getHostFromMailbox($mailbox) | |
| 30 | + { | |
| 31 | + $mailboxParts = is_array($mailbox) ? $mailbox : self::parseMailboxString($mailbox); | |
| 32 | + | |
| 33 | + return @$mailboxParts['host']; | |
| 34 | + } | |
| 35 | + | |
| 36 | + public static function getSslModeFromMailbox($mailbox) | |
| 37 | + { | |
| 38 | + $mailboxParts = is_array($mailbox) ? $mailbox : self::parseMailboxString($mailbox); | |
| 39 | + | |
| 40 | + if (in_array('ssl', $mailboxParts['path'])) { | |
| 41 | + return 'ssl'; | |
| 42 | + } | |
| 43 | + | |
| 44 | + return false; | |
| 45 | + } | |
| 46 | + | |
| 47 | + public static function expectedNumberOfMessages($sequence) | |
| 48 | + { | |
| 49 | + if (strpos($sequence, ',') > 0) { | |
| 50 | + return count(explode(',', $sequence)); | |
| 51 | + } elseif (strpos($sequence, ':') > 0) { | |
| 52 | + $range = explode(':', $sequence); | |
| 53 | + | |
| 54 | + return $range[1] - $range[0]; | |
| 55 | + } else { | |
| 56 | + return 1; | |
| 57 | + } | |
| 58 | + } | |
| 59 | + | |
| 60 | + public static function unique() | |
| 61 | + { | |
| 62 | + return md5(microtime().time().rand(1000, 9999)); | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Get name from full mailbox string. | |
| 67 | + * | |
| 68 | + * @param $mailbox | |
| 69 | + * | |
| 70 | + * @return mixed|string | |
| 71 | + */ | |
| 72 | + public static function getMailboxName($mailbox) | |
| 73 | + { | |
| 74 | + $mailboxParts = explode('}', $mailbox, 2); | |
| 75 | + | |
| 76 | + return empty($mailboxParts[1]) ? 'INBOX' : $mailboxParts[1]; | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * | |
| 81 | + * @param $address | |
| 82 | + * @param $defaultHost | |
| 83 | + * | |
| 84 | + * @return string | |
| 85 | + */ | |
| 86 | + public static function sanitizeAddress($address, $defaultHost = 'UNKNOWN') | |
| 87 | + { | |
| 88 | + $addressList = imap_rfc822_parse_adrlist($address, $defaultHost); | |
| 89 | + | |
| 90 | + $sanitizedAddress = []; | |
| 91 | + foreach ($addressList as $addressEntry) { | |
| 92 | + $sanitizedAddress[] = imap_rfc822_write_address($addressEntry->mailbox, $addressEntry->host, $addressEntry->personal); | |
| 93 | + } | |
| 94 | + | |
| 95 | + return implode(', ', $sanitizedAddress); | |
| 96 | + } | |
| 97 | + | |
| 98 | + public static function writeAddressFromEnvelope($addressList) | |
| 99 | + { | |
| 100 | + if (empty($addressList)) { | |
| 101 | + return null; | |
| 102 | + } | |
| 103 | + | |
| 104 | + $sanitizedAddress = []; | |
| 105 | + foreach ($addressList as $addressEntry) { | |
| 106 | + $name = empty($addressEntry[0]) ? '' : $addressEntry[0]; | |
| 107 | + $parsedAddressEntry = imap_rfc822_write_address($addressEntry[2], $addressEntry[3], $name); | |
| 108 | + if (substr($parsedAddressEntry, -3) == '@""') { | |
| 109 | + $parsedAddressEntry = substr($parsedAddressEntry, 0, strlen($parsedAddressEntry) - 3).': '; | |
| 110 | + } | |
| 111 | + $sanitizedAddress[] = $parsedAddressEntry; | |
| 112 | + } | |
| 113 | + | |
| 114 | + return implode(', ', $sanitizedAddress); | |
| 115 | + } | |
| 116 | + | |
| 117 | + public static function getListAttributesValue($attributes) | |
| 118 | + { | |
| 119 | + $attributesValue = 0; | |
| 120 | + | |
| 121 | + foreach ($attributes as $attribute) { | |
| 122 | + switch ($attribute) { | |
| 123 | + case '\\NoInferiors': | |
| 124 | + $attributesValue |= LATT_NOINFERIORS; | |
| 125 | + break; | |
| 126 | + case '\\NoSelect': | |
| 127 | + $attributesValue |= LATT_NOSELECT; | |
| 128 | + break; | |
| 129 | + case '\\Marked': | |
| 130 | + $attributesValue |= LATT_MARKED; | |
| 131 | + break; | |
| 132 | + case '\\UnMarked': | |
| 133 | + $attributesValue |= LATT_UNMARKED; | |
| 134 | + break; | |
| 135 | + case '\\Referral': | |
| 136 | + $attributesValue |= LATT_REFERRAL; | |
| 137 | + break; | |
| 138 | + case '\\HasChildren': | |
| 139 | + $attributesValue |= LATT_HASCHILDREN; | |
| 140 | + break; | |
| 141 | + case '\\HasNoChildren': | |
| 142 | + $attributesValue |= LATT_HASNOCHILDREN; | |
| 143 | + break; | |
| 144 | + } | |
| 145 | + } | |
| 146 | + | |
| 147 | + return $attributesValue; | |
| 148 | + } | |
| 149 | + | |
| 150 | + public static function isValidImap1Connection($imap) | |
| 151 | + { | |
| 152 | + return is_resource($imap) && get_resource_type($imap) == 'imap'; | |
| 153 | + } | |
| 154 | + | |
| 155 | + public static function isValidImap2Connection($imap) | |
| 156 | + { | |
| 157 | + return Connection::isValid($imap); | |
| 158 | + } | |
| 159 | + | |
| 160 | + public static function getAddressObjectList($addressList, $defaultHost = 'UNKNOWN') | |
| 161 | + { | |
| 162 | + $addressObjectList = []; | |
| 163 | + foreach ($addressList as $toAddress) { | |
| 164 | + $email = explode('@', $toAddress->getEmail()); | |
| 165 | + | |
| 166 | + $addressObject = (object)[ | |
| 167 | + 'mailbox' => $email[0], | |
| 168 | + 'host' => $email[1] ?? $defaultHost, | |
| 169 | + ]; | |
| 170 | + | |
| 171 | + $personal = $toAddress->getName(); | |
| 172 | + if ($personal) { | |
| 173 | + $addressObject->personal = $personal; | |
| 174 | + } | |
| 175 | + | |
| 176 | + $addressObjectList[] = $addressObject; | |
| 177 | + } | |
| 178 | + | |
| 179 | + return $addressObjectList; | |
| 180 | + } | |
| 181 | + | |
| 182 | + public static function isBackportCall($backtrace, $depth) | |
| 183 | + { | |
| 184 | + return isset($backtrace[$depth + 1]['function']) | |
| 185 | + && preg_match('/^imap_/', $backtrace[$depth + 1]['function']) | |
| 186 | + && preg_match('/^imap2_/', $backtrace[$depth]['function']) | |
| 187 | + && substr($backtrace[$depth + 1]['function'], 4) == substr($backtrace[$depth]['function'], 5); | |
| 188 | + } | |
| 189 | + | |
| 190 | + public static function isRetrofitResource($imap) | |
| 191 | + { | |
| 192 | + return is_resource($imap) && get_resource_type($imap) == 'imap'; | |
| 193 | + } | |
| 194 | +} | |