Merge pull request #1 from civicrm/master
[civicrm-core.git] / CRM / Mailing / MailStore / Mbox.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33
28518c90
EM
34/**
35 * Class CRM_Mailing_MailStore_Mbox
36 */
6a488035
TO
37class CRM_Mailing_MailStore_Mbox extends CRM_Mailing_MailStore {
38
39 /**
fe482240 40 * Connect to and lock the supplied file and make sure the two mail dirs exist.
6a488035 41 *
90c8230e
TO
42 * @param string $file
43 * Mbox to operate upon.
6a488035 44 *
dd244018 45 * @return \CRM_Mailing_MailStore_Mbox
6a488035 46 */
00be9182 47 public function __construct($file) {
6a488035
TO
48 $this->_transport = new ezcMailMboxTransport($file);
49 flock($this->_transport->fh, LOCK_EX);
50
51 $this->_leftToProcess = count($this->_transport->listMessages());
52
353ffa53
TO
53 $this->_ignored = $this->maildir(implode(DIRECTORY_SEPARATOR, array(
54 'CiviMail.ignored',
55 date('Y'),
56 date('m'),
acb1052e 57 date('d'),
353ffa53
TO
58 )));
59 $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, array(
60 'CiviMail.processed',
61 date('Y'),
62 date('m'),
acb1052e 63 date('d'),
353ffa53 64 )));
6a488035
TO
65 }
66
67 /**
25606795 68 * Empty the mail source (if it was processed fully) and unlock the file.
6a488035 69 */
00be9182 70 public function __destruct() {
6a488035
TO
71 if ($this->_leftToProcess === 0) {
72 // FIXME: the ftruncate() call does not work for some reason
73 if ($this->_debug) {
74 print "trying to delete the mailbox\n";
75 }
76 ftruncate($this->_transport->fh, 0);
77 }
78 flock($this->_transport->fh, LOCK_UN);
79 }
80
81 /**
fe482240 82 * Fetch the specified message to the local ignore folder.
6a488035 83 *
90c8230e
TO
84 * @param int $nr
85 * Number of the message to fetch.
6a488035 86 */
00be9182 87 public function markIgnored($nr) {
6a488035
TO
88 if ($this->_debug) {
89 print "copying message $nr to ignored folder\n";
90 }
91 $set = new ezcMailStorageSet($this->_transport->fetchByMessageNr($nr), $this->_ignored);
acb1052e 92 $parser = new ezcMailParser();
6a488035
TO
93 $parser->parseMail($set);
94 $this->_leftToProcess--;
95 }
96
97 /**
fe482240 98 * Fetch the specified message to the local processed folder.
6a488035 99 *
90c8230e
TO
100 * @param int $nr
101 * Number of the message to fetch.
6a488035 102 */
00be9182 103 public function markProcessed($nr) {
6a488035
TO
104 if ($this->_debug) {
105 print "copying message $nr to processed folder\n";
106 }
107 $set = new ezcMailStorageSet($this->_transport->fetchByMessageNr($nr), $this->_processed);
acb1052e 108 $parser = new ezcMailParser();
6a488035
TO
109 $parser->parseMail($set);
110 $this->_leftToProcess--;
111 }
96025800 112
6a488035 113}