Merge pull request #19057 from ixiam/dev/core#2173
[civicrm-core.git] / CRM / Mailing / MailStore.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 class CRM_Mailing_MailStore {
18 /**
19 * flag to decide whether to print debug messages
20 * @var bool
21 */
22 public $_debug = FALSE;
23
24 /**
25 * Return the proper mail store implementation, based on config settings.
26 *
27 * @param string $name
28 * Name of the settings set from civimail_mail_settings to use (null for default).
29 *
30 * @throws Exception
31 * @return CRM_Mailing_MailStore
32 * mail store implementation for processing CiviMail-bound emails
33 */
34 public static function getStore($name = NULL) {
35 $dao = new CRM_Core_DAO_MailSettings();
36 $dao->domain_id = CRM_Core_Config::domainID();
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
42 $protocols = CRM_Core_PseudoConstant::get('CRM_Core_DAO_MailSettings', 'protocol', [], 'validate');
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);
54 }
55 else {
56 throw new Exception("Unknown protocol {$mailSettings['protocol']}");
57 }
58 }
59
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) {
71 case 'IMAP':
72 case 'IMAP_XOAUTH2':
73 return [
74 // For backward compat with pre-release XOAuth2 configurations
75 'auth' => $protocol === 'IMAP_XOAUTH2' ? 'XOAuth2' : 'Password',
76 // In a simpler world:
77 // 'auth' => 'Password',
78 'factory' => function($mailSettings) {
79 $useXOAuth2 = ($mailSettings['auth'] === 'XOAuth2');
80 return new CRM_Mailing_MailStore_Imap($mailSettings['server'], $mailSettings['username'], $mailSettings['password'], (bool) $mailSettings['is_ssl'], $mailSettings['source'], $useXOAuth2);
81 },
82 ];
83
84 case 'POP3':
85 return [
86 'factory' => function ($mailSettings) {
87 return new CRM_Mailing_MailStore_Pop3($mailSettings['server'], $mailSettings['username'], $mailSettings['password'], (bool) $mailSettings['is_ssl']);
88 },
89 ];
90
91 case 'Maildir':
92 return [
93 'factory' => function ($mailSettings) {
94 return new CRM_Mailing_MailStore_Maildir($mailSettings['source']);
95 },
96 ];
97
98 case 'Localdir':
99 return [
100 'factory' => function ($mailSettings) {
101 return new CRM_Mailing_MailStore_Localdir($mailSettings['source']);
102 },
103 ];
104
105 // DO NOT USE the mbox transport for anything other than testing
106 // in particular, it does not clear the mbox afterwards
107 case 'mbox':
108 return [
109 'factory' => function ($mailSettings) {
110 return new CRM_Mailing_MailStore_Mbox($mailSettings['source']);
111 },
112 ];
113
114 default:
115 return [];
116 }
117 }
118
119 /**
120 * Return all emails in the mail store.
121 *
122 * @return array
123 * array of ezcMail objects
124 */
125 public function allMails() {
126 return $this->fetchNext(0);
127 }
128
129 /**
130 * Expunge the messages marked for deletion; stub function to be redefined by IMAP store.
131 */
132 public function expunge() {
133 }
134
135 /**
136 * Return the next X messages from the mail store.
137 *
138 * @param int $count
139 * Number of messages to fetch (0 to fetch all).
140 *
141 * @return array
142 * array of ezcMail objects
143 */
144 public function fetchNext($count = 1) {
145 $offset = 1;
146 if (isset($this->_transport->options->uidReferencing) and $this->_transport->options->uidReferencing) {
147 $offset = $this->_transport->listUniqueIdentifiers();
148 $offset = array_shift($offset);
149 }
150 try {
151 $set = $this->_transport->fetchFromOffset($offset, $count);
152 if ($this->_debug) {
153 print "fetching $count messages\n";
154 }
155 }
156 catch (ezcMailOffsetOutOfRangeException$e) {
157 if ($this->_debug) {
158 print "got to the end of the mailbox\n";
159 }
160 return [];
161 }
162 $mails = [];
163 $parser = new ezcMailParser();
164 //set property text attachment as file CRM-5408
165 $parser->options->parseTextAttachmentsAsFiles = TRUE;
166
167 foreach ($set->getMessageNumbers() as $nr) {
168 if ($this->_debug) {
169 print "retrieving message $nr\n";
170 }
171 $single = $parser->parseMail($this->_transport->fetchByMessageNr($nr));
172 $mails[$nr] = $single[0];
173 }
174 return $mails;
175 }
176
177 /**
178 * Point to (and create if needed) a local Maildir for storing retrieved mail
179 *
180 * @param string $name
181 * Name of the Maildir.
182 *
183 * @throws Exception
184 * @return string
185 * path to the Maildir's cur directory
186 */
187 public function maildir($name) {
188 $config = CRM_Core_Config::singleton();
189 $dir = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $name;
190 foreach ([
191 'cur',
192 'new',
193 'tmp',
194 ] as $sub) {
195 if (!file_exists($dir . DIRECTORY_SEPARATOR . $sub)) {
196 if ($this->_debug) {
197 print "creating $dir/$sub\n";
198 }
199 if (!mkdir($dir . DIRECTORY_SEPARATOR . $sub, 0700, TRUE)) {
200 throw new Exception('Could not create ' . $dir . DIRECTORY_SEPARATOR . $sub);
201 }
202 }
203 }
204 return $dir . DIRECTORY_SEPARATOR . 'cur';
205 }
206
207 }