payload; // Check 1: Honeypot field should be empty if ( self::check_honeypot( $data ) ) { $context->setMeta( 'spam_reason', 'honeypot_filled' ); return true; } // Check 2: Time trap - submission should not be instant $reason = self::time_trap_reason( $data, (int) $context->formId ); if ( null !== $reason ) { $context->setMeta( 'spam_reason', $reason ); return true; } return false; } /** * The protection a form uses (2.6.0). * * A form's own choice (metadata.spam) wins. A form saved before 2.6.0 has * none and keeps working exactly as it did: the hidden trap field was * always checked, and the time trap followed the site's minimum time. * * @param array|null $schema Form schema (the published one at submit time). * @return array{honeypot: bool, timeTrap: bool, minSeconds: int, own: bool} */ public static function for_form( ?array $schema ): array { $own = is_array( $schema['metadata'][ self::FORM_KEY ] ?? null ) ? $schema['metadata'][ self::FORM_KEY ] : array(); $siteMin = self::min_seconds(); $honeypot = isset( $own['honeypot'] ) && is_bool( $own['honeypot'] ) ? $own['honeypot'] : true; $timeTrap = isset( $own['timeTrap'] ) && is_bool( $own['timeTrap'] ) ? $own['timeTrap'] : $siteMin > 0; return array( 'honeypot' => $honeypot, 'timeTrap' => $timeTrap, // A form that asks for the time trap while the site minimum is 0 // uses the usual minimum, so "on" always means on. 'minSeconds' => $siteMin > 0 ? $siteMin : self::MIN_SUBMISSION_TIME, 'own' => isset( $own['honeypot'] ) || isset( $own['timeTrap'] ), ); } /** * Spam settings a new form starts with: the site defaults, which are on * for new installs (2.6.0). * * @return array{honeypot: bool, timeTrap: bool} */ public static function new_form_defaults(): array { return array( 'honeypot' => self::is_enabled(), 'timeTrap' => self::min_seconds() > 0, ); } /** * Give a new form's schema the default spam settings. A choice already in * the schema (a template, an import, a copy) is kept. * * @param array $schema Schema. * @return array */ public static function with_new_form_defaults( array $schema ): array { if ( ! isset( $schema['metadata'] ) || ! is_array( $schema['metadata'] ) ) { $schema['metadata'] = array(); } $own = is_array( $schema['metadata'][ self::FORM_KEY ] ?? null ) ? $schema['metadata'][ self::FORM_KEY ] : array(); foreach ( self::new_form_defaults() as $key => $value ) { if ( ! isset( $own[ $key ] ) || ! is_bool( $own[ $key ] ) ) { $own[ $key ] = $value; } } $schema['metadata'][ self::FORM_KEY ] = $own; return $schema; } /** * Why a submission is blocked as spam, or null when it passes (2.6.0). * * @param array $data Submitted answers (with website_url and form_rendered_at). * @param int $form_id Form ID. * @param array $protection From for_form(). * @param bool $skip_time_trap True for a site admin testing a form. * @param int|null $now Unix time. Defaults to now. * @return string|null honeypot_filled, time_token_missing, time_ticket, time_token_expired, submission_too_fast or null. */ public static function block_reason( array $data, int $form_id, array $protection, bool $skip_time_trap = false, ?int $now = null ): ?string { if ( ! empty( $protection['honeypot'] ) ) { $value = $data[ self::HONEYPOT_FIELD ] ?? ''; if ( is_scalar( $value ) && '' !== (string) $value ) { return 'honeypot_filled'; } } if ( ! empty( $protection['timeTrap'] ) && ! $skip_time_trap ) { $info = self::token_info( $data[ self::TIME_FIELD ] ?? null, $form_id ); if ( null === $info ) { // A page ticket is not a submit token: it must be traded first. return null !== self::ticket_time( $data[ self::TIME_FIELD ] ?? null, $form_id ) ? 'time_ticket' : 'time_token_missing'; } $now = $now ?? time(); $age = $now - $info['time']; // A token older than the lifetime is not accepted as proof of // time. The page refreshes it first (a cached page keeps working). // A classic post cannot refresh, so it may use it once. if ( $age > self::TOKEN_TTL && empty( $protection['acceptStale'] ) ) { return 'time_token_expired'; } // A refreshed token was issued to a visitor who had the form open // for hours, so the minimum time does not apply to it. A fresh // token must be at least the minimum seconds old. if ( ! $info['refreshed'] && $age < max( 1, (int) ( $protection['minSeconds'] ?? self::MIN_SUBMISSION_TIME ) ) ) { return 'submission_too_fast'; } } return null; } /** * The render ticket printed into a page: a signed build time for a form. * A cached page hands the same ticket to every visitor, so a ticket is * never accepted for a submission. The browser trades it for a * per-visitor submit token (exchange_ticket) on first interaction. * * @param int $form_id Form ID. * @param int|null $time Build time (default now). * @return string Ticket: t1.