<?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 SesnFRX_Profile_Update

{



    public function user_profile_updates($user)

    {



        // Old Data



        $user_id = get_current_user_id();



        $user_data = get_userdata($user_id);



        // Access user information



        $username = $user_data->user_login;



        $email = $user_data->user_email;



        $display_name = $user_data->display_name;



        $first_name = $user_data->first_name;



        $last_name = $user_data->last_name;



        // Get the new information from the form data.



        $new_first_name = ' ';



        $new_last_name = ' ';



        $new_display_name = ' ';



        $new_password = ' ';



        $device_id = ' ';



        $new_email = ' ';



        if (isset($_POST['sensfrx_profile_up_nonce']) && wp_verify_nonce(sanitize_text_field($_POST['sensfrx_profile_up_nonce']), 'sensfrx_profile_up_action')) {



            $new_first_name = isset($_POST['account_first_name']) ? sanitize_text_field($_POST['account_first_name']) : sanitize_text_field('');



            $new_last_name = isset($_POST['account_last_name']) ? sanitize_text_field($_POST['account_last_name']) : sanitize_text_field('');



            $new_display_name = isset($_POST['account_display_name']) ? sanitize_text_field($_POST['account_display_name']) : sanitize_text_field('');



            $new_password = '';



            if (isset($_POST['password_1']) && strlen(sanitize_text_field($_POST['password_1'])) > 0) {



                $new_password = sanitize_text_field($_POST['password_1']);



            }



            if ($new_password) {



                $new_password = '1';



            } else {



                $new_password = '0';



            }



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



                $new_email = sanitize_email($_POST['account_email']);



            } else {



                $new_email = null;



            }



            $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 = [];



        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'],



            ]);



        }



        $json_data = [



            'name'     => [



                'from' => $first_name . ' ' . $last_name,



                'to'   => $new_first_name . ' ' . $new_last_name,



            ],



            'username' => [



                'from' => null,



                'to'   => null,



            ],



            'email'    => [



                'from' => $email,



                'to'   => $new_email,



            ],



            'phone'    => [



                'from' => null,



                'to'   => null,



            ],



            'password' => [



                'changed' => $new_password,



            ],



        ];



        global $wpdb;



        $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) {



            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'],



                ]);



                global $wpdb;



                $sensfrx_approve = null;



                $update_profile = $obj->updateAttempt('profile_update_succeeded', $user_id, $device_id, $json_data);



                // Administrator Access Approved



                $sensfrx_whitelisting_check = null;



                $sensfrx_whitelisting_check_email = $email;



                $sensfrx_white_list_admin_users = get_users(['role' => 'administrator']);



                $sensfrx_white_list_admin_info_array = [];



                foreach ($sensfrx_white_list_admin_users as $user_data) {



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



                        $sensfrx_whitelisting_check = true;



                        break;



                    }



                }



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



                }



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



                    return;



                } else {



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



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

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

                            $insert_data  = "Profile Update | " . $email . " | Approved | Reason: " . $errorMessage;

                            SensFRX_DB_Helper::insert_activity_log('Profile Update', $insert_data);

                        } else {

                            $res_shadow = SensFRX_DB_Helper::safe_select(

                                'sensfrx_shadow_activity',

                                'sensfrx_shadow_status',

                                '',

                                [],

                                '',

                                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($update_profile['status']) && null !== $update_profile['status'] && isset($update_profile['severity']) && null !== $update_profile['severity']) {



                                        $status = $update_profile['status'];



                                        $severity = $update_profile['severity'];



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



                                            $sensfrx_approve = 'approved';



                                        } else {



                                            $sensfrx_approve = $status;



                                        }



                                        $insert_data = "Profile Update | " . $email . " | " . ucfirst($sensfrx_approve) . " | Risk: " . $severity;

                                        SensFRX_DB_Helper::insert_activity_log('Profile Update', $insert_data);



                                        $site_title = get_bloginfo('name');



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



                                            $device = $update_profile['device'];



                                            $device = (array) $device;



                                            $to = $email;



                                            // Get default email content (no database customization)

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

                                            $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 = [



                                                '{$client_full_name}'   => $first_name,



                                                '{$time}'               => $time,



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



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



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



                                                '{$date}'               => $date,



                                                '{$default_brand_name}' => $site_title,



                                            ];



                                            $subject = strtr($trans_after_deny_subject, $replacements);



                                            // 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);



                                        }



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



                                            $lost_pass_url = wp_lostpassword_url();



                                            $device = $update_profile['device'];



                                            $device = (array) $device;



                                            $to = $email;



                                            // Get default email content (no database customization)

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

                                            $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 = [



                                                '{$client_full_name}'   => $first_name,



                                                '{$time}'               => $time,



                                                '{$date}'               => $date,



                                                '{$allow_url}'          => $allowurl,



                                                '{$deny_url}'           => $denyurl,



                                                '{$default_brand_name}' => $site_title,



                                            ];



                                            $subject = strtr($trans_after_deny_subject, $replacements);



                                            // 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);



                                        }



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



                                            $device = $update_profile['device'];



                                            $device = (array) $device;



                                            $to = $email;



                                            // Get default email content (no database customization)

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

                                            $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 = [



                                                '{$client_full_name}'   => $first_name,



                                                '{$time}'               => $time,



                                                '{$date}'               => $date,



                                                '{$default_brand_name}' => $site_title,



                                            ];



                                            $subject = strtr($trans_after_deny_subject, $replacements);



                                            // 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);



                                            sleep(3);



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



                                            if (true === $results) {



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



                                            } else {



                                                $message = $results->get_error_message();



                                            }



                                            $login_url = site_url('wp-login.php?action=as_ch_dn&as_msg=' . urlencode($message), 'login');



                                            wp_logout();



                                            wp_safe_redirect($login_url);



                                            exit;



                                        }



                                    } else {



                                        $insert_data = "Profile Update | " . $email . " | Approved | Reason: API error fallback";

                                        SensFRX_DB_Helper::insert_activity_log('Profile Update', $insert_data);



                                    }



                                } else {



                                    $insert_data = "Profile Update | " . $email . " | Allowed (Shadow Mode) | Risk: " . $update_profile['severity'];

                                    SensFRX_DB_Helper::insert_activity_log('Profile Update', $insert_data);



                                }



                            } else {



                                $insert_data = "Profile Update | " . $email . " | Error | Reason: Shadow Mode data fetch failed";

                                SensFRX_DB_Helper::insert_activity_log('Profile Update', $insert_data);



                            }

                        }



                    } else {



                        $user_email_ato = $email;



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

                        SensFRX_DB_Helper::insert_activity_log('Profile Update', $insert_data);



                    }



                }



            }



        }



    }



}

