= 0; $i-- ) { $n = (int) $digits[ $i ]; if ( $alt ) { $n *= 2; if ( $n > 9 ) { $n -= 9; } } $sum += $n; $alt = ! $alt; } return 0 === $sum % 10; } /** API keys, session tokens and similar: long unbroken strings. */ public static function looks_like_secret( $value ) { $v = trim( (string) $value ); return (bool) preg_match( '/^[A-Za-z0-9_\-\.=+\/]{32,}$/', $v ) && ! is_email( $v ) && ! preg_match( '#^https?://#i', $v ); } /** * Turn the posted list into clean [label, key, type, value] fields. * * @param mixed $raw List of [label, name, type, value]. * @return array[] */ public static function clean_fields( $raw ) { $out = array(); if ( ! is_array( $raw ) ) { return $out; } foreach ( array_slice( $raw, 0, self::MAX_FIELDS ) as $f ) { if ( ! is_array( $f ) || count( $f ) < 4 ) { continue; } list( $label, $key, $type, $value ) = array_map( static function ( $v ) { return is_scalar( $v ) ? (string) $v : ''; }, array_slice( array_values( $f ), 0, 4 ) ); $type = sanitize_key( $type ); $label = mb_substr( sanitize_text_field( $label ), 0, 80 ); $key = mb_substr( sanitize_text_field( $key ), 0, 80 ); $value = mb_substr( sanitize_textarea_field( $value ), 0, 2000 ); if ( '' === trim( $value ) || in_array( $type, array( 'password', 'file', 'hidden', 'submit', 'button', 'reset', 'image' ), true ) ) { continue; } if ( preg_match( self::BLOCKED, $key ) || preg_match( self::BLOCKED, $label ) || self::looks_like_card( $value ) || self::looks_like_secret( $value ) ) { continue; } $out[] = array( 'label' => '' !== $label ? $label : $key, 'key' => $key, 'type' => $type, 'value' => $value, ); } return $out; } private static function client_ip() { return isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; } /** Browsers send an Origin header with the beacon: it must be this site. */ private static function same_origin() { $origin = isset( $_SERVER['HTTP_ORIGIN'] ) ? sanitize_url( wp_unslash( $_SERVER['HTTP_ORIGIN'] ) ) : ''; if ( '' === $origin ) { return true; } return wp_parse_url( $origin, PHP_URL_HOST ) === wp_parse_url( home_url(), PHP_URL_HOST ); } /** * Handle a form event from the beacon. Body: { e: 'form', u: url, t: page title, n: form name, f: fields }. * * @return int Contact ID or 0. */ public static function handle( array $body ) { if ( ! self::enabled() || ! self::same_origin() ) { return 0; } $url = isset( $body['u'] ) ? esc_url_raw( (string) $body['u'] ) : ''; if ( ! $url || wp_parse_url( $url, PHP_URL_HOST ) !== wp_parse_url( home_url(), PHP_URL_HOST ) ) { return 0; } $fields = self::clean_fields( $body['f'] ?? array() ); if ( ! $fields ) { return 0; } $repeat = 'nodebook_af_' . md5( $url . wp_json_encode( $fields ) ); if ( get_transient( $repeat ) ) { return 0; } $ip = self::client_ip(); $limit = 'nodebook_afip_' . md5( $ip . wp_salt( 'nonce' ) ); $count = (int) get_transient( $limit ); if ( $count >= self::PER_HOUR ) { return 0; } set_transient( $limit, $count + 1, HOUR_IN_SECONDS ); set_transient( $repeat, 1, self::REPEAT ); $page = isset( $body['t'] ) ? mb_substr( sanitize_text_field( (string) $body['t'] ), 0, 150 ) : ''; $name = isset( $body['n'] ) ? mb_substr( sanitize_text_field( (string) $body['n'] ), 0, 100 ) : ''; // "form-123" or "wpcf7-f12" is not a name a person gave the form; the page title says more. if ( ! preg_match( '/^[\p{L}\s_\-]{3,}$/u', $name ) ) { $name = $page; } $name = ucfirst( trim( str_replace( array( '_', '-' ), ' ', $name ) ) ); /** * Filter fields of a generic form before capture. Return an empty array to skip it. * * @param array $fields Clean fields. * @param string $url Page the form was on. */ $fields = (array) apply_filters( 'nodebook_anyform_fields', $fields, $url ); if ( ! $fields ) { return 0; } return Nodebook_Capture::from_fields( $fields, __( 'Website form', 'nodebook' ), $name, 0, array( 'url' => $url ) ); } }