<?php
/**
 * ###############################################
 *
 * SWIFT Framework
 * _______________________________________________
 *
 * @author		Varun Shoor
 *
 * @package		SWIFT
 * @copyright	Copyright (c) 2001-2012, Kayako
 * @license		http://www.kayako.com/license
 * @link		http://www.kayako.com
 *
 * ###############################################
 */

/**
 * The Email Queue Mailbox Management Class
 *
 * @author Varun Shoor
 */
class SWIFT_EmailQueueMailbox extends SWIFT_EmailQueue
{
	// Core Constants
	const SMTP_NONSSL = 'nonssl';
	const SMTP_TLS = 'tls';
	const SMTP_SSL = 'ssl';
	const SMTP_SSLV2 = 'sslv2';
	const SMTP_SSLV3 = 'sslv3';

	/**
	 * Constructor
	 *
	 * @author Varun Shoor
	 * @param SWIFT_Data $_SWIFT_DataObject The SWIFT_Data Object
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_Exception If the Record could not be loaded
	 */
	public function __construct(SWIFT_Data $_SWIFT_DataObject)
	{
		parent::__construct($_SWIFT_DataObject);

		return true;
	}

	/**
	 * Destructor
	 *
	 * @author Varun Shoor
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function __destruct()
	{
		parent::__destruct();

		return true;
	}

	/**
	 * Check to see if its a valid SMTP Type
	 *
	 * @author Varun Shoor
	 * @param constant $_smtpType The SMTP Type
	 * @return bool "true" on Success, "false" otherwise
	 */
	static public function IsValidSMTPType($_smtpType)
	{
		if ($_smtpType == self::SMTP_TLS || $_smtpType == self::SMTP_SSL || $_smtpType == self::SMTP_SSLV2 || $_smtpType == self::SMTP_SSLV3 ||
				$_smtpType == self::SMTP_NONSSL)
		{
			return true;
		}

		return false;
	}

	/**
	 * Create a new Email Queue
	 *
	 * @author Varun Shoor
	 * @param string $_queueEmail The Queue Email Address
	 * @param object $_EmailQueueTypeObject The Email Queue Type Object Pointer (NEWS/TICKETS)
	 * @param string $_fetchType The Queue Fetch Type
	 * @param string $_queuePrefix The Queue Prefix
	 * @param string $_customFromName The Custom From Name
	 * @param string $_customFromEmail The Custom From Email
	 * @param string $_queueSignature The Queue Signature
	 * @param bool $_registrationRequired Whether the user should be registered for message acceptance to work
	 * @param bool $_isEnabled Whether this Queue is Enabled
	 * @param string $_hostName The Mailbox Hostname
	 * @param int $_port The Mailbox Port
	 * @param string $_userName The Mailbox User Name
	 * @param string $_userPassword The Mailbox Password
	 * @param bool $_leaveCopyOnServer Whether to Leave Copy on Server
	 * @param bool $_useQueueSMTP Whether to use Queue SMTP (for all objects created from this queue)
	 * @param constant $_smtpType The Queue SMTP Type
	 * @param bool $_forceQueue (OPTIONAL)
	 * @return mixed "SWIFT_EmailQueueMailbox" (OBJECT) on Success, "false" otherwise
	 * @throws SWIFT_EmailQueue_Exception When Invalid Data is Specified or the Object couldnt be created
	 */
	static public function Create($_queueEmail, SWIFT_EmailQueueType $_EmailQueueTypeObject, $_fetchType, $_queuePrefix, $_customFromName,
			$_customFromEmail, $_queueSignature, $_registrationRequired, $_isEnabled, $_hostName, $_port, $_userName, $_userPassword,
			$_leaveCopyOnServer, $_useQueueSMTP, $_smtpType, $_forceQueue = true)
	{
		if (!self::IsValidSMTPType($_smtpType) || $_fetchType == self::FETCH_PIPE)
		{
			throw new SWIFT_EmailQueue_Exception('Invalid Data Specified');

			return false;
		}

		$_emailQueueID = parent::Create($_queueEmail, $_fetchType, $_EmailQueueTypeObject, $_queuePrefix, $_customFromName, $_customFromEmail,
				$_queueSignature, $_registrationRequired, $_isEnabled, false);

		$_SWIFT_EmailQueueMailboxObject = new SWIFT_EmailQueueMailbox(new SWIFT_DataID($_emailQueueID));
		if (!$_SWIFT_EmailQueueMailboxObject instanceof SWIFT_EmailQueueMailbox || !$_SWIFT_EmailQueueMailboxObject->GetIsClassLoaded())
		{
			throw new SWIFT_EmailQueue_Exception('Unable to Load Email Queue (MAILBOX)');

			return false;
		}

		$_SWIFT_EmailQueueMailboxObject->UpdatePool('host', $_hostName);
		$_SWIFT_EmailQueueMailboxObject->UpdatePool('port', intval($_port));
		$_SWIFT_EmailQueueMailboxObject->UpdatePool('username', $_userName);
		$_SWIFT_EmailQueueMailboxObject->UpdatePool('userpassword', $_userPassword);
		$_SWIFT_EmailQueueMailboxObject->UpdatePool('leavecopyonserver', intval($_leaveCopyOnServer));
		$_SWIFT_EmailQueueMailboxObject->UpdatePool('usequeuesmtp', intval($_useQueueSMTP));
		$_SWIFT_EmailQueueMailboxObject->UpdatePool('forcequeue', intval($_forceQueue));
		$_SWIFT_EmailQueueMailboxObject->UpdatePool('smtptype', $_smtpType);

		$_SWIFT_EmailQueueMailboxObject->ProcessUpdatePool();

		self::RebuildCache();

		return $_SWIFT_EmailQueueMailboxObject;
	}

	/**
	 * Update the Email Queue Record
	 *
	 * @author Varun Shoor
	 * @param string $_queueEmail The Queue Email Address
	 * @param object $_EmailQueueTypeObject The Email Queue Type Object Pointer (NEWS/TICKETS)
	 * @param string $_fetchType The Queue Fetch Type
	 * @param string $_queuePrefix The Queue Prefix
	 * @param string $_customFromName The Custom From Name
	 * @param string $_customFromEmail The Custom From Email
	 * @param string $_queueSignature The Queue Signature
	 * @param bool $_registrationRequired Whether the user should be registered for message acceptance to work
	 * @param bool $_isEnabled Whether this Queue is Enabled
	 * @param string $_hostName The Mailbox Hostname
	 * @param int $_port The Mailbox Port
	 * @param string $_userName The Mailbox User Name
	 * @param string $_userPassword The Mailbox Password
	 * @param bool $_leaveCopyOnServer Whether to Leave Copy on Server
	 * @param bool $_useQueueSMTP Whether to use Queue SMTP (for all objects created from this queue)
	 * @param constant $_smtpType The Queue SMTP Type
	 * @param bool $_forceQueue (OPTIONAL)
	 * @return bool "true" on Success, "false" otherwise
	 * @throws SWIFT_EmailQueue_Exception If the Class is not Loaded or If Invalid Data is Provided
	 */
	public function Update($_queueEmail, SWIFT_EmailQueueType $_EmailQueueTypeObject, $_fetchType, $_queuePrefix, $_customFromName,
			$_customFromEmail, $_queueSignature, $_registrationRequired, $_isEnabled, $_hostName, $_port, $_userName, $_userPassword,
			$_leaveCopyOnServer, $_useQueueSMTP, $_smtpType, $_forceQueue = true)
	{
		if (!$this->GetIsClassLoaded())
		{
			throw new SWIFT_EmailQueue_Exception(SWIFT_CLASSNOTLOADED);

			return false;
		} else if (!self::IsValidSMTPType($_smtpType) || $this->GetProperty('fetchtype') == self::FETCH_PIPE) {
			throw new SWIFT_EmailQueue_Exception('Invalid Data Specified');

			return false;
		}

		$_updateResult = parent::Update($_queueEmail, $_EmailQueueTypeObject, $_queuePrefix, $_customFromName, $_customFromEmail, $_queueSignature,
				$_registrationRequired, $_isEnabled, false);
		if (!$_updateResult)
		{
			throw new SWIFT_EmailQueue_Exception('Failed to Update Email Queue (MAILBOX)');

			return false;
		}

		$this->UpdatePool('host', $_hostName);
		$this->UpdatePool('port', intval($_port));
		$this->UpdatePool('username', $_userName);
		$this->UpdatePool('fetchtype', $_fetchType);
		$this->UpdatePool('userpassword', $_userPassword);
		$this->UpdatePool('leavecopyonserver', intval($_leaveCopyOnServer));
		$this->UpdatePool('usequeuesmtp', intval($_useQueueSMTP));
		$this->UpdatePool('forcequeue', intval($_forceQueue));
		$this->UpdatePool('smtptype', $_smtpType);

		$this->ProcessUpdatePool();

		self::RebuildCache();

		return true;
	}
}
?>