<?php



if (!defined('ABSPATH')) {



  exit; // Exit if accessed directly.



}



// Load email template class

if (!class_exists('SensFRX_email_template')) {

	require_once plugin_dir_path( __FILE__ ) . 'email-templates/class-sensfrx-email-template.php';

}



class SensFRX_Registration_Function {



  public function registration_function($errors, $username, $user_email) {



		$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');



		$user_email = $user_email;



		$display_username = $username;



		global $wpdb;



		$sensfrx_approve = null;



		$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_new_account_fraud_allow = $sensfrx_policy_options['sensfrx_new_account_fraud'];



			if ($sensfrx_new_account_fraud_allow) {



				$sensfrx_new_account_fraud_allow = true;



			} else {



				$sensfrx_new_account_fraud_allow = false;



			}



			if ( true == $sensfrx_new_account_fraud_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_register = $obj->registerAttempt('register_succeeded', $device_id, array('email'=>$user_email,'name'=>$display_username,'phone'=>'','password'=>''));



					if (isset($res_register) && gettype(!empty($res_register)) !== 'string') {



						if (isset($res_register['status']) && $res_register['status'] === 'error' && $res_register['error'] === 'USE_CASE_DISABLED') {

							$errorMessage = $res_register['message'] ?? 'Something went wrong';

							$user_email_ato = $user_email;

							$insert_data = "Registration | " . $user_email_ato . " | Approved | Reason: " . $errorMessage;

							SensFRX_DB_Helper::insert_activity_log('New Account', $insert_data);

							return $errors;

					} else {



						$res_shadow = SensFRX_DB_Helper::safe_select(

							'sensfrx_shadow_activity',

							'sensfrx_shadow_status',

							'',

							array(),

							'',

							ARRAY_A

						);



						if (is_array($res_shadow) && !empty($res_shadow) && isset($res_shadow[0]['sensfrx_shadow_status'])) {



								if ( '0' == $res_shadow[0]['sensfrx_shadow_status'] ) {



									if (isset($res_register['status']) && null !== $res_register['status'] && isset($res_register['severity']) && null !== $res_register['severity']) {



										$status = $res_register['status'];



										$severity = $res_register['severity'];



										if($status == 'allow' || $status == 'challenge' || $status == 'low') {



											$sensfrx_approve = 'approved';



										} else {



											$sensfrx_approve = $status;



										}



										$site_title = get_bloginfo( 'name' ); 



										$email = $user_email;



										/* The below code is checking the value of the variable . If the value is truthy



										(evaluates to true), it sets  to true. Otherwise, it sets  to



										false. */



										$checkbox_allow = $sensfrx_policy_options['sensfrx_allow_register_email'];



										if ($checkbox_allow) {



											$checkbox_allow = true;



										} else {



											$checkbox_allow = false;



										}



										$checkbox_challenge = $sensfrx_policy_options['sensfrx_challenge_register_email'];



										if ($checkbox_challenge) {



											$checkbox_challenge = true;



										} else {



											$checkbox_challenge = false;



										}



										$checkbox_deny = $sensfrx_policy_options['sensfrx_deny_register_email'];



										if ($checkbox_deny) {



											$checkbox_deny = true;



										} else {



											$checkbox_deny = false;



										}



										if ('allow' == $status && 'low' == $severity) {



											$user_email_ato = $user_email;



											$insert_data = "Registration | " . $user_email_ato . " | " . ucfirst($sensfrx_approve) . " | Risk: " . $severity;

											



											if ($status == 'challenge') {



												$insert_data = "Registration | " . $user_email_ato . " | Pending review | Risk: " . $severity;



											}

											SensFRX_DB_Helper::insert_activity_log('New Account', $insert_data);



											return $errors;



										} else if ('allow' == $status && 'medium' == $severity) {	



											if (true == $checkbox_allow) {



												$device = $res_register['device'];



												$device = (array) $device;



												$to = $email;



												global $wpdb;



											// Get default email content (no database customization)

											$default_email = SensFRX_email_template::get_default_message('registration_allow');

											$reg_medium_subject = $default_email['subject'];

											$reg_medium_message = $default_email['message'];



											$date = date('Y M d');



											$time = date('H:i:s');



											$allowurl = site_url() . '?as_did_1337575=' . $device['device_id'] . '&asda_ac=1';



											$denyurl = site_url() . '?as_did_1337575=' . $device['device_id'] . '&asda_ac=0';



											$replacements = [



												'{$fullname}' => $to,



												'{$ip_address}' => $device['ip'],



										'{$location}' => $device['location'],



										'{$device_name}' => $device['name'],



										'{$time}' => $time,



										'{$date}' => $date,



										'{$default_brand_name}' => $site_title,



									];



									// Use universal email wrapper for professional formatting



									$mailResult = SensFRX_email_template::wrap_and_send( $to, $reg_medium_subject, $reg_medium_message, $site_title, $replacements );



												$user_email_ato = $user_email;



												$insert_data = "Registration | " . $user_email_ato . " | " . ucfirst($sensfrx_approve) . " | Risk: " . $severity;

												SensFRX_DB_Helper::insert_activity_log('New Account', $insert_data);



												return $errors;	



											} else {



												$user_email_ato = $user_email;



												$insert_data = "Registration | " . $user_email_ato . " | Approved (Allow policy off) | Risk: " . $severity;

												SensFRX_DB_Helper::insert_activity_log('New Account', $insert_data);



												return $errors;	



											}



										} else if ('challenge' == $status && 'high' == $severity) {



											if (true == $checkbox_challenge) {



												$device = $res_register['device'];



												$device = (array) $device;



												$to = $email;



											// Get default email content (no database customization)

											$default_email = SensFRX_email_template::get_default_message('registration_challenge');

											$reg_challenge_subject = $default_email['subject'];

											$reg_challenge_message = $default_email['message'];



											$date = date('Y M d');



											$time = date('H:i:s');



											$allowurl = site_url() . '?as_did_1337575=' . $device['device_id'] . '&asda_ac=1';



											$denyurl = site_url() . '?as_did_1337575=' . $device['device_id'] . '&asda_ac=0';



											$replacements = [



										'{$full_name}' => $to,



										'{$default_brand_name}' => $site_title,



										'{$date}' => $date,



										'{$time}' => $time,



									];



									// Use universal email wrapper for professional formatting



									$mailResult = SensFRX_email_template::wrap_and_send( $to, $reg_challenge_subject, $reg_challenge_message, $site_title, $replacements );



												$user_email_ato = $user_email;



												$insert_data = "Registration | " . $user_email_ato . " | " . ucfirst($sensfrx_approve) . " | Risk: " . $severity;



												if ($status == 'challenge') {



													$insert_data = "Registration | " . $user_email_ato . " | Pending review | Risk: " . $severity;



												}

												SensFRX_DB_Helper::insert_activity_log('New Account', $insert_data);



												$errors->add('registration_error', __('Sensfrx - Your registration is currently under manual review. It will be completed once approved by the administrator', 'woocommerce'));



												return $errors;		



											} else {



												$user_email_ato = $user_email;



												$insert_data = "Registration | " . $user_email_ato . " | Approved (Challenge policy off) | Risk: " . $severity;

												SensFRX_DB_Helper::insert_activity_log('New Account', $insert_data);



												return $errors;	



											}



										} else if ('deny' == $status && 'critical' == $severity) {



											if (true == $checkbox_deny) {



												$device = $res_register['device'];



												$device = (array) $device;



												$to = $email;



											// Get default email content (no database customization)

											$default_email = SensFRX_email_template::get_default_message('registration_deny');

											$login_deny_reg_subject = $default_email['subject'];

											$login_deny_reg_message = $default_email['message'];



											$date = date('Y M d');



											$time = date('H:i:s');



											$allowurl = site_url() . '?as_did_1337575=' . $device['device_id'] . '&asda_ac=1';



											$denyurl = site_url() . '?as_did_1337575=' . $device['device_id'] . '&asda_ac=0';



											$replacements = [



												'{$client_full_name}' => $to,



										'{$default_brand_name}' => $site_title,



										'{$date}' => $date,



										'{$time}' => $time,



									];



									// Use universal email wrapper for professional formatting



									$mailResult = SensFRX_email_template::wrap_and_send( $to, $login_deny_reg_subject, $login_deny_reg_message, $site_title, $replacements );



												$user_email_ato = $user_email;



												$insert_data = "Registration | " . $user_email_ato . " | " . ucfirst($sensfrx_approve) . " | Risk: " . $severity;

												SensFRX_DB_Helper::insert_activity_log('New Account', $insert_data);



												$message = 'Your account is blocked due to Suspicious Registration Activities.';



												$errors->add('registration_error', __('Sensfrx - Registration cannot be completed due to detected fraudulent activity.', 'woocommerce'));



												return $errors;



											} else {



												$user_email_ato = $user_email;



												$insert_data = "Registration | " . $user_email_ato . " | Approved (Deny policy off) | Risk: " . $severity;

												SensFRX_DB_Helper::insert_activity_log('New Account', $insert_data);



												return $errors;	



											}	



										} else {



											return $errors;



										}



									} else {



										$user_email_ato = $user_email;



										$insert_data = "Registration | " . $user_email_ato . " | Approved | Reason: API error fallback";

										SensFRX_DB_Helper::insert_activity_log('New Account', $insert_data);



										return $errors;



									}



								} else {



									$user_email_ato = $user_email;



									$insert_data = "Registration | " . $user_email_ato . " | Allowed (Shadow Mode) | Risk: " . $res_register['severity'];

									SensFRX_DB_Helper::insert_activity_log('New Account', $insert_data);



									return $errors;



								}



							} else {



								$user_email_ato = $user_email;



								$insert_data = "Registration | " . $user_email_ato . " | Error | Reason: Shadow Mode data fetch failed";

								SensFRX_DB_Helper::insert_activity_log('New Account', $insert_data);



								return $errors;



							}



						}



					} else {



						$user_email_ato = $user_email;



						$insert_data = "Registration | " . $user_email_ato . " | Error | Reason: Unformatted API response";

						SensFRX_DB_Helper::insert_activity_log('New Account', $insert_data);



						return $errors;



					}



				}



			} else {



				return $errors;



			}



		} else {



			return $errors;



		}



	}



}



