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