<?php 
//check if session is already start or not, if not then start
// session_status() is used to return the current session status.
//0 ----> PHP_SESSION_DISABLED if sessions are disabled.
//1 ----> PHP_SESSION_NONE if sessions are enabled, but none exists.
//2 ----> PHP_SESSION_ACTIVE if sessions are enabled, and one exists.

$session_start_var = session_status();
if ( $session_start_var == PHP_SESSION_ACTIVE) {	 
} else{
	 session_start();
}

unset($_SESSION['formscript_added']);
add_filter('widget_text', 'do_shortcode');
if(!is_admin() && $GLOBALS['pagenow'] !== 'post.php'){	
	add_shortcode('FormBuilder', 'formbuilder_shortcode');
}
add_filter( 'term_description', 'do_shortcode' );

function remove_fm_cssjs_version( $src ) {
	if( strpos( $src, '?onload=' ) ){
		$src = remove_query_arg( 'onload', $src );
	}
	if( strpos( $src, '?render=' ) ){
		$src = remove_query_arg( 'render', $src );
	}
	return $src;
}

function formbuilder_shortcode($atts, $content=null){ 
	extract(shortcode_atts(array(
		'id' => '',
	), $atts));
	


	 global $post;   
	 $pageid = $post->ID; 
	 $_SESSION['cs_pageid'] = $pageid;
	 $current_url = $_SESSION['cs_current_url'] = get_permalink($pageid);

	
	$content = do_shortcode($content);
	$content = dap_clean_shortcode_content($content);	
	
	if (defined('SITEROOT')) $lldocroot = SITEROOT; else $lldocroot = $_SERVER['DOCUMENT_ROOT'];
	include_once ($lldocroot . "/dap/dap-config.php");

	$session = Dap_Session::getSession();
	$user = $session->getUser();
	/*$_SESSION['dapregister']="";
	unset($_SESSION['dapregister']);*/
	add_filter( 'style_loader_src', 'remove_fm_cssjs_version', 15, 1 );
	add_filter( 'script_loader_src', 'remove_fm_cssjs_version', 15, 1 );
	
	
	/****including css/js here , wp_footer not working in some site*********/
	/*wp_enqueue_script('countries-js', SITE_URL_DAP.'/dap/inc/tp/forms/js/countries.js', array('jquery'));
	wp_enqueue_script('form_front', SITE_URL_DAP.'/dap/inc/tp/forms/js/form_front.js', array('jquery'));
	wp_enqueue_style('form-builder-bootstrap', SITE_URL_DAP.'/dap/inc/tp/forms/css/form-builder-bootstrap.css');
	wp_enqueue_style('form-builder-sweet', '//cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css');
	wp_enqueue_script('validate-min-sweet', '//cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js');
	/******including css/js ends *******/
	
	
	// Added for where jquery is already included
	$script_style ="";
	if(wp_script_is('jquery')) {
		 
		//added for if the jquery from wp is including in the footer
		 add_action('wp_footer','myformbuilder_style'); 
		 
		 //THie check is only for Hello Elementor theme
		$theme = wp_get_theme(); // gets the current theme		 
		if ( 'Hello Elementor' == $theme->name || 'Hello Elementor' == $theme->parent_theme ) {
			$script_style .= ' 
			<script src="'.SITE_URL_DAP.'/dap/inc/tp/forms/js/countries.js"></script>
			<script src="'.SITE_URL_DAP.'/dap/inc/tp/forms/js/form_front.js?v=2"></script>
			<script src="//cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
			 <link href="'.SITE_URL_DAP.'/dap/inc/tp/forms/css/form-builder-bootstrap.css">
			 <link href="//cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css" rel="stylesheet"> ';	
		}
		
	} else {
		 
		// insert jQuery
		$script_style = '<script src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>';
		$script_style .= ' 
		<script src="'.SITE_URL_DAP.'/dap/inc/tp/forms/js/countries.js"></script>
		<script src="'.SITE_URL_DAP.'/dap/inc/tp/forms/js/form_front.js?v=2"></script>
		<script src="//cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
		 <link href="'.SITE_URL_DAP.'/dap/inc/tp/forms/css/form-builder-bootstrap.css">
		 <link href="//cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css" rel="stylesheet"> ';	
	
	}
	
	$script_style .= ' 		 
		 <style>#error-page p, #error-page .wp-die-message {font-size: 0; line-height: 0; margin: 0; display: none;   padding: 0;} .file-upload-wrapper.commonUploadHideShow{display:none}</style>';
	
	$site_url = get_site_url();
	$base_path = dirname(__FILE__);
	$id = urldecode($id);
	if(!empty($id)){
		$getFieldData = Dap_FormBuilder::loadFormFieldById( $id );
		$dap_signup_id = $id;
		$form_name  = $getFieldData->getFormName();
		$product_id = $getFieldData->getProductId();
		$template_name  = $getFieldData->getTemplateName();
		$template_data = $getFieldData->getTemplateData();
		$country = $getFieldData->getCountry();
		$state = $getFieldData->getState();
		$redirect_url = $getFieldData->getRedirectUrl();
		$autologin = $getFieldData->getAutoLogin();
		$captcha = $getFieldData->getCaptcha();
		$allow_file_upload = $getFieldData->getAllowFileUpload();
		$allowed_types_of_file = $getFieldData->getAllowedTypesOfFile();
		$max_upload_size = $getFieldData->getMaxFileUploadSize();
		$max_number_of_file = $getFieldData->getMaxNumberOfFile();
		if($allow_file_upload == 'Y'){
			$allowed_file_upload_types =   dapMemberGetMessageByKeyAndAction('myprofile','allowed_file_upload_types');
			$file_upload_max_size =   dapMemberGetMessageByKeyAndAction('myprofile','file_upload_max_size');
			$allowed_types_of_filearr = explode('|' , $allowed_types_of_file);
			$allowedTypesOfFileHidden = array_filter($allowed_types_of_filearr);
			$strallowedTypesOfFileHidden = implode(',' , $allowedTypesOfFileHidden);
			$allowed_file_upload_types = str_replace( '%%ALLOWED_FILES%%',$strallowedTypesOfFileHidden, $allowed_file_upload_types); 
			$file_upload_max_size = str_replace( '%%MAX_UPLOAD_SIZE%%',$max_upload_size.'MB', $file_upload_max_size); 
		}
		
		$formaction_without_captcha = 	SITE_URL_DAP ."/dap/signup_submit.php";
		$formaction_with_captcha = SITE_URL_DAP ."/dap/signup_submit_captcha.php";
		$onsubmit_fun = 'return validate_dap_direct_signup_with_captcha();';
		$google_site_key=Dap_Config::get("GOOGLE_SITE_KEY");

		/******GDPR DATA starts*********/
		$ip = $_SERVER['REMOTE_ADDR'];
		$gdprcountry = dapGetGDPRStatus($ip);
		$currentdate = date('Y-m-d h:i:s'); 
		 $gdpr_compliance_date='<input type="hidden" name="currentdate"  id="currentdate"  value="'.$currentdate.'" />';
		if($gdprcountry == 1){
		    $template_data = str_replace( '[GDPRTERMSVISIBILITY]',"display:block", $template_data); 
		    $template_data = str_replace( '[GDPRVALUE]',"GDPR_CONSENT_YES", $template_data); 
		    $gdpr_compliance_date .= '<input type="hidden" name="gdpr_compliance_date"  id="gdpr_compliance_date"  value="'.$currentdate.'" />';
		}else{
		    $template_data = str_replace( '[GDPRTERMSVISIBILITY]',"display:none", $template_data);
		    $template_data = str_replace( '[GDPRVALUE]',"NOT_GDPR_USER", $template_data); 
		    $gdpr_compliance_date .= '<input type="hidden" name="gdpr_compliance_date"  id="gdpr_compliance_date"  value="" />'; 
		}
		
		$gdpr_req = GDPR_REQUIRED;
		$gdpr_required = '<input type="hidden" name="gdpr_req_msg"  id="gdpr_req_msg"  value="'.$gdpr_req.'" />';
		
		$template_data = $template_data.$gdpr_required.$gdpr_compliance_date;
		/******GDPR DATA ends*********/

		$flength = 4;
		$rand_fid =  substr(str_shuffle(md5(time())),0,$flength);
		if(isset($_GET['err']) && $_GET['err'] != ""){
			$data  =  '<div class="form_container formbuilder_captcha" id="form_cont_'.$rand_fid.'" style=" width: 100%; float: left; margin: 0px auto;        text-align: center;"><div class="error_empty_capcha"><p style=" width: auto;   border: 2px solid red;display: inline-block;margin: 10px 0; padding: 7px 60px;line-height: normal; font-size: 14px; font-weight: 600;color:red; font-family: sans-serif;">'.$_GET['err'].' </p></div>'.$template_data.'</div>';
		}else if($captcha == "Y"){
			if(!empty($google_site_key)){
				 
				$data  =  '<div class="form_container formbuilder_captcha" id="form_cont_'.$rand_fid.'">'.$template_data.'</div>';
			}else{
				 
				$data  =  '<div class="form_container formbuilder_captcha" id="form_cont_'.$rand_fid.'" style=" width: 100%; float: left; margin: 0px auto;        text-align: center;"><div class="error_empty_capcha"><p style=" width: auto;   border: 2px solid red;display: inline-block;margin: 10px 0; padding: 7px 60px;line-height: normal; font-size: 14px; font-weight: 600;color:red; font-family: sans-serif;">ERROR: Google Captcha Site key is Empty </p></div>'.$template_data.'</div>';
			}
		}else{
			$data  =  '<div class="form_container formbuilder_captcha" id="form_cont_'.$rand_fid.'">'.$template_data.'</div>';
		}
		
		?>
		
		<?php

		if($captcha == "Y"){
			if(!empty($google_site_key)){
				$data = str_replace( '[FORM_ACTION]',$formaction_with_captcha, $data);
				$data = str_replace( '[ONSUBMIT_FUNCTION]',$onsubmit_fun, $data);
				$data = str_replace( '[GOOGLE_SITE_KEY]',$google_site_key, $data);
				$data = str_replace( '[CAPTCHA_FRONTEND]','block', $data);
				$data = str_replace( '[CAPTCHA_BACKEND]','none', $data);
				$data = str_replace( '[CAPTCHA_ID]','cap_'.$rand_fid, $data);
				$data = str_replace( '[CAPTCHA_DATAID]','captt_'.$rand_fid, $data);
				if(!isset($_SESSION['formscript_added'])){
					 fb_captcha_data($google_site_key );
					 
					$_SESSION['formscript_added'] = 'Y';
				}
			}
		}else{
			$data = str_replace( '[FORM_ACTION]',$formaction_without_captcha, $data);
			$data = str_replace( '[ONSUBMIT_FUNCTION]','', $data);
			$data = str_replace( '[GOOGLE_SITE_KEY]', '', $data);
			$data = str_replace( '[CAPTCHA_FRONTEND]','none', $data);
			$data = str_replace( '[CAPTCHA_BACKEND]','none', $data);
		}

		$data = str_replace( '[PRODUCT_ID]',$product_id, $data);
		$data = str_replace( '[REDIRECT_URL]',$redirect_url, $data);
		$data = str_replace( '[STATE_NAME]',$state, $data);
		$data = str_replace( '[COUNTRY_NAME]',$country, $data);
		$data = str_replace( '[AUTO_LOGIN]',$autologin, $data);
		
		$data = str_replace( '[first_name_req]', FIRST_NAME_REQUIRED , $data);
		$data = str_replace( '[last_name_req]', LAST_NAME_REQUIRED , $data);
		$data = str_replace( '[email_req]', EMAIL_REQUIRED , $data);
		$data = str_replace( '[phone_req]', PHONE_REQUIRED , $data);
		$data = str_replace( '[country_req]', COUNTRY_REQUIRED , $data);
		$data = str_replace( '[state_req]', STATE_REQUIRED , $data);
		$data = str_replace( '[city_req]', CITY_REQUIRED , $data);
		$data = str_replace( '[zip_req]', ZIP_REQUIRED , $data);
		$data = str_replace( '[addr_req]', ADDRESS_REQUIRED , $data);
		$data = str_replace( '[terms_req]', TERMS_CONDITION_REQUIRED , $data);
		$data = str_replace( '[additional_req]', ADDITIONAL_REQUIRED , $data);
		$data = str_replace( '[captcha_req]', PLEASE_SELECT_CAPTCHA , $data);
		$data = str_replace( '[please_wait]', PLEASE_WAIT , $data);
		if($captcha == "Y"){
		
		
		$script_style .= '<script>
			var google_site_key = "'. $google_site_key.'";
		</script>';
		
	}

	

	$script_style .= '<script>var allow_file_upload =  "'.$allow_file_upload.'";
	var allowed_types_of_file =  "'.$allowed_types_of_file.'";
	var max_upload_size =  "'.$max_upload_size.'";
	var max_number_of_file =  "'.$max_number_of_file.'";
	var dap_signup_id =  "'.$dap_signup_id.'";
	var site_url =  "'.$site_url.'";
	var base_path =  "'.$base_path.'";
	var current_url =  "'.$current_url.'";
	var allowed_file_upload_types =  "'.$allowed_file_upload_types.'";
	var file_upload_max_size =  "'.$file_upload_max_size.'"</script>';

		 return $script_style.$data;
	}
}  

function fb_captcha_data($google_site_key ){
		//wp_enqueue_script('recaptcha', 'https://www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit', array('jquery'));
		echo' <script src="https://www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit" async defer></script>';
		//echo "<script>myCallBack();	</script>"	;
	 echo "<script>
		function myCallBack() {
		jQuery('.frontend_captcha').each(function(index, el) {
			var widgetId = grecaptcha.render(el, {'sitekey' : ''+google_site_key+''});
			jQuery(this).attr('data-id', widgetId);
		});
	}	</script>"	;
 
	?>

	
	<style>.tcb-notification , #tve_notification_box{height: 100px !important;} .tcb-notification .tcb-notification-text{ height: 100px !important;    float: left;    overflow: scroll;}  .tcb-elem-placeholder {
	display: block !important;	}
	</style>
<?php 
} 

function myformbuilder_style() {
	wp_enqueue_script('countries-js', SITE_URL_DAP.'/dap/inc/tp/forms/js/countries.js', array('jquery'));
	wp_enqueue_script('form_front', SITE_URL_DAP.'/dap/inc/tp/forms/js/form_front.js?v=9', array('jquery'));
	wp_enqueue_style('form-builder-bootstrap', SITE_URL_DAP.'/dap/inc/tp/forms/css/form-builder-bootstrap.css');
	wp_enqueue_style('form-builder-sweet', '//cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css');
	wp_enqueue_script('validate-min-sweet', '//cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js');
}
 
 
?>
