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