@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Ai1ec_Captcha_Provider interface.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.2
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Captcha
|
||||
*/
|
||||
abstract class Ai1ec_Captcha_Provider extends Ai1ec_Base {
|
||||
|
||||
/**
|
||||
* Settings object.
|
||||
*
|
||||
* @var Ai1ec_Settings
|
||||
*/
|
||||
protected $_settings = null;
|
||||
|
||||
/**
|
||||
* Theme loader object.
|
||||
*
|
||||
* @var Ai1ec_Theme_Loader
|
||||
*/
|
||||
protected $_theme_loader = null;
|
||||
|
||||
/**
|
||||
* Whether provider is configured or not.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $_is_configured = null;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Ai1ec_Registry_Object $registry
|
||||
*
|
||||
* @return Ai1ec_Captcha_Provider
|
||||
*
|
||||
* @throws Ai1ec_Bootstrap_Exception
|
||||
*/
|
||||
public function __construct( Ai1ec_Registry_Object $registry ) {
|
||||
parent::__construct( $registry );
|
||||
$this->_settings = $registry->get( 'model.settings' );
|
||||
$this->_theme_loader = $registry->get( 'theme.loader' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns settings array.
|
||||
*
|
||||
* @param bool $enable_rendering Whether setting HTML will be rendered or not.
|
||||
*
|
||||
* @return array Array of settings.
|
||||
*/
|
||||
abstract public function get_settings( $enable_rendering = true );
|
||||
|
||||
/**
|
||||
* Returns captcha challenge.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function get_challenge();
|
||||
|
||||
/**
|
||||
* Validates challenge.
|
||||
*
|
||||
* @param array Challenge response data.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function validate_challenge( array $data );
|
||||
|
||||
/**
|
||||
* Returns provider name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function get_name();
|
||||
|
||||
/**
|
||||
* Returns whether provider is properly configured or not.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_configured() {
|
||||
if ( null !== $this->_is_configured ) {
|
||||
return $this->_is_configured;
|
||||
}
|
||||
$this->_is_configured = true;
|
||||
foreach ( $this->get_settings() as $key => $setting ) {
|
||||
$value = $this->_settings->get( $key );
|
||||
if ( empty( $value ) ) {
|
||||
$this->_is_configured = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_is_configured;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Nocaptcha provider.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.2
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.
|
||||
*/
|
||||
class Ai1ec_Captcha_Nocaptcha_Provider extends Ai1ec_Captcha_Provider {
|
||||
|
||||
/**
|
||||
* Returns settings array.
|
||||
*
|
||||
* @param bool $enable_rendering Whether setting HTML will be rendered or not.
|
||||
*
|
||||
* @return array Array of settings.
|
||||
*/
|
||||
public function get_settings( $enable_rendering = true ) {
|
||||
return array(
|
||||
'google_nocaptcha_public_key' => array(
|
||||
'type' => 'string',
|
||||
'version' => AI1ECFS_PLUGIN_NAME,
|
||||
'renderer' => array(
|
||||
'class' => 'input',
|
||||
'tab' => 'extensions',
|
||||
'item' => 'interactive',
|
||||
'type' => 'normal',
|
||||
'label' => __(
|
||||
'reCAPTCHA V2 public key:',
|
||||
AI1ECFS_PLUGIN_NAME
|
||||
),
|
||||
'condition' => $enable_rendering,
|
||||
),
|
||||
'value' => '',
|
||||
),
|
||||
'google_nocaptcha_private_key' => array(
|
||||
'type' => 'string',
|
||||
'version' => AI1ECFS_PLUGIN_NAME,
|
||||
'renderer' => array(
|
||||
'class' => 'input',
|
||||
'tab' => 'extensions',
|
||||
'item' => 'interactive',
|
||||
'type' => 'normal',
|
||||
'label' => __(
|
||||
'reCAPTCHA V2 private key:',
|
||||
AI1ECFS_PLUGIN_NAME
|
||||
),
|
||||
'condition' => $enable_rendering,
|
||||
),
|
||||
'value' => '',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns captcha challenge.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_challenge() {
|
||||
$args = array(
|
||||
'nocaptcha_key' => $this->_settings->get(
|
||||
'google_nocaptcha_public_key'
|
||||
),
|
||||
);
|
||||
|
||||
return $this->_theme_loader->get_file(
|
||||
'captcha/nocaptcha/challenge.twig',
|
||||
$args,
|
||||
false
|
||||
)->get_content();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates challenge.
|
||||
*
|
||||
* @param array Challenge response data.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function validate_challenge( array $data ) {
|
||||
|
||||
$response['message'] = Ai1ec_I18n::__(
|
||||
'Please try verifying you are human again.'
|
||||
);
|
||||
$response['success'] = false;
|
||||
|
||||
if ( empty( $data['g-recaptcha-response'] ) ) {
|
||||
$response['message'] = Ai1ec_I18n::_(
|
||||
'There was an error reading the human verification data. Please try again.'
|
||||
);
|
||||
$response['success'] = false;
|
||||
}
|
||||
$url = add_query_arg(
|
||||
array(
|
||||
'secret' => $this->_settings->get(
|
||||
'google_nocaptcha_private_key'
|
||||
),
|
||||
'response' => $data['g-recaptcha-response'],
|
||||
),
|
||||
'https://www.google.com/recaptcha/api/siteverify'
|
||||
);
|
||||
$json_resp = wp_remote_get( $url );
|
||||
if ( is_wp_error( $json_resp ) ) {
|
||||
return $response;
|
||||
}
|
||||
$resp = json_decode( $json_resp['body'], true );
|
||||
if (
|
||||
isset( $resp['success'] ) &&
|
||||
$resp['success']
|
||||
) {
|
||||
$response = array(
|
||||
'success' => true,
|
||||
);
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns provider name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name() {
|
||||
return 'Google reCAPTCHA V2';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* ReCaptcha provider.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.2
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Captcha.Provider
|
||||
*/
|
||||
class Ai1ec_Captcha_Recaptcha_Provider extends Ai1ec_Captcha_Provider {
|
||||
|
||||
/**
|
||||
* Returns settings array.
|
||||
*
|
||||
* @param bool $enable_rendering Whether setting HTML will be rendered or not.
|
||||
*
|
||||
* @return array Array of settings.
|
||||
*/
|
||||
public function get_settings( $enable_rendering = true ) {
|
||||
|
||||
return array(
|
||||
'google_recaptcha_public_key' => array(
|
||||
'type' => 'string',
|
||||
'version' => AI1ECFS_PLUGIN_NAME,
|
||||
'renderer' => array(
|
||||
'class' => 'input',
|
||||
'tab' => 'extensions',
|
||||
'item' => 'interactive',
|
||||
'type' => 'normal',
|
||||
'label' => __(
|
||||
'reCAPTCHA public key:',
|
||||
AI1ECFS_PLUGIN_NAME
|
||||
),
|
||||
'condition' => $enable_rendering,
|
||||
),
|
||||
'value' => '',
|
||||
),
|
||||
'google_recaptcha_private_key' => array(
|
||||
'type' => 'string',
|
||||
'version' => AI1ECFS_PLUGIN_NAME,
|
||||
'renderer' => array(
|
||||
'class' => 'input',
|
||||
'tab' => 'extensions',
|
||||
'item' => 'interactive',
|
||||
'type' => 'normal',
|
||||
'label' => __(
|
||||
'reCAPTCHA private key:',
|
||||
AI1ECFS_PLUGIN_NAME
|
||||
),
|
||||
'condition' => $enable_rendering,
|
||||
),
|
||||
'value' => '',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns captcha challenge.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_challenge() {
|
||||
$args = array(
|
||||
'verification_words' => Ai1ec_I18n::__( 'Human verification' ),
|
||||
'loading_recaptcha' => Ai1ec_I18n::__( 'Loading reCAPTCHA...' ),
|
||||
'recaptcha_key' => $this->_settings->get(
|
||||
'google_recaptcha_public_key'
|
||||
),
|
||||
);
|
||||
|
||||
return $this->_theme_loader->get_file(
|
||||
'captcha/recaptcha/challenge.twig',
|
||||
$args,
|
||||
false
|
||||
)->get_content();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates challenge.
|
||||
*
|
||||
* @param array Challenge response data.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function validate_challenge( array $data ) {
|
||||
$response = array( 'success' => true );
|
||||
if (
|
||||
empty( $data['recaptcha_challenge_field'] ) ||
|
||||
empty( $data['recaptcha_response_field'] )
|
||||
) {
|
||||
$response['message'] = Ai1ec_I18n::_(
|
||||
'There was an error reading the human verification data. Please try again.'
|
||||
);
|
||||
$response['success'] = false;
|
||||
}
|
||||
|
||||
$remoteAddress = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : null;
|
||||
|
||||
require_once( AI1EC_VENDOR_PATH . 'recaptcha/recaptchalib.php' );
|
||||
$resp = recaptcha_check_answer(
|
||||
$this->_settings->get( 'google_recaptcha_private_key' ),
|
||||
$remoteAddress,
|
||||
$data['recaptcha_challenge_field'],
|
||||
$data['recaptcha_response_field']
|
||||
);
|
||||
|
||||
if ( ! $resp->is_valid ) {
|
||||
$response['message'] = Ai1ec_I18n::__(
|
||||
'Please try verifying you are human again.'
|
||||
);
|
||||
$response['success'] = false;
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns provider name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name() {
|
||||
return 'Google reCAPTCHA';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Captcha providers handler class.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.2
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Captcha
|
||||
*/
|
||||
class Ai1ec_Captcha_Providers extends Ai1ec_Base {
|
||||
|
||||
/**
|
||||
* List of available captcha providers.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_providers = null;
|
||||
|
||||
/**
|
||||
* Returns list of available providers.
|
||||
*
|
||||
* @return array List of providers.
|
||||
*/
|
||||
public function get_providers() {
|
||||
if ( null !== $this->_providers ) {
|
||||
return $this->_providers;
|
||||
}
|
||||
$built_in = array(
|
||||
'Ai1ec_Captcha_Recaptcha_Provider',
|
||||
'Ai1ec_Captcha_Nocaptcha_Provider',
|
||||
);
|
||||
$all_providers = apply_filters( 'ai1ec_captcha_providers', $built_in );
|
||||
if ( empty( $all_providers ) ) {
|
||||
return array();
|
||||
}
|
||||
$providers = array();
|
||||
foreach ( $all_providers as $provider_class ) {
|
||||
$provider = new $provider_class( $this->_registry );
|
||||
if ( ! $provider instanceof Ai1ec_Captcha_Provider ) {
|
||||
continue;
|
||||
}
|
||||
$providers[] = $provider;
|
||||
}
|
||||
|
||||
return $providers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns providers settings.
|
||||
*
|
||||
* @return array Providers settings.
|
||||
*/
|
||||
public function get_providers_as_settings() {
|
||||
$all_providers = $this->get_providers();
|
||||
$settings = array();
|
||||
foreach ( $all_providers as $provider ) {
|
||||
$settings[] = array(
|
||||
'text' => $provider->get_name(),
|
||||
'value' => get_class( $provider ),
|
||||
'settings' => $provider->get_settings(),
|
||||
);
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user