| @@ -1,498 +1,0 @@ | ||
| 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 Message | |
| 15 | -{ | |
| 16 | - /** | |
| 17 | - * Returns an array of messages matching the given search criteria. | |
| 18 | - * | |
| 19 | - * @param $imap | |
| 20 | - * @param $criteria | |
| 21 | - * @param $flags | |
| 22 | - * @param $charset | |
| 23 | - * | |
| 24 | - * @return array|false|mixed | |
| 25 | - */ | |
| 26 | - public static function search($imap, $criteria, $flags = SE_FREE, $charset = "") | |
| 27 | - { | |
| 28 | - if (!is_a($imap, Connection::class)) { | |
| 29 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 30 | - } | |
| 31 | - | |
| 32 | - $client = $imap->getClient(); | |
| 33 | - #$client->setDebug(true); | |
| 34 | - | |
| 35 | - $result = $client->search($imap->getMailboxName(), $criteria, $flags & SE_UID); | |
| 36 | - | |
| 37 | - if (empty($result->count())) { | |
| 38 | - return false; | |
| 39 | - } | |
| 40 | - | |
| 41 | - $messages = $result->get(); | |
| 42 | - foreach ($messages as &$message) { | |
| 43 | - $message = is_numeric($message) ? intval($message) : $message; | |
| 44 | - } | |
| 45 | - | |
| 46 | - return $messages; | |
| 47 | - } | |
| 48 | - | |
| 49 | - public static function sort($imap, $criteria, $reverse, $flags = 0, $searchCriteria = null, $charset = null) | |
| 50 | - { | |
| 51 | - if (is_a($imap, Connection::class)) { | |
| 52 | - $client = $imap->getClient(); | |
| 53 | - #$client->setDebug(true); | |
| 54 | - | |
| 55 | - $result = $client->search($imap->getMailboxName(), $criteria, $flags & SE_UID); | |
| 56 | - | |
| 57 | - if (empty($result->count())) { | |
| 58 | - return false; | |
| 59 | - } | |
| 60 | - | |
| 61 | - $messages = $result->get(); | |
| 62 | - foreach ($messages as &$message) { | |
| 63 | - $message = is_numeric($message) ? intval($message) : $message; | |
| 64 | - } | |
| 65 | - | |
| 66 | - return $messages; | |
| 67 | - } | |
| 68 | - | |
| 69 | - return imap_sort($imap, $criteria, $reverse, $flags, $searchCriteria, $charset); | |
| 70 | - } | |
| 71 | - | |
| 72 | - public static function headerInfo($imap, $messageNum, $fromLength = 0, $subjectLength = 0, $defaultHost = null) | |
| 73 | - { | |
| 74 | - if (!is_a($imap, Connection::class)) { | |
| 75 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 76 | - } | |
| 77 | - | |
| 78 | - $client = $imap->getClient(); | |
| 79 | - #$client->setDebug(true); | |
| 80 | - | |
| 81 | - $messages = $client->fetch($imap->getMailboxName(), $messageNum, false, [ | |
| 82 | - 'BODY.PEEK[HEADER.FIELDS (SUBJECT FROM TO CC REPLY-TO DATE SIZE REFERENCES)]', | |
| 83 | - 'ENVELOPE', | |
| 84 | - 'INTERNALDATE', | |
| 85 | - 'UID', | |
| 86 | - 'FLAGS', | |
| 87 | - 'RFC822.SIZE', | |
| 88 | - 'RFC822.HEADER', | |
| 89 | - ]); | |
| 90 | - | |
| 91 | - if (empty($messages)) { | |
| 92 | - return false; | |
| 93 | - } | |
| 94 | - | |
| 95 | - foreach ($messages as $message) { | |
| 96 | - return HeaderInfo::fromMessage($message, $defaultHost); | |
| 97 | - } | |
| 98 | - } | |
| 99 | - | |
| 100 | - public static function headers($imap) | |
| 101 | - { | |
| 102 | - if (!is_a($imap, Connection::class)) { | |
| 103 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 104 | - } | |
| 105 | - | |
| 106 | - $client = $imap->getClient(); | |
| 107 | - #$client->setDebug(true); | |
| 108 | - | |
| 109 | - $status = $client->status($imap->getMailboxName(), ['MESSAGES']); | |
| 110 | - if (empty($status['MESSAGES'])) { | |
| 111 | - return []; | |
| 112 | - } | |
| 113 | - | |
| 114 | - $sequence = '1:'.intval($status['MESSAGES']); | |
| 115 | - $messages = $client->fetch($imap->getMailboxName(), $sequence, false, [ | |
| 116 | - 'BODY.PEEK[HEADER.FIELDS (SUBJECT FROM TO CC REPLYTO MESSAGEID DATE SIZE REFERENCES)]', | |
| 117 | - #'UID', | |
| 118 | - 'FLAGS', | |
| 119 | - 'INTERNALDATE', | |
| 120 | - 'RFC822.SIZE', | |
| 121 | - #'ENVELOPE', | |
| 122 | - 'RFC822.HEADER', | |
| 123 | - ]); | |
| 124 | - | |
| 125 | - if (empty($messages)) { | |
| 126 | - return []; | |
| 127 | - } | |
| 128 | - | |
| 129 | - $headers = []; | |
| 130 | - foreach ($messages as $message) { | |
| 131 | - $from = ' '; | |
| 132 | - if ($message->from != 'no_host') { | |
| 133 | - $from = imap_rfc822_parse_adrlist($message->from, 'no_host'); | |
| 134 | - $from = isset($from[0]->personal) ? $from[0]->personal : $message->from; | |
| 135 | - } | |
| 136 | - | |
| 137 | - $date = explode(' ', $message->internaldate); | |
| 138 | - $subject = empty($message->subject) ? ' ' : $message->subject; | |
| 139 | - $unseen = empty($message->flags['SEEN']) ? 'U' : ' '; | |
| 140 | - $flagged = empty($message->flags['FLAGGED']) ? ' ' : 'F'; | |
| 141 | - $answered = empty($message->flags['ANSWERED']) ? ' ' : 'A'; | |
| 142 | - $draft = empty($message->flags['DRAFT']) ? ' ' : 'D'; | |
| 143 | - $deleted = empty($message->flags['DELETED']) ? ' ' : 'X'; | |
| 144 | - | |
| 145 | - $header = ' '.$unseen.$flagged.$answered.$draft.$deleted.' ' | |
| 146 | - .str_pad($message->id, 3, ' ', STR_PAD_LEFT).')'.$date[0].' '.str_pad($from, 20, ' ').' ' | |
| 147 | - .substr($subject, 0, 25).' ('.$message->size.' chars)'; | |
| 148 | - | |
| 149 | - $headers[] = $header; | |
| 150 | - } | |
| 151 | - | |
| 152 | - return $headers; | |
| 153 | - } | |
| 154 | - | |
| 155 | - public static function body($imap, $messageNum, $flags = 0) | |
| 156 | - { | |
| 157 | - if (!is_a($imap, Connection::class)) { | |
| 158 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 159 | - } | |
| 160 | - | |
| 161 | - $client = $imap->getClient(); | |
| 162 | - #$client->setDebug(true); | |
| 163 | - | |
| 164 | - $isUid = boolval($flags & FT_UID); | |
| 165 | - | |
| 166 | - $messages = $client->fetch($imap->getMailboxName(), $messageNum, $isUid, ['BODY[TEXT]']); | |
| 167 | - | |
| 168 | - return $messages[$messageNum]->bodypart['TEXT']; | |
| 169 | - } | |
| 170 | - | |
| 171 | - public static function fetchBody($imap, $messageNum, $section, $flags = 0) | |
| 172 | - { | |
| 173 | - if (!is_a($imap, Connection::class)) { | |
| 174 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 175 | - } | |
| 176 | - | |
| 177 | - $client = $imap->getClient(); | |
| 178 | - #$client->setDebug(true); | |
| 179 | - | |
| 180 | - $isUid = boolval($flags & FT_UID); | |
| 181 | - $messages = $client->fetch($imap->getMailboxName(), $messageNum, $isUid, ['BODY['.$section.']']); | |
| 182 | - | |
| 183 | - if (empty($messages)) { | |
| 184 | - trigger_error(Errors::badMessageNumber(debug_backtrace(), 1), E_USER_WARNING); | |
| 185 | - | |
| 186 | - return false; | |
| 187 | - } | |
| 188 | - | |
| 189 | - if ($section) { | |
| 190 | - return $messages[$messageNum]->bodypart[$section]; | |
| 191 | - } | |
| 192 | - | |
| 193 | - return $messages[$messageNum]->body; | |
| 194 | - } | |
| 195 | - | |
| 196 | - public static function fetchMime($imap, $messageNum, $section, $flags = 0) | |
| 197 | - { | |
| 198 | - if (!is_a($imap, Connection::class)) { | |
| 199 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 200 | - } | |
| 201 | - | |
| 202 | - if ($messageNum <= 0 || !is_int($messageNum)) { | |
| 203 | - trigger_error(Errors::badMessageNumber(debug_backtrace(), 1), E_USER_WARNING); | |
| 204 | - | |
| 205 | - return false; | |
| 206 | - } | |
| 207 | - | |
| 208 | - $client = $imap->getClient(); | |
| 209 | - #$client->setDebug(true); | |
| 210 | - | |
| 211 | - $isUid = boolval($flags & FT_UID); | |
| 212 | - | |
| 213 | - $sectionKey = $section.'.MIME'; | |
| 214 | - $messages = $client->fetch($imap->getMailboxName(), $messageNum, $isUid, ['BODY['.$sectionKey.']']); | |
| 215 | - | |
| 216 | - if (empty($messages)) { | |
| 217 | - return ""; | |
| 218 | - } | |
| 219 | - | |
| 220 | - if ($section && isset($messages[$messageNum]->bodypart[$sectionKey])) { | |
| 221 | - return $messages[$messageNum]->bodypart[$sectionKey]; | |
| 222 | - } | |
| 223 | - | |
| 224 | - return $messages[$messageNum]->body; | |
| 225 | - } | |
| 226 | - | |
| 227 | - public static function saveBody($imap, $file, $messageNum, $section = "", $flags = 0) | |
| 228 | - { | |
| 229 | - if (is_a($imap, Connection::class)) { | |
| 230 | - $client = $imap->getClient(); | |
| 231 | - #$client->setDebug(true); | |
| 232 | - | |
| 233 | - $messages = $client->fetch($imap->getMailboxName(), $messageNum, false, ['BODY['.$section.']']); | |
| 234 | - | |
| 235 | - $body = $section ? $messages[$messageNum]->bodypart[$section] : $messages[$messageNum]->body; | |
| 236 | - | |
| 237 | - return file_put_contents($file, $body); | |
| 238 | - } | |
| 239 | - | |
| 240 | - return imap_savebody($imap, $file, $messageNum, $section, $flags); | |
| 241 | - } | |
| 242 | - | |
| 243 | - public static function fetchStructure($imap, $messageNum, $flags = 0) | |
| 244 | - { | |
| 245 | - if (!is_a($imap, Connection::class)) { | |
| 246 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 247 | - } | |
| 248 | - | |
| 249 | - $client = $imap->getClient(); | |
| 250 | - #$client->setDebug(true); | |
| 251 | - | |
| 252 | - $isUid = boolval($flags & FT_UID); | |
| 253 | - | |
| 254 | - $messages = $client->fetch($imap->getMailboxName(), $messageNum, $isUid, ['BODYSTRUCTURE']); | |
| 255 | - | |
| 256 | - if (empty($messages)) { | |
| 257 | - return false; | |
| 258 | - } | |
| 259 | - | |
| 260 | - foreach ($messages as $message) { | |
| 261 | - return BodyStructure::fromMessage($message); | |
| 262 | - } | |
| 263 | - } | |
| 264 | - | |
| 265 | - public static function fetchHeader($imap, $messageNum, $flags = 0) | |
| 266 | - { | |
| 267 | - if (!is_a($imap, Connection::class)) { | |
| 268 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 269 | - } | |
| 270 | - | |
| 271 | - /* | |
| 272 | - * FT_UID - The message_num argument is a UID | |
| 273 | - FT_INTERNAL - The return string is in "internal" format, without any attempt to canonicalize to CRLF newlines | |
| 274 | - FT_PREFETCHTEXT | |
| 275 | - * */ | |
| 276 | - | |
| 277 | - $client = $imap->getClient(); | |
| 278 | - #$client->setDebug(true); | |
| 279 | - | |
| 280 | - $isUid = boolval($flags & FT_UID); | |
| 281 | - | |
| 282 | - $messages = $client->fetch($imap->getMailboxName(), $messageNum, $isUid, ['BODY[HEADER]']); | |
| 283 | - | |
| 284 | - if (empty($messages)) { | |
| 285 | - return false; | |
| 286 | - } | |
| 287 | - | |
| 288 | - foreach ($messages as $message) { | |
| 289 | - return $message->bodypart['HEADER'] ?? false; | |
| 290 | - } | |
| 291 | - } | |
| 292 | - | |
| 293 | - public static function fetchOverview($imap, $sequence, $flags = 0) | |
| 294 | - { | |
| 295 | - if (!is_a($imap, Connection::class)) { | |
| 296 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 297 | - } | |
| 298 | - | |
| 299 | - $client = $imap->getClient(); | |
| 300 | - #$client->setDebug(true); | |
| 301 | - | |
| 302 | - $messages = $client->fetch($imap->getMailboxName(), $sequence, false, [ | |
| 303 | - 'BODY[HEADER.FIELDS (SUBJECT FROM TO CC REPLYTO MESSAGEID DATE SIZE REFERENCES)]', | |
| 304 | - 'UID', | |
| 305 | - 'FLAGS', | |
| 306 | - 'INTERNALDATE', | |
| 307 | - 'RFC822.SIZE', | |
| 308 | - 'ENVELOPE', | |
| 309 | - 'RFC822.HEADER', | |
| 310 | - ]); | |
| 311 | - | |
| 312 | - if ($sequence != '*' && count($messages) < Functions::expectedNumberOfMessages($sequence)) { | |
| 313 | - return []; | |
| 314 | - } | |
| 315 | - | |
| 316 | - $overview = []; | |
| 317 | - foreach ($messages as $message) { | |
| 318 | - #var_dump($message); | |
| 319 | - #die(); | |
| 320 | - $messageEntry = (object)[ | |
| 321 | - 'subject' => $message->envelope[1], | |
| 322 | - 'from' => Functions::writeAddressFromEnvelope($message->envelope[2]), | |
| 323 | - 'to' => $message->get('to'), | |
| 324 | - 'date' => $message->envelope[0], | |
| 325 | - 'message_id' => $message->envelope[9], | |
| 326 | - 'references' => $message->references, | |
| 327 | - 'in_reply_to' => $message->envelope[8], | |
| 328 | - 'size' => $message->size, | |
| 329 | - 'uid' => $message->uid, | |
| 330 | - 'msgno' => $message->id, | |
| 331 | - 'recent' => intval($message->flags['RECENT'] ?? 0), | |
| 332 | - 'flagged' => intval($message->flags['FLAGGED'] ?? 0), | |
| 333 | - 'answered' => intval($message->flags['ANSWERED'] ?? 0), | |
| 334 | - 'deleted' => intval($message->flags['DELETED'] ?? 0), | |
| 335 | - 'seen' => intval($message->flags['SEEN'] ?? 0), | |
| 336 | - 'draft' => intval($message->flags['DRAFT'] ?? 0), | |
| 337 | - 'udate' => strtotime($message->internaldate), | |
| 338 | - ]; | |
| 339 | - | |
| 340 | - if (empty($messageEntry->subject)) { | |
| 341 | - unset($messageEntry->subject); | |
| 342 | - } | |
| 343 | - | |
| 344 | - if (empty($messageEntry->references)) { | |
| 345 | - unset($messageEntry->references); | |
| 346 | - } | |
| 347 | - | |
| 348 | - if (empty($messageEntry->in_reply_to)) { | |
| 349 | - unset($messageEntry->in_reply_to); | |
| 350 | - } | |
| 351 | - | |
| 352 | - if (empty($messageEntry->to)) { | |
| 353 | - unset($messageEntry->to); | |
| 354 | - } | |
| 355 | - | |
| 356 | - $overview[] = $messageEntry; | |
| 357 | - } | |
| 358 | - | |
| 359 | - return $overview; | |
| 360 | - } | |
| 361 | - | |
| 362 | - public static function delete($imap, $messageNums, $flags = 0) | |
| 363 | - { | |
| 364 | - if (!is_a($imap, Connection::class)) { | |
| 365 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 366 | - } | |
| 367 | - | |
| 368 | - $mailboxName = $imap->getMailboxName(); | |
| 369 | - | |
| 370 | - if ($mailboxName === Connection::DEFAULT_MAILBOX_GMAIL) { | |
| 371 | - Mail::move($imap, $messageNums, Connection::GMAIL_TRASH_MAILBOX, $flags); | |
| 372 | - } else { | |
| 373 | - $client = $imap->getClient(); | |
| 374 | - $uids = ImapHelpers::idToUid($imap, $messageNums); | |
| 375 | - $client->flag($mailboxName, $uids, $client->flags['DELETED']); | |
| 376 | - } | |
| 377 | - | |
| 378 | - return true; | |
| 379 | - } | |
| 380 | - | |
| 381 | - public static function undelete($imap, $messageNums, $flags = 0) | |
| 382 | - { | |
| 383 | - if (is_a($imap, Connection::class)) { | |
| 384 | - $client = $imap->getClient(); | |
| 385 | - #$client->setDebug(true); | |
| 386 | - | |
| 387 | - $messages = $client->fetch($imap->getMailboxName(), $messageNums, false, ['UID']); | |
| 388 | - foreach ($messages as $message) { | |
| 389 | - $client->unflag($imap->getMailboxName(), $message->uid, $client->flags['DELETED']); | |
| 390 | - } | |
| 391 | - | |
| 392 | - return true; | |
| 393 | - } | |
| 394 | - | |
| 395 | - return imap_undelete($imap, $messageNums, $flags); | |
| 396 | - } | |
| 397 | - | |
| 398 | - public static function expunge($imap) | |
| 399 | - { | |
| 400 | - if (is_a($imap, Connection::class)) { | |
| 401 | - $client = $imap->getClient(); | |
| 402 | - | |
| 403 | - return $client->expunge($imap->getMailboxName()); | |
| 404 | - } | |
| 405 | - | |
| 406 | - return imap_expunge($imap); | |
| 407 | - } | |
| 408 | - | |
| 409 | - /** | |
| 410 | - * Sets flags on messages. | |
| 411 | - * | |
| 412 | - * @param $imap | |
| 413 | - * @param $sequence | |
| 414 | - * @param $flag | |
| 415 | - * @param $options | |
| 416 | - * | |
| 417 | - * @return bool | |
| 418 | - */ | |
| 419 | - public static function setFlagFull($imap, $sequence, $flag, $options = 0) | |
| 420 | - { | |
| 421 | - if (!is_a($imap, Connection::class)) { | |
| 422 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 423 | - } | |
| 424 | - | |
| 425 | - $client = $imap->getClient(); | |
| 426 | - | |
| 427 | - if (!($options & ST_UID)) { | |
| 428 | - $messages = $client->fetch($imap->getMailboxName(), $sequence, false, ['UID']); | |
| 429 | - | |
| 430 | - $uid = []; | |
| 431 | - foreach ($messages as $message) { | |
| 432 | - $uid[] = $message->uid; | |
| 433 | - } | |
| 434 | - | |
| 435 | - $sequence = implode(',', $uid); | |
| 436 | - } | |
| 437 | - | |
| 438 | - return $client->flag($imap->getMailboxName(), $sequence, strtoupper(substr($flag, 1))); | |
| 439 | - } | |
| 440 | - | |
| 441 | - /** | |
| 442 | - * Clears flags on messages. | |
| 443 | - * | |
| 444 | - * @param $imap | |
| 445 | - * @param $sequence | |
| 446 | - * @param $flag | |
| 447 | - * @param $options | |
| 448 | - * | |
| 449 | - * @return false|string | |
| 450 | - */ | |
| 451 | - public static function clearFlagFull($imap, $sequence, $flag, $options = 0) | |
| 452 | - { | |
| 453 | - if (!is_a($imap, Connection::class)) { | |
| 454 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 455 | - } | |
| 456 | - | |
| 457 | - $client = $imap->getClient(); | |
| 458 | - | |
| 459 | - if (!($options & ST_UID)) { | |
| 460 | - $messages = $client->fetch($imap->getMailboxName(), $sequence, false, ['UID']); | |
| 461 | - | |
| 462 | - $uid = []; | |
| 463 | - foreach ($messages as $message) { | |
| 464 | - $uid[] = $message->uid; | |
| 465 | - } | |
| 466 | - | |
| 467 | - $sequence = implode(',', $uid); | |
| 468 | - } | |
| 469 | - | |
| 470 | - $client->unflag($imap->getMailboxName(), $sequence, strtoupper(substr($flag, 1))); | |
| 471 | - | |
| 472 | - return false; | |
| 473 | - } | |
| 474 | - | |
| 475 | - public static function msgno($imap, $messageUid) | |
| 476 | - { | |
| 477 | - if (!is_a($imap, Connection::class)) { | |
| 478 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 479 | - } | |
| 480 | - | |
| 481 | - $client = $imap->getClient(); | |
| 482 | - | |
| 483 | - $msgNo = ImapHelpers::uidToId($imap, $messageUid); | |
| 484 | - | |
| 485 | - return is_numeric($msgNo) ? intval($msgNo) : $msgNo; | |
| 486 | - } | |
| 487 | - | |
| 488 | - public static function uid($imap, $messageNum) | |
| 489 | - { | |
| 490 | - if (!is_a($imap, Connection::class)) { | |
| 491 | - return Errors::invalidImapConnection(debug_backtrace(), 1, false); | |
| 492 | - } | |
| 493 | - | |
| 494 | - $uid = ImapHelpers::idToUid($imap, $messageNum); | |
| 495 | - | |
| 496 | - return is_numeric($uid) ? intval($uid) : $uid; | |
| 497 | - } | |
| 498 | -} | |