<?php

if (!defined('ABSPATH')) {

  exit; // Exit if accessed directly.

}



class SensFRX_Password_Reset_Track {

  public function password_reset_track_woo(WP_User $user, $new_pass) {

		$device_id = '';

		if (isset($_POST['device_id'])) {

			if (!isset($_POST['sensfrx_password_profile_update_nonce']) || !wp_verify_nonce(sanitize_text_field($_POST['sensfrx_password_profile_update_nonce']), 'sensfrx_password_profile_update_action')) {

				$device_id = sanitize_text_field($_POST['device_id']);

			} elseif (!isset($_POST['woocommerce-reset-password-nonce']) || !wp_verify_nonce(sanitize_text_field($_POST['woocommerce-reset-password-nonce']), 'woocommerce-reset-password-action')) {

				$device_id = sanitize_text_field($_POST['device_id']);

			} else {

				$device_id = null;

			}

		}

		if (empty($device_id)) {

			$device_id = null;

			$device_id_1 = null;

			$device_id_2 = null;

			$device_id_3 = null;

			$device_id_4 = null;

			$res_bot = null;

			if (isset($_COOKIE['device0'])) {   

				$device_id_1 = sanitize_text_field($_COOKIE['device0']);

			} 

			if (isset($_COOKIE['device1'])) {   

				$device_id_2 = sanitize_text_field($_COOKIE['device1']);

			}  

			if (isset($_COOKIE['device2'])) {   

				$device_id_3 = sanitize_text_field($_COOKIE['device2']);

			}  

			if (isset($_COOKIE['device3'])) {   

				$device_id_4 = sanitize_text_field($_COOKIE['device3']);

			}   

			$device_id = $device_id_1 . $device_id_2 . $device_id_3 . $device_id_4;

			if ( empty($device_id) ) {

				$device_id = null;

			} 

		}

		$options = get_option('sensfrx_options', SensFRX::default_options());

		$sensfrx_policy_options = get_option('sensfrx_policy_options', SensFRX::default_policy_options());

		require_once(SENSFRX_DIR . 'sensfrx-php-sdk/SensFRX/autoload.php');

		$obj = new SensFRX\SensFRX([

		'property_id' => $options['sensfrx_property_id'],

		'property_secret' => $options['sensfrx_property_secret']

		]);

		$sensfrx_master_fraud_allow = $sensfrx_policy_options['sensfrx_master_fraud'];

		if ($sensfrx_master_fraud_allow) {

			$sensfrx_master_fraud_allow = true;

		} else {

			$sensfrx_master_fraud_allow = false;

		}

		if ( true == $sensfrx_master_fraud_allow) {

			$sensfrx_reset_password_allow = $sensfrx_policy_options['sensfrx_reset_password_fraud'];

			if ($sensfrx_reset_password_allow) {

				$sensfrx_reset_password_allow = true;

			} else {

				$sensfrx_reset_password_allow = false;

			}

			if ( true == $sensfrx_reset_password_allow) {

				if (isset($options['sensfrx_property_id']) && isset($options['sensfrx_property_secret'])) {

					$obj = new SensFRX\SensFRX([

						'property_id' => $options['sensfrx_property_id'],

						'property_secret' => $options['sensfrx_property_secret']

					]);

					$res = $obj->passwordResetAttempt('reset_password_succeeded', $user->data->ID, $device_id, array('email'=>$user->data->user_email,'username'=>$user->data->user_login));

					$user_email_ato = $user->data->user_email;

					$insert_data = "User " . $user_email_ato . " attempted to reset password.";

					SensFRX_DB_Helper::insert_activity_log('Password Reset', $insert_data);

				}

			}

		} 

	}

}

