Merge pull request #7115 from mollux/CRM-17486
[civicrm-core.git] / CRM / Mailing / MailStore / Maildir.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34require_once 'ezc/Base/src/ezc_bootstrap.php';
35require_once 'ezc/autoload/mail_autoload.php';
28518c90
EM
36
37/**
38 * Class CRM_Mailing_MailStore_Maildir
39 */
6a488035
TO
40class CRM_Mailing_MailStore_Maildir extends CRM_Mailing_MailStore {
41
42 /**
fe482240 43 * Connect to the supplied dir and make sure the two mail dirs exist.
6a488035 44 *
90c8230e
TO
45 * @param string $dir
46 * Dir to operate upon.
6a488035 47 *
dd244018 48 * @return \CRM_Mailing_MailStore_Maildir
6a488035 49 */
00be9182 50 public function __construct($dir) {
6a488035
TO
51 $this->_dir = $dir;
52
353ffa53
TO
53 $this->_ignored = $this->maildir(implode(DIRECTORY_SEPARATOR, array(
54 'CiviMail.ignored',
55 date('Y'),
56 date('m'),
79d7553f 57 date('d'),
353ffa53
TO
58 )));
59 $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, array(
60 'CiviMail.processed',
61 date('Y'),
62 date('m'),
79d7553f 63 date('d'),
353ffa53 64 )));
6a488035
TO
65 }
66
67 /**
fe482240 68 * Return the next X messages from the mail store.
6a488035
TO
69 * FIXME: in CiviCRM 2.2 this always returns all the emails
70 *
90c8230e
TO
71 * @param int $count
72 * Number of messages to fetch FIXME: ignored in CiviCRM 2.2 (assumed to be 0, i.e., fetch all).
6a488035 73 *
a6c01b45
CW
74 * @return array
75 * array of ezcMail objects
6a488035 76 */
00be9182 77 public function fetchNext($count = 0) {
6a488035 78 $mails = array();
79d7553f 79 $parser = new ezcMailParser();
25606795 80 // set property text attachment as file CRM-5408
6a488035
TO
81 $parser->options->parseTextAttachmentsAsFiles = TRUE;
82
83 foreach (array(
353ffa53 84 'cur',
79d7553f 85 'new',
353ffa53 86 ) as $subdir) {
6a488035
TO
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
353ffa53
TO
100 $set = new ezcMailFileSet(array($path));
101 $single = $parser->parseMail($set);
6a488035
TO
102 $mails[$path] = $single[0];
103 }
104 }
105 return $mails;
106 }
107
108 /**
fe482240 109 * Fetch the specified message to the local ignore folder.
6a488035 110 *
90c8230e
TO
111 * @param int $file
112 * File location of the message to fetch.
6a488035 113 *
77b97be7 114 * @throws Exception
6a488035 115 */
00be9182 116 public function markIgnored($file) {
6a488035
TO
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 /**
fe482240 127 * Fetch the specified message to the local processed folder.
6a488035 128 *
90c8230e
TO
129 * @param int $file
130 * File location of the message to fetch.
6a488035 131 *
77b97be7 132 * @throws Exception
6a488035 133 */
00be9182 134 public function markProcessed($file) {
6a488035
TO
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 }
96025800 143
6a488035 144}