Merge pull request #15759 from tunbola/active-option-values-for-custom-group
[civicrm-core.git] / CRM / Mailing / MailStore.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Mailing_MailStore {
18 /**
19 * flag to decide whether to print debug messages
20 * @var bool
21 */
22 public $_debug = FALSE;
23
24 /**
25 * Return the proper mail store implementation, based on config settings.
26 *
27 * @param string $name
28 * Name of the settings set from civimail_mail_settings to use (null for default).
29 *
30 * @throws Exception
31 * @return object
32 * mail store implementation for processing CiviMail-bound emails
33 */
34 public static function getStore($name = NULL) {
35 $dao = new CRM_Core_DAO_MailSettings();
36 $dao->domain_id = CRM_Core_Config::domainID();
37 $name ? $dao->name = $name : $dao->is_default = 1;
38 if (!$dao->find(TRUE)) {
39 throw new Exception("Could not find entry named $name in civicrm_mail_settings");
40 }
41
42 $protocols = CRM_Core_PseudoConstant::get('CRM_Core_DAO_MailSettings', 'protocol');
43 if (empty($protocols[$dao->protocol])) {
44 throw new Exception("Empty mail protocol");
45 }
46
47 switch ($protocols[$dao->protocol]) {
48 case 'IMAP':
49 return new CRM_Mailing_MailStore_Imap($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl, $dao->source);
50
51 case 'POP3':
52 return new CRM_Mailing_MailStore_Pop3($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl);
53
54 case 'Maildir':
55 return new CRM_Mailing_MailStore_Maildir($dao->source);
56
57 case 'Localdir':
58 return new CRM_Mailing_MailStore_Localdir($dao->source);
59
60 // DO NOT USE the mbox transport for anything other than testing
61 // in particular, it does not clear the mbox afterwards
62
63 case 'mbox':
64 return new CRM_Mailing_MailStore_Mbox($dao->source);
65
66 default:
67 throw new Exception("Unknown protocol {$dao->protocol}");
68 }
69 }
70
71 /**
72 * Return all emails in the mail store.
73 *
74 * @return array
75 * array of ezcMail objects
76 */
77 public function allMails() {
78 return $this->fetchNext(0);
79 }
80
81 /**
82 * Expunge the messages marked for deletion; stub function to be redefined by IMAP store.
83 */
84 public function expunge() {
85 }
86
87 /**
88 * Return the next X messages from the mail store.
89 *
90 * @param int $count
91 * Number of messages to fetch (0 to fetch all).
92 *
93 * @return array
94 * array of ezcMail objects
95 */
96 public function fetchNext($count = 1) {
97 $offset = 1;
98 if (isset($this->_transport->options->uidReferencing) and $this->_transport->options->uidReferencing) {
99 $offset = $this->_transport->listUniqueIdentifiers();
100 $offset = array_shift($offset);
101 }
102 try {
103 $set = $this->_transport->fetchFromOffset($offset, $count);
104 if ($this->_debug) {
105 print "fetching $count messages\n";
106 }
107 }
108 catch (ezcMailOffsetOutOfRangeException$e) {
109 if ($this->_debug) {
110 print "got to the end of the mailbox\n";
111 }
112 return [];
113 }
114 $mails = [];
115 $parser = new ezcMailParser();
116 //set property text attachment as file CRM-5408
117 $parser->options->parseTextAttachmentsAsFiles = TRUE;
118
119 foreach ($set->getMessageNumbers() as $nr) {
120 if ($this->_debug) {
121 print "retrieving message $nr\n";
122 }
123 $single = $parser->parseMail($this->_transport->fetchByMessageNr($nr));
124 $mails[$nr] = $single[0];
125 }
126 return $mails;
127 }
128
129 /**
130 * Point to (and create if needed) a local Maildir for storing retrieved mail
131 *
132 * @param string $name
133 * Name of the Maildir.
134 *
135 * @throws Exception
136 * @return string
137 * path to the Maildir's cur directory
138 */
139 public function maildir($name) {
140 $config = CRM_Core_Config::singleton();
141 $dir = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $name;
142 foreach ([
143 'cur',
144 'new',
145 'tmp',
146 ] 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 }