Merge pull request #4983 from colemanw/CRM-15842
[civicrm-core.git] / CRM / Mailing / MailStore.php
CommitLineData
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class 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
a6c01b45
CW
46 * @return object
47 * mail store implementation for processing CiviMail-bound emails
6a488035 48 */
8785b897 49 public static function getStore($name = NULL) {
317fceb4 50 $dao = new CRM_Core_DAO_MailSettings();
353ffa53 51 $dao->domain_id = CRM_Core_Config::domainID();
6a488035
TO
52 $name ? $dao->name = $name : $dao->is_default = 1;
53 if (!$dao->find(TRUE)) {
54 throw new Exception("Could not find entry named $name in civicrm_mail_settings");
55 }
56
cbf48754 57 $protocols = CRM_Core_PseudoConstant::get('CRM_Core_DAO_MailSettings', 'protocol');
d25c89a1
JP
58 if (empty($protocols[$dao->protocol])) {
59 throw new Exception("Empty mail protocol");
60 }
6a488035
TO
61
62 switch ($protocols[$dao->protocol]) {
63 case 'IMAP':
64 return new CRM_Mailing_MailStore_Imap($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl, $dao->source);
65
66 case 'POP3':
67 return new CRM_Mailing_MailStore_Pop3($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl);
68
69 case 'Maildir':
70 return new CRM_Mailing_MailStore_Maildir($dao->source);
71
72 case 'Localdir':
73 return new CRM_Mailing_MailStore_Localdir($dao->source);
74
75 // DO NOT USE the mbox transport for anything other than testing
76 // in particular, it does not clear the mbox afterwards
77
78 case 'mbox':
79 return new CRM_Mailing_MailStore_Mbox($dao->source);
80
81 default:
82 throw new Exception("Unknown protocol {$dao->protocol}");
83 }
84 }
85
86 /**
87 * Return all emails in the mail store
88 *
a6c01b45
CW
89 * @return array
90 * array of ezcMail objects
6a488035 91 */
00be9182 92 public function allMails() {
6a488035
TO
93 return $this->fetchNext(0);
94 }
95
96 /**
97 * Expunge the messages marked for deletion; stub function to be redefined by IMAP store
98 */
35f7561f
TO
99 public function expunge() {
100 }
6a488035
TO
101
102 /**
103 * Return the next X messages from the mail store
104 *
90c8230e
TO
105 * @param int $count
106 * Number of messages to fetch (0 to fetch all).
6a488035 107 *
a6c01b45
CW
108 * @return array
109 * array of ezcMail objects
6a488035 110 */
00be9182 111 public function fetchNext($count = 1) {
6a488035
TO
112 if (isset($this->_transport->options->uidReferencing) and $this->_transport->options->uidReferencing) {
113 $offset = array_shift($this->_transport->listUniqueIdentifiers());
114 }
115 else {
116 $offset = 1;
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 }
128 return array();
129 }
130 $mails = array();
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;
158 foreach (array(
353ffa53
TO
159 'cur',
160 'new',
317fceb4 161 'tmp',
353ffa53 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}