Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | class CRM_Mailing_MailStore { | |
34 | // flag to decide whether to print debug messages | |
35 | var $_debug = FALSE; | |
36 | ||
37 | /** | |
25606795 | 38 | * Return the proper mail store implementation, based on config settings. |
6a488035 | 39 | * |
90c8230e TO |
40 | * @param string $name |
41 | * Name of the settings set from civimail_mail_settings to use (null for default). | |
6a488035 | 42 | * |
77b97be7 | 43 | * @throws Exception |
a6c01b45 CW |
44 | * @return object |
45 | * mail store implementation for processing CiviMail-bound emails | |
6a488035 | 46 | */ |
8785b897 | 47 | public static function getStore($name = NULL) { |
317fceb4 | 48 | $dao = new CRM_Core_DAO_MailSettings(); |
353ffa53 | 49 | $dao->domain_id = CRM_Core_Config::domainID(); |
6a488035 TO |
50 | $name ? $dao->name = $name : $dao->is_default = 1; |
51 | if (!$dao->find(TRUE)) { | |
52 | throw new Exception("Could not find entry named $name in civicrm_mail_settings"); | |
53 | } | |
54 | ||
cbf48754 | 55 | $protocols = CRM_Core_PseudoConstant::get('CRM_Core_DAO_MailSettings', 'protocol'); |
d25c89a1 JP |
56 | if (empty($protocols[$dao->protocol])) { |
57 | throw new Exception("Empty mail protocol"); | |
58 | } | |
6a488035 TO |
59 | |
60 | switch ($protocols[$dao->protocol]) { | |
61 | case 'IMAP': | |
62 | return new CRM_Mailing_MailStore_Imap($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl, $dao->source); | |
63 | ||
64 | case 'POP3': | |
65 | return new CRM_Mailing_MailStore_Pop3($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl); | |
66 | ||
67 | case 'Maildir': | |
68 | return new CRM_Mailing_MailStore_Maildir($dao->source); | |
69 | ||
70 | case 'Localdir': | |
71 | return new CRM_Mailing_MailStore_Localdir($dao->source); | |
72 | ||
73 | // DO NOT USE the mbox transport for anything other than testing | |
74 | // in particular, it does not clear the mbox afterwards | |
75 | ||
76 | case 'mbox': | |
77 | return new CRM_Mailing_MailStore_Mbox($dao->source); | |
78 | ||
79 | default: | |
80 | throw new Exception("Unknown protocol {$dao->protocol}"); | |
81 | } | |
82 | } | |
83 | ||
84 | /** | |
fe482240 | 85 | * Return all emails in the mail store. |
6a488035 | 86 | * |
a6c01b45 CW |
87 | * @return array |
88 | * array of ezcMail objects | |
6a488035 | 89 | */ |
00be9182 | 90 | public function allMails() { |
6a488035 TO |
91 | return $this->fetchNext(0); |
92 | } | |
93 | ||
94 | /** | |
25606795 | 95 | * Expunge the messages marked for deletion; stub function to be redefined by IMAP store. |
6a488035 | 96 | */ |
35f7561f TO |
97 | public function expunge() { |
98 | } | |
6a488035 TO |
99 | |
100 | /** | |
fe482240 | 101 | * Return the next X messages from the mail store. |
6a488035 | 102 | * |
90c8230e TO |
103 | * @param int $count |
104 | * Number of messages to fetch (0 to fetch all). | |
6a488035 | 105 | * |
a6c01b45 CW |
106 | * @return array |
107 | * array of ezcMail objects | |
6a488035 | 108 | */ |
00be9182 | 109 | public function fetchNext($count = 1) { |
9372d656 | 110 | $offset = 1; |
6a488035 | 111 | if (isset($this->_transport->options->uidReferencing) and $this->_transport->options->uidReferencing) { |
9372d656 | 112 | $offset = $this->_transport->listUniqueIdentifiers(); |
113 | $offset = array_shift($offset); | |
6a488035 TO |
114 | } |
115 | try { | |
116 | $set = $this->_transport->fetchFromOffset($offset, $count); | |
117 | if ($this->_debug) { | |
118 | print "fetching $count messages\n"; | |
119 | } | |
120 | } | |
353ffa53 | 121 | catch (ezcMailOffsetOutOfRangeException$e) { |
6a488035 TO |
122 | if ($this->_debug) { |
123 | print "got to the end of the mailbox\n"; | |
124 | } | |
125 | return array(); | |
126 | } | |
127 | $mails = array(); | |
317fceb4 | 128 | $parser = new ezcMailParser(); |
6a488035 TO |
129 | //set property text attachment as file CRM-5408 |
130 | $parser->options->parseTextAttachmentsAsFiles = TRUE; | |
131 | ||
132 | foreach ($set->getMessageNumbers() as $nr) { | |
133 | if ($this->_debug) { | |
134 | print "retrieving message $nr\n"; | |
135 | } | |
136 | $single = $parser->parseMail($this->_transport->fetchByMessageNr($nr)); | |
137 | $mails[$nr] = $single[0]; | |
138 | } | |
139 | return $mails; | |
140 | } | |
141 | ||
142 | /** | |
143 | * Point to (and create if needed) a local Maildir for storing retrieved mail | |
144 | * | |
90c8230e TO |
145 | * @param string $name |
146 | * Name of the Maildir. | |
6a488035 | 147 | * |
77b97be7 | 148 | * @throws Exception |
a6c01b45 CW |
149 | * @return string |
150 | * path to the Maildir's cur directory | |
6a488035 | 151 | */ |
00be9182 | 152 | public function maildir($name) { |
6a488035 TO |
153 | $config = CRM_Core_Config::singleton(); |
154 | $dir = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $name; | |
155 | foreach (array( | |
353ffa53 TO |
156 | 'cur', |
157 | 'new', | |
317fceb4 | 158 | 'tmp', |
353ffa53 | 159 | ) as $sub) { |
6a488035 TO |
160 | if (!file_exists($dir . DIRECTORY_SEPARATOR . $sub)) { |
161 | if ($this->_debug) { | |
162 | print "creating $dir/$sub\n"; | |
163 | } | |
164 | if (!mkdir($dir . DIRECTORY_SEPARATOR . $sub, 0700, TRUE)) { | |
165 | throw new Exception('Could not create ' . $dir . DIRECTORY_SEPARATOR . $sub); | |
166 | } | |
167 | } | |
168 | } | |
169 | return $dir . DIRECTORY_SEPARATOR . 'cur'; | |
170 | } | |
96025800 | 171 | |
6a488035 | 172 | } |