?? GreyFile — Mystic File Browser

Current path: home/webdevt/www/cryptoimpot.fr/wp-content/plugins/divi-contact-form-helper/app/Frontend/Request/



?? Go up: /home/webdevt/www/cryptoimpot.fr/wp-content/plugins/divi-contact-form-helper/app/Frontend

?? Viewing: Process_Form_Request.php

<?php

namespace KS_PAC_DCFH\Frontend\Request;

use KS_PAC_DCFH\Admin\Controllers\Email_Handler;

if (!defined('ABSPATH')) {
    exit;
}

class Process_Form_Request
{
    private $post_request;

    private $post_repository;

    private $process_request = true;

    public function init(): void
    {
        add_filter('et_contact_page_email_to', [$this, 'maybe_filter_page_email_to']);
        add_filter('et_contact_page_headers', [$this, 'maybe_filter_contact_page_headers'], 10, 3);
        add_action('et_pb_contact_form_submit', [$this, 'maybe_log_entries'], 10, 3);
    }

    public function maybe_filter_page_email_to($email)
    {
        $this->post_request = new Post_Request();
        $this->post_repository = Post_Repository::instance();
        $email_handler = Email_Handler::instance();
        // Disable admin email if the setting is turned off.
        if (!$this->post_repository->is_admin_email_enabled) {
            $email = '';
        }
        // Stop processing if the submission threshold has been reached.
        if ($this->post_repository->is_threshold_reached) {
            $this->process_request = false;
        }
        // Block request if honeypot field was triggered (likely a bot).
        if ($this->post_request->is_honeypot_triggered()) {
            $this->process_request = false;
        }
        // Stop processing if Google reCAPTCHA v2 verification fails.
        if (!$this->post_request->is_google_recaptcha_v2_verified()) {
            $this->process_request = false;
        }
        // Stop processing if Cloudflare Turnstile verification fails.
        if (!$this->post_request->is_turnstile_verified()) {
            $this->process_request = false;
        }
        // Block request if the sender's email is blacklisted.
        if ($email_handler->is_blacklisted_email()) {
            $this->process_request = false;
        }
        // Disable email sending if configured to do so.
        $this->maybe_disabled_emailing();
        // Proceed with form handling if all checks passed.
        if ($this->process_request) {
            // If enabled, set the "From" email address to the submitter's email to improve deliverability and reply handling.
            if ($this->post_repository->is_admin_email_from_submitter_email) {
                $email_handler->set_mail_from($this->post_repository->sender_email);
            }
            // Retrieve the configured "From" name for admin emails, apply merge tags, and set it for the email handler.
            $admin_email_from_name = $this->post_repository->admin_email_from_name;
            $admin_email_from_name = ks_pac_dcfh_merge_tags()::get_merge_tags_values($admin_email_from_name);
            $email_handler->set_mail_from_name($admin_email_from_name);
            // Retrieve admin email routing addresses from the handler.
            $admin_emails_routing = $email_handler->get_admin_emails_routing();
            if (!empty($admin_emails_routing)) {
                $email = $admin_emails_routing;
            }
            // Save form entry.
            $this->save_form_submission();
            // Handle uploaded files
            $this->store_form_uploaded_files();
            // Send the form submission payload to the configured external endpoint.
            $this->submit_form_payload();
            // Hook into wp_mail to optionally modify email parameters before sending.
            add_filter('wp_mail', [$this, 'maybe_filter_wp_mail'], 999);
        }

        return $email;
    }

    public function maybe_filter_contact_page_headers($headers, $contact_name, $contact_email)
    {
        // Return If Send Email False
        if (!$this->process_request) {
            return $headers;
        }
        /**
         * If Zapier settings enabled send email BCC to zapier
         * @since 1.0.0
         */
        if ($this->post_repository->is_zapier_enabled) {
            $headers[] = sprintf('Bcc: %s', sanitize_email($this->post_repository->zapier_mailbox_address));
        }
        /**
         * If Pabbly settings enabled send email BCC to zapier
         * @since 1.0.3
         */
        if ($this->post_repository->is_pabbly_enabled) {
            $headers[] = sprintf('Bcc: %s', sanitize_email($this->post_repository->pabbly_mailbox_address));
        }
        /**
         * Admin Email Cc & Bcc Headers
         * @since 1.7.21
         */
        $email_handler = Email_Handler::instance();
        $headers = $email_handler->add_admin_headers($headers);
        /**
         * Admin Formatted Message
         * @since 1.3
         */
        if ($this->post_repository->is_custom_message_richtext) {
            $headers[] = 'Content-Type: text/html; charset=UTF-8';
        }

        return $headers;
    }

    public function maybe_log_entries($processed_fields_values, $et_contact_error, $contact_form_info): void
    {
        if (ks_pac_dcfh_db()::is_setting_enabled('pwh_dcfh_logs_enabled')) {
            $filtered_data = [];
            foreach ($_POST as $key => $value) { // phpcs:ignore WordPress.Security.NonceVerification
                // Include keys that start with "et_pb_contact_"
                if (strpos($key, 'et_pb_contact_') === 0) {
                    $filtered_data[$key] = $value;
                } // Include keys that match the dynamic nonce pattern
                elseif (preg_match('/^_wpnonce-et-pb-contact-form-submitted-\d+$/', $key)) {
                    $filtered_data[$key] = $value;
                } // Include the _wp_http_referer key explicitly
                elseif ($key === '_wp_http_referer') {
                    $filtered_data[$key] = $value;
                }
            }
            if (!empty($filtered_data)) {
                ks_pac_dcfh_log($filtered_data, false, 'divi_contact_form'); // phpcs:ignore WordPress.Security.NonceVerification
            }
        }
    }

    public function maybe_modify_post_dates($data, $post_arr)
    {
        if (!empty($post_arr['post_modified']) && !empty($post_arr['post_modified_gmt'])) {
            $data['post_modified'] = '0000-00-00 00:00:00';
            $data['post_modified_gmt'] = '0000-00-00 00:00:00';
        }

        return $data;
    }

    public function maybe_filter_wp_mail($attr)
    {
        $contact_form_num = $this->post_request->contact_form_num;
        $_wpnonce = isset($_POST["_wpnonce-et-pb-contact-form-submitted-$contact_form_num"]) && wp_verify_nonce(sanitize_text_field($_POST["_wpnonce-et-pb-contact-form-submitted-$contact_form_num"]),
                'et-pb-contact-form-submit');
        if ($_wpnonce && array_key_exists("et_pb_contactform_submit_$contact_form_num", wp_unslash($_POST))) {
            if (!isset($_POST["pwh_dcfh_et_pb_contactform_submit_$contact_form_num"])) {
                $_POST["pwh_dcfh_et_pb_contactform_submit_$contact_form_num"] = 'et_contact_proccess';
            }
            unset($_POST["et_pb_contactform_submit_$contact_form_num"]);
            $message = $attr['message'];
            /*
            * This section of code checks if the message contains the string "et_pb_html_field" using regular expression.
            * If it does, it replaces everything before "et_pb_html_field" with an empty string.
            */
            if (strpos($message, "et_pb_html_field") !== false) {
                $message = preg_replace('/.*et_pb_html_field/', '', $message);
            }
            /**
             * If sending files as attachments is enabled and there are files in the temporary directory,
             * attach them to the post and set a message about the attached files.
             */
            $attachments = $this->post_repository->attachments;
            $attached_file_message = '';
            if (!empty($attachments['uploaded_dir']) && $this->post_repository->send_files_as_attachment) {
                $attr['attachments'] = $attachments['uploaded_dir'];
                $attached_file_message = $this->post_repository->attached_files_message;
            }
            // Email Subject Merge Tags
            $subject = $this->post_repository->admin_email_subject;
            if (!empty($subject)) {
                $subject = ks_pac_dcfh_merge_tags()::get_merge_tags_values($subject);
                $attr['subject'] = $subject;
            }
            /**
             * Extract merge tag values from the given message.
             */
            $message = ks_pac_dcfh_merge_tags()::get_merge_tags_values($message);
            /**
             * Replaces the placeholder '%%dcfh_all_fields%%'
             * in the given message with dynamic content fetched using the pwh_dcfh_email_merge_tags_handler().
             */
            if (preg_match('/%%dcfh_all_fields%%/i', $message)) {
                $message = str_replace("%%dcfh_all_fields%%", ks_pac_dcfh_merge_tags()::get_all_fields_data($this->post_repository->is_custom_message_richtext), $message);
            }
            // Replaced Final Message
            $attr['message'] = $message.PHP_EOL.$attached_file_message;
        }

        return $attr;
    }

    private function maybe_disabled_emailing(): void
    {
        if (!$this->process_request) {
            add_filter('is_email', '__return_false');
            add_filter('pre_wp_mail', '__return_false');
        }
    }

    private function save_form_submission(): void
    {
        if (
            !$this->post_repository->save_entry_to_db ||
            empty($this->post_repository->contact_form_id)
        ) {
            return;
        }
        add_filter('wp_insert_post_data', [$this, 'maybe_modify_post_dates'], PHP_INT_MAX, 2);
        //global $current_user;
        //$user_id = $current_user->ID ?? 0;
        $user_id = get_current_user_id();
        $post_status = 'draft';
        $sender_email = $this->post_repository->sender_email;
        $is_spam_submitter = ks_pac_dcfh_post_meta()::is_spam($sender_email);
        if ($is_spam_submitter) {
            $post_status = 'spam';
        }
        $post_id = wp_insert_post([
            'post_type' => 'pwh_dcfh',
            'post_status' => $post_status,
            'post_name' => uniqid('entry-', true),
            'post_content' => wp_slash(maybe_serialize($this->post_repository->contact_form_fields)),
            'post_excerpt' => wp_slash(maybe_serialize($this->post_repository->contact_form_data)),
            'post_author' => $user_id,
            'comment_status' => 'closed',
            'ping_status' => 'closed',
            'post_date' => current_time('mysql'),
            'post_date_gmt' => current_time('mysql', 1),
            'post_modified' => '0000-00-00 00:00:00',
            'post_modified_gmt' => '0000-00-00 00:00:00',
        ], true);
        if (!is_wp_error($post_id)) {
            $meta_data = [
                '_pwh_dcfh_page_id' => $this->post_repository->page_id,
                '_pwh_dcfh_contact_form_id' => $this->post_repository->contact_form_id,
                '_pwh_dcfh_form_fields' => $this->post_repository->contact_form_fields_str,
                '_pwh_dcfh_contact_email' => $sender_email,
                '_pwh_dcfh_referer_url' => $this->post_repository->referer_url,
            ];
            if ($this->post_repository->collect_ip_useragent_details) {
                $meta_data['_pwh_dcfh_ip_address'] = $this->post_repository->ip_address;
                $meta_data['_pwh_dcfh_user_agent'] = $this->post_repository->user_agent;
            }
            foreach ($meta_data as $key => $value) {
                update_post_meta($post_id, $key, $value);
            }
            $this->post_repository->entry_number = $post_id;
            wp_update_post(['ID' => $post_id, 'post_title' => 'entry-'.$post_id]);
            ks_pac_dcfh_post_meta()::update_post_status_and_modified_date($post_id, $post_status);
            // Update Form Settings > Total Entries
            $contact_form_entries = ks_pac_dcfh_post_meta()::get_contact_form_total_entries($this->post_repository->contact_form_id);
            ks_pac_dcfh_db()::update_contact_form_total_entries($this->post_repository->contact_form_id, $contact_form_entries);
            remove_filter('wp_insert_post_data', [$this, 'maybe_modify_post_dates'], PHP_INT_MAX);
        }
    }

    private function submit_form_payload(): void
    {
        $post_repository = $this->post_repository;
        $endpoint_url = $post_repository->post_payload_endpoint;
        if (!empty($endpoint_url)) {
            $post_request = $this->post_request;
            $processed_data = $post_request->contact_form_data;
            $contact_form_id = $post_repository->contact_form_id;
            $entry_number = $post_repository->entry_number;
            $attachments = $post_repository->attachments['baseurl'] ?? [];
            $referer_url = $post_repository->referer_url;
            $response = wp_remote_request($endpoint_url, [
                'method' => 'POST',
                'body' => [
                    'contact_form_id' => $contact_form_id,
                    'entry_number' => $entry_number,
                    'processed_data' => $processed_data,
                    'attachments' => $attachments,
                    'referer_url' => $referer_url,
                    'ip_address' => ks_pac_dcfh_general_helper()::get_ip_address(),
                    'user_agent' => ks_pac_dcfh_general_helper()::get_user_agent(),
                    'raw_data' => $_POST, // phpcs:ignore WordPress.Security.NonceVerification
                ],
                'headers' => [
                    'Content-Type' => 'application/x-www-form-urlencoded',
                    'Authorization' => 'Basic '.$contact_form_id,
                    'Token' => 'Bearer '.$contact_form_id,
                ],
            ]);
            if (is_wp_error($response)) {
                $response->get_error_message();
            } else {
                wp_remote_retrieve_body($response);
            }
        }
    }

    private function store_form_uploaded_files()
    {
        $this->post_request->get_files_from_temp_dir();
        if ($this->post_repository->save_files_to_media) {
            $this->post_request->move_files_to_media_library($this->post_repository->entry_number);
        } else {
            $this->post_request->move_files_to_form_dir();
        }
    }
}


??

??