| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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-2019 |
| 32 | */ |
| 33 | class CRM_Mailing_MailStore { |
| 34 | /** |
| 35 | * flag to decide whether to print debug messages |
| 36 | * @var bool |
| 37 | */ |
| 38 | public $_debug = FALSE; |
| 39 | |
| 40 | /** |
| 41 | * Return the proper mail store implementation, based on config settings. |
| 42 | * |
| 43 | * @param string $name |
| 44 | * Name of the settings set from civimail_mail_settings to use (null for default). |
| 45 | * |
| 46 | * @throws Exception |
| 47 | * @return object |
| 48 | * mail store implementation for processing CiviMail-bound emails |
| 49 | */ |
| 50 | public static function getStore($name = NULL) { |
| 51 | $dao = new CRM_Core_DAO_MailSettings(); |
| 52 | $dao->domain_id = CRM_Core_Config::domainID(); |
| 53 | $name ? $dao->name = $name : $dao->is_default = 1; |
| 54 | if (!$dao->find(TRUE)) { |
| 55 | throw new Exception("Could not find entry named $name in civicrm_mail_settings"); |
| 56 | } |
| 57 | |
| 58 | $protocols = CRM_Core_PseudoConstant::get('CRM_Core_DAO_MailSettings', 'protocol'); |
| 59 | if (empty($protocols[$dao->protocol])) { |
| 60 | throw new Exception("Empty mail protocol"); |
| 61 | } |
| 62 | |
| 63 | switch ($protocols[$dao->protocol]) { |
| 64 | case 'IMAP': |
| 65 | return new CRM_Mailing_MailStore_Imap($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl, $dao->source); |
| 66 | |
| 67 | case 'POP3': |
| 68 | return new CRM_Mailing_MailStore_Pop3($dao->server, $dao->username, $dao->password, (bool) $dao->is_ssl); |
| 69 | |
| 70 | case 'Maildir': |
| 71 | return new CRM_Mailing_MailStore_Maildir($dao->source); |
| 72 | |
| 73 | case 'Localdir': |
| 74 | return new CRM_Mailing_MailStore_Localdir($dao->source); |
| 75 | |
| 76 | // DO NOT USE the mbox transport for anything other than testing |
| 77 | // in particular, it does not clear the mbox afterwards |
| 78 | |
| 79 | case 'mbox': |
| 80 | return new CRM_Mailing_MailStore_Mbox($dao->source); |
| 81 | |
| 82 | default: |
| 83 | throw new Exception("Unknown protocol {$dao->protocol}"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Return all emails in the mail store. |
| 89 | * |
| 90 | * @return array |
| 91 | * array of ezcMail objects |
| 92 | */ |
| 93 | public function allMails() { |
| 94 | return $this->fetchNext(0); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Expunge the messages marked for deletion; stub function to be redefined by IMAP store. |
| 99 | */ |
| 100 | public function expunge() { |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Return the next X messages from the mail store. |
| 105 | * |
| 106 | * @param int $count |
| 107 | * Number of messages to fetch (0 to fetch all). |
| 108 | * |
| 109 | * @return array |
| 110 | * array of ezcMail objects |
| 111 | */ |
| 112 | public function fetchNext($count = 1) { |
| 113 | $offset = 1; |
| 114 | if (isset($this->_transport->options->uidReferencing) and $this->_transport->options->uidReferencing) { |
| 115 | $offset = $this->_transport->listUniqueIdentifiers(); |
| 116 | $offset = array_shift($offset); |
| 117 | } |
| 118 | try { |
| 119 | $set = $this->_transport->fetchFromOffset($offset, $count); |
| 120 | if ($this->_debug) { |
| 121 | print "fetching $count messages\n"; |
| 122 | } |
| 123 | } |
| 124 | catch (ezcMailOffsetOutOfRangeException$e) { |
| 125 | if ($this->_debug) { |
| 126 | print "got to the end of the mailbox\n"; |
| 127 | } |
| 128 | return []; |
| 129 | } |
| 130 | $mails = []; |
| 131 | $parser = new ezcMailParser(); |
| 132 | //set property text attachment as file CRM-5408 |
| 133 | $parser->options->parseTextAttachmentsAsFiles = TRUE; |
| 134 | |
| 135 | foreach ($set->getMessageNumbers() as $nr) { |
| 136 | if ($this->_debug) { |
| 137 | print "retrieving message $nr\n"; |
| 138 | } |
| 139 | $single = $parser->parseMail($this->_transport->fetchByMessageNr($nr)); |
| 140 | $mails[$nr] = $single[0]; |
| 141 | } |
| 142 | return $mails; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Point to (and create if needed) a local Maildir for storing retrieved mail |
| 147 | * |
| 148 | * @param string $name |
| 149 | * Name of the Maildir. |
| 150 | * |
| 151 | * @throws Exception |
| 152 | * @return string |
| 153 | * path to the Maildir's cur directory |
| 154 | */ |
| 155 | public function maildir($name) { |
| 156 | $config = CRM_Core_Config::singleton(); |
| 157 | $dir = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $name; |
| 158 | foreach ([ |
| 159 | 'cur', |
| 160 | 'new', |
| 161 | 'tmp', |
| 162 | ] as $sub) { |
| 163 | if (!file_exists($dir . DIRECTORY_SEPARATOR . $sub)) { |
| 164 | if ($this->_debug) { |
| 165 | print "creating $dir/$sub\n"; |
| 166 | } |
| 167 | if (!mkdir($dir . DIRECTORY_SEPARATOR . $sub, 0700, TRUE)) { |
| 168 | throw new Exception('Could not create ' . $dir . DIRECTORY_SEPARATOR . $sub); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | return $dir . DIRECTORY_SEPARATOR . 'cur'; |
| 173 | } |
| 174 | |
| 175 | } |