Merge pull request #6460 from yashodha/CRM-16943
[civicrm-core.git] / CRM / Mailing / MailStore / Localdir.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 * $Id$
33 *
34 */
35
36require_once 'ezc/Base/src/ezc_bootstrap.php';
37require_once 'ezc/autoload/mail_autoload.php';
28518c90
EM
38
39/**
40 * Class CRM_Mailing_MailStore_Localdir
41 */
6a488035
TO
42class CRM_Mailing_MailStore_Localdir extends CRM_Mailing_MailStore {
43
44 /**
fe482240 45 * Connect to the supplied dir and make sure the two mail dirs exist.
6a488035 46 *
90c8230e
TO
47 * @param string $dir
48 * Dir to operate upon.
6a488035 49 *
dd244018 50 * @return \CRM_Mailing_MailStore_Localdir
6a488035 51 */
00be9182 52 public function __construct($dir) {
6a488035
TO
53 $this->_dir = $dir;
54
353ffa53
TO
55 $this->_ignored = $this->maildir(implode(DIRECTORY_SEPARATOR, array(
56 'CiviMail.ignored',
57 date('Y'),
58 date('m'),
317fceb4 59 date('d'),
353ffa53
TO
60 )));
61 $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, array(
62 'CiviMail.processed',
63 date('Y'),
64 date('m'),
317fceb4 65 date('d'),
353ffa53 66 )));
6a488035
TO
67 }
68
69 /**
fe482240 70 * Return the next X messages from the mail store.
6a488035
TO
71 * FIXME: in CiviCRM 2.2 this always returns all the emails
72 *
90c8230e
TO
73 * @param int $count
74 * Number of messages to fetch FIXME: ignored in CiviCRM 2.2 (assumed to be 0, i.e., fetch all).
6a488035 75 *
a6c01b45
CW
76 * @return array
77 * array of ezcMail objects
6a488035 78 */
00be9182 79 public function fetchNext($count = 0) {
6a488035
TO
80 $mails = array();
81 $path = rtrim($this->_dir, DIRECTORY_SEPARATOR);
82
83 if ($this->_debug) {
84
85 print "fetching $count messages\n";
86
87 }
88
89 $directory = new DirectoryIterator($path);
90 foreach ($directory as $entry) {
91 if ($entry->isDot()) {
92 continue;
93 }
35f7561f
TO
94 if (count($mails) >= $count) {
95 break;
96 }
6a488035
TO
97
98 $file = $path . DIRECTORY_SEPARATOR . $entry->getFilename();
99 if ($this->_debug) {
100 print "retrieving message $file\n";
101 }
102
103 $set = new ezcMailFileSet(array($file));
317fceb4 104 $parser = new ezcMailParser();
6a488035
TO
105 //set property text attachment as file CRM-5408
106 $parser->options->parseTextAttachmentsAsFiles = TRUE;
107
108 $mail = $parser->parseMail($set);
109
110 if (!$mail) {
111 return CRM_Core_Error::createAPIError(ts('%1 could not be parsed',
353ffa53
TO
112 array(1 => $file)
113 ));
6a488035
TO
114 }
115 $mails[$file] = $mail[0];
116 }
117
118 if ($this->_debug && (count($mails) <= 0)) {
119
120 print "No messages found\n";
121
122 }
123
124 return $mails;
125 }
126
127 /**
fe482240 128 * Fetch the specified message to the local ignore folder.
6a488035 129 *
90c8230e
TO
130 * @param int $file
131 * File location of the message to fetch.
6a488035 132 *
77b97be7 133 * @throws Exception
6a488035
TO
134 * @return void
135 */
00be9182 136 public function markIgnored($file) {
6a488035
TO
137 if ($this->_debug) {
138 print "moving $file to ignored folder\n";
139 }
140 $target = $this->_ignored . DIRECTORY_SEPARATOR . basename($file);
141 if (!rename($file, $target)) {
142 throw new Exception("Could not rename $file to $target");
143 }
144 }
145
146 /**
fe482240 147 * Fetch the specified message to the local processed folder.
6a488035 148 *
90c8230e
TO
149 * @param int $file
150 * File location of the message to fetch.
6a488035 151 *
77b97be7 152 * @throws Exception
6a488035
TO
153 * @return void
154 */
00be9182 155 public function markProcessed($file) {
6a488035
TO
156 if ($this->_debug) {
157 print "moving $file to processed folder\n";
158 }
159 $target = $this->_processed . DIRECTORY_SEPARATOR . basename($file);
160 if (!rename($file, $target)) {
161 throw new Exception("Could not rename $file to $target");
162 }
163 }
96025800 164
6a488035 165}