<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Helpers;
/**
 *
 * @package JREALTIMEANALYTICS::components::com_jrealtimeanalytics
 * @subpackage framework
 * @subpackage helpers
 * @author Joomla! Extensions Store
 * @copyright (C) 2014 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
 *         
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;
use Joomla\CMS\Mail\Mail;
use Joomla\CMS\Mail\MailHelper;

/**
 * Subclassing mailer object to avoid old J Error 500 J DocumentError template error.php
 *
 * @package JREALTIMEANALYTICS::components::com_jrealtimeanalytics
 * @subpackage framework
 * @subpackage helpers
 * @since 2.3
 */
class Mailer extends Mail {
	/**
	 * Exception object occurred
	 *
	 * @var Object
	 * @access public
	 */
	public $exception;
	
	/**
	 * Error details occurred
	 *
	 * @var Object
	 * @access public
	 */
	public $errorDetails;
	
	/**
	 * Singleton instance
	 *
	 * @param string $id
	 * @return mixed
	 */
	public static function getInstance($id = 'Joomla', $exceptions = true) {
		$conf = Factory::getApplication()->getConfig ();
		
		$smtpauth = ($conf->get ( 'smtpauth' ) == 0) ? null : 1;
		$smtpuser = $conf->get ( 'smtpuser' );
		$smtppass = $conf->get ( 'smtppass' );
		$smtphost = $conf->get ( 'smtphost' );
		$smtpsecure = $conf->get ( 'smtpsecure' );
		$smtpport = $conf->get ( 'smtpport' );
		$mailfrom = $conf->get ( 'mailfrom' );
		$fromname = $conf->get ( 'fromname' );
		$mailer = $conf->get ( 'mailer' );
		
		// Create a Mail object
		$mail = new static();
		
		// Set default sender without Reply-to
		$mail->SetFrom ( MailHelper::cleanLine ( $mailfrom ), MailHelper::cleanLine ( $fromname ), 0 );
		$mail->IsHTML(true);
		
		// Default mailer is to use PHP's mail function
		switch ($mailer) {
			case 'smtp' :
				$mail->useSMTP ( $smtpauth, $smtphost, $smtpuser, $smtppass, $smtpsecure, $smtpport );
				break;
			
			case 'sendmail' :
				$mail->IsSendmail ();
				break;
			
			default :
				$mail->IsMail ();
				break;
		}
		
		if (empty ( self::$instances [$id] )) {
			self::$instances [$id] = $mail;
		}
		
		return self::$instances [$id];
	}
	
	/**
	 * Send an email using Exceptions of PHPMailer
	 *
	 * @access public
	 * @throws RuntimeException
	 * @return boolean
	 */
	public function sendUsingExceptions() {
		if (Factory::getApplication()->getConfig ()->get ( 'mailonline', 1 )) {
			if (($this->Mailer == 'mail') && ! function_exists ( 'mail' )) {
				return false;
			}
			// Try send now
			try {
				if (! $this->PreSend ()) {
					$this->errorDetails = $this->ErrorInfo;
					return false;
				}
				
				// Disable the SMTPAutoTLS in every case, rely only on the global configuration parameter
				$this->SMTPAutoTLS = false;
					
				$sent = $this->PostSend ();
				if(!$sent) {
					$this->errorDetails = $this->ErrorInfo;
				}
				return $sent;
			} catch ( \PHPMailer\PHPMailer\Exception $e ) {
				$this->mailHeader = '';
				$this->exception = $e;
				$this->errorDetails = $this->ErrorInfo;
				return false;
			} catch ( \Exception $e ) {
				$this->mailHeader = '';
				$this->exception = $e;
				$this->errorDetails = $this->ErrorInfo;
				return false;
			}
		} else {
			Factory::getApplication ()->enqueueMessage ( Text::_ ( 'JLIB_MAIL_FUNCTION_OFFLINE' ) );
			return false;
		}
	}
}