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