'sanitize_text_field', ] ); // Register full_name setting register_setting( ABMSENSE_PREFIX . 'options', ABMSENSE_PREFIX . 'full_name', [ 'sanitize_callback' => 'sanitize_text_field', ] ); // Register report_email setting register_setting( ABMSENSE_PREFIX . 'options', ABMSENSE_PREFIX . 'report_email', [ 'sanitize_callback' => 'abmsense_sanitize_email', ] ); // Register report_frequency setting register_setting( ABMSENSE_PREFIX . 'options', ABMSENSE_PREFIX . 'report_frequency', [ 'sanitize_callback' => 'abmsense_sanitize_frequency', ] ); // Register display_consent_message setting register_setting( ABMSENSE_PREFIX . 'options', ABMSENSE_PREFIX . 'display_consent_message', [ 'sanitize_callback' => 'abmsense_sanitize_checkbox', ] ); // Register show_table_menu setting register_setting( ABMSENSE_PREFIX . 'options', ABMSENSE_PREFIX . 'show_table_menu', [ 'sanitize_callback' => 'abmsense_sanitize_checkbox', ] ); } // Sanitize email addresses function abmsense_sanitize_email($input) { // For multiple emails $emails = explode(',', $input); $sanitized_emails = array_map('sanitize_email', $emails); return implode(',', array_filter($sanitized_emails)); } // Sanitize report frequency function abmsense_sanitize_frequency($input) { $allowed_values = ['daily', 'weekly', 'monthly']; return in_array($input, $allowed_values) ? $input : 'weekly'; } // Sanitize checkbox values function abmsense_sanitize_checkbox($input) { return (isset($input) && true == $input) ? '1' : '0'; } // Add this function to utility.php function abmsense_get_api_request_args($body) { // Try to locate a valid CA certificate bundle $ca_bundle_paths = array( plugin_dir_path(__FILE__) . 'cacert.pem', // Plugin's own CA bundle ABSPATH . WPINC . '/certificates/ca-bundle.crt', // WordPress bundle '/etc/ssl/certs/ca-certificates.crt', // Debian/Ubuntu '/etc/pki/tls/certs/ca-bundle.crt', // RHEL/CentOS '/etc/ssl/ca-bundle.pem', // OpenSUSE '/usr/local/share/certs/ca-root-nss.crt' // FreeBSD ); $ca_bundle = null; foreach ($ca_bundle_paths as $path) { if (file_exists($path)) { $ca_bundle = $path; break; } } return array( 'method' => 'POST', 'headers' => array('Content-Type' => 'application/json'), 'body' => $body, 'timeout' => 30, 'sslverify' => true, 'sslcertificates' => $ca_bundle, // Use the best CA bundle available ); } // Update the save_data function in settings.php function abmsense_save_data( $server_name, $report_email, $report_frequency, $is_consent, $date_registered, $version_installed, $full_name = '', $consent_update = false, $sku = '', $real_time_alert = '' ) { $config = include('db_config.php'); $api_url = $config['upsert_abmsense_our_customers_details']; $public_key = $config['public_key']; $data = [ 'customer_name' => $server_name, 'report_email' => $report_email, 'report_frequency' => $report_frequency, 'is_consent' => $is_consent, 'date_registered' => $date_registered, 'version_installed' => $version_installed, 'full_name' => $full_name, 'consent_update' => $consent_update, 'sku' => $sku, 'real_time_alert' => $real_time_alert ]; $encrypted_data = abmsense_encrypt_data($data, $public_key); if (!$encrypted_data) { abmsense_log('Failed to encrypt data', 'error'); return false; } $args = abmsense_get_api_request_args(wp_json_encode($encrypted_data)); $response = abmsense_safe_api_request($api_url, $args); if (is_wp_error($response)) { abmsense_log('API Error: ' . $response->get_error_message(), 'error'); return false; } $response_code = wp_remote_retrieve_response_code($response); $response_body = wp_remote_retrieve_body($response); $response_data = json_decode($response_body, true); if ($response_code !== 200 || !isset($response_data['success']) || !$response_data['success']) { abmsense_log('API Error: Unexpected response - ' . $response_body, 'error'); return false; } // Get the CID using get_customer_cid endpoint $cid_data = [ 'customer_name' => $server_name ]; $encrypted_cid_data = abmsense_encrypt_data($cid_data, $public_key); if (!$encrypted_cid_data) { abmsense_log('Failed to encrypt CID request data', 'error'); return false; } $cid_args = abmsense_get_api_request_args(wp_json_encode($encrypted_cid_data)); $cid_response = abmsense_safe_api_request($config['get_customer_cid'], $cid_args); if (is_wp_error($cid_response)) { abmsense_log('API Error: Failed to get CID - ' . $cid_response->get_error_message(), 'error'); return false; } $cid_response_code = wp_remote_retrieve_response_code($cid_response); $cid_response_body = wp_remote_retrieve_body($cid_response); $cid_response_data = json_decode($cid_response_body, true); if ($cid_response_code !== 200 || !isset($cid_response_data['success']) || !$cid_response_data['success']) { abmsense_log('API Error: Failed to get CID - ' . $cid_response_body, 'error'); return false; } if (!isset($cid_response_data['data']['cid'])) { abmsense_log('API Error: Missing CID in get_customer_cid response - ' . $cid_response_body, 'error'); return false; } $cid = $cid_response_data['data']['cid']; // Now create the user using add_abmsense_user endpoint $user_data = [ 'customer_name' => $server_name, 'full_name' => $full_name, 'email' => $report_email, 'real_time_alert' => 'No', 'scheduled_report' => $report_frequency ]; $encrypted_user_data = abmsense_encrypt_data($user_data, $public_key); if (!$encrypted_user_data) { abmsense_log('Failed to encrypt user data', 'error'); return false; } $user_args = abmsense_get_api_request_args(wp_json_encode($encrypted_user_data)); $user_response = abmsense_safe_api_request($config['add_abmsense_user'], $user_args); if (is_wp_error($user_response)) { abmsense_log('API Error: ' . $user_response->get_error_message(), 'error'); return false; } $user_response_code = wp_remote_retrieve_response_code($user_response); $user_response_body = wp_remote_retrieve_body($user_response); $user_response_data = json_decode($user_response_body, true); if ($user_response_code !== 200 || !isset($user_response_data['success']) || !$user_response_data['success']) { // Check for "Email already exists" and treat as success if (strpos($user_response_body, 'Email already exists') !== false) { return true; } abmsense_log('API Error: Failed to create user - ' . $user_response_body . ' - Maybe Customer is reactivated', 'error'); return false; } // Save to WordPress options update_option(ABMSENSE_PREFIX . 'server_name', $server_name); update_option(ABMSENSE_PREFIX . 'report_email', $report_email); update_option(ABMSENSE_PREFIX . 'report_frequency', $report_frequency); update_option(ABMSENSE_PREFIX . 'is_consent', $is_consent); update_option(ABMSENSE_PREFIX . 'date_registered', $date_registered); update_option(ABMSENSE_PREFIX . 'version_installed', $version_installed); update_option(ABMSENSE_PREFIX . 'cid', $cid); // Set a transient to indicate successful registration set_transient(ABMSENSE_PREFIX . 'registration_complete', true, HOUR_IN_SECONDS); if ($user_response_code === 200 && isset($user_response_data['success']) && $user_response_data['success']) { update_option(ABMSENSE_PREFIX . 'version_installed', $version_installed); } return true; } // Validate email addresses function abmsense_validate_emails($emails) { foreach (explode(',', $emails) as $email) { if (!filter_var(trim($email), FILTER_VALIDATE_EMAIL)) { return false; } } return true; } // Get the plugin version from the main file function abmsense_get_version() { global $wp_filesystem; if (empty($wp_filesystem)) { require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); } $file = plugin_dir_path(__FILE__) . 'abmsense.php'; $content = $wp_filesystem->get_contents($file); if ($content && preg_match('/Version:\s*([^\s]+)/', $content, $matches)) { return $matches[1]; } return 'Unknown'; } // Display the plugin wp-admin settings page function abmsense_settings_page() { $current_user = wp_get_current_user(); $admin_email = $current_user->user_email; $report_email = get_option(ABMSENSE_PREFIX . 'report_email', $admin_email); $report_frequency = get_option(ABMSENSE_PREFIX . 'report_frequency', 'weekly'); $customer_name = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; $version_installed = abmsense_get_version(); $date_registered = gmdate('Y-m-d'); // Get consent value from API $config = include('db_config.php'); $public_key = $config['public_key']; $data = [ 'customer_name' => $customer_name ]; $encrypted_data = abmsense_encrypt_data($data, $public_key); if ($encrypted_data) { $args = abmsense_get_api_request_args(wp_json_encode($encrypted_data)); $response = abmsense_safe_api_request($config['check_consent_enabled'], $args); if (!is_wp_error($response)) { $response_data = json_decode(wp_remote_retrieve_body($response), true); if (isset($response_data['success']) && $response_data['success'] && isset($response_data['is_consent_enabled'])) { $is_consent = $response_data['is_consent_enabled'] ? '1' : '0'; update_option(ABMSENSE_PREFIX . 'display_consent_message', $is_consent); } } } $is_consent = get_option(ABMSENSE_PREFIX . 'display_consent_message', '0'); if (isset($_POST['submit'])) { if (!isset($_POST[ABMSENSE_PREFIX . 'nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST[ABMSENSE_PREFIX . 'nonce'])), ABMSENSE_PREFIX . 'save_settings')) { echo "Nonce verification failed"; return; } $customer_name = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; $is_consent = isset($_POST[ABMSENSE_PREFIX . 'display_consent_message']) ? 1 : 0; // Get existing report email $report_email = get_option(ABMSENSE_PREFIX . 'report_email', ''); if (empty($report_email)) { $current_user = wp_get_current_user(); $report_email = $current_user->user_email; } // Get existing full name $full_name = get_option(ABMSENSE_PREFIX . 'full_name', ''); if (empty($full_name)) { $current_user = wp_get_current_user(); $full_name = $current_user->display_name; } // Update consent via API $config = include('db_config.php'); $api_url = $config['upsert_abmsense_our_customers_details']; $public_key = $config['public_key']; $data = [ 'customer_name' => $customer_name, 'is_consent' => $is_consent, 'report_email' => $report_email, 'consent_update' => true, // Flag to indicate this is a consent-only update 'version_installed' => $version_installed, 'sku' => 'ABM-1000-ID-MONTHLY-FREE-TIER', 'real_time_alert' => 'No', 'full_name' => $full_name ]; $encrypted_data = abmsense_encrypt_data($data, $public_key); if ($encrypted_data) { $args = abmsense_get_api_request_args(wp_json_encode($encrypted_data)); $response = abmsense_safe_api_request($api_url, $args); if (!is_wp_error($response)) { $response_code = wp_remote_retrieve_response_code($response); $response_body = wp_remote_retrieve_body($response); $response_data = json_decode($response_body, true); if ($response_code === 200 && isset($response_data['success']) && $response_data['success']) { // Update local WordPress option update_option(ABMSENSE_PREFIX . 'display_consent_message', $is_consent); echo "
Consent setting updated successfully.
Failed to update consent setting.
Failed to communicate with the API.
Failed to encrypt data.
' . esc_html__('Required database table does not exist. Please deactivate and reactivate the plugin.', 'abmsense') . '
'; return; } // Fetch results from the custom table $esc_table_name = esc_sql($table_name); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $results = $wpdb->get_results( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared "SELECT * FROM `$esc_table_name` LIMIT %d", 1000 ), ARRAY_A ); if ($wpdb->last_error) { abmsense_log('Database error: ' . $wpdb->last_error, 'error'); echo '' . esc_html__('Error fetching data from the database. Please try again later.', 'abmsense') . '
'; return; } // Cache the results wp_cache_set($cache_key, $results, '', HOUR_IN_SECONDS); } // Ensure proper UTF-8 output if (!headers_sent()) { header('Content-Type: text/html; charset=utf-8'); } echo 'No data available.
| ID | Page Title | Time Spent (s) | Page Views | Visitor IP | Header IP | Visitor City | Visitor Country | Visitor Company | Account ID | Last Update | '; echo '
|---|---|---|---|---|---|---|---|---|---|---|
| ' . esc_html($value) . ' | '; } echo '