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