Merge pull request #4107 from davecivicrm/CRM-15258
[civicrm-core.git] / CRM / Mailing / MailStore.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 */
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 *
77b97be7 42 * @param string $name name of the settings set from civimail_mail_settings to use (null for default)
6a488035 43 *
77b97be7 44 * @throws Exception
6a488035
TO
45 * @return object mail store implementation for processing CiviMail-bound emails
46 */
8785b897 47 public static function getStore($name = NULL) {
6a488035
TO
48 $dao = new CRM_Core_DAO_MailSettings;
49 $dao->domain_id = CRM_Core_Config::domainID();
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 /**
85 * Return all emails in the mail store
86 *
87 * @return array array of ezcMail objects
88 */
89 function allMails() {
90 return $this->fetchNext(0);
91 }
92
93 /**
94 * Expunge the messages marked for deletion; stub function to be redefined by IMAP store
95 */
96 function expunge() {}
97
98 /**
99 * Return the next X messages from the mail store
100 *
101 * @param int $count number of messages to fetch (0 to fetch all)
102 *
103 * @return array array of ezcMail objects
104 */
105 function fetchNext($count = 1) {
106 if (isset($this->_transport->options->uidReferencing) and $this->_transport->options->uidReferencing) {
107 $offset = array_shift($this->_transport->listUniqueIdentifiers());
108 }
109 else {
110 $offset = 1;
111 }
112 try {
113 $set = $this->_transport->fetchFromOffset($offset, $count);
114 if ($this->_debug) {
115 print "fetching $count messages\n";
116 }
117 }
118 catch(ezcMailOffsetOutOfRangeException$e) {
119 if ($this->_debug) {
120 print "got to the end of the mailbox\n";
121 }
122 return array();
123 }
124 $mails = array();
125 $parser = new ezcMailParser;
126 //set property text attachment as file CRM-5408
127 $parser->options->parseTextAttachmentsAsFiles = TRUE;
128
129 foreach ($set->getMessageNumbers() as $nr) {
130 if ($this->_debug) {
131 print "retrieving message $nr\n";
132 }
133 $single = $parser->parseMail($this->_transport->fetchByMessageNr($nr));
134 $mails[$nr] = $single[0];
135 }
136 return $mails;
137 }
138
139 /**
140 * Point to (and create if needed) a local Maildir for storing retrieved mail
141 *
77b97be7 142 * @param string $name name of the Maildir
6a488035 143 *
77b97be7 144 * @throws Exception
6a488035
TO
145 * @return string path to the Maildir's cur directory
146 */
147 function maildir($name) {
148 $config = CRM_Core_Config::singleton();
149 $dir = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $name;
150 foreach (array(
151 'cur', 'new', 'tmp') as $sub) {
152 if (!file_exists($dir . DIRECTORY_SEPARATOR . $sub)) {
153 if ($this->_debug) {
154 print "creating $dir/$sub\n";
155 }
156 if (!mkdir($dir . DIRECTORY_SEPARATOR . $sub, 0700, TRUE)) {
157 throw new Exception('Could not create ' . $dir . DIRECTORY_SEPARATOR . $sub);
158 }
159 }
160 }
161 return $dir . DIRECTORY_SEPARATOR . 'cur';
162 }
163}
164