Merge pull request #4893 from colemanw/INFRA-132
[civicrm-core.git] / CRM / Mailing / MailStore / Localdir.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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('CiviMail.ignored', date('Y'), date('m'), date('d'))));
56 $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, array('CiviMail.processed', date('Y'), date('m'), date('d'))));
57 }
58
59 /**
60 * Return the next X messages from the mail store
61 * FIXME: in CiviCRM 2.2 this always returns all the emails
62 *
63 * @param int $count
64 * Number of messages to fetch FIXME: ignored in CiviCRM 2.2 (assumed to be 0, i.e., fetch all).
65 *
66 * @return array
67 * array of ezcMail objects
68 */
69 public function fetchNext($count = 0) {
70 $mails = array();
71 $path = rtrim($this->_dir, DIRECTORY_SEPARATOR);
72
73 if ($this->_debug) {
74
75 print "fetching $count messages\n";
76
77 }
78
79 $directory = new DirectoryIterator($path);
80 foreach ($directory as $entry) {
81 if ($entry->isDot()) {
82 continue;
83 }
84 if (count($mails) >= $count) {
85 break;
86 }
87
88 $file = $path . DIRECTORY_SEPARATOR . $entry->getFilename();
89 if ($this->_debug) {
90 print "retrieving message $file\n";
91 }
92
93 $set = new ezcMailFileSet(array($file));
94 $parser = new ezcMailParser;
95 //set property text attachment as file CRM-5408
96 $parser->options->parseTextAttachmentsAsFiles = TRUE;
97
98 $mail = $parser->parseMail($set);
99
100 if (!$mail) {
101 return CRM_Core_Error::createAPIError(ts('%1 could not be parsed',
102 array(1 => $file)
103 ));
104 }
105 $mails[$file] = $mail[0];
106 }
107
108 if ($this->_debug && (count($mails) <= 0)) {
109
110 print "No messages found\n";
111
112 }
113
114 return $mails;
115 }
116
117 /**
118 * Fetch the specified message to the local ignore folder
119 *
120 * @param int $file
121 * File location of the message to fetch.
122 *
123 * @throws Exception
124 * @return void
125 */
126 public function markIgnored($file) {
127 if ($this->_debug) {
128 print "moving $file to ignored folder\n";
129 }
130 $target = $this->_ignored . DIRECTORY_SEPARATOR . basename($file);
131 if (!rename($file, $target)) {
132 throw new Exception("Could not rename $file to $target");
133 }
134 }
135
136 /**
137 * Fetch the specified message to the local processed folder
138 *
139 * @param int $file
140 * File location of the message to fetch.
141 *
142 * @throws Exception
143 * @return void
144 */
145 public function markProcessed($file) {
146 if ($this->_debug) {
147 print "moving $file to processed folder\n";
148 }
149 $target = $this->_processed . DIRECTORY_SEPARATOR . basename($file);
150 if (!rename($file, $target)) {
151 throw new Exception("Could not rename $file to $target");
152 }
153 }
154 }