'contact', 'wpforms' => 'contact', 'gravityforms' => 'contact', 'elementor' => 'contact', 'fluentform' => 'contact', 'formidable' => 'contact', 'comments' => 'comment', 'register' => 'register', 'woo_register' => 'register', 'login' => 'login', ); /** Form plugins whose forms can be switched on and off one by one (Settings -> formconfirmed -> Forms). */ const PER_FORM = array( 'cf7', 'wpforms', 'gravityforms', 'fluentform', 'formidable' ); /** Error codes of the service that mean "no check took place" (not the visitor's fault). */ const NO_CHECK_CODES = array( 'service-disabled', 'site-paused', 'invalid-input-secret', 'missing-input-secret', 'bad-request' ); /** The result of the current submission. */ private static $result = null; /** The decision for the current submission: pass, hold or reject. */ private static $decision = null; /** The adapter the current submission was checked for. */ private static $checked = ''; /** Raw WPForms entry of the current submission (wpforms_process_before). */ private static $wpforms_entry = array(); public static function init() { add_action( 'formconfirmed_integration_report', array( __CLASS__, 'report_integration' ) ); add_action( 'formconfirmed_integration_retry', array( __CLASS__, 'report_integration' ) ); add_shortcode( 'formconfirmed', array( __CLASS__, 'shortcode' ) ); if ( ! self::on() || self::paused() ) { return; } if ( ! wp_next_scheduled( 'formconfirmed_integration_report' ) ) { wp_schedule_event( time() + 60, 'hourly', 'formconfirmed_integration_report' ); } add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_script' ) ); add_action( 'login_enqueue_scripts', array( __CLASS__, 'register_script' ) ); add_filter( 'script_loader_tag', array( __CLASS__, 'async_tag' ), 10, 2 ); foreach ( self::active_adapters() as $a ) { call_user_func( array( __CLASS__, 'adapter_' . $a ) ); } } /** Opaque per-installation references, derived only from server-selected adapter/form keys. */ const FORM_NAMESPACE = 'formconfirmed_form_namespace'; public static function form_ref( $adapter, $form_id = 0 ) { if ( ! is_string( $adapter ) || ! in_array( $adapter, array_merge( self::ADAPTERS, array( 'custom' ) ), true ) ) { return ''; } if ( in_array( $adapter, self::PER_FORM, true ) ) { if ( ! ( is_int( $form_id ) || is_string( $form_id ) ) || ! preg_match( '/^[1-9][0-9]{0,17}$/D', (string) $form_id ) ) { return ''; } $key = (string) $form_id; } elseif ( 'elementor' === $adapter ) { if ( ! is_string( $form_id ) || ! preg_match( '/^[1-9][0-9]{0,17}:[a-f0-9]{6,32}$/D', $form_id ) ) { return ''; } $key = $form_id; } elseif ( 'custom' === $adapter ) { if ( ! is_string( $form_id ) || ! preg_match( '/^[a-zA-Z0-9_-]{1,80}$/D', $form_id ) ) { return ''; } $key = $form_id; } else { $key = 'site'; } $seed = get_option( self::FORM_NAMESPACE, null ); if ( null === $seed ) { try { $new = bin2hex( random_bytes( 32 ) ); } catch ( Exception $e ) { return ''; } $seed = add_option( self::FORM_NAMESPACE, $new, '', false ) ? $new : get_option( self::FORM_NAMESPACE, '' ); } if ( ! is_string( $seed ) || ! preg_match( '/^[a-f0-9]{64}$/D', $seed ) ) { return ''; } return 'fcf_' . substr( hash_hmac( 'sha256', "fc-form-v1\n" . $adapter . "\n" . $key, $seed ), 0, 32 ); } /** Only a resolved Elementor record supplies the post and widget identity. No POST fallback. */ public static function elementor_form_key( $record ) { if ( ! is_object( $record ) || ! method_exists( $record, 'get_form_settings' ) ) { return ''; } $post = $record->get_form_settings( 'form_post_id' ); $id = $record->get_form_settings( 'id' ); if ( ! ( is_int( $post ) || is_string( $post ) ) || ! preg_match( '/^[1-9][0-9]{0,17}$/D', (string) $post ) || ! is_string( $id ) || ! preg_match( '/^[a-f0-9]{6,32}$/D', $id ) ) { return ''; } return (string) $post . ':' . $id; } /** Persist only bounded technical delivery status; never response text, keys or hostnames. */ public static function report_status() { $raw = get_option( 'formconfirmed_report_status', array() ); if ( ! is_array( $raw ) || ! isset( $raw['attempt'] ) ) { $raw = get_transient( 'formconfirmed_report_status' ); } $raw = is_array( $raw ) ? $raw : array(); $out = array( 'attempt'=>0, 'success'=>0, 'next_retry'=>0, 'failures'=>0, 'ok'=>false, 'checks_ok'=>null ); foreach ( array( 'attempt', 'success', 'next_retry' ) as $k ) { if ( isset( $raw[$k] ) && is_int( $raw[$k] ) && $raw[$k]>=0 && $raw[$k]<=time()+3600 ) { $out[$k]=$raw[$k]; } } // Upgrade the former two-hour transient without inventing a successful delivery. if ( ! $out['attempt'] && isset( $raw['at'] ) && is_int( $raw['at'] ) && $raw['at']>0 && $raw['at']<=time()+60 ) { $out['attempt']=$raw['at']; if ( true === ( $raw['ok'] ?? null ) ) { $out['success']=$raw['at']; } } $out['ok'] = true === ( $raw['ok'] ?? null ); $out['failures'] = isset( $raw['failures'] ) && is_int( $raw['failures'] ) ? max(0,min(12,$raw['failures'])) : 0; $out['checks_ok'] = isset( $raw['checks_ok'] ) && is_bool( $raw['checks_ok'] ) ? $raw['checks_ok'] : null; return $out; } /** Small authenticated technical report. This is a plugin assertion, not binary attestation. */ public static function report_integration( $manual = false ) { if ( ! self::on() || self::paused() ) { return false; } $state = self::report_status(); $now = time(); if ( $state['attempt'] && $now < $state['attempt']+30 ) { return false; } if ( ! $manual && $state['next_retry']>$now ) { return false; } $o = self::opt(); $host = strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ); if ( '' === $host ) { return false; } $base = rtrim( FORMCONFIRMED_API, '/' ); $url = $base . '/integration'; $parts = wp_parse_url( $url ); if ( ! is_array( $parts ) || ! empty( $parts['query'] ) || ! empty( $parts['fragment'] ) || ! empty( $parts['user'] ) || ! empty( $parts['pass'] ) ) { return false; } try { $nonce = bin2hex( random_bytes( 16 ) ); } catch ( Exception $e ) { return false; } $ts = (string) $now; $raw = wp_json_encode( array( 'hostname'=>$host, 'api'=>$base, 'widget'=>$base.'/widget.js', 'sitekey'=>$o['site_key'], 'component'=>'wordpress-plugin', 'plugin_version'=>FORMCONFIRMED_VERSION ) ); $message = "fc-ops-v1\nPOST\n" . $parts['path'] . "\n" . $ts . "\n" . $nonce . "\n" . hash( 'sha256', $raw ); $r = wp_remote_post( $url, array( 'timeout'=>self::TIMEOUT, 'redirection'=>0, 'limit_response_size'=>16384, 'headers'=>array( 'Content-Type'=>'application/json', 'X-FC-Key'=>$o['site_key'], 'X-FC-Time'=>$ts, 'X-FC-Nonce'=>$nonce, 'X-FC-Signature'=>hash_hmac( 'sha256', $message, hash( 'sha256', $o['secret'] ) ) ), 'body'=>$raw, ) ); $j = is_wp_error( $r ) ? null : json_decode( (string) wp_remote_retrieve_body( $r ), true ); $ok = ! is_wp_error( $r ) && 200 === (int) wp_remote_retrieve_response_code( $r ) && is_array( $j ) && true === ( $j['success'] ?? null ); $fields = array( 'api_matches', 'widget_matches', 'key_matches', 'secret_valid', 'host_allowed' ); $ok = $ok && isset( $j['checks'] ) && is_array( $j['checks'] ) && count( $j['checks'] ) === count( $fields ); foreach ( $fields as $field ) { if ( ! isset( $j['checks'][$field] ) || ! is_bool( $j['checks'][$field] ) ) { $ok=false; } } $state['attempt']=$now; $state['ok']=$ok; $state['checks_ok']=$ok ? ! in_array( false, $j['checks'], true ) : null; $state['failures']=$ok ? 0 : min(12,$state['failures']+1); $state['next_retry']=$ok ? 0 : $now+(int)min(3600,300*pow(2,min(4,$state['failures']-1))); if ( $ok ) { $state['success']=$now; } update_option( 'formconfirmed_report_status', $state, false ); delete_transient( 'formconfirmed_report_status' ); wp_clear_scheduled_hook( 'formconfirmed_integration_retry' ); if ( ! $ok ) { wp_schedule_single_event( $state['next_retry'], 'formconfirmed_integration_retry' ); } return $ok; } public static function stop_reporting() { wp_clear_scheduled_hook( 'formconfirmed_integration_report' ); wp_clear_scheduled_hook( 'formconfirmed_integration_retry' ); delete_transient( 'formconfirmed_report_status' ); } /** First activation: take over the keys of the form protection tab of the formexposed plugin. */ public static function activate() { $o = get_option( self::OPT ); if ( ! is_array( $o ) ) { $alt = get_option( self::OPTION_FORMEXPOSED_PROTECT ); $neu = array(); if ( is_array( $alt ) && ! empty( $alt['site_key'] ) && ! empty( $alt['secret'] ) ) { $neu = array( 'site_key' => (string) $alt['site_key'], 'secret' => (string) $alt['secret'], 'adapters' => isset( $alt['adapters'] ) ? (array) $alt['adapters'] : self::DEFAULT_ADAPTERS, 'content' => ! empty( $alt['content'] ), 'uncertain' => isset( $alt['uncertain'] ) && 'reject' === $alt['uncertain'] ? 'reject' : 'hold', 'imported' => time(), ); } add_option( self::OPT, $neu, '', false ); } Formconfirmed_Held::schedule(); } /** The saved settings, with defaults. */ public static function opt() { $o = get_option( self::OPT, array() ); $o = is_array( $o ) ? $o : array(); $o = wp_parse_args( $o, array( 'site_key' => '', 'secret' => '', 'adapters' => self::DEFAULT_ADAPTERS, 'content' => true, 'uncertain' => 'hold', 'backup' => true, 'forms' => array(), 'new_forms' => true, 'stats' => array(), 'problem' => array(), 'ok_at' => 0, 'imported' => 0, ) ); $o['adapters'] = array_values( array_intersect( self::ADAPTERS, (array) $o['adapters'] ) ); $o['uncertain'] = 'reject' === $o['uncertain'] ? 'reject' : 'hold'; $o['content'] = (bool) $o['content']; $o['backup'] = (bool) $o['backup']; $o['new_forms'] = (bool) $o['new_forms']; $forms = array(); foreach ( self::PER_FORM as $a ) { $forms[ $a ] = array(); if ( isset( $o['forms'][ $a ] ) && is_array( $o['forms'][ $a ] ) ) { foreach ( $o['forms'][ $a ] as $id => $on ) { if ( ctype_digit( (string) $id ) && '0' !== (string) $id ) { $forms[ $a ][ (string) $id ] = (bool) $on; } } } } $o['forms'] = $forms; $o['stats'] = wp_parse_args( (array) $o['stats'], array( 'allow' => 0, 'flag' => 0, 'block' => 0, 'unreachable' => 0, 'fallback' => 0, 'held' => 0, 'rejected' => 0 ) ); return $o; } public static function save_opt( $o ) { update_option( self::OPT, $o, false ); } /** Both keys are there. */ public static function on() { $o = self::opt(); return '' !== $o['site_key'] && '' !== $o['secret']; } /** * Switched off in the settings of a site where the form protection tab of the formexposed plugin * would otherwise check the same submissions. That plugin stands down while this one is active * (since formexposed 1.10.0); an older version would check every token twice, and a token is * valid once - so this plugin waits until formexposed is updated. */ public static function paused() { return defined( 'FORMEXPOSED_VERSION' ) && version_compare( FORMEXPOSED_VERSION, '1.10.0', '<' ) && class_exists( 'Formexposed_Protect' ) && Formexposed_Protect::on(); } /** The adapters that are switched on AND whose form plugin is active. */ public static function active_adapters() { $out = array(); foreach ( self::opt()['adapters'] as $a ) { if ( self::available( $a ) ) { $out[] = $a; } } return $out; } /** Is the form plugin (or core feature) of an adapter present? */ public static function available( $a ) { switch ( $a ) { case 'cf7': return defined( 'WPCF7_VERSION' ); case 'wpforms': return defined( 'WPFORMS_VERSION' ) || function_exists( 'wpforms' ); case 'gravityforms': return class_exists( 'GFForms' ); case 'elementor': return defined( 'ELEMENTOR_PRO_VERSION' ); case 'fluentform': return defined( 'FLUENTFORM' ) || defined( 'FLUENTFORM_VERSION' ); case 'formidable': return class_exists( 'FrmAppHelper' ); case 'woo_register': return class_exists( 'WooCommerce' ); case 'register': return (bool) get_option( self::OPTION_USERS_CAN_REGISTER ); default: return true; // comments, login } } /** * Is one form protected? Forms of the plugins in PER_FORM follow their own switch; a form nobody * has decided on yet follows "protect new forms automatically" (on by default). The other places * (Elementor Pro forms, comments, registration, login) have one switch each. */ public static function form_enabled( $adapter, $id ) { if ( ! in_array( $adapter, self::PER_FORM, true ) ) { return true; } $o = self::opt(); $id = (string) $id; return isset( $o['forms'][ $adapter ][ $id ] ) ? $o['forms'][ $adapter ][ $id ] : $o['new_forms']; } /** The forms of a form plugin on this site: array(id => title). Empty when the plugin is not active. */ public static function forms_of( $adapter ) { $out = array(); if ( ! self::available( $adapter ) ) { return $out; } switch ( $adapter ) { case 'cf7': case 'wpforms': $posts = get_posts( array( 'post_type' => 'cf7' === $adapter ? 'wpcf7_contact_form' : 'wpforms', 'post_status' => 'any', 'posts_per_page' => 200, 'orderby' => 'title', 'order' => 'ASC', ) ); foreach ( $posts as $p ) { $out[ (string) $p->ID ] = '' !== $p->post_title ? $p->post_title : '#' . $p->ID; } break; case 'gravityforms': if ( class_exists( 'GFAPI' ) && method_exists( 'GFAPI', 'get_forms' ) ) { foreach ( (array) GFAPI::get_forms() as $f ) { if ( isset( $f['id'] ) ) { $out[ (string) $f['id'] ] = isset( $f['title'] ) ? (string) $f['title'] : '#' . $f['id']; } } } break; case 'fluentform': if ( function_exists( 'wpFluent' ) ) { $rows = wpFluent()->table( 'fluentform_forms' )->select( array( 'id', 'title' ) )->orderBy( 'title' )->get(); foreach ( is_iterable( $rows ) ? $rows : array() as $f ) { $out[ (string) $f->id ] = (string) $f->title; } } break; case 'formidable': if ( class_exists( 'FrmForm' ) && method_exists( 'FrmForm', 'getAll' ) ) { foreach ( (array) FrmForm::getAll( array( 'is_template' => 0, 'status' => 'published' ), 'name' ) as $f ) { $out[ (string) $f->id ] = (string) $f->name; } } break; } return $out; } public static function labels() { return array( 'cf7' => 'Contact Form 7', 'wpforms' => 'WPForms', 'gravityforms' => 'Gravity Forms', 'elementor' => __( 'Elementor Pro forms', 'formconfirmed' ), 'fluentform' => 'Fluent Forms', 'formidable' => 'Formidable Forms', 'comments' => __( 'Comments', 'formconfirmed' ), 'register' => __( 'WordPress registration', 'formconfirmed' ), 'woo_register' => __( 'WooCommerce account registration', 'formconfirmed' ), 'login' => __( 'WordPress login', 'formconfirmed' ), ); } // ------------------------------------------------------------------ front end /** The widget script is registered on every page, but only loaded where a protected form is printed. */ public static function register_script() { wp_register_script( 'formconfirmed-widget', FORMCONFIRMED_API . '/widget.js', array(), FORMCONFIRMED_VERSION, array( 'in_footer' => true ) ); wp_register_script( 'formconfirmed-forms', plugins_url( 'assets/js/forms.js', FORMCONFIRMED_FILE ), array(), FORMCONFIRMED_VERSION, array( 'in_footer' => true ) ); } /** Load the widget (and, for forms that are equipped in the browser, the small helper). */ private static function load( $helper = false ) { if ( ! wp_script_is( 'formconfirmed-widget', 'registered' ) ) { self::register_script(); } wp_enqueue_script( 'formconfirmed-widget' ); Formconfirmed_Backup::load(); if ( $helper && ! wp_script_is( 'formconfirmed-forms', 'enqueued' ) ) { wp_enqueue_script( 'formconfirmed-forms' ); $sel = array(); if ( in_array( 'elementor', self::active_adapters(), true ) ) { $sel[] = 'form.elementor-form'; } if ( in_array( 'fluentform', self::active_adapters(), true ) ) { foreach ( array_keys( self::forms_of( 'fluentform' ) ) as $fid ) { if ( self::form_enabled( 'fluentform', $fid ) ) { $sel[] = 'form.frm-fluent-form[data-form_id="' . absint( $fid ) . '"]'; } } } wp_add_inline_script( 'formconfirmed-forms', 'window.formconfirmedForms = ' . wp_json_encode( array( 'sel' => implode( ',', $sel ), 'sitekey' => self::opt()['site_key'], 'purpose' => self::PURPOSES['elementor'], ) ) . ';', 'before' ); } } public static function async_tag( $tag, $handle ) { return 'formconfirmed-widget' === $handle ? str_replace( ' src=', ' async defer src=', $tag ) : $tag; } /** The element the widget turns into the hidden token field, a trap field and the badge. */ public static function element( $adapter, $purpose = '' ) { self::load(); $purpose = '' !== $purpose ? $purpose : self::PURPOSES[ $adapter ]; return '
'; } /** A purpose the service accepts: lower case letters, digits, - and _, starting with a letter. */ public static function clean_purpose( $purpose ) { $purpose = strtolower( trim( (string) $purpose ) ); return preg_match( '/^[a-z][a-z0-9_-]{0,63}$/', $purpose ) ? $purpose : 'contact'; } /** * Shortcode [formconfirmed purpose="contact"]: the widget for a form of your own (inside its