Commit | Line | Data |
---|---|---|
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 | ||
34 | require_once 'ezc/Base/src/ezc_bootstrap.php'; | |
35 | require_once 'ezc/autoload/mail_autoload.php'; | |
28518c90 EM |
36 | |
37 | /** | |
38 | * Class CRM_Mailing_MailStore_Localdir | |
39 | */ | |
6a488035 TO |
40 | class CRM_Mailing_MailStore_Localdir 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_Localdir |
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'), | |
317fceb4 | 57 | date('d'), |
353ffa53 TO |
58 | ))); |
59 | $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, array( | |
60 | 'CiviMail.processed', | |
61 | date('Y'), | |
62 | date('m'), | |
317fceb4 | 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 TO |
78 | $mails = array(); |
79 | $path = rtrim($this->_dir, DIRECTORY_SEPARATOR); | |
80 | ||
81 | if ($this->_debug) { | |
82 | ||
83 | print "fetching $count messages\n"; | |
84 | ||
85 | } | |
86 | ||
87 | $directory = new DirectoryIterator($path); | |
88 | foreach ($directory as $entry) { | |
89 | if ($entry->isDot()) { | |
90 | continue; | |
91 | } | |
35f7561f TO |
92 | if (count($mails) >= $count) { |
93 | break; | |
94 | } | |
6a488035 TO |
95 | |
96 | $file = $path . DIRECTORY_SEPARATOR . $entry->getFilename(); | |
97 | if ($this->_debug) { | |
98 | print "retrieving message $file\n"; | |
99 | } | |
100 | ||
101 | $set = new ezcMailFileSet(array($file)); | |
317fceb4 | 102 | $parser = new ezcMailParser(); |
25606795 | 103 | // set property text attachment as file CRM-5408 |
6a488035 TO |
104 | $parser->options->parseTextAttachmentsAsFiles = TRUE; |
105 | ||
106 | $mail = $parser->parseMail($set); | |
107 | ||
108 | if (!$mail) { | |
109 | return CRM_Core_Error::createAPIError(ts('%1 could not be parsed', | |
353ffa53 TO |
110 | array(1 => $file) |
111 | )); | |
6a488035 TO |
112 | } |
113 | $mails[$file] = $mail[0]; | |
114 | } | |
115 | ||
116 | if ($this->_debug && (count($mails) <= 0)) { | |
117 | ||
118 | print "No messages found\n"; | |
119 | ||
120 | } | |
121 | ||
122 | return $mails; | |
123 | } | |
124 | ||
125 | /** | |
fe482240 | 126 | * Fetch the specified message to the local ignore folder. |
6a488035 | 127 | * |
90c8230e TO |
128 | * @param int $file |
129 | * File location of the message to fetch. | |
6a488035 | 130 | * |
77b97be7 | 131 | * @throws Exception |
6a488035 | 132 | */ |
00be9182 | 133 | public function markIgnored($file) { |
6a488035 TO |
134 | if ($this->_debug) { |
135 | print "moving $file to ignored folder\n"; | |
136 | } | |
137 | $target = $this->_ignored . DIRECTORY_SEPARATOR . basename($file); | |
138 | if (!rename($file, $target)) { | |
139 | throw new Exception("Could not rename $file to $target"); | |
140 | } | |
141 | } | |
142 | ||
143 | /** | |
fe482240 | 144 | * Fetch the specified message to the local processed folder. |
6a488035 | 145 | * |
90c8230e TO |
146 | * @param int $file |
147 | * File location of the message to fetch. | |
6a488035 | 148 | * |
77b97be7 | 149 | * @throws Exception |
6a488035 | 150 | */ |
00be9182 | 151 | public function markProcessed($file) { |
6a488035 TO |
152 | if ($this->_debug) { |
153 | print "moving $file to processed folder\n"; | |
154 | } | |
155 | $target = $this->_processed . DIRECTORY_SEPARATOR . basename($file); | |
156 | if (!rename($file, $target)) { | |
157 | throw new Exception("Could not rename $file to $target"); | |
158 | } | |
159 | } | |
96025800 | 160 | |
6a488035 | 161 | } |