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 |
a6c01b45 CW |
31 | * @return object |
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'); |
d25c89a1 JP |
43 | if (empty($protocols[$dao->protocol])) { |
44 | throw new Exception("Empty mail protocol"); | |
45 | } | |
6a488035 TO |
46 | |
47 | switch ($protocols[$dao->protocol]) { | |
48 | case 'IMAP': | |
49 | return new CRM_Mailing_MailStore_Imap($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl, $dao->source); | |
50 | ||
01a9c9d7 SL |
51 | case 'IMAP_XOAUTH2': |
52 | return new CRM_Mailing_MailStore_Imap($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl, $dao->source, TRUE); | |
53 | ||
6a488035 TO |
54 | case 'POP3': |
55 | return new CRM_Mailing_MailStore_Pop3($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl); | |
56 | ||
57 | case 'Maildir': | |
58 | return new CRM_Mailing_MailStore_Maildir($dao->source); | |
59 | ||
60 | case 'Localdir': | |
61 | return new CRM_Mailing_MailStore_Localdir($dao->source); | |
62 | ||
63 | // DO NOT USE the mbox transport for anything other than testing | |
64 | // in particular, it does not clear the mbox afterwards | |
65 | ||
66 | case 'mbox': | |
67 | return new CRM_Mailing_MailStore_Mbox($dao->source); | |
68 | ||
69 | default: | |
70 | throw new Exception("Unknown protocol {$dao->protocol}"); | |
71 | } | |
72 | } | |
73 | ||
74 | /** | |
fe482240 | 75 | * Return all emails in the mail store. |
6a488035 | 76 | * |
a6c01b45 CW |
77 | * @return array |
78 | * array of ezcMail objects | |
6a488035 | 79 | */ |
00be9182 | 80 | public function allMails() { |
6a488035 TO |
81 | return $this->fetchNext(0); |
82 | } | |
83 | ||
84 | /** | |
25606795 | 85 | * Expunge the messages marked for deletion; stub function to be redefined by IMAP store. |
6a488035 | 86 | */ |
35f7561f TO |
87 | public function expunge() { |
88 | } | |
6a488035 TO |
89 | |
90 | /** | |
fe482240 | 91 | * Return the next X messages from the mail store. |
6a488035 | 92 | * |
90c8230e TO |
93 | * @param int $count |
94 | * Number of messages to fetch (0 to fetch all). | |
6a488035 | 95 | * |
a6c01b45 CW |
96 | * @return array |
97 | * array of ezcMail objects | |
6a488035 | 98 | */ |
00be9182 | 99 | public function fetchNext($count = 1) { |
9372d656 | 100 | $offset = 1; |
6a488035 | 101 | if (isset($this->_transport->options->uidReferencing) and $this->_transport->options->uidReferencing) { |
9372d656 | 102 | $offset = $this->_transport->listUniqueIdentifiers(); |
103 | $offset = array_shift($offset); | |
6a488035 TO |
104 | } |
105 | try { | |
106 | $set = $this->_transport->fetchFromOffset($offset, $count); | |
107 | if ($this->_debug) { | |
108 | print "fetching $count messages\n"; | |
109 | } | |
110 | } | |
353ffa53 | 111 | catch (ezcMailOffsetOutOfRangeException$e) { |
6a488035 TO |
112 | if ($this->_debug) { |
113 | print "got to the end of the mailbox\n"; | |
114 | } | |
be2fb01f | 115 | return []; |
6a488035 | 116 | } |
be2fb01f | 117 | $mails = []; |
317fceb4 | 118 | $parser = new ezcMailParser(); |
6a488035 TO |
119 | //set property text attachment as file CRM-5408 |
120 | $parser->options->parseTextAttachmentsAsFiles = TRUE; | |
121 | ||
122 | foreach ($set->getMessageNumbers() as $nr) { | |
123 | if ($this->_debug) { | |
124 | print "retrieving message $nr\n"; | |
125 | } | |
126 | $single = $parser->parseMail($this->_transport->fetchByMessageNr($nr)); | |
127 | $mails[$nr] = $single[0]; | |
128 | } | |
129 | return $mails; | |
130 | } | |
131 | ||
132 | /** | |
133 | * Point to (and create if needed) a local Maildir for storing retrieved mail | |
134 | * | |
90c8230e TO |
135 | * @param string $name |
136 | * Name of the Maildir. | |
6a488035 | 137 | * |
77b97be7 | 138 | * @throws Exception |
a6c01b45 CW |
139 | * @return string |
140 | * path to the Maildir's cur directory | |
6a488035 | 141 | */ |
00be9182 | 142 | public function maildir($name) { |
6a488035 TO |
143 | $config = CRM_Core_Config::singleton(); |
144 | $dir = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $name; | |
be2fb01f | 145 | foreach ([ |
7e8c8317 SL |
146 | 'cur', |
147 | 'new', | |
148 | 'tmp', | |
149 | ] as $sub) { | |
6a488035 TO |
150 | if (!file_exists($dir . DIRECTORY_SEPARATOR . $sub)) { |
151 | if ($this->_debug) { | |
152 | print "creating $dir/$sub\n"; | |
153 | } | |
154 | if (!mkdir($dir . DIRECTORY_SEPARATOR . $sub, 0700, TRUE)) { | |
155 | throw new Exception('Could not create ' . $dir . DIRECTORY_SEPARATOR . $sub); | |
156 | } | |
157 | } | |
158 | } | |
159 | return $dir . DIRECTORY_SEPARATOR . 'cur'; | |
160 | } | |
96025800 | 161 | |
6a488035 | 162 | } |