Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | class CRM_Mailing_MailStore { | |
7e8c8317 SL |
34 | /** |
35 | * flag to decide whether to print debug messages | |
36 | * @var bool | |
37 | */ | |
38 | public $_debug = FALSE; | |
6a488035 TO |
39 | |
40 | /** | |
25606795 | 41 | * Return the proper mail store implementation, based on config settings. |
6a488035 | 42 | * |
90c8230e TO |
43 | * @param string $name |
44 | * Name of the settings set from civimail_mail_settings to use (null for default). | |
6a488035 | 45 | * |
77b97be7 | 46 | * @throws Exception |
a6c01b45 CW |
47 | * @return object |
48 | * mail store implementation for processing CiviMail-bound emails | |
6a488035 | 49 | */ |
8785b897 | 50 | public static function getStore($name = NULL) { |
317fceb4 | 51 | $dao = new CRM_Core_DAO_MailSettings(); |
353ffa53 | 52 | $dao->domain_id = CRM_Core_Config::domainID(); |
6a488035 TO |
53 | $name ? $dao->name = $name : $dao->is_default = 1; |
54 | if (!$dao->find(TRUE)) { | |
55 | throw new Exception("Could not find entry named $name in civicrm_mail_settings"); | |
56 | } | |
57 | ||
cbf48754 | 58 | $protocols = CRM_Core_PseudoConstant::get('CRM_Core_DAO_MailSettings', 'protocol'); |
d25c89a1 JP |
59 | if (empty($protocols[$dao->protocol])) { |
60 | throw new Exception("Empty mail protocol"); | |
61 | } | |
6a488035 TO |
62 | |
63 | switch ($protocols[$dao->protocol]) { | |
64 | case 'IMAP': | |
65 | return new CRM_Mailing_MailStore_Imap($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl, $dao->source); | |
66 | ||
67 | case 'POP3': | |
68 | return new CRM_Mailing_MailStore_Pop3($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl); | |
69 | ||
70 | case 'Maildir': | |
71 | return new CRM_Mailing_MailStore_Maildir($dao->source); | |
72 | ||
73 | case 'Localdir': | |
74 | return new CRM_Mailing_MailStore_Localdir($dao->source); | |
75 | ||
76 | // DO NOT USE the mbox transport for anything other than testing | |
77 | // in particular, it does not clear the mbox afterwards | |
78 | ||
79 | case 'mbox': | |
80 | return new CRM_Mailing_MailStore_Mbox($dao->source); | |
81 | ||
82 | default: | |
83 | throw new Exception("Unknown protocol {$dao->protocol}"); | |
84 | } | |
85 | } | |
86 | ||
87 | /** | |
fe482240 | 88 | * Return all emails in the mail store. |
6a488035 | 89 | * |
a6c01b45 CW |
90 | * @return array |
91 | * array of ezcMail objects | |
6a488035 | 92 | */ |
00be9182 | 93 | public function allMails() { |
6a488035 TO |
94 | return $this->fetchNext(0); |
95 | } | |
96 | ||
97 | /** | |
25606795 | 98 | * Expunge the messages marked for deletion; stub function to be redefined by IMAP store. |
6a488035 | 99 | */ |
35f7561f TO |
100 | public function expunge() { |
101 | } | |
6a488035 TO |
102 | |
103 | /** | |
fe482240 | 104 | * Return the next X messages from the mail store. |
6a488035 | 105 | * |
90c8230e TO |
106 | * @param int $count |
107 | * Number of messages to fetch (0 to fetch all). | |
6a488035 | 108 | * |
a6c01b45 CW |
109 | * @return array |
110 | * array of ezcMail objects | |
6a488035 | 111 | */ |
00be9182 | 112 | public function fetchNext($count = 1) { |
9372d656 | 113 | $offset = 1; |
6a488035 | 114 | if (isset($this->_transport->options->uidReferencing) and $this->_transport->options->uidReferencing) { |
9372d656 | 115 | $offset = $this->_transport->listUniqueIdentifiers(); |
116 | $offset = array_shift($offset); | |
6a488035 TO |
117 | } |
118 | try { | |
119 | $set = $this->_transport->fetchFromOffset($offset, $count); | |
120 | if ($this->_debug) { | |
121 | print "fetching $count messages\n"; | |
122 | } | |
123 | } | |
353ffa53 | 124 | catch (ezcMailOffsetOutOfRangeException$e) { |
6a488035 TO |
125 | if ($this->_debug) { |
126 | print "got to the end of the mailbox\n"; | |
127 | } | |
be2fb01f | 128 | return []; |
6a488035 | 129 | } |
be2fb01f | 130 | $mails = []; |
317fceb4 | 131 | $parser = new ezcMailParser(); |
6a488035 TO |
132 | //set property text attachment as file CRM-5408 |
133 | $parser->options->parseTextAttachmentsAsFiles = TRUE; | |
134 | ||
135 | foreach ($set->getMessageNumbers() as $nr) { | |
136 | if ($this->_debug) { | |
137 | print "retrieving message $nr\n"; | |
138 | } | |
139 | $single = $parser->parseMail($this->_transport->fetchByMessageNr($nr)); | |
140 | $mails[$nr] = $single[0]; | |
141 | } | |
142 | return $mails; | |
143 | } | |
144 | ||
145 | /** | |
146 | * Point to (and create if needed) a local Maildir for storing retrieved mail | |
147 | * | |
90c8230e TO |
148 | * @param string $name |
149 | * Name of the Maildir. | |
6a488035 | 150 | * |
77b97be7 | 151 | * @throws Exception |
a6c01b45 CW |
152 | * @return string |
153 | * path to the Maildir's cur directory | |
6a488035 | 154 | */ |
00be9182 | 155 | public function maildir($name) { |
6a488035 TO |
156 | $config = CRM_Core_Config::singleton(); |
157 | $dir = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $name; | |
be2fb01f | 158 | foreach ([ |
7e8c8317 SL |
159 | 'cur', |
160 | 'new', | |
161 | 'tmp', | |
162 | ] as $sub) { | |
6a488035 TO |
163 | if (!file_exists($dir . DIRECTORY_SEPARATOR . $sub)) { |
164 | if ($this->_debug) { | |
165 | print "creating $dir/$sub\n"; | |
166 | } | |
167 | if (!mkdir($dir . DIRECTORY_SEPARATOR . $sub, 0700, TRUE)) { | |
168 | throw new Exception('Could not create ' . $dir . DIRECTORY_SEPARATOR . $sub); | |
169 | } | |
170 | } | |
171 | } | |
172 | return $dir . DIRECTORY_SEPARATOR . 'cur'; | |
173 | } | |
96025800 | 174 | |
6a488035 | 175 | } |