(NFC) Update CRM/Friend CRM/Grant CRM/Group CRM/Mailing to be up to date to a future...
[civicrm-core.git] / CRM / Mailing / MailStore / Localdir.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
28518c90
EM
34/**
35 * Class CRM_Mailing_MailStore_Localdir
36 */
6a488035
TO
37class CRM_Mailing_MailStore_Localdir extends CRM_Mailing_MailStore {
38
39 /**
fe482240 40 * Connect to the supplied dir and make sure the two mail dirs exist.
6a488035 41 *
90c8230e
TO
42 * @param string $dir
43 * Dir to operate upon.
6a488035 44 *
dd244018 45 * @return \CRM_Mailing_MailStore_Localdir
6a488035 46 */
00be9182 47 public function __construct($dir) {
6a488035
TO
48 $this->_dir = $dir;
49
be2fb01f 50 $this->_ignored = $this->maildir(implode(DIRECTORY_SEPARATOR, [
7e8c8317
SL
51 'CiviMail.ignored',
52 date('Y'),
53 date('m'),
54 date('d'),
55 ]));
be2fb01f 56 $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, [
7e8c8317
SL
57 'CiviMail.processed',
58 date('Y'),
59 date('m'),
60 date('d'),
61 ]));
6a488035
TO
62 }
63
64 /**
fe482240 65 * Return the next X messages from the mail store.
6a488035
TO
66 * FIXME: in CiviCRM 2.2 this always returns all the emails
67 *
90c8230e
TO
68 * @param int $count
69 * Number of messages to fetch FIXME: ignored in CiviCRM 2.2 (assumed to be 0, i.e., fetch all).
6a488035 70 *
a6c01b45
CW
71 * @return array
72 * array of ezcMail objects
6a488035 73 */
00be9182 74 public function fetchNext($count = 0) {
be2fb01f 75 $mails = [];
6a488035
TO
76 $path = rtrim($this->_dir, DIRECTORY_SEPARATOR);
77
78 if ($this->_debug) {
79
80 print "fetching $count messages\n";
81
82 }
83
84 $directory = new DirectoryIterator($path);
85 foreach ($directory as $entry) {
86 if ($entry->isDot()) {
87 continue;
88 }
35f7561f
TO
89 if (count($mails) >= $count) {
90 break;
91 }
6a488035
TO
92
93 $file = $path . DIRECTORY_SEPARATOR . $entry->getFilename();
94 if ($this->_debug) {
95 print "retrieving message $file\n";
96 }
97
be2fb01f 98 $set = new ezcMailFileSet([$file]);
317fceb4 99 $parser = new ezcMailParser();
25606795 100 // set property text attachment as file CRM-5408
6a488035
TO
101 $parser->options->parseTextAttachmentsAsFiles = TRUE;
102
103 $mail = $parser->parseMail($set);
104
105 if (!$mail) {
106 return CRM_Core_Error::createAPIError(ts('%1 could not be parsed',
be2fb01f 107 [1 => $file]
353ffa53 108 ));
6a488035
TO
109 }
110 $mails[$file] = $mail[0];
111 }
112
113 if ($this->_debug && (count($mails) <= 0)) {
114
115 print "No messages found\n";
116
117 }
118
119 return $mails;
120 }
121
122 /**
fe482240 123 * Fetch the specified message to the local ignore folder.
6a488035 124 *
90c8230e
TO
125 * @param int $file
126 * File location of the message to fetch.
6a488035 127 *
77b97be7 128 * @throws Exception
6a488035 129 */
00be9182 130 public function markIgnored($file) {
6a488035
TO
131 if ($this->_debug) {
132 print "moving $file to ignored folder\n";
133 }
134 $target = $this->_ignored . DIRECTORY_SEPARATOR . basename($file);
135 if (!rename($file, $target)) {
136 throw new Exception("Could not rename $file to $target");
137 }
138 }
139
140 /**
fe482240 141 * Fetch the specified message to the local processed folder.
6a488035 142 *
90c8230e
TO
143 * @param int $file
144 * File location of the message to fetch.
6a488035 145 *
77b97be7 146 * @throws Exception
6a488035 147 */
00be9182 148 public function markProcessed($file) {
6a488035
TO
149 if ($this->_debug) {
150 print "moving $file to processed folder\n";
151 }
152 $target = $this->_processed . DIRECTORY_SEPARATOR . basename($file);
153 if (!rename($file, $target)) {
154 throw new Exception("Could not rename $file to $target");
155 }
156 }
96025800 157
6a488035 158}