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