Improve protected field metadata
[civicrm-core.git] / CRM / Mailing / MailStore / Maildir.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
35 * Class CRM_Mailing_MailStore_Maildir
36 */
37 class CRM_Mailing_MailStore_Maildir extends CRM_Mailing_MailStore {
38
39 /**
40 * Connect to the supplied dir and make sure the two mail dirs exist.
41 *
42 * @param string $dir
43 * Dir to operate upon.
44 *
45 * @return \CRM_Mailing_MailStore_Maildir
46 */
47 public function __construct($dir) {
48 $this->_dir = $dir;
49
50 $this->_ignored = $this->maildir(implode(DIRECTORY_SEPARATOR, [
51 'CiviMail.ignored',
52 date('Y'),
53 date('m'),
54 date('d'),
55 ]));
56 $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, [
57 'CiviMail.processed',
58 date('Y'),
59 date('m'),
60 date('d'),
61 ]));
62 }
63
64 /**
65 * Return the next X messages from the mail store.
66 * FIXME: in CiviCRM 2.2 this always returns all the emails
67 *
68 * @param int $count
69 * Number of messages to fetch FIXME: ignored in CiviCRM 2.2 (assumed to be 0, i.e., fetch all).
70 *
71 * @return array
72 * array of ezcMail objects
73 */
74 public function fetchNext($count = 0) {
75 $mails = [];
76 $parser = new ezcMailParser();
77 // set property text attachment as file CRM-5408
78 $parser->options->parseTextAttachmentsAsFiles = TRUE;
79
80 foreach ([
81 'cur',
82 'new',
83 ] as $subdir) {
84 $dir = $this->_dir . DIRECTORY_SEPARATOR . $subdir;
85 foreach (scandir($dir) as $file) {
86 if ($file == '.' or $file == '..') {
87 continue;
88 }
89 $path = $dir . DIRECTORY_SEPARATOR . $file;
90
91 if ($this->_debug) {
92
93 print "retrieving message $path\n";
94
95 }
96
97 $set = new ezcMailFileSet([$path]);
98 $single = $parser->parseMail($set);
99 $mails[$path] = $single[0];
100 }
101 }
102 return $mails;
103 }
104
105 /**
106 * Fetch the specified message to the local ignore folder.
107 *
108 * @param int $file
109 * File location of the message to fetch.
110 *
111 * @throws Exception
112 */
113 public function markIgnored($file) {
114 if ($this->_debug) {
115 print "moving $file to ignored folder\n";
116 }
117 $target = $this->_ignored . DIRECTORY_SEPARATOR . basename($file);
118 if (!rename($file, $target)) {
119 throw new Exception("Could not rename $file to $target");
120 }
121 }
122
123 /**
124 * Fetch the specified message to the local processed folder.
125 *
126 * @param int $file
127 * File location of the message to fetch.
128 *
129 * @throws Exception
130 */
131 public function markProcessed($file) {
132 if ($this->_debug) {
133 print "moving $file to processed folder\n";
134 }
135 $target = $this->_processed . DIRECTORY_SEPARATOR . basename($file);
136 if (!rename($file, $target)) {
137 throw new Exception("Could not rename $file to $target");
138 }
139 }
140
141 }