Merge pull request #6460 from yashodha/CRM-16943
[civicrm-core.git] / CRM / Mailing / MailStore / Maildir.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_Maildir
41 */
6a488035
TO
42class CRM_Mailing_MailStore_Maildir 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_Maildir
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'),
79d7553f 59 date('d'),
353ffa53
TO
60 )));
61 $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, array(
62 'CiviMail.processed',
63 date('Y'),
64 date('m'),
79d7553f 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 80 $mails = array();
79d7553f 81 $parser = new ezcMailParser();
6a488035
TO
82 //set property text attachment as file CRM-5408
83 $parser->options->parseTextAttachmentsAsFiles = TRUE;
84
85 foreach (array(
353ffa53 86 'cur',
79d7553f 87 'new',
353ffa53 88 ) as $subdir) {
6a488035
TO
89 $dir = $this->_dir . DIRECTORY_SEPARATOR . $subdir;
90 foreach (scandir($dir) as $file) {
91 if ($file == '.' or $file == '..') {
92 continue;
93 }
94 $path = $dir . DIRECTORY_SEPARATOR . $file;
95
96 if ($this->_debug) {
97
98 print "retrieving message $path\n";
99
100 }
101
353ffa53
TO
102 $set = new ezcMailFileSet(array($path));
103 $single = $parser->parseMail($set);
6a488035
TO
104 $mails[$path] = $single[0];
105 }
106 }
107 return $mails;
108 }
109
110 /**
fe482240 111 * Fetch the specified message to the local ignore folder.
6a488035 112 *
90c8230e
TO
113 * @param int $file
114 * File location of the message to fetch.
6a488035 115 *
77b97be7 116 * @throws Exception
6a488035
TO
117 * @return void
118 */
00be9182 119 public function markIgnored($file) {
6a488035
TO
120 if ($this->_debug) {
121 print "moving $file to ignored folder\n";
122 }
123 $target = $this->_ignored . DIRECTORY_SEPARATOR . basename($file);
124 if (!rename($file, $target)) {
125 throw new Exception("Could not rename $file to $target");
126 }
127 }
128
129 /**
fe482240 130 * Fetch the specified message to the local processed folder.
6a488035 131 *
90c8230e
TO
132 * @param int $file
133 * File location of the message to fetch.
6a488035 134 *
77b97be7 135 * @throws Exception
6a488035
TO
136 * @return void
137 */
00be9182 138 public function markProcessed($file) {
6a488035
TO
139 if ($this->_debug) {
140 print "moving $file to processed folder\n";
141 }
142 $target = $this->_processed . DIRECTORY_SEPARATOR . basename($file);
143 if (!rename($file, $target)) {
144 throw new Exception("Could not rename $file to $target");
145 }
146 }
96025800 147
6a488035 148}