Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
28518c90 EM |
18 | /** |
19 | * Class CRM_Mailing_MailStore_Imap | |
20 | */ | |
6a488035 TO |
21 | class CRM_Mailing_MailStore_Imap extends CRM_Mailing_MailStore { |
22 | ||
23 | /** | |
fe482240 | 24 | * Connect to the supplied IMAP server and make sure the two mailboxes exist. |
6a488035 | 25 | * |
90c8230e TO |
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. | |
01a9c9d7 SL |
36 | * @param bool $useXOAUTH2 |
37 | * Use XOAUTH2 authentication method | |
6a488035 | 38 | * |
dd244018 | 39 | * @return \CRM_Mailing_MailStore_Imap |
6a488035 | 40 | */ |
01a9c9d7 | 41 | public function __construct($host, $username, $password, $ssl = TRUE, $folder = 'INBOX', $useXOAUTH2 = FALSE) { |
6a488035 TO |
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 | ||
bd63411f | 53 | $options = [ |
54 | 'listLimit' => defined('MAIL_BATCH_SIZE') ? MAIL_BATCH_SIZE : 1000, | |
55 | 'ssl' => $ssl, | |
56 | 'uidReferencing' => TRUE, | |
57 | ]; | |
6a488035 | 58 | $this->_transport = new ezcMailImapTransport($host, NULL, $options); |
c2f7a076 | 59 | if ($useXOAUTH2) { |
01a9c9d7 SL |
60 | $this->_transport->authenticate($username, $password, ezcMailImapTransport::AUTH_XOAUTH2); |
61 | } | |
62 | else { | |
63 | $this->_transport->authenticate($username, $password); | |
64 | } | |
6a488035 TO |
65 | $this->_transport->selectMailbox($folder); |
66 | ||
be2fb01f CW |
67 | $this->_ignored = implode($this->_transport->getHierarchyDelimiter(), [$folder, 'CiviMail', 'ignored']); |
68 | $this->_processed = implode($this->_transport->getHierarchyDelimiter(), [$folder, 'CiviMail', 'processed']); | |
353ffa53 | 69 | $boxes = $this->_transport->listMailboxes(); |
6a488035 TO |
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 | */ | |
00be9182 | 87 | public function expunge() { |
6a488035 TO |
88 | $this->_transport->expunge(); |
89 | } | |
90 | ||
91 | /** | |
fe482240 | 92 | * Move the specified message to the ignored folder. |
6a488035 | 93 | * |
90c8230e TO |
94 | * @param int $nr |
95 | * Number of the message to move. | |
6a488035 | 96 | */ |
00be9182 | 97 | public function markIgnored($nr) { |
6a488035 TO |
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 | /** | |
fe482240 | 107 | * Move the specified message to the processed folder. |
6a488035 | 108 | * |
90c8230e TO |
109 | * @param int $nr |
110 | * Number of the message to move. | |
6a488035 | 111 | */ |
00be9182 | 112 | public function markProcessed($nr) { |
6a488035 TO |
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 | } | |
96025800 | 120 | |
6a488035 | 121 | } |