Merge pull request #4696 from colemanw/CRM-15669
[civicrm-core.git] / CRM / Mailing / MailStore / Maildir.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_Maildir
41 */
42 class CRM_Mailing_MailStore_Maildir 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 dir to operate upon
48 *
49 * @return \CRM_Mailing_MailStore_Maildir
50 */
51 public function __construct($dir) {
52 $this->_dir = $dir;
53
54 $this->_ignored = $this->maildir(implode(DIRECTORY_SEPARATOR, array('CiviMail.ignored', date('Y'), date('m'), date('d'))));
55 $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, array('CiviMail.processed', date('Y'), date('m'), date('d'))));
56 }
57
58 /**
59 * Return the next X messages from the mail store
60 * FIXME: in CiviCRM 2.2 this always returns all the emails
61 *
62 * @param int $count number of messages to fetch FIXME: ignored in CiviCRM 2.2 (assumed to be 0, i.e., fetch all)
63 *
64 * @return array array of ezcMail objects
65 */
66 public function fetchNext($count = 0) {
67 $mails = array();
68 $parser = new ezcMailParser;
69 //set property text attachment as file CRM-5408
70 $parser->options->parseTextAttachmentsAsFiles = TRUE;
71
72 foreach (array(
73 'cur', 'new') as $subdir) {
74 $dir = $this->_dir . DIRECTORY_SEPARATOR . $subdir;
75 foreach (scandir($dir) as $file) {
76 if ($file == '.' or $file == '..') {
77 continue;
78 }
79 $path = $dir . DIRECTORY_SEPARATOR . $file;
80
81 if ($this->_debug) {
82
83 print "retrieving message $path\n";
84
85 }
86
87 $set = new ezcMailFileSet(array($path));
88 $single = $parser->parseMail($set);
89 $mails[$path] = $single[0];
90 }
91 }
92 return $mails;
93 }
94
95 /**
96 * Fetch the specified message to the local ignore folder
97 *
98 * @param integer $file file location of the message to fetch
99 *
100 * @throws Exception
101 * @return void
102 */
103 public function markIgnored($file) {
104 if ($this->_debug) {
105 print "moving $file to ignored folder\n";
106 }
107 $target = $this->_ignored . DIRECTORY_SEPARATOR . basename($file);
108 if (!rename($file, $target)) {
109 throw new Exception("Could not rename $file to $target");
110 }
111 }
112
113 /**
114 * Fetch the specified message to the local processed folder
115 *
116 * @param integer $file file location of the message to fetch
117 *
118 * @throws Exception
119 * @return void
120 */
121 public function markProcessed($file) {
122 if ($this->_debug) {
123 print "moving $file to processed folder\n";
124 }
125 $target = $this->_processed . DIRECTORY_SEPARATOR . basename($file);
126 if (!rename($file, $target)) {
127 throw new Exception("Could not rename $file to $target");
128 }
129 }
130 }