<?php

require_once("gdprfunction.php");

function wpfresh5_settings_gdpr() {
    if( isset( $_POST['wpfresh5_save_settings'] ) ) {
        wpfresh5_save_settings( 'gdpr' );
    }

    $settings = wpfresh5_get_plugin_settings();
    ?>
        <form action="" method="post" id="wpfresh5-settings-form">
            <div id="wpfresh5-tabs" >
                <h2 class="nav-tab-wrapper">
                    <a href="#wpfresh5-gdpr1-tab" class="nav-tab nav-tab-active"><i class="fa fa-gears"></i> General</a>
                    <a href="#wpfresh5-gdpr9-tab" class="nav-tab"><i class="fa fa-user"></i> EU Visitors</a>
                    <a href="#wpfresh5-gdpr2-tab" class="nav-tab" ><i class="fa fa-check-square-o"></i> Cookie Consent </a>
                    <a href="#wpfresh5-gdpr3-tab" class="nav-tab"><i class="fa fa-circle-o-notch"></i> Terms And Conditions</a>
                    <a href="#wpfresh5-gdpr4-tab" class="nav-tab"><i class="fa fa-copyright"></i> Privacy Policy</a>
                    <a href="#wpfresh5-gdpr5-tab" class="nav-tab"><i class="fa fa-exchange"></i> Right to be forgotten</a>
                    <a href="#wpfresh5-gdpr6-tab" class="nav-tab"><i class="fa fa-file-archive-o"></i> Data Access</a>
                    <a href="#wpfresh5-gdpr7-tab" class="nav-tab"><i class="fa fa-fire"></i> Data Breach</a>
                    <a href="#wpfresh5-gdpr8-tab" class="nav-tab"><i class="fa fa-history"></i> Data Rectification</a>
                    <a href="#wpfresh5-gdpr10-tab" class="nav-tab"><i class="fa fa-envelope"></i> Unsubscribe</a>
                    <a href="#wpfresh5-gdpr11-tab" class="nav-tab"><i class="fa fa-envelope-open-o"></i> Contact Forms</a>
                </h2>
                <div style="clear:both"></div>
                <div id="wpfresh5-gdpr2-tab" class="tab-inner"><?php wpfresh5_settings_gdpr_tab_cookie( $settings );?></div>
                <div id="wpfresh5-gdpr3-tab" class="tab-inner"><?php wpfresh5_settings_gdpr_tab_terms( $settings );?></div>
                <div id="wpfresh5-gdpr4-tab" class="tab-inner"><?php wpfresh5_settings_gdpr_tab_privacy( $settings );?></div>
                <div id="wpfresh5-gdpr5-tab" class="tab-inner"><?php wpfresh5_settings_gdpr_tab_forgotten( $settings );?></div>
                <div id="wpfresh5-gdpr6-tab" class="tab-inner"><?php wpfresh5_settings_gdpr_tab_dataaccess( $settings );?></div>
                <div id="wpfresh5-gdpr7-tab" class="tab-inner"><?php wpfresh5_settings_gdpr_tab_databreach( $settings );?></div>
                <div id="wpfresh5-gdpr8-tab" class="tab-inner"><?php wpfresh5_settings_gdpr_tab_datarectification( $settings );?></div>
                <div id="wpfresh5-gdpr9-tab" class="tab-inner"><?php wpfresh5_settings_gdpr_tab_euvisitors( $settings );?></div>
                <div id="wpfresh5-gdpr10-tab" class="tab-inner"><?php wpfresh5_settings_gdpr_tab_unsubscribe( $settings );?></div>
                <div id="wpfresh5-gdpr11-tab" class="tab-inner"><?php wpfresh5_settings_gdpr_tab_cforms( $settings );?></div>
                <div id="wpfresh5-gdpr1-tab" class="tab-inner" style="display:block"><?php wpfresh5_settings_gdpr_tab_general( $settings );?></div>
            </div>
            <?php wpfresh5_field_hidden( 'wpfresh5_save_settings', 1 ); ?>
            <br/>
            <br/>
            <input type="submit" class="button button-primary" value="Save All Changes" />
        </form>
    <?php
}

function wpfresh5_save_settings_gdpr() {
	return true;
}

function wpfresh5_settings_gdpr_tab_general()
{
	require_once("introduction.php");
}
function wpfresh5_settings_gdpr_tab_cookie()
{
	// require_once("gdprfunction.php");
	require_once("cookie.php");
}
function wpfresh5_settings_gdpr_tab_terms()
{
	require_once("tandc.php");
}
function wpfresh5_settings_gdpr_tab_privacy()
{
	require_once("privacypolicy.php");
}
function wpfresh5_settings_gdpr_tab_forgotten()
{
	require_once("rightforgot.php");
}
function wpfresh5_settings_gdpr_tab_dataaccess()
{
	require_once("dataaccess.php");
}
function wpfresh5_settings_gdpr_tab_databreach()
{
	require_once("databreach.php");
}

function wpfresh5_settings_gdpr_tab_datarectification()
{
	require_once("datarectification.php");
}

function wpfresh5_settings_gdpr_tab_euvisitors()
{
	require_once("eu.php");
}
function wpfresh5_settings_gdpr_tab_unsubscribe()
{
	require_once("pro.php");
}

function wpfresh5_settings_gdpr_tab_cforms() {
  $pref='wpf5gdpr-';
  if( isset( $_POST['wpfresh5_save_gdprchecks'] ) ) {
    $check_comments = isset( $_POST['wpfresh5_gdpr_checkcomm'] ) ? intval ( $_POST['wpfresh5_gdpr_checkcomm'] ) : 0;
    update_option( $pref.'check-comments', $check_comments );

    $check_cf7 = isset( $_POST['wpfresh5_gdpr_checkcf7'] ) ? intval ( $_POST['wpfresh5_gdpr_checkcf7'] ) : 0;
    update_option( $pref.'check-cf7', $check_cf7 );
  }
  $comms = get_option( $pref.'check-comments' );
  $wpcf7 = get_option( $pref.'check-cf7' );
?>
<h3>Contact Forms - <a href="https://wpfreshstart5.com/support/knowledgebase/contact-forms/" target="_blank" style="font-size:0.8em">Support</a></h3>
<p>Adds a checkbox to any contact or comment forms on your website to comply with GDPR laws</p>
<label for="wpfresh5_gdpr_checkcomm"><input type="checkbox" name="wpfresh5_gdpr_checkcomm" id="wpfresh5_gdpr_checkcomm" value="1" <?php checked( $comms ); ?>> Add checkbox to comment forms </label><br/><br/>
<label for="wpfresh5_gdpr_checkcf7"><input type="checkbox" name="wpfresh5_gdpr_checkcf7" id="wpfresh5_gdpr_checkcf7" value="1" <?php checked( $wpcf7 ); ?>> Add checkbox to WPCF7 forms </label>
<input type="hidden" value="1" name="wpfresh5_save_gdprchecks">
<?php
}

function wpfs5_gdpr_install()
{//install settings
	$pref='wpf5gdpr-';
	//User request table create
	global $wpdb;
$charset_collate = '';
  if (!empty($wpdb->charset))
  {
  $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
  }
 if (!empty($wpdb->collate))
 {
        $charset_collate .= " COLLATE {$wpdb->collate}";
 }
 $table_name = $wpdb->prefix . 'wpfresh5_gdpr_requests';


	 $sql = "CREATE TABLE IF NOT EXISTS {$table_name} (" .

			"`id` bigint(11) NOT NULL AUTO_INCREMENT,".
             "`user` varchar(255) NOT NULL,".
			 "`email` varchar(255) NOT NULL,".
			 "`login` varchar(255) NOT NULL,".
			 "`type` varchar(255) NOT NULL,".
              "`ip` varchar(255) NOT NULL,".
               "`value` varchar(255) NOT NULL,".
			   "`updatedvalue` varchar(255) NOT NULL,".
			   "`recorded` varchar(255) NOT NULL,".
			   "`action` varchar(255) NOT NULL,".
			   "`actiontime` varchar(255) NOT NULL,".
                  "PRIMARY KEY (`id`)".
                     ") {$charset_collate} ENGINE=InnoDB;";
                   $wpdb->query($sql);
    //pro subscription table


	//cookie settings
add_option($pref.'show','n');
add_option($pref.'notice','');
add_option($pref.'cookie-style','');
add_option($pref.'cookie-text-color','#000000');
add_option($pref.'cookie-bg-color','#e6ffff');
add_option($pref.'cookie-distance','0px');
add_option($pref.'cookie-position','bottom:0px;right:0px');
add_option($pref.'cookie-block','1');
add_option($pref.'cookie-accept-button','OK');
add_option($pref.'cookie-eu','0');
   //tandc settings
   add_option($pref.'tandc-version','1.0');
   add_option($pref.'tandc-lusr','0');
   add_option($pref.'tandc-nlusr','0');
   add_option($pref.'tandc-bef','0');
   add_option($pref.'tandc-aft','h');
   add_option($pref.'tandc-eu','0');
   //Privacy Policy settings
   add_option($pref.'pp-version','1.0');
   add_option($pref.'pp-lusr','0');
   add_option($pref.'pp-nlusr','0');
   add_option($pref.'pp-bef','0');
   add_option($pref.'pp-aft','h');
   add_option($pref.'pp-eu','0');
   //data to be forgotten
   $current_user = wp_get_current_user();
   $currentemail=$current_user->user_email;
   add_option($pref.'rtbf-email',$currentemail);
   add_option($pref.'rtbf-title','Your data has been forgotten');
   add_option($pref.'rtbf-message','');
   add_option($pref.'rtbf-reassign','0');
   add_option($pref.'rtbf-reassign-user',get_current_user_id());
   //data access request
   $current_user = wp_get_current_user();
   $currentemail=$current_user->user_email;
   add_option($pref.'da-email',$currentemail);
   add_option($pref.'da-title','Your data');
   add_option($pref.'da-message','');
   //data breach request
   $current_user = wp_get_current_user();
   $currentemail=$current_user->user_email;
   add_option($pref.'dbr-email',$currentemail);
   add_option($pref.'dbr-title','Data Breach Information');
   add_option($pref.'dbr-message','');
   add_option($pref.'dbr-user','1');
    //data rectification request
   $current_user = wp_get_current_user();
   $currentemail=$current_user->user_email;
   add_option($pref.'drr-email',$currentemail);
   add_option($pref.'drr-title','Data rectification notice');
   add_option($pref.'drr-message','');
   //gdpr EU
   add_option($pref.'eu-active','0');
   add_option($pref.'eu-redirect','');
   add_option($pref.'eu-people','0');
   //gdpr pro unsubscribe
   add_option($pref.'uns-notification','');
   add_option($pref.'uns-emailbody','');
   add_option($pref.'uns-email',$currentemail);
   //plugin version
   if(get_option($pref.'plugin_version'))
   {
	   update_option($pref.'plugin_version','2.0');
   }
   else
   {
	   add_option($pref.'plugin_version','2.0');
   }

}
function wpfs5_gdpr_uninstall()
{
	/*
	//uninstall settings
	$pref='wpf5gdpr-';
	//user request table create

	//cookie settings
delete_option($pref.'show');
delete_option($pref.'notice');
delete_option($pref.'cookie-style');
delete_option($pref.'cookie-text-color');
delete_option($pref.'cookie-bg-color');
delete_option($pref.'cookie-position');
delete_option($pref.'cookie-distance');
delete_option($pref.'cookie-block');
delete_option($pref.'cookie-accept-button');
delete_option($pref.'cookie-eu');
   //tandc settings
   delete_option($pref.'tandc-version');
   delete_option($pref.'tandc-lusr');
   delete_option($pref.'tandc-nlusr');
   delete_option($pref.'tandc-bef');
   delete_option($pref.'tandc-aft');
   delete_option($pref.'tandc-eu');
   //privacy policy settings
   delete_option($pref.'pp-version');
   delete_option($pref.'pp-lusr');
   delete_option($pref.'pp-nlusr');
   delete_option($pref.'pp-bef');
   delete_option($pref.'pp-aft');
   delete_option($pref.'pp-eu');
   //data to be forgotten
   delete_option($pref.'rtbf-email');
   delete_option($pref.'rtbf-title');
   delete_option($pref.'rtbf-message');
   delete_option($pref.'rtbf-reassign');
   delete_option($pref.'rtbf-reassign-user');
   //data access request
   delete_option($pref.'da-email');
   delete_option($pref.'da-title');
   delete_option($pref.'da-message');
   //data branch request
   delete_option($pref.'dbr-email');
   delete_option($pref.'dbr-title');
   delete_option($pref.'dbr-message');
   //data rectification request
   delete_option($pref.'drr-email');
   delete_option($pref.'drr-title');
   delete_option($pref.'drr-message');
   //gdpr EU
   delete_option($pref.'eu-active');
   delete_option($pref.'eu-redirect');
   //gdpr unsubscribe
   delete_option($pref.'uns-notification');
   delete_option($pref.'uns-emailbody');
   delete_option($pref.'uns-email');
   */
}

//-------------------------------------------------------------
/*function active_gdrp_box()
{//Show box to user if its activated
require_once("gdprfunction.php");
	$pref='wpf5gdpr-';
	if(get_option($pref.'show')=='n')
	{

	}
	else if(get_option($pref.'show')=='y')
	{
		show_gdpr_box();

	}
	else if(get_option($pref.'show')=='h')
	{
		if(is_home())
		{
		show_gdpr_box();
		}

	}
	else if(wpfresh5_rmhttpurlandmatch(get_option($pref.'show'))==1)
	{
		show_gdpr_box();
	}

}
function show_gdpr_box()
{//display or not the box according to cookie
	$gdrpname="TEKGDRP";
	$pref='wpf5gdpr-';
	if(isset($_COOKIE[$gdrpname]))
	{

	}
	else
	{
		if(get_option($pref.'cookie-eu')=='1')
		{
			require_once("gdprfunction.php");
			if(wpfresh5x_is_eu_visitor()==1)
			{
		require_once("box.php");
			}
		}
		else
		{
		require_once("box.php");
		}
	}

}


add_action('wp_footer','active_gdrp_box');*/
//----Terms and Conditions-----
/*function show_gdpr_tandc_accept_button()
{//add accept button to terms and condition page
	require_once("gdprfunction.php");
	return wpfresh5_tandcacceptbutton();

}
add_shortcode('wpgdprTandC','show_gdpr_tandc_accept_button');
*/
function wpfresh5_add_gdpr_tandc_accept_cookie()
{//add accept button to terms and condition page
	require_once("gdprfunction.php");
	wpfresh5_gdprsettandccookie();
}



if(isset($_POST['gdprnlgtandc'])) {
  add_action('init', 'wpfresh5_add_gdpr_tandc_accept_cookie' );
}

function wpfresh5_tandcusermetaorcookiesetornot()
{//redirection to terms and condition page
require_once("gdprfunction.php");
$pref='wpf5gdpr-';
if(get_option($pref.'tandc-eu')=='1')
{
	if(wpfresh5_is_eu_visitor()==1)
	{
		if(wpfresh5_tandc_pp_pagecheck(get_option('wpf5gdprcomp-pp-bef'))!=1)
			{
	require_once("gdprfunction.php");
	$pref='wpf5gdpr-';
	if(wpfresh5_rmhttpurlandmatch(get_option($pref.'tandc-bef'))!=1)
	   wpfresh5_gdpr_check_tandc_cookie_or_usermeta();
			}
	}
}
else
{
if(wpfresh5_tandc_pp_pagecheck(get_option('wpf5gdprcomp-pp-bef'))!=1)
	{
	require_once("gdprfunction.php");
	$pref='wpf5gdpr-';
	if(wpfresh5_rmhttpurlandmatch(get_option($pref.'tandc-bef'))!=1)
	wpfresh5_gdpr_check_tandc_cookie_or_usermeta();
	}
}

}

add_action('wp_head','wpfresh5_tandcusermetaorcookiesetornot');

//-----Privacy Policy---------
/*function show_gdpr_pp_accept_button()
{//add accept button to Privacy Policy page
	require_once("gdprfunction.php");
	return wpfresh5_ppacceptbutton();

}
add_shortcode('wpgdprPP','show_gdpr_pp_accept_button');
function add_gdpr_pp_accept_cookie()
{//add accept button to terms and condition page


	require_once("gdprfunction.php");
	wpfresh5_gdprsetppcookie();


}
*/


if(isset($_POST['gdprnlgpp'])){
add_action('init','add_gdpr_pp_accept_cookie');}
function wpfresh5_ppusermetaorcookiesetornot()
{//redirection to privacy policy page
 require_once("gdprfunction.php");
 $pref='wpf5gdpr-';
  if(get_option($pref.'pp-eu')=='1')
  {
	  require_once("gdprfunction.php");
	  if(wpfresh5_is_eu_visitor()==1)
	  {
		  if(wpfresh5_tandc_pp_pagecheck(get_option('wpf5gdprcomp-tandc-bef'))!=1)
	{
	require_once("gdprfunction.php");
	$pref='wpf5gdpr-';
	if(wpfresh5_rmhttpurlandmatch(get_option($pref.'pp-bef'))!=1)
	wpfresh5_gdpr_check_pp_cookie_or_usermeta();
	}
	  }
  }
  else
  {
    if(wpfresh5_tandc_pp_pagecheck(get_option('wpf5gdprcomp-tandc-bef'))!=1)
	{
	require_once("gdprfunction.php");
	$pref='wpf5gdpr-';
	if(wpfresh5_rmhttpurlandmatch(get_option($pref.'pp-bef'))!=1)
	wpfresh5_gdpr_check_pp_cookie_or_usermeta();
	}
  }

}

add_action('wp_head','wpfresh5_ppusermetaorcookiesetornot');

//-----common page matching function for TandC and privacy policy

function  wpfresh5_tandc_pp_pagecheck($url)
{//cookie match url and show alert
if( get_the_ID()==$url )
{
	return 1;
}
else
{
	return 0;
}
}
//------------Public request form using shortcode------------
add_shortcode('WPFS5GDPR','wpfresh5_gdpr_form');
function wpfresh5_gdpr_form()
{
	require_once("gdprfunction.php");
	require_once("userform2.php");
	return wpfresh5_gdpr_user_req_form();
}


//----------------Set Session if not set-----------
function wpfresh5_session_start()
{//Set session if not isset
  if( !is_admin() && !session_id() )
  {
    session_start( [
        'read_and_close' => true,
    ] );
  }
}

add_action('init', 'wpfresh5_session_start', -1000);
//-----------Redirect EU People-------------------
function wpfresh5_gdprEUReedirect()
{
	$pref='wpf5gdpr-';
  $active = get_option($pref.'eu-active');
  // var_dump($active);die();
  if( !$active )
    return;
	$https = ((!empty($_SERVER['HTTPS'])) && ($_SERVER['HTTPS'] != 'off')) ? true : false;
	if( $https )
    $eupage= "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
  else
   $eupage= "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

	require_once("gdprfunction.php");
	if(/*(get_option($pref.'eu-active')=='1')&&(*/wpfresh5_is_eu_visitor() == 1 )
	{
		$peoplecount=(int)get_option($pref.'eu-people');
		$peoplecount++;
		update_option($pref.'eu-people',$peoplecount);
		if($eupage==get_option($pref.'eu-redirect'))
		{}
		else if((filter_var(get_option($pref.'eu-redirect'),FILTER_VALIDATE_URL)))
		{
			echo "<script>window.location='".get_option($pref.'eu-redirect')."'</script>";
		}
	}
}
add_action('wp_head', 'wpfresh5_gdprEUReedirect');

?>