Merge pull request #19766 from WeMoveEU/faster-select2-groups
[civicrm-core.git] / CRM / Mailing / MailStore / Localdir.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_Localdir
20 */
21 class CRM_Mailing_MailStore_Localdir 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_Localdir
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 $path = rtrim($this->_dir, DIRECTORY_SEPARATOR);
61
62 if ($this->_debug) {
63
64 print "fetching $count messages\n";
65
66 }
67
68 $directory = new DirectoryIterator($path);
69 foreach ($directory as $entry) {
70 if ($entry->isDot()) {
71 continue;
72 }
73 if (count($mails) >= $count) {
74 break;
75 }
76
77 $file = $path . DIRECTORY_SEPARATOR . $entry->getFilename();
78 if ($this->_debug) {
79 print "retrieving message $file\n";
80 }
81
82 $set = new ezcMailFileSet([$file]);
83 $parser = new ezcMailParser();
84 // set property text attachment as file CRM-5408
85 $parser->options->parseTextAttachmentsAsFiles = TRUE;
86
87 $mail = $parser->parseMail($set);
88
89 if (!$mail) {
90 return CRM_Core_Error::createAPIError(ts('%1 could not be parsed',
91 [1 => $file]
92 ));
93 }
94 $mails[$file] = $mail[0];
95 }
96
97 if ($this->_debug && (count($mails) <= 0)) {
98
99 print "No messages found\n";
100
101 }
102
103 return $mails;
104 }
105
106 /**
107 * Fetch the specified message to the local ignore folder.
108 *
109 * @param int $file
110 * File location of the message to fetch.
111 *
112 * @throws Exception
113 */
114 public function markIgnored($file) {
115 if ($this->_debug) {
116 print "moving $file to ignored folder\n";
117 }
118 $target = $this->_ignored . DIRECTORY_SEPARATOR . basename($file);
119 if (!rename($file, $target)) {
120 throw new Exception("Could not rename $file to $target");
121 }
122 }
123
124 /**
125 * Fetch the specified message to the local processed folder.
126 *
127 * @param int $file
128 * File location of the message to fetch.
129 *
130 * @throws Exception
131 */
132 public function markProcessed($file) {
133 if ($this->_debug) {
134 print "moving $file to processed folder\n";
135 }
136 $target = $this->_processed . DIRECTORY_SEPARATOR . basename($file);
137 if (!rename($file, $target)) {
138 throw new Exception("Could not rename $file to $target");
139 }
140 }
141
142 }