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 | class CRM_Mailing_MailStore { | |
7e8c8317 SL |
18 | /** |
19 | * flag to decide whether to print debug messages | |
20 | * @var bool | |
21 | */ | |
22 | public $_debug = FALSE; | |
6a488035 TO |
23 | |
24 | /** | |
25606795 | 25 | * Return the proper mail store implementation, based on config settings. |
6a488035 | 26 | * |
90c8230e TO |
27 | * @param string $name |
28 | * Name of the settings set from civimail_mail_settings to use (null for default). | |
6a488035 | 29 | * |
77b97be7 | 30 | * @throws Exception |
dae4aa68 | 31 | * @return CRM_Mailing_MailStore |
a6c01b45 | 32 | * mail store implementation for processing CiviMail-bound emails |
6a488035 | 33 | */ |
8785b897 | 34 | public static function getStore($name = NULL) { |
317fceb4 | 35 | $dao = new CRM_Core_DAO_MailSettings(); |
353ffa53 | 36 | $dao->domain_id = CRM_Core_Config::domainID(); |
6a488035 TO |
37 | $name ? $dao->name = $name : $dao->is_default = 1; |
38 | if (!$dao->find(TRUE)) { | |
39 | throw new Exception("Could not find entry named $name in civicrm_mail_settings"); | |
40 | } | |
41 | ||
01a9c9d7 | 42 | $protocols = CRM_Core_PseudoConstant::get('CRM_Core_DAO_MailSettings', 'protocol', [], 'validate'); |
f928a0b0 TO |
43 | |
44 | // Prepare normalized/hookable representation of the mail settings. | |
45 | $mailSettings = $dao->toArray(); | |
46 | $mailSettings['protocol'] = $protocols[$mailSettings['protocol']] ?? NULL; | |
47 | $protocolDefaults = self::getProtocolDefaults($mailSettings['protocol']); | |
48 | $mailSettings = array_merge($protocolDefaults, $mailSettings); | |
49 | ||
50 | CRM_Utils_Hook::alterMailStore($mailSettings); | |
51 | ||
52 | if (!empty($mailSettings['factory'])) { | |
53 | return call_user_func($mailSettings['factory'], $mailSettings); | |
d25c89a1 | 54 | } |
f928a0b0 TO |
55 | else { |
56 | throw new Exception("Unknown protocol {$mailSettings['protocol']}"); | |
57 | } | |
58 | } | |
6a488035 | 59 | |
f928a0b0 TO |
60 | /** |
61 | * @param string $protocol | |
62 | * Ex: 'IMAP', 'Maildir' | |
63 | * @return array | |
64 | * List of properties to merge into the $mailSettings. | |
65 | * The most important property is 'factory' with signature: | |
66 | * | |
67 | * function($mailSettings): CRM_Mailing_MailStore | |
68 | */ | |
69 | private static function getProtocolDefaults($protocol) { | |
70 | switch ($protocol) { | |
6a488035 | 71 | case 'IMAP': |
f928a0b0 | 72 | return [ |
3d839125 | 73 | 'auth' => 'Password', |
f928a0b0 TO |
74 | 'factory' => function($mailSettings) { |
75 | $useXOAuth2 = ($mailSettings['auth'] === 'XOAuth2'); | |
76 | return new CRM_Mailing_MailStore_Imap($mailSettings['server'], $mailSettings['username'], $mailSettings['password'], (bool) $mailSettings['is_ssl'], $mailSettings['source'], $useXOAuth2); | |
77 | }, | |
78 | ]; | |
01a9c9d7 | 79 | |
6a488035 | 80 | case 'POP3': |
f928a0b0 TO |
81 | return [ |
82 | 'factory' => function ($mailSettings) { | |
83 | return new CRM_Mailing_MailStore_Pop3($mailSettings['server'], $mailSettings['username'], $mailSettings['password'], (bool) $mailSettings['is_ssl']); | |
84 | }, | |
85 | ]; | |
6a488035 TO |
86 | |
87 | case 'Maildir': | |
f928a0b0 TO |
88 | return [ |
89 | 'factory' => function ($mailSettings) { | |
90 | return new CRM_Mailing_MailStore_Maildir($mailSettings['source']); | |
91 | }, | |
92 | ]; | |
6a488035 TO |
93 | |
94 | case 'Localdir': | |
f928a0b0 TO |
95 | return [ |
96 | 'factory' => function ($mailSettings) { | |
97 | return new CRM_Mailing_MailStore_Localdir($mailSettings['source']); | |
98 | }, | |
99 | ]; | |
6a488035 TO |
100 | |
101 | // DO NOT USE the mbox transport for anything other than testing | |
102 | // in particular, it does not clear the mbox afterwards | |
6a488035 | 103 | case 'mbox': |
f928a0b0 TO |
104 | return [ |
105 | 'factory' => function ($mailSettings) { | |
106 | return new CRM_Mailing_MailStore_Mbox($mailSettings['source']); | |
107 | }, | |
108 | ]; | |
6a488035 TO |
109 | |
110 | default: | |
f928a0b0 | 111 | return []; |
6a488035 TO |
112 | } |
113 | } | |
114 | ||
115 | /** | |
fe482240 | 116 | * Return all emails in the mail store. |
6a488035 | 117 | * |
a6c01b45 CW |
118 | * @return array |
119 | * array of ezcMail objects | |
6a488035 | 120 | */ |
00be9182 | 121 | public function allMails() { |
6a488035 TO |
122 | return $this->fetchNext(0); |
123 | } | |
124 | ||
125 | /** | |
25606795 | 126 | * Expunge the messages marked for deletion; stub function to be redefined by IMAP store. |
6a488035 | 127 | */ |
35f7561f TO |
128 | public function expunge() { |
129 | } | |
6a488035 TO |
130 | |
131 | /** | |
fe482240 | 132 | * Return the next X messages from the mail store. |
6a488035 | 133 | * |
90c8230e TO |
134 | * @param int $count |
135 | * Number of messages to fetch (0 to fetch all). | |
6a488035 | 136 | * |
a6c01b45 CW |
137 | * @return array |
138 | * array of ezcMail objects | |
6a488035 | 139 | */ |
00be9182 | 140 | public function fetchNext($count = 1) { |
9372d656 | 141 | $offset = 1; |
6a488035 | 142 | if (isset($this->_transport->options->uidReferencing) and $this->_transport->options->uidReferencing) { |
9372d656 | 143 | $offset = $this->_transport->listUniqueIdentifiers(); |
144 | $offset = array_shift($offset); | |
6a488035 TO |
145 | } |
146 | try { | |
147 | $set = $this->_transport->fetchFromOffset($offset, $count); | |
148 | if ($this->_debug) { | |
149 | print "fetching $count messages\n"; | |
150 | } | |
151 | } | |
353ffa53 | 152 | catch (ezcMailOffsetOutOfRangeException$e) { |
6a488035 TO |
153 | if ($this->_debug) { |
154 | print "got to the end of the mailbox\n"; | |
155 | } | |
be2fb01f | 156 | return []; |
6a488035 | 157 | } |
be2fb01f | 158 | $mails = []; |
317fceb4 | 159 | $parser = new ezcMailParser(); |
6a488035 TO |
160 | //set property text attachment as file CRM-5408 |
161 | $parser->options->parseTextAttachmentsAsFiles = TRUE; | |
162 | ||
163 | foreach ($set->getMessageNumbers() as $nr) { | |
164 | if ($this->_debug) { | |
165 | print "retrieving message $nr\n"; | |
166 | } | |
167 | $single = $parser->parseMail($this->_transport->fetchByMessageNr($nr)); | |
168 | $mails[$nr] = $single[0]; | |
169 | } | |
170 | return $mails; | |
171 | } | |
172 | ||
173 | /** | |
174 | * Point to (and create if needed) a local Maildir for storing retrieved mail | |
175 | * | |
90c8230e TO |
176 | * @param string $name |
177 | * Name of the Maildir. | |
6a488035 | 178 | * |
77b97be7 | 179 | * @throws Exception |
a6c01b45 CW |
180 | * @return string |
181 | * path to the Maildir's cur directory | |
6a488035 | 182 | */ |
00be9182 | 183 | public function maildir($name) { |
6a488035 TO |
184 | $config = CRM_Core_Config::singleton(); |
185 | $dir = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $name; | |
be2fb01f | 186 | foreach ([ |
7e8c8317 SL |
187 | 'cur', |
188 | 'new', | |
189 | 'tmp', | |
190 | ] as $sub) { | |
6a488035 TO |
191 | if (!file_exists($dir . DIRECTORY_SEPARATOR . $sub)) { |
192 | if ($this->_debug) { | |
193 | print "creating $dir/$sub\n"; | |
194 | } | |
195 | if (!mkdir($dir . DIRECTORY_SEPARATOR . $sub, 0700, TRUE)) { | |
196 | throw new Exception('Could not create ' . $dir . DIRECTORY_SEPARATOR . $sub); | |
197 | } | |
198 | } | |
199 | } | |
200 | return $dir . DIRECTORY_SEPARATOR . 'cur'; | |
201 | } | |
96025800 | 202 | |
6a488035 | 203 | } |