Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
28518c90 EM |
18 | /** |
19 | * Class CRM_Mailing_MailStore_Localdir | |
20 | */ | |
6a488035 TO |
21 | class CRM_Mailing_MailStore_Localdir extends CRM_Mailing_MailStore { |
22 | ||
23 | /** | |
fe482240 | 24 | * Connect to the supplied dir and make sure the two mail dirs exist. |
6a488035 | 25 | * |
90c8230e TO |
26 | * @param string $dir |
27 | * Dir to operate upon. | |
6a488035 | 28 | * |
dd244018 | 29 | * @return \CRM_Mailing_MailStore_Localdir |
6a488035 | 30 | */ |
00be9182 | 31 | public function __construct($dir) { |
6a488035 TO |
32 | $this->_dir = $dir; |
33 | ||
be2fb01f | 34 | $this->_ignored = $this->maildir(implode(DIRECTORY_SEPARATOR, [ |
7e8c8317 SL |
35 | 'CiviMail.ignored', |
36 | date('Y'), | |
37 | date('m'), | |
38 | date('d'), | |
39 | ])); | |
be2fb01f | 40 | $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, [ |
7e8c8317 SL |
41 | 'CiviMail.processed', |
42 | date('Y'), | |
43 | date('m'), | |
44 | date('d'), | |
45 | ])); | |
6a488035 TO |
46 | } |
47 | ||
48 | /** | |
fe482240 | 49 | * Return the next X messages from the mail store. |
6a488035 TO |
50 | * FIXME: in CiviCRM 2.2 this always returns all the emails |
51 | * | |
90c8230e TO |
52 | * @param int $count |
53 | * Number of messages to fetch FIXME: ignored in CiviCRM 2.2 (assumed to be 0, i.e., fetch all). | |
6a488035 | 54 | * |
a6c01b45 CW |
55 | * @return array |
56 | * array of ezcMail objects | |
6a488035 | 57 | */ |
00be9182 | 58 | public function fetchNext($count = 0) { |
be2fb01f | 59 | $mails = []; |
6a488035 TO |
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 | } | |
35f7561f TO |
73 | if (count($mails) >= $count) { |
74 | break; | |
75 | } | |
6a488035 TO |
76 | |
77 | $file = $path . DIRECTORY_SEPARATOR . $entry->getFilename(); | |
78 | if ($this->_debug) { | |
79 | print "retrieving message $file\n"; | |
80 | } | |
81 | ||
be2fb01f | 82 | $set = new ezcMailFileSet([$file]); |
317fceb4 | 83 | $parser = new ezcMailParser(); |
25606795 | 84 | // set property text attachment as file CRM-5408 |
6a488035 TO |
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', | |
be2fb01f | 91 | [1 => $file] |
353ffa53 | 92 | )); |
6a488035 TO |
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 | /** | |
fe482240 | 107 | * Fetch the specified message to the local ignore folder. |
6a488035 | 108 | * |
90c8230e TO |
109 | * @param int $file |
110 | * File location of the message to fetch. | |
6a488035 | 111 | * |
77b97be7 | 112 | * @throws Exception |
6a488035 | 113 | */ |
00be9182 | 114 | public function markIgnored($file) { |
6a488035 TO |
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 | /** | |
fe482240 | 125 | * Fetch the specified message to the local processed folder. |
6a488035 | 126 | * |
90c8230e TO |
127 | * @param int $file |
128 | * File location of the message to fetch. | |
6a488035 | 129 | * |
77b97be7 | 130 | * @throws Exception |
6a488035 | 131 | */ |
00be9182 | 132 | public function markProcessed($file) { |
6a488035 TO |
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 | } | |
96025800 | 141 | |
6a488035 | 142 | } |