<?php
/**
 * ------------------------------------------------------------------------
 * JA Login module
 * ------------------------------------------------------------------------
 * Copyright (C) 2004-2018 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
 * @license - GNU/GPL, http://www.gnu.org/licenses/gpl.html
 * Author: J.O.O.M Solutions Co., Ltd
 * Websites: http://www.joomlart.com - http://www.joomlancers.com
 * ------------------------------------------------------------------------
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;
use Joomla\CMS\Captcha\Captcha;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Helper\ModuleHelper;

// Include the syndicate functions only once
require_once (dirname(__FILE__) . '/helper.php');
if(version_compare(JVERSION, '4','ge')){
	HTMLHelper::_('bootstrap.tooltip');
}else{
	HTMLHelper::_('behavior.tooltip');
}
include_once(dirname(__FILE__).'/assets/asset.php');

$params->def('greeting', 1);
$type = modJALoginHelper::getType();
$return = modJALoginHelper::getReturnURL($params, $type);

$user = Factory::getUser();

$option = $app->input->get('option', '');
$task = $app->input->get('task', '');
if(!$user->id && $option !== "com_users" && $option !== "com_contact" && $params->get('show_register_form')) {
	$app = Factory::getApplication();
	$captchaType = $app->get('captcha');
	if ($captchaType) {
		$captcha = Captcha::getInstance($captchaType);
	}

	// these codes need to be trigger with plugin event 
	// to be able to interfere with the information authentication process when logging in
	// Check if reCAPTCHA is enabled
	if ($params->get('enable_gg_recaptcha', 0)) {
		$input = $app->input;
		
		// If this is a form submission
		if ($input->getMethod() === 'POST') {
			$recaptchaResponse = $input->get('g-recaptcha-response', '', 'string');
			if ($recaptchaResponse) {
				$secret_key = $params->get('gg_secret_key', '');
				$verify = modJALoginHelper::verifyRecaptcha($secret_key, $recaptchaResponse);
				
				if (!$verify->success || $verify->score < 0.5) {
					$app->enqueueMessage('Invalid reCAPTCHA verification', 'error');
					$app->redirect(JRoute::_('index.php'));
					return;
				}
			}
		}
	}
	// end
}

//Secret Key: only apply in Joomla 3
$tfa  = null;
if(version_compare(JVERSION, '3.0', 'ge')){
	$tfa = PluginHelper::getPlugin('twofactorauth'); 
}
$lang = Factory::getLanguage();
$lang->load('plg_user_terms', JPATH_ADMINISTRATOR);
//DISPLAYING
require ModuleHelper::getLayoutPath($module->module, $params->get('layout', 'default'));