| 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 | |
| 39 | /** |
| 40 | * Class CRM_Mailing_MailStore_Mbox |
| 41 | */ |
| 42 | class 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 | * |
| 47 | * @param string $file mbox to operate upon |
| 48 | * |
| 49 | * @return \CRM_Mailing_MailStore_Mbox |
| 50 | */ |
| 51 | function __construct($file) { |
| 52 | $this->_transport = new ezcMailMboxTransport($file); |
| 53 | flock($this->_transport->fh, LOCK_EX); |
| 54 | |
| 55 | $this->_leftToProcess = count($this->_transport->listMessages()); |
| 56 | |
| 57 | $this->_ignored = $this->maildir(implode(DIRECTORY_SEPARATOR, array('CiviMail.ignored', date('Y'), date('m'), date('d')))); |
| 58 | $this->_processed = $this->maildir(implode(DIRECTORY_SEPARATOR, array('CiviMail.processed', date('Y'), date('m'), date('d')))); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Empty the mail source (if it was processed fully) and unlock the file |
| 63 | * |
| 64 | * @return void |
| 65 | */ |
| 66 | function __destruct() { |
| 67 | if ($this->_leftToProcess === 0) { |
| 68 | // FIXME: the ftruncate() call does not work for some reason |
| 69 | if ($this->_debug) { |
| 70 | print "trying to delete the mailbox\n"; |
| 71 | } |
| 72 | ftruncate($this->_transport->fh, 0); |
| 73 | } |
| 74 | flock($this->_transport->fh, LOCK_UN); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Fetch the specified message to the local ignore folder |
| 79 | * |
| 80 | * @param integer $nr number of the message to fetch |
| 81 | * |
| 82 | * @return void |
| 83 | */ |
| 84 | function markIgnored($nr) { |
| 85 | if ($this->_debug) { |
| 86 | print "copying message $nr to ignored folder\n"; |
| 87 | } |
| 88 | $set = new ezcMailStorageSet($this->_transport->fetchByMessageNr($nr), $this->_ignored); |
| 89 | $parser = new ezcMailParser; |
| 90 | $parser->parseMail($set); |
| 91 | $this->_leftToProcess--; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Fetch the specified message to the local processed folder |
| 96 | * |
| 97 | * @param integer $nr number of the message to fetch |
| 98 | * |
| 99 | * @return void |
| 100 | */ |
| 101 | function markProcessed($nr) { |
| 102 | if ($this->_debug) { |
| 103 | print "copying message $nr to processed folder\n"; |
| 104 | } |
| 105 | $set = new ezcMailStorageSet($this->_transport->fetchByMessageNr($nr), $this->_processed); |
| 106 | $parser = new ezcMailParser; |
| 107 | $parser->parseMail($set); |
| 108 | $this->_leftToProcess--; |
| 109 | } |
| 110 | } |
| 111 | |