Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-11-23-22-46-27
[civicrm-core.git] / CRM / Mailing / MailStore / Maildir.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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 */
33
34 require_once 'ezc/Base/src/ezc_bootstrap.php';
35 require_once 'ezc/autoload/mail_autoload.php';
36
37 /**
38 * Class CRM_Mailing_MailStore_Maildir
39 */
40 class CRM_Mailing_MailStore_Maildir extends CRM_Mailing_MailStore {
41
42 /**
43 * Connect to the supplied dir and make sure the two mail dirs exist.
44 *
45 * @param string $dir
46 * Dir to operate upon.
47 *
48 * @return \CRM_Mailing_MailStore_Maildir
49 */
50 public function __construct($dir) {
51 $this->_dir = $dir;
52
53 $this->_ignored = $this->maildir(implode(DIRECTORY_SEPARATOR, array(
54 'CiviMail.ignored',
55 date('Y'),
56 date('m'),
57 date('d'),
58 )));
59 $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, array(
60 'CiviMail.processed',
61 date('Y'),
62 date('m'),
63 date('d'),
64 )));
65 }
66
67 /**
68 * Return the next X messages from the mail store.
69 * FIXME: in CiviCRM 2.2 this always returns all the emails
70 *
71 * @param int $count
72 * Number of messages to fetch FIXME: ignored in CiviCRM 2.2 (assumed to be 0, i.e., fetch all).
73 *
74 * @return array
75 * array of ezcMail objects
76 */
77 public function fetchNext($count = 0) {
78 $mails = array();
79 $parser = new ezcMailParser();
80 // set property text attachment as file CRM-5408
81 $parser->options->parseTextAttachmentsAsFiles = TRUE;
82
83 foreach (array(
84 'cur',
85 'new',
86 ) as $subdir) {
87 $dir = $this->_dir . DIRECTORY_SEPARATOR . $subdir;
88 foreach (scandir($dir) as $file) {
89 if ($file == '.' or $file == '..') {
90 continue;
91 }
92 $path = $dir . DIRECTORY_SEPARATOR . $file;
93
94 if ($this->_debug) {
95
96 print "retrieving message $path\n";
97
98 }
99
100 $set = new ezcMailFileSet(array($path));
101 $single = $parser->parseMail($set);
102 $mails[$path] = $single[0];
103 }
104 }
105 return $mails;
106 }
107
108 /**
109 * Fetch the specified message to the local ignore folder.
110 *
111 * @param int $file
112 * File location of the message to fetch.
113 *
114 * @throws Exception
115 */
116 public 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 int $file
130 * File location of the message to fetch.
131 *
132 * @throws Exception
133 */
134 public function markProcessed($file) {
135 if ($this->_debug) {
136 print "moving $file to processed folder\n";
137 }
138 $target = $this->_processed . DIRECTORY_SEPARATOR . basename($file);
139 if (!rename($file, $target)) {
140 throw new Exception("Could not rename $file to $target");
141 }
142 }
143
144 }