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