Merge pull request #3204 from GinkgoFJG/CRM-14662
[civicrm-core.git] / CRM / Mailing / MailStore / Imap.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 require_once 'ezc/Base/src/ezc_bootstrap.php';
37 require_once 'ezc/autoload/mail_autoload.php';
38 class CRM_Mailing_MailStore_Imap extends CRM_Mailing_MailStore {
39
40 /**
41 * Connect to the supplied IMAP server and make sure the two mailboxes exist
42 *
43 * @param string $host host to connect to
44 * @param string $username authentication username
45 * @param string $password authentication password
46 * @param bool $ssl whether to use IMAP or IMAPS
47 * @param string $folder name of the inbox folder
48 *
49 * @return \CRM_Mailing_MailStore_Imap
50 */
51 function __construct($host, $username, $password, $ssl = TRUE, $folder = 'INBOX') {
52 // default to INBOX if an empty string
53 if (!$folder) {
54 $folder = 'INBOX';
55 }
56
57 if ($this->_debug) {
58
59 print "connecting to $host, authenticating as $username and selecting $folder\n";
60
61 }
62
63 $options = array('ssl' => $ssl, 'uidReferencing' => TRUE);
64 $this->_transport = new ezcMailImapTransport($host, NULL, $options);
65 $this->_transport->authenticate($username, $password);
66 $this->_transport->selectMailbox($folder);
67
68 $this->_ignored = implode($this->_transport->getHierarchyDelimiter(), array($folder, 'CiviMail', 'ignored'));
69 $this->_processed = implode($this->_transport->getHierarchyDelimiter(), array($folder, 'CiviMail', 'processed'));
70 $boxes = $this->_transport->listMailboxes();
71
72 if ($this->_debug) {
73 print 'mailboxes found: ' . implode(', ', $boxes) . "\n";
74 }
75
76 if (!in_array(strtolower($this->_ignored), array_map('strtolower', $boxes))) {
77 $this->_transport->createMailbox($this->_ignored);
78 }
79
80 if (!in_array(strtolower($this->_processed), array_map('strtolower', $boxes))) {
81 $this->_transport->createMailbox($this->_processed);
82 }
83 }
84
85 /**
86 * Expunge the messages marked for deletion, CRM-7356
87 */
88 function expunge() {
89 $this->_transport->expunge();
90 }
91
92 /**
93 * Move the specified message to the ignored folder
94 *
95 * @param integer $nr number of the message to move
96 *
97 * @return void
98 */
99 function markIgnored($nr) {
100 if ($this->_debug) {
101 print "setting $nr as seen and moving it to the ignored mailbox\n";
102 }
103 $this->_transport->setFlag($nr, 'SEEN');
104 $this->_transport->copyMessages($nr, $this->_ignored);
105 $this->_transport->delete($nr);
106 }
107
108 /**
109 * Move the specified message to the processed folder
110 *
111 * @param integer $nr number of the message to move
112 *
113 * @return void
114 */
115 function markProcessed($nr) {
116 if ($this->_debug) {
117 print "setting $nr as seen and moving it to the processed mailbox\n";
118 }
119 $this->_transport->setFlag($nr, 'SEEN');
120 $this->_transport->copyMessages($nr, $this->_processed);
121 $this->_transport->delete($nr);
122 }
123 }
124