Overview

Namespaces

  • None
  • Quform
    • Traduttore_Registry

Classes

  • Quform
  • Quform_Admin_InsertForm
  • Quform_Admin_Page
  • Quform_Admin_Page_Controller
  • Quform_Admin_Page_Dashboard
  • Quform_Admin_Page_Entries
  • Quform_Admin_Page_Entries_Edit
  • Quform_Admin_Page_Entries_List
  • Quform_Admin_Page_Entries_View
  • Quform_Admin_Page_Factory
  • Quform_Admin_Page_Forms_Add
  • Quform_Admin_Page_Forms_Edit
  • Quform_Admin_Page_Forms_List
  • Quform_Admin_Page_Help
  • Quform_Admin_Page_Preview
  • Quform_Admin_Page_Settings
  • Quform_Admin_Page_Tools
  • Quform_Admin_Page_Tools_ExportEntries
  • Quform_Admin_Page_Tools_ExportForm
  • Quform_Admin_Page_Tools_Home
  • Quform_Admin_Page_Tools_ImportForm
  • Quform_Admin_Page_Tools_Migrate
  • Quform_Admin_Page_Tools_Uninstall
  • Quform_Api
  • Quform_Block
  • Quform_Builder
  • Quform_Captcha
  • Quform_ClassLoader
  • Quform_Confirmation
  • Quform_Container
  • Quform_Dashboard_Widget
  • Quform_Dispatcher
  • Quform_Element
  • Quform_Element_Captcha
  • Quform_Element_Checkbox
  • Quform_Element_Column
  • Quform_Element_Container
  • Quform_Element_Container_Iterator
  • Quform_Element_Date
  • Quform_Element_Email
  • Quform_Element_Factory
  • Quform_Element_Field
  • Quform_Element_File
  • Quform_Element_Group
  • Quform_Element_Hidden
  • Quform_Element_Honeypot
  • Quform_Element_Html
  • Quform_Element_Multi
  • Quform_Element_Multiselect
  • Quform_Element_Name
  • Quform_Element_Page
  • Quform_Element_Password
  • Quform_Element_Radio
  • Quform_Element_Recaptcha
  • Quform_Element_Row
  • Quform_Element_Select
  • Quform_Element_Submit
  • Quform_Element_Text
  • Quform_Element_Textarea
  • Quform_Element_Time
  • Quform_Entry_Controller
  • Quform_Entry_Exporter
  • Quform_Entry_List_Settings
  • Quform_Entry_List_Table
  • Quform_Entry_Processor
  • Quform_Entry_UserSearcher
  • Quform_Filter_Abstract
  • Quform_Filter_Alpha
  • Quform_Filter_AlphaNumeric
  • Quform_Filter_Digits
  • Quform_Filter_Regex
  • Quform_Filter_Static
  • Quform_Filter_StripTags
  • Quform_Filter_Trim
  • Quform_Form
  • Quform_Form_Controller
  • Quform_Form_Exporter
  • Quform_Form_Factory
  • Quform_Form_Importer
  • Quform_Form_Iterator
  • Quform_Form_List_Settings
  • Quform_Form_List_Table
  • Quform_Form_Processor
  • Quform_License
  • Quform_Migrator
  • Quform_NonceRefresher
  • Quform_Notification
  • Quform_Notification_Resender
  • Quform_Options
  • Quform_Permissions
  • Quform_Repository
  • Quform_ScriptLoader
  • Quform_Session
  • Quform_Settings
  • Quform_Shortcode
  • Quform_Themes
  • Quform_TokenReplacer
  • Quform_Toolbar
  • Quform_Translations
  • Quform_Updater
  • Quform_Upgrader
  • Quform_Uploader
  • Quform_Validator_Abstract
  • Quform_Validator_Alpha
  • Quform_Validator_AlphaNumeric
  • Quform_Validator_Array
  • Quform_Validator_Captcha
  • Quform_Validator_Date
  • Quform_Validator_Digits
  • Quform_Validator_Duplicate
  • Quform_Validator_Email
  • Quform_Validator_FileUpload
  • Quform_Validator_GreaterThan
  • Quform_Validator_Honeypot
  • Quform_Validator_Identical
  • Quform_Validator_InArray
  • Quform_Validator_Length
  • Quform_Validator_LessThan
  • Quform_Validator_Recaptcha
  • Quform_Validator_Regex
  • Quform_Validator_Required
  • Quform_Validator_Static
  • Quform_Validator_Time
  • Quform_View
  • Quform_ViewFactory
  • Quform_Widget_Form
  • Quform_Widget_Popup

Interfaces

  • Quform_Attachable
  • Quform_Element_Editable
  • Quform_Filter_Interface
  • Quform_Validator_Interface

Constants

  • Quform\Traduttore_Registry\TRANSIENT_KEY_PLUGIN
  • Quform\Traduttore_Registry\TRANSIENT_KEY_THEME

Functions

  • Quform\Traduttore_Registry\add_project
  • Quform\Traduttore_Registry\clean_translations_cache
  • Quform\Traduttore_Registry\get_available_locales
  • Quform\Traduttore_Registry\get_installed_translations
  • Quform\Traduttore_Registry\get_translations
  • Quform\Traduttore_Registry\register_clean_translations_cache
  • Quform\Traduttore_Registry\sanitize_date
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: /**
  4:  * @copyright Copyright (c) 2009-2022 ThemeCatcher (https://www.themecatcher.net)
  5:  */
  6: class Quform_Validator_Recaptcha extends Quform_Validator_Abstract
  7: {
  8:     const MISSING_INPUT_SECRET = 'recaptchaMissingInputSecret';
  9:     const INVALID_INPUT_SECRET = 'recaptchaInvalidInputSecret';
 10:     const MISSING_INPUT_RESPONSE = 'recaptchaMissingInputResponse';
 11:     const INVALID_INPUT_RESPONSE = 'recaptchaInvalidInputResponse';
 12:     const ERROR = 'recaptchaError';
 13:     const SCORE_TOO_LOW = 'recaptchaScoreTooLow';
 14: 
 15:     /**
 16:      * Mapping of reCAPTCHA error codes to message template keys
 17:      *
 18:      * @var array
 19:      */
 20:     protected $errorCodes = array(
 21:         'missing-input-secret' => self::MISSING_INPUT_SECRET,
 22:         'invalid-input-secret' => self::INVALID_INPUT_SECRET,
 23:         'missing-input-response' => self::MISSING_INPUT_RESPONSE,
 24:         'invalid-input-response' => self::INVALID_INPUT_RESPONSE
 25:     );
 26: 
 27:     /**
 28:      * @param   string   $value  The reCAPTCHA response
 29:      * @return  boolean          True if valid false otherwise
 30:      */
 31:     public function isValid($value)
 32:     {
 33:         $this->reset();
 34: 
 35:         $provider = $this->config('provider');
 36: 
 37:         if ($provider == 'hcaptcha') {
 38:             $url = 'https://hcaptcha.com/siteverify';
 39:         } elseif ($provider == 'turnstile') {
 40:             $url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
 41:         } else {
 42:             $url = 'https://www.google.com/recaptcha/api/siteverify';
 43:         }
 44: 
 45:         $params = array(
 46:             'secret' => $this->config('secretKey'),
 47:             'response' => $value,
 48:             'remoteip' => Quform::getClientIp()
 49:         );
 50: 
 51:         $response = wp_remote_post($url, array('body' => $params));
 52:         $response = wp_remote_retrieve_body($response);
 53:         $response = json_decode($response, true);
 54: 
 55:         if ( ! is_array($response) || ! isset($response['success'])) {
 56:             $this->error(self::ERROR);
 57:             return false;
 58:         }
 59: 
 60:         if ( ! $response['success']) {
 61:             if (isset($response['error-codes']) && is_array($response['error-codes']) && count($response['error-codes'])) {
 62:                 foreach ($response['error-codes'] as $error) {
 63:                     if (array_key_exists($error, $this->errorCodes)) {
 64:                         $this->error($this->errorCodes[$error]);
 65:                     } else {
 66:                         $this->error(self::ERROR);
 67:                     }
 68: 
 69:                     return false;
 70:                 }
 71:             } else {
 72:                 $this->error(self::ERROR);
 73:                 return false;
 74:             }
 75:         }
 76: 
 77:         if ( ! in_array($provider, array('hcaptcha', 'turnstile'), true) && $this->config('version') == 'v3') {
 78:             if (isset($response['score'], $response['action']) && $response['action'] == 'quform' && is_numeric($response['score'])) {
 79:                 $threshold = (float) $this->config('threshold');
 80: 
 81:                 if ($response['score'] < $threshold) {
 82:                     $this->error(self::SCORE_TOO_LOW);
 83:                     return false;
 84:                 }
 85:             } else {
 86:                 $this->error(self::ERROR);
 87:                 return false;
 88:             }
 89:         }
 90: 
 91:         return true;
 92:     }
 93: 
 94:     /**
 95:      * Get all message templates or the single message with the given key
 96:      *
 97:      * @param   string|null   $key
 98:      * @return  array|string
 99:      */
100:     public static function getMessageTemplates($key = null)
101:     {
102:         $messageTemplates = array(
103:             self::MISSING_INPUT_SECRET => __('The secret parameter is missing',  'quform'),
104:             self::INVALID_INPUT_SECRET => __('The secret parameter is invalid or malformed',  'quform'),
105:             self::MISSING_INPUT_RESPONSE => __('The response parameter is missing',  'quform'),
106:             self::INVALID_INPUT_RESPONSE => __('The response parameter is invalid or malformed',  'quform'),
107:             self::ERROR => __('An error occurred, please try again',  'quform'),
108:             self::SCORE_TOO_LOW => __('Sorry, your submission failed our automated spam checks',  'quform'),
109:         );
110: 
111:         if (is_string($key)) {
112:             return array_key_exists($key, $messageTemplates) ? $messageTemplates[$key] : null;
113:         }
114: 
115:         return $messageTemplates;
116:     }
117: 
118:     /**
119:      * Get the default config for this validator
120:      *
121:      * @param   string|null  $key  Get the config by key, if omitted the full config is returned
122:      * @return  array
123:      */
124:     public static function getDefaultConfig($key = null)
125:     {
126:         $config = apply_filters('quform_default_config_validator_recaptcha', array(
127:             'secretKey' => '',
128:             'version' => 'v2',
129:             'threshold' => '0.5',
130:             'messages' => array(
131:                 self::MISSING_INPUT_SECRET => '',
132:                 self::INVALID_INPUT_SECRET => '',
133:                 self::MISSING_INPUT_RESPONSE => '',
134:                 self::INVALID_INPUT_RESPONSE => '',
135:                 self::ERROR => '',
136:                 self::SCORE_TOO_LOW => ''
137:             )
138:         ));
139: 
140:         $config['type'] = 'recaptcha';
141: 
142:         if (Quform::isNonEmptyString($key)) {
143:             return Quform::get($config, $key);
144:         }
145: 
146:         return $config;
147:     }
148: }
149: 
API documentation generated by ApiGen