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