Merge pull request #16386 from civicrm/5.22
[civicrm-core.git] / CRM / Mailing / MailStore / Mbox.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class CRM_Mailing_MailStore_Mbox
20 */
21 class CRM_Mailing_MailStore_Mbox extends CRM_Mailing_MailStore {
22
23 /**
24 * Connect to and lock the supplied file and make sure the two mail dirs exist.
25 *
26 * @param string $file
27 * Mbox to operate upon.
28 *
29 * @return \CRM_Mailing_MailStore_Mbox
30 */
31 public function __construct($file) {
32 $this->_transport = new ezcMailMboxTransport($file);
33 flock($this->_transport->fh, LOCK_EX);
34
35 $this->_leftToProcess = count($this->_transport->listMessages());
36
37 $this->_ignored = $this->maildir(implode(DIRECTORY_SEPARATOR, [
38 'CiviMail.ignored',
39 date('Y'),
40 date('m'),
41 date('d'),
42 ]));
43 $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, [
44 'CiviMail.processed',
45 date('Y'),
46 date('m'),
47 date('d'),
48 ]));
49 }
50
51 /**
52 * Empty the mail source (if it was processed fully) and unlock the file.
53 */
54 public function __destruct() {
55 if ($this->_leftToProcess === 0) {
56 // FIXME: the ftruncate() call does not work for some reason
57 if ($this->_debug) {
58 print "trying to delete the mailbox\n";
59 }
60 ftruncate($this->_transport->fh, 0);
61 }
62 flock($this->_transport->fh, LOCK_UN);
63 }
64
65 /**
66 * Fetch the specified message to the local ignore folder.
67 *
68 * @param int $nr
69 * Number of the message to fetch.
70 */
71 public function markIgnored($nr) {
72 if ($this->_debug) {
73 print "copying message $nr to ignored folder\n";
74 }
75 $set = new ezcMailStorageSet($this->_transport->fetchByMessageNr($nr), $this->_ignored);
76 $parser = new ezcMailParser();
77 $parser->parseMail($set);
78 $this->_leftToProcess--;
79 }
80
81 /**
82 * Fetch the specified message to the local processed folder.
83 *
84 * @param int $nr
85 * Number of the message to fetch.
86 */
87 public function markProcessed($nr) {
88 if ($this->_debug) {
89 print "copying message $nr to processed folder\n";
90 }
91 $set = new ezcMailStorageSet($this->_transport->fetchByMessageNr($nr), $this->_processed);
92 $parser = new ezcMailParser();
93 $parser->parseMail($set);
94 $this->_leftToProcess--;
95 }
96
97 }