<?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\Form\Form;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Language\Text;
use Joomla\Registry\Registry;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Component\ComponentHelper;

?>

<ul class="ja-login<?php echo $params->get('moduleclass_sfx','')?>">
<?php if($type == 'logout') : ?>
	<li>
	<form action="<?php echo Route::_('index.php', true, $params->get('usesecure')); ?>" method="post" name="form-login" id="login-form">
<?php if ($params->get('greeting')) : ?>
	<div class="login-greeting">
	<?php if($params->get('name') == 0) :
		echo Text::sprintf('HINAME', $user->get('username'));
	 else :
		echo Text::sprintf('HINAME', $user->get('name'));
	 endif; ?>
	</div>
<?php endif; ?>
	<div class="logout-button">
		<input type="submit" name="Submit" class="button btn" value="<?php echo Text::_('JLOGOUT'); ?>" />
	</div>

	<input type="hidden" name="option" value="com_users" />
	<input type="hidden" name="task" value="user.logout" />
	<input type="hidden" name="return" value="<?php echo $return; ?>" />
	<?php echo HTMLHelper::_('form.token');?>
</form>
	</li>
<?php else : ?>
	<?php if ($params->get('enable_gg_recaptcha', 0)) : ?>
		<script src="https://www.google.com/recaptcha/api.js?render=<?php echo $params->get('gg_site_key'); ?>"></script>
		<script>
		document.addEventListener('DOMContentLoaded', function() {
			const loginForm = document.getElementById('login-form');
			loginForm.addEventListener('submit', function(e) {
				e.preventDefault();
				grecaptcha.ready(function() { 
					grecaptcha.execute('<?php echo $params->get('gg_site_key'); ?>', {action: 'login'})
						.then(function(token) {
							// Add recaptcha token to form
							const tokenInput = document.createElement('input');
							tokenInput.type = 'hidden';
							tokenInput.name = 'g-recaptcha-response';
							tokenInput.value = token;
							loginForm.appendChild(tokenInput);
							// Submit the form
							loginForm.submit();
						});
				});
			});
		});
		</script>
	<?php endif; ?>
	<li>
		<a class="login-switch" href="<?php echo Route::_('index.php?option=com_users&view=login');?>" onclick="showBox('ja-user-login','mod_login_username',this, window.event || event);return false;" title="<?php echo Text::_('TXT_LOGIN');?>"><span><?php echo Text::_('TXT_LOGIN');?></span></a>

	<!--LOFIN FORM content-->
	<div id="ja-user-login">
	<?php if(PluginHelper::isEnabled('authentication', 'openid')) : ?>
        <?php HTMLHelper::_('script', 'openid.js'); ?>
    <?php endif; ?>
	  <form action="<?php echo Route::_('index.php', true, $params->get('usesecure')); ?>" method="post" name="form-login" id="login-form" >
			<div class="pretext">
				<?php echo $params->get('pretext'); ?>
			</div>
			<fieldset class="userdata">
				<p id="form-login-username">
					<label for="modlgn-username"><?php echo Text::_('JAUSERNAME') ?></label>
					<input id="modlgn-username" type="text" name="username" class="inputbox"  size="18" />
				</p>
				<p id="form-login-password">
					<label for="modlgn-passwd"><?php echo Text::_('JGLOBAL_PASSWORD') ?></label>
					<input id="modlgn-passwd" type="password" name="password" class="inputbox" size="18"  />
				</p>
				<?php if (!is_null($tfa) && $tfa != array()):?>
				<p class="login-input secretkey">
					<label class="" for="secretkey" id="secretkey-lbl" aria-invalid="false"><?php echo Text::_('JASECRETKEY') ?></label>
					<input type="text" size="25" value="" id="secretkey" name="secretkey">
				</p>
				<?php endif; ?>
				<?php if (PluginHelper::isEnabled('system', 'remember')) : ?>
				<p id="form-login-remember">
					<label for="modlgn-remember"><?php echo Text::_('JAREMEMBER_ME') ?></label>
					<input id="modlgn-remember" type="checkbox" name="remember" class="inputbox" value="yes"/>
				</p>
				<?php endif; ?>
				<input type="submit" name="Submit" class="button btn" value="<?php echo Text::_('JABUTTON_LOGIN'); ?>" />
				<input type="hidden" name="option" value="com_users" />
				<input type="hidden" name="task" value="user.login" />
				<input type="hidden" name="return" value="<?php echo $return; ?>" />
				<?php echo HTMLHelper::_('form.token'); ?>
			</fieldset>
			<ul>
				<li>
					<a href="<?php echo Route::_('index.php?option=com_users&view=reset'); ?>">
					<?php echo Text::_('FORGOT_YOUR_PASSWORD'); ?></a>
				</li>
				<li>
					<a href="<?php echo Route::_('index.php?option=com_users&view=remind'); ?>">
					<?php echo Text::_('FORGOT_YOUR_USERNAME'); ?></a>
				</li>
				<?php
				$usersConfig = ComponentHelper::getParams('com_users');
				if ($usersConfig->get('allowUserRegistration')) : ?>
				<li>
					<a href="<?php echo Route::_('index.php?option=com_users&view=registration'); ?>">
						<?php echo Text::_('REGISTER'); ?></a>
				</li>
				<?php endif; ?>
			</ul>
	        <?php echo $params->get('posttext'); ?>
	    </form>
    </div>

	</li>
	<?php
		$jinput = Factory::getApplication()->input;
		$option = $jinput->get('option', '', 'CMD');
		$task = $jinput->get('task', '', 'CMD');
		if($option!='com_user' && $task != 'register' && $params->get('show_register_form')) { ?>
	<li>
		<a class="register-switch" href="<?php echo Route::_("index.php?option=com_users&task=registration");?>" onclick="showBox('ja-user-register','namemsg',this, window.event || event);return false;" >
			<span><?php echo Text::_('REGISTER');?></span>
		</a>
		<!--Register FORM content-->
		<div id="ja-user-register" <?php if(!empty($captcha)) echo "class='hascaptcha'"; ?> >
			<?php
			HTMLHelper::_('behavior.keepalive');
			if(version_compare(JVERSION, '4', 'ge')){
				HTMLHelper::_('behavior.formvalidator');
			}else{
				HTMLHelper::_('behavior.formvalidation');
			}
			?>

			<form id="member-registration" action="<?php echo Route::_('index.php?option=com_users&task=registration.register'); ?>" method="post" class="form-validate">
				<fieldset>
				<?php if (isset($fieldset->label)):// If the fieldset has a label set, display it as the legend.?>
					<legend><?php echo Text::_($fieldset->label);?></legend>
				<?php endif;?>
					<dl>
						<dt>
							<label  class="required" for="jform_name" id="jform_name-lbl" title=""><?php echo Text::_( 'JANAME' ); ?>:</label>
							<em> (*)</em>
						</dt>						
						<dd><input type="text" size="30" class="required" value="" id="jform_name" name="jform[name]"></dd>

						<dt>
							<label title="" class="required" for="jform_username" id="jform_username-lbl"><?php echo Text::_( 'JAUSERNAME' ); ?>:</label>
							<em> (*)</em>	
						</dt>						
						<dd><input type="text" size="30" class="validate-username required" value="" id="jform_username" name="jform[username]"></dd>

						<dt>
							<label title="" class="required" for="jform_password1" id="jform_password1-lbl"><?php echo Text::_( 'JGLOBAL_PASSWORD' ); ?>:</label>
							<em> (*)</em>
						</dt>						
						<dd><input type="password" size="30" class="validate-password required" autocomplete="off" value="" id="jform_password1" name="jform[password1]"></dd>
						
						<dt>
							<label title="" class="required" for="jform_password2" id="jform_password2-lbl"><?php echo Text::_( 'JGLOBAL_REPASSWORD' ); ?>:</label>
							<em> (*)</em>
						</dt>						
						<dd><input type="password" size="30" class="validate-password required" autocomplete="off" value="" id="jform_password2" name="jform[password2]"></dd>
						
						<dt>
							<label title="" class="required" for="jform_email1" id="jform_email1-lbl"><?php echo Text::_( 'JAEMAIL' ); ?>:</label>
							<em> (*)</em>	
						</dt>						
						<dd><input type="text" size="30" class="validate-email required" value="" id="jform_email1" name="jform[email1]"></dd>
						
						<dt>
							<label title="" class="required" for="jform_email2" id="jform_email2-lbl"><?php echo Text::_( 'JACONFIRM_EMAIL_ADDRESS'); ?>:</label>
							<em> (*)</em>	
						</dt>
						<dd><input type="text" size="30" class="validate-email required" value="" id="jform_email2" name="jform[email2]"></dd>
						
						<?php if(!empty($captcha)): ?>
						<dt>
							<label title="" class="required"  id="jform_captcha-lbl"><?php echo Text::_( 'JACAPTCHA'); ?>:</label>
							<em> (*)</em>
						</dt>
						<dd>
							</dd>
						<?php endif; ?>
						
						<?php
						    $privacy = PluginHelper::getPlugin( 'system', 'privacyconsent' );
						    if (!empty($privacy)) {
                                if(version_compare(JVERSION, '4', 'ge')){
                              		FormHelper::addFieldPath(JPATH_SITE . '/plugins/system/privacyconsent/src/Field');
                              		Form::addFormPath(JPATH_SITE . '/plugins/system/privacyconsent/forms');
						    	}else{

                              		FormHelper::addFieldPath(JPATH_SITE . '/plugins/system/privacyconsent/field');
                              		Form::addFormPath(JPATH_SITE . '/plugins/system/privacyconsent/privacyconsent');
	    						}
                                $form2 = new Form('jform');
                                $form2->loadFile('privacyconsent');
                                $fields = $form2->getFieldset('privacyconsent');
                            
                                $params = new Registry($privacy->params);
                                $privacyArticleId = $params->get('privacy_article');
                                $privacynote      = $params->get('privacy_note');
                                $form2->setFieldAttribute('privacy', 'article', $privacyArticleId, 'privacyconsent');
                                $form2->setFieldAttribute('privacy', 'note', $privacynote, 'privacyconsent');
                                foreach ($fields as $kf => $field) {
                                    echo str_replace('privacyconsent[privacy]','jform[privacyconsent][privacy]',$field->renderField());
                                }
						    }
						?>

						<?php
						    $term = PluginHelper::getPlugin( 'user', 'terms' );
						    if (!empty($term)) {
                                
                                if(version_compare(JVERSION, '4', 'ge')){
                              		FormHelper::addFieldPrefix('Joomla\\Plugin\\User\\Terms\\Field');
                              		Form::addFormPath(JPATH_SITE . '/plugins/user/terms/forms');
						    	}else{
									FormHelper::addFieldPath(JPATH_SITE . '/plugins/user/terms/field');
	                                Form::addFormPath(JPATH_SITE . '/plugins/user/terms/terms');
	    						}
                                $form3 = new Form('jform');
                                $form3->loadFile('terms');
                                $fields = $form3->getFieldset('terms');
                            
                                $params = new Registry($term->params);
                                $termsarticle = $params->get('terms_article');
                                $termsnote    = $params->get('terms_note');
                                $form3->setFieldAttribute('terms', 'article', $termsarticle, 'terms');
                                $form3->setFieldAttribute('terms', 'note', $termsnote, 'terms');
                                foreach ($fields as $kf => $field) {
                                    echo str_replace('terms[terms]','jform[terms][terms]',$field->renderField());
                                }
						    }
						?>
					</dl>
				</fieldset>
				<br/>
				<p><?php echo Text::_("DESC_REQUIREMENT"); ?></p>
				<button type="submit" class="validate btn"><?php echo Text::_('JAREGISTER');?></button>
				<div>
					<input type="hidden" name="option" value="com_users" />
					<input type="hidden" name="task" value="registration.register" />
					<?php echo HTMLHelper::_('form.token');?>
				</div>
			</form>
				<!-- Old code -->
		</div>
	</li>
	<?php } ?>
		<!--LOFIN FORM content-->
<?php endif; ?>
</ul>