<?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_API_Check_Comment {



  function sensfrx_check_comment($commentdata) {



		$payload = array(



			'author'   => isset( $commentdata['comment_author'] ) ? sanitize_text_field( wp_unslash( $commentdata['comment_author'] ) ) : '',



			'email'    => isset( $commentdata['comment_author_email'] ) ? sanitize_text_field( wp_unslash( $commentdata['comment_author_email'] ) ) : '',



			'content'  => isset( $commentdata['comment_content'] ) ? sanitize_text_field( wp_unslash( $commentdata['comment_content'] ) ) : '',



			'post_id'  => isset( $commentdata['comment_post_ID'] ) ? sanitize_text_field( wp_unslash( $commentdata['comment_post_ID'] ) ) : '',



			'user_ip'  => isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '',



			'user_agent' => isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '',



		);



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



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



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



		} 



		global $wpdb;



		if ( is_user_logged_in() ) {



			$user_id = get_current_user_id();



		} else {



			$user_id = null;



		}



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



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



		$sensfrx_whitelisting_check_checkbox_allow = $sensfrx_policy_options['sensfrx_whitle_list_email'];



		if ($sensfrx_whitelisting_check_checkbox_allow) {



			$sensfrx_whitelisting_check_checkbox_allow = true;



		} else {



			$sensfrx_whitelisting_check_checkbox_allow = false;



		}



		// Administrator Access Approved 



		$sensfrx_whitelisting_check_email = null;



		if (isset($user_id) && !empty($user_id)) {



			$sensfrx_whitelisting_check_email = $user_id;



		}



		$sensfrx_whitelisting_check = null;



		$sensfrx_white_list_admin_users = get_users(array('role' => 'administrator'));



		$sensfrx_white_list_admin_info_array = array();



		foreach ($sensfrx_white_list_admin_users as $user_data) {



			if ($user_data->ID === $sensfrx_whitelisting_check_email) {



				$sensfrx_whitelisting_check = true;



				break;



			}



		}



		if(true === $sensfrx_whitelisting_check && true === $sensfrx_whitelisting_check_checkbox_allow) {



			return $commentdata;;



		} else {



			$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_bot_fraud_allow = $sensfrx_policy_options['sensfrx_bot_fraud'];



				if ($sensfrx_bot_fraud_allow) {



					$sensfrx_bot_fraud_allow = true;



				} else {



					$sensfrx_bot_fraud_allow = false;



				}



				if ( true == $sensfrx_bot_fraud_allow) {



					if (isset($user_id)) {



						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_bot = $obj->isBot($device_id, $user_id);



						}	



					} else {



						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_bot = $obj->isBot($device_id, '');



						}



					}



					$customer = WC()->customer;



					if ($customer) {



						$first_name = $customer->get_first_name(); 



						$last_name = $customer->get_last_name(); 



						$email = $customer->get_email();



					} else {



						$first_name = ''; 



						$last_name = ''; 



						$email = $commentdata['comment_author_email'];



					}



					if ($res_bot) {



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



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

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

								$user_email_ato = $email;

								$insert_data = "User " . $user_email_ato . " attempted a comment activity. The comment was approved,  Cause " . $errorMessage;

								SensFRX_DB_Helper::insert_activity_log('Bot Security - Comments', $insert_data);

								return $commentdata;

							} else {



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



									$status = $res_bot['status'];



									$severity = $res_bot['severity'];



									$site_title = get_bloginfo( 'name' );



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



										$sensfrx_approve = 'approved';



									} else {



										$sensfrx_approve = $status;



									}



									$obj = new SensFRX\SensFRX([



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



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



								]);



								$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 ( gettype(!empty($res_bot)) == 'string' ) {



												return $commentdata;;



											} else {



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



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



														$user_email_ato = $commentdata['comment_author_email'];



														$insert_data = $user_email_ato . " attempted suspicious comment activity. The comment was proceed due to a risk score level of " . $severity . ". Comment Post ID - ". $commentdata['comment_post_ID'];



														SensFRX_DB_Helper::insert_activity_log('Bot Security - Comments', $insert_data);



														return $commentdata;



													} 



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



														$device = $res_bot['device'];



														$device = (array) $device;



														$to = $commentdata['comment_author_email'];



														// Get default email content (no database customization)

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

														$trans_after_deny_subject = $default_email['subject'];

														$trans_after_deny_message = $default_email['message'];



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



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



														$replacements = [



															'{$fullname}' => $first_name . ' ' . $last_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, $trans_after_deny_subject, $trans_after_deny_message, $site_title, $replacements );



														global $wpdb;



														$user_email_ato = $commentdata['comment_author_email'];



														$insert_data = $user_email_ato . " attempted suspicious comment activity. The comment was proceed due to a risk score level of " . $severity . ". Comment Post ID - ". $commentdata['comment_post_ID'];



														SensFRX_DB_Helper::insert_activity_log('Bot Security - Comments', $insert_data);



														return $commentdata;



													} 



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



														$device = $res_bot['device'];



														$device = (array) $device;



														$to = $commentdata['comment_author_email'];



														// Get default email content (no database customization)

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

														$trans_after_deny_subject = $default_email['subject'];

														$trans_after_deny_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}' => $first_name . ' ' . $last_name,



															'{$time}' => $time,



															'{$date}' => $date,



															'{$allow_url}' => $allowurl,



															'{$deny_url}' => $denyurl,



															'{$default_brand_name}' => $site_title,



														];



														// Use universal email wrapper for professional formatting



														$mailResult = SensFRX_email_template::wrap_and_send( $to, $trans_after_deny_subject, $trans_after_deny_message, $site_title, $replacements );



														$user_email_ato = $commentdata['comment_author_email'];



														$insert_data = $user_email_ato . " attempted suspicious comment activity. The comment was marked as pending due to ". $severity ." severity. Comment Post ID - ". $commentdata['comment_post_ID'];



														SensFRX_DB_Helper::insert_activity_log('Bot Security - Comments', $insert_data);



														$commentdata['comment_approved'] = 0;



														wp_die('Sensfrx - We have detected unusual activity with your recent comment, and as a precautionary measure, it has set as Pending moderation.');



														return $commentdata;



													}



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



														$device = $res_bot['device'];



														$device = (array) $device;



														$to = $commentdata['comment_author_email'];



														// Get default email content (no database customization)

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

														$trans_after_deny_subject = $default_email['subject'];

														$trans_after_deny_message = $default_email['message'];



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



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



														$replacements = [



															'{$client_full_name}' => $first_name . ' ' . $last_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, $trans_after_deny_subject, $trans_after_deny_message, $site_title, $replacements );



														$user_email_ato = $commentdata['comment_author_email'];



														$insert_data = $user_email_ato . " attempted suspicious comment activity. The comment activity was ". $sensfrx_approve ." due to risk score level of ". $severity .". Comment Post ID - ". $commentdata['comment_post_ID'];



														SensFRX_DB_Helper::insert_activity_log('Bot Security - Comments', $insert_data);



														$current_user = wp_get_current_user();



														$results = retrieve_password($current_user->user_login);									



														if ( true === $results ) {



															$message = 'Your account is blocked due to Suspicious Bot Activities. We have sent a password reset email, please reset your password to activate your account again.';



														} else {



															$message = $results->get_error_message();



														}



														wp_die('Sensfrx - We have detected unusual activity with your recent comment, and as a precautionary measure, it has been blocked.');



														return $commentdata;



													} 



													return $commentdata;



												} else {

													return $commentdata;

												}	



											}



										} else {



											$user_email_ato = $email;



											$insert_data = "User " . $user_email_ato . " attempted comment activity. Sensfrx flagged the log in with a " . $severity . " risk score. but since Shadow Mode was enabled, it was allowed.";



											SensFRX_DB_Helper::insert_activity_log('Bot Security - Comments', $insert_data);



											return $commentdata;



										}



									} else {



										$user_email_ato = $email;



										$insert_data = "User " . $user_email_ato . " attempted a comment activity. An error occurred while fetching Shadow Mode data.";



										SensFRX_DB_Helper::insert_activity_log('Bot Security - Comments', $insert_data);



										return $commentdata;



									}



								} else {

									return $commentdata;

								}

							}



						} else {

							return $commentdata;

						}



					} else {

						return $commentdata;

					}



				} else {



					return $commentdata;



				}



			} else {



				return $commentdata;



			}



		}



	}



}



