Merge pull request #19766 from WeMoveEU/faster-select2-groups
[civicrm-core.git] / CRM / Mailing / MailStore / Imap.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class CRM_Mailing_MailStore_Imap
20 */
21 class CRM_Mailing_MailStore_Imap extends CRM_Mailing_MailStore {
22
23 /**
24 * Connect to the supplied IMAP server and make sure the two mailboxes exist.
25 *
26 * @param string $host
27 * Host to connect to.
28 * @param string $username
29 * Authentication username.
30 * @param string $password
31 * Authentication password.
32 * @param bool $ssl
33 * Whether to use IMAP or IMAPS.
34 * @param string $folder
35 * Name of the inbox folder.
36 * @param bool $useXOAUTH2
37 * Use XOAUTH2 authentication method
38 *
39 * @return \CRM_Mailing_MailStore_Imap
40 */
41 public function __construct($host, $username, $password, $ssl = TRUE, $folder = 'INBOX', $useXOAUTH2 = FALSE) {
42 // default to INBOX if an empty string
43 if (!$folder) {
44 $folder = 'INBOX';
45 }
46
47 if ($this->_debug) {
48
49 print "connecting to $host, authenticating as $username and selecting $folder\n";
50
51 }
52
53 $options = [
54 'listLimit' => defined('MAIL_BATCH_SIZE') ? MAIL_BATCH_SIZE : 1000,
55 'ssl' => $ssl,
56 'uidReferencing' => TRUE,
57 ];
58 $this->_transport = new ezcMailImapTransport($host, NULL, $options);
59 if ($useXOAUTH2) {
60 $this->_transport->authenticate($username, $password, ezcMailImapTransport::AUTH_XOAUTH2);
61 }
62 else {
63 $this->_transport->authenticate($username, $password);
64 }
65 $this->_transport->selectMailbox($folder);
66
67 $this->_ignored = implode($this->_transport->getHierarchyDelimiter(), [$folder, 'CiviMail', 'ignored']);
68 $this->_processed = implode($this->_transport->getHierarchyDelimiter(), [$folder, 'CiviMail', 'processed']);
69 $boxes = $this->_transport->listMailboxes();
70
71 if ($this->_debug) {
72 print 'mailboxes found: ' . implode(', ', $boxes) . "\n";
73 }
74
75 if (!in_array(strtolower($this->_ignored), array_map('strtolower', $boxes))) {
76 $this->_transport->createMailbox($this->_ignored);
77 }
78
79 if (!in_array(strtolower($this->_processed), array_map('strtolower', $boxes))) {
80 $this->_transport->createMailbox($this->_processed);
81 }
82 }
83
84 /**
85 * Expunge the messages marked for deletion, CRM-7356
86 */
87 public function expunge() {
88 $this->_transport->expunge();
89 }
90
91 /**
92 * Move the specified message to the ignored folder.
93 *
94 * @param int $nr
95 * Number of the message to move.
96 */
97 public function markIgnored($nr) {
98 if ($this->_debug) {
99 print "setting $nr as seen and moving it to the ignored mailbox\n";
100 }
101 $this->_transport->setFlag($nr, 'SEEN');
102 $this->_transport->copyMessages($nr, $this->_ignored);
103 $this->_transport->delete($nr);
104 }
105
106 /**
107 * Move the specified message to the processed folder.
108 *
109 * @param int $nr
110 * Number of the message to move.
111 */
112 public function markProcessed($nr) {
113 if ($this->_debug) {
114 print "setting $nr as seen and moving it to the processed mailbox\n";
115 }
116 $this->_transport->setFlag($nr, 'SEEN');
117 $this->_transport->copyMessages($nr, $this->_processed);
118 $this->_transport->delete($nr);
119 }
120
121 }