Repository navigation
Add built-in ALTCHA CAPTCHA support #3688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Crabcyborg
wants to merge
28
commits into
master
Choose a base branch
from
feat/altcha-captcha
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
c312e2d
feat(captcha): add built-in ALTCHA support
Crabcyborg 566c7c3
fix(captcha): address ALTCHA review findings
Crabcyborg 4703a8e
feat(captcha): simplify ALTCHA presentation
Crabcyborg dc010ee
Merge master into feat/altcha-captcha
Crabcyborg a3b4373
Merge master into feat/altcha-captcha
Crabcyborg 5430688
Merge branch 'master' into feat/altcha-captcha
Crabcyborg 1bfd9a4
fix(captcha): address remaining ALTCHA review notes
Crabcyborg f41199b
fix(captcha): address ALTCHA review follow-ups
Crabcyborg 179d282
fix(captcha): version the ALTCHA script
Crabcyborg e9799dd
Spend embedded form ALTCHA solutions and skip assets without a captcha
Crabcyborg d64188e
docs(captcha): explain the ALTCHA upgrade default
Crabcyborg 9dab156
Merge branch 'master' into feat/altcha-captcha
Crabcyborg f6cebe3
Merge branch 'master' into feat/altcha-captcha
Crabcyborg 373b5f6
fix(admin): translate the ALTCHA captcha tab label
Crabcyborg 4add2a9
Merge origin/master into feat/altcha-captcha
Crabcyborg 4089f7d
Load the ALTCHA script with a single version parameter
Crabcyborg dae2675
Show the Bokmal ALTCHA translation for Norwegian Nynorsk sites
Crabcyborg b7cd123
Keep a configured CAPTCHA provider when the saved one has no keys
Crabcyborg a44a941
Describe the floating ALTCHA layout in the form builder
Crabcyborg 8775c63
Keep a rejected ALTCHA widget unverified until the visitor verifies a…
Crabcyborg e377358
Show a floating ALTCHA widget in its field when it has an error
Crabcyborg af12531
Connect the ALTCHA error to its checkbox on forms without AJAX
Crabcyborg 3333ae2
Wait for the checkbox click in ALTCHA's click-only mode
Crabcyborg 21e1746
Show the loading state while ALTCHA finishes on submit
Crabcyborg 2b27806
Use wp_list_pluck in the ALTCHA replay index test
Crabcyborg 29949fa
Use the Captcha field's message when ALTCHA asks for a click
Crabcyborg e2c0662
Keep ALTCHA proofs for uploads and entries apart
Crabcyborg ae9d00d
Merge branch 'master' into feat/altcha-captcha
Crabcyborg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,394 @@ | ||
| <?php | ||
| /** | ||
| * The ALTCHA captcha type, the challenge endpoint, and assets. | ||
| * | ||
| * @package Formidable | ||
| */ | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| die( 'You are not allowed to call this page directly.' ); | ||
| } | ||
|
|
||
| /** | ||
| * ALTCHA integration with Lite's Captcha field. | ||
| * | ||
| * @since x.x | ||
| */ | ||
| class FrmAltchaController { | ||
|
|
||
| /** | ||
| * Verified solutions waiting to be spent, keyed by challenge ID. | ||
| * | ||
| * A solution is only spent once the whole entry passes validation, see claim_solution(). | ||
| * That way a visitor who fixes another field and submits again keeps their solution. | ||
| * | ||
| * This is not keyed by form. A captcha in an embedded form belongs to the embedded form, | ||
| * but it is validated with the parent form's entry. | ||
| * | ||
| * @var array<string, array{id: string, expires: int, field_id: int, message: string}> | ||
| */ | ||
| private static $pending = array(); | ||
|
|
||
| /** | ||
| * Whether ALTCHA is the active captcha type. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @return bool | ||
| */ | ||
| public static function is_active() { | ||
| return FrmCaptchaFactory::get_settings_object() instanceof FrmAltchaSettings; | ||
| } | ||
|
|
||
| /** | ||
| * Load the widget instead of a remote captcha script when ALTCHA is active. | ||
| * | ||
| * Lite registers the URL under its captcha-api handle, so the widget loads once however | ||
| * many captcha fields the page has. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function api_url() { | ||
| self::styles(); | ||
| return self::script_url(); | ||
| } | ||
|
|
||
| /** | ||
| * Verify the posted solution locally instead of calling a remote service. | ||
| * | ||
| * Lite reads the result like a siteverify response. Failures keep Lite's behavior, so | ||
| * the field's error message and the CAPTCHA spam handling setting apply as usual. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param FrmAltchaSettings $captcha_settings The active captcha type's settings. | ||
| * @param array|object $field The captcha field. | ||
| * | ||
| * @return array | ||
| */ | ||
| public static function verify_response( $captcha_settings, $field ) { | ||
| $payload = FrmAppHelper::get_post_param( $captcha_settings->token_field, '', 'sanitize_text_field' ); | ||
| $result = FrmAltchaChallengeHelper::verify( $payload ); | ||
|
|
||
| if ( $result['verified'] && FrmAltchaReplayHelper::is_used( $result['id'] ) ) { | ||
| $result['verified'] = false; | ||
| $result['reason'] = 'replayed'; | ||
| } | ||
|
|
||
| if ( ! $result['verified'] ) { | ||
| $code = 'missing' === $result['reason'] ? 'missing-input-response' : 'altcha-' . $result['reason']; | ||
|
|
||
| return array( | ||
| 'success' => false, | ||
| 'error-codes' => array( $code ), | ||
| ); | ||
| } | ||
|
|
||
| self::$pending[ $result['id'] ] = array( | ||
| 'id' => $result['id'], | ||
| 'expires' => $result['expires'], | ||
| 'field_id' => (int) ( is_object( $field ) ? $field->id : $field['id'] ), | ||
| 'message' => self::invalid_message( $field ), | ||
| ); | ||
|
|
||
| return array( 'success' => true ); | ||
| } | ||
|
|
||
| /** | ||
| * Ask the visitor to verify when the form is posted without a solution. | ||
| * | ||
| * Lite and Pro report a missing CAPTCHA response as "The captcha is missing from this form". For | ||
| * ALTCHA it means the visitor submitted before verifying, for example before clicking the checkbox | ||
| * in the "Only when the visitor clicks the checkbox" mode, so the message the widget shows is used. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param array $errors Validation errors so far. | ||
| * @param object $posted_field The field being validated. | ||
| * @param mixed $value The posted value. | ||
| * @param array $args Validation arguments, including the error key's field ID. | ||
| * | ||
| * @return array | ||
| */ | ||
| public static function missing_solution_message( $errors, $posted_field, $value, $args ) { | ||
| $key = 'field' . ( $args['id'] ?? $posted_field->id ); | ||
|
|
||
| if ( ! is_array( $errors ) || ! isset( $errors[ $key ] ) || ! self::is_active() ) { | ||
| return $errors; | ||
| } | ||
|
|
||
| $token = FrmAppHelper::get_post_param( FrmCaptchaFactory::get_settings_object()->token_field, '', 'sanitize_text_field' ); | ||
|
|
||
| if ( '' !== $token || __( 'The captcha is missing from this form', 'formidable' ) !== $errors[ $key ] ) { | ||
| return $errors; | ||
| } | ||
|
|
||
| $errors[ $key ] = FrmAltchaHelper::verification_required_message( $posted_field ); | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| /** | ||
| * Spend the verified solution once the entry has no other errors. | ||
| * | ||
| * If a parallel request with the same payload spent it first, the entry is rejected. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param array $errors Validation errors so far. | ||
| * | ||
| * @return array | ||
| */ | ||
| public static function claim_solution( $errors ) { | ||
| $solutions = self::$pending; | ||
| self::$pending = array(); | ||
|
|
||
| if ( ! is_array( $errors ) || array() !== $errors ) { | ||
| return $errors; | ||
| } | ||
|
|
||
| foreach ( $solutions as $solution ) { | ||
| $errors = self::claim_pending_solution( $errors, $solution ); | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| /** | ||
| * Spend one verified solution, and add the captcha field's error when it cannot be spent. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param array $errors Validation errors so far. | ||
| * @param array{id: string, expires: int, field_id: int, message: string} $pending The verified solution. | ||
| * | ||
| * @return array | ||
| */ | ||
| private static function claim_pending_solution( $errors, $pending ) { | ||
| if ( FrmAltchaReplayHelper::claim( $pending['id'], $pending['expires'] ) ) { | ||
| return $errors; | ||
| } | ||
|
|
||
| $reason = FrmAltchaReplayHelper::storage_failed() ? 'storage_failed' : 'replayed'; | ||
|
|
||
| /** | ||
| * Fires when an ALTCHA solution is rejected after the rest of the entry was valid. | ||
| * | ||
| * Other rejections go through Lite's frm_captcha_validation_failed action. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param string $reason replayed when the solution was already spent, or storage_failed | ||
| * when the replay table could not be created or written. | ||
| * @param object|null $field The captcha field. | ||
| */ | ||
| do_action( 'frm_altcha_validation_failed', $reason, FrmField::getOne( $pending['field_id'] ) ); | ||
|
|
||
| if ( 'storage_failed' === $reason ) { | ||
| // Trying again cannot help when the replay table cannot be written, so point the visitor to the site owner. | ||
| $pending['message'] = __( 'The CAPTCHA on this form is not set up correctly. Please contact the site administrator.', 'formidable' ); | ||
| } | ||
|
|
||
| $errors[ 'field' . $pending['field_id'] ] = $pending['message']; | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| /** | ||
| * Send a fresh challenge to the widget. | ||
| * | ||
| * Challenges are random and single use, so the response must never be cached. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @return void | ||
| */ | ||
| public static function send_challenge() { | ||
| nocache_headers(); | ||
| if ( ! headers_sent() ) { | ||
| header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0', true ); | ||
| } | ||
| // An unknown scope gets a form challenge. | ||
| $scope = FrmAppHelper::get_param( 'scope', FrmAltchaChallengeHelper::SCOPE_FORM, 'get', 'sanitize_key' ); | ||
| wp_send_json( FrmAltchaChallengeHelper::create( array( 'scope' => $scope ) ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Load the widget for a form with a captcha field anywhere, so it is ready when a later page loads over AJAX. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param array $params Form parameters, including form_id. | ||
| * | ||
| * @return void | ||
| */ | ||
| public static function maybe_enqueue_for_form( $params ) { | ||
| $form_id = is_array( $params ) && isset( $params['form_id'] ) ? (int) $params['form_id'] : 0; | ||
|
|
||
| if ( 0 === $form_id || ! self::is_active() ) { | ||
| return; | ||
| } | ||
|
|
||
| // No limit here. With a limit of one, a form without a captcha field can return null instead of an empty array. | ||
| if ( ! FrmField::get_all_types_in_form( $form_id, 'captcha', '', 'include' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| self::styles(); | ||
| // The same handle and dependency Lite uses, so the script is never loaded twice. | ||
| wp_enqueue_script( 'captcha-api', self::script_url(), array( 'formidable' ), FrmAppHelper::plugin_version(), true ); | ||
| } | ||
|
|
||
| /** | ||
| * The widget script URL. WordPress adds the plugin version when it prints the tag, so a new | ||
| * build is never served from cache. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @return string | ||
| */ | ||
| private static function script_url() { | ||
| $url = FrmAppHelper::plugin_url() . '/js/altcha.js'; | ||
| $dependencies = array( 'formidable' ); | ||
| $language = self::translation_language(); | ||
|
|
||
| if ( '' !== $language ) { | ||
| $handle = 'frm-altcha-language-' . $language; | ||
| wp_register_script( $handle, FrmAppHelper::plugin_url() . '/js/altcha/i18n/' . $language . '.js', array(), FrmAppHelper::$plug_version, true ); | ||
| $dependencies[] = $handle; | ||
| } | ||
|
|
||
| // Register before Lite does, so both loading paths retain the translation dependency. | ||
| wp_register_script( 'captcha-api', $url, $dependencies, FrmAppHelper::plugin_version(), true ); | ||
|
|
||
| return $url; | ||
| } | ||
|
|
||
| /** | ||
| * Find the selected translation, falling back to the base language and then English. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @return string Translation filename without its extension, or empty for built-in English. | ||
| */ | ||
| private static function translation_language() { | ||
| $language = strtolower( str_replace( '_', '-', self::language() ) ); | ||
|
|
||
| if ( ! preg_match( '/^[a-z]{2}(?:-[a-z0-9]+)*$/D', $language ) ) { | ||
| return ''; | ||
| } | ||
|
|
||
| $base = explode( '-', $language )[0]; | ||
| $path = FrmAppHelper::plugin_path() . '/js/altcha/i18n/'; | ||
| // Regional locales without their own file, such as fr-be or es-mx, use the closest shipped variant. | ||
| // Spanish outside Spain uses the Latin American translation. Norwegian Nynorsk, and Norwegian | ||
| // without a written standard, use the Bokmal translation. | ||
| $variants = array( | ||
| 'es' => 'es' === $language ? 'es-es' : 'es-419', | ||
| 'fr' => 'fr-fr', | ||
| 'nn' => 'nb', | ||
| 'no' => 'nb', | ||
| 'pt' => 'pt-pt', | ||
| 'zh' => 'zh-cn', | ||
| ); | ||
| $fallbacks = array( $language, $base, $variants[ $base ] ?? $base ); | ||
|
|
||
| foreach ( $fallbacks as $candidate ) { | ||
| if ( 'en' !== $base && is_file( $path . $candidate . '.js' ) ) { | ||
| return $candidate; | ||
| } | ||
| } | ||
|
|
||
| return ''; | ||
| } | ||
|
|
||
| /** | ||
| * The language to tell the widget: the translation that is loaded, or the site language for English. | ||
| * | ||
| * The widget only matches a translation with the same base language, so a locale such as nn-no | ||
| * would show English next to the loaded nb translation. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function widget_language() { | ||
| $translation = self::translation_language(); | ||
| return '' === $translation ? self::language() : $translation; | ||
| } | ||
|
|
||
| /** | ||
| * The stylesheet that maps the active form style onto the widget. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @return void | ||
| */ | ||
| public static function styles() { | ||
| $file = 'altcha.css'; | ||
| wp_enqueue_style( 'frm-altcha', FrmAppHelper::plugin_url() . '/css/' . $file, array(), FrmAppHelper::$plug_version ); | ||
| } | ||
|
|
||
| /** | ||
| * Show an ALTCHA preview in the form builder instead of a missing image. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param string $url Preview image URL. | ||
| * @param string $image_name Active captcha type. | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function builder_image_url( $url, $image_name ) { | ||
| if ( FrmAltchaSettings::TYPE !== $image_name ) { | ||
| return $url; | ||
| } | ||
|
|
||
| return FrmAppHelper::plugin_url() . '/images/altcha.svg'; | ||
| } | ||
|
|
||
| /** | ||
| * The widget language, from the site locale. The widget falls back to the base language, then English. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @return string | ||
| */ | ||
| public static function language() { | ||
| $language = strtolower( str_replace( '_', '-', determine_locale() ) ); | ||
|
|
||
| /** | ||
| * Filters the ALTCHA widget language, such as de or pt-br. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param string $language Language code. | ||
| */ | ||
| $filtered = apply_filters( 'frm_altcha_language', $language ); | ||
|
|
||
| return is_string( $filtered ) && '' !== $filtered ? $filtered : $language; | ||
| } | ||
|
|
||
| /** | ||
| * The captcha field's own error message, or the global one, like Lite. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param array|object $field The captcha field. | ||
| * | ||
| * @return string | ||
| */ | ||
| private static function invalid_message( $field ) { | ||
| $message = FrmField::get_option( $field, 'invalid' ); | ||
|
|
||
| if ( is_string( $message ) && '' !== $message ) { | ||
| return $message; | ||
| } | ||
|
|
||
| $frm_settings = FrmAppHelper::get_settings(); | ||
|
|
||
| return is_string( $frm_settings->re_msg ) ? $frm_settings->re_msg : ''; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.