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