Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -11,58 +11,73 @@
|
||||
namespace IPS\Helpers\Form;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use DateInterval;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use IPS\Data\Cache;
|
||||
use IPS\DateTime;
|
||||
use IPS\Helpers\Form;
|
||||
use IPS\IPS;
|
||||
use IPS\Member;
|
||||
use IPS\Output;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use function defined;
|
||||
use function func_get_args;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Captcha class for Form Builder
|
||||
*/
|
||||
class _Captcha extends FormAbstract
|
||||
class Captcha extends FormAbstract
|
||||
{
|
||||
/**
|
||||
* CAPTCHA Class
|
||||
*/
|
||||
protected $captcha = NULL;
|
||||
protected mixed $captcha = NULL;
|
||||
|
||||
/**
|
||||
* Does the configured CAPTCHA service support being added in a modal?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function supportsModal()
|
||||
public static function supportsModal(): bool
|
||||
{
|
||||
if ( \IPS\Settings::i()->bot_antispam_type === 'none' )
|
||||
if ( Settings::i()->bot_antispam_type === 'none' )
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
$class = '\IPS\Helpers\Form\Captcha\\' . mb_ucfirst( \IPS\Settings::i()->bot_antispam_type );
|
||||
$class = '\IPS\Helpers\Form\Captcha\\' . IPS::mb_ucfirst( Settings::i()->bot_antispam_type );
|
||||
return ( !isset( $class::$supportsModal ) or $class::$supportsModal ); // isset() check is for backwards compatibility
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @see \IPS\Helpers\Form\FormAbstract::__construct
|
||||
* @see FormAbstract::__construct
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$params = \func_get_args();
|
||||
$params = func_get_args();
|
||||
if ( !isset( $params[0] ) )
|
||||
{
|
||||
$params[0] = 'captcha_field';
|
||||
}
|
||||
|
||||
if ( \IPS\Settings::i()->bot_antispam_type != 'none' )
|
||||
if ( Settings::i()->bot_antispam_type != 'none' )
|
||||
{
|
||||
$class = '\IPS\Helpers\Form\Captcha\\' . mb_ucfirst( \IPS\Settings::i()->bot_antispam_type );
|
||||
$class = '\IPS\Helpers\Form\Captcha\\' . IPS::mb_ucfirst( Settings::i()->bot_antispam_type );
|
||||
if ( !class_exists( $class ) )
|
||||
{
|
||||
\IPS\Output::i()->error( 'unexpected_captcha', '4S262/1', 500, 'unexpected_captcha_admin' );
|
||||
Output::i()->error( 'unexpected_captcha', '4S262/1', 500, 'unexpected_captcha_admin' );
|
||||
}
|
||||
$this->captcha = new $class;
|
||||
}
|
||||
@@ -91,7 +106,7 @@ class _Captcha extends FormAbstract
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function html()
|
||||
public function html(): string
|
||||
{
|
||||
if ( $this->captcha === NULL )
|
||||
{
|
||||
@@ -103,10 +118,10 @@ class _Captcha extends FormAbstract
|
||||
/**
|
||||
* Get HTML
|
||||
*
|
||||
* @param \IPS\Helpers\Form|null $form Form helper object
|
||||
* @param Form|null $form Form helper object
|
||||
* @return string
|
||||
*/
|
||||
public function rowHtml( $form=NULL )
|
||||
public function rowHtml( Form $form=NULL ): string
|
||||
{
|
||||
if ( $this->captcha === NULL )
|
||||
{
|
||||
@@ -114,13 +129,13 @@ class _Captcha extends FormAbstract
|
||||
}
|
||||
return ( method_exists( $this->captcha, 'rowHtml' ) and !$this->error ) ? $this->captcha->rowHtml() : parent::rowHtml( $form );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Value
|
||||
*
|
||||
* @return bool|null TRUE/FALSE indicate if the test passed or not. NULL indicates the test failed, but the captcha system will display an error so we don't have to.
|
||||
* @return bool|null TRUE/FALSE indicate if the test passed or not. NULL indicates the test failed, but the captcha system will display an error so we don't have to.
|
||||
*/
|
||||
public function getValue()
|
||||
public function getValue(): ?bool
|
||||
{
|
||||
if ( $this->captcha === NULL )
|
||||
{
|
||||
@@ -130,16 +145,16 @@ class _Captcha extends FormAbstract
|
||||
{
|
||||
/* If we previously did an AJAX validate which is still valid, return true */
|
||||
$cached = NULL;
|
||||
$cacheKey = 'captcha-val-' . $this->name . '-' . \IPS\Member::loggedIn()->ip_address;
|
||||
$cacheKey = 'captcha-val-' . $this->name . '-' . Member::loggedIn()->ip_address;
|
||||
try
|
||||
{
|
||||
$cached = \IPS\Data\Cache::i()->getWithExpire( $cacheKey, TRUE );
|
||||
$cached = Cache::i()->getWithExpire( $cacheKey, TRUE );
|
||||
}
|
||||
catch( \Exception $ex ) { }
|
||||
catch( Exception $ex ) { }
|
||||
|
||||
if ( $cached )
|
||||
{
|
||||
unset( \IPS\Data\Cache::i()->$cacheKey );
|
||||
unset( Cache::i()->$cacheKey );
|
||||
return TRUE;
|
||||
}
|
||||
/* Otherwise, check with service */
|
||||
@@ -149,9 +164,9 @@ class _Captcha extends FormAbstract
|
||||
$return = $this->captcha->verify();
|
||||
|
||||
/* If it's valid and we're doing an AJAX validate, save that in the session so the next request doesn't check again */
|
||||
if ( $return and \IPS\Request::i()->isAjax() )
|
||||
if ( $return and Request::i()->isAjax() )
|
||||
{
|
||||
\IPS\Data\Cache::i()->storeWithExpire( $cacheKey, time(), \IPS\DateTime::create()->add( new \DateInterval( 'PT1M' ) ), TRUE );
|
||||
Cache::i()->storeWithExpire( $cacheKey, time(), DateTime::create()->add( new DateInterval( 'PT1M' ) ), TRUE );
|
||||
}
|
||||
|
||||
/* Return */
|
||||
@@ -163,14 +178,14 @@ class _Captcha extends FormAbstract
|
||||
/**
|
||||
* Validate
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws InvalidArgumentException
|
||||
* @return TRUE
|
||||
*/
|
||||
public function validate()
|
||||
public function validate(): bool
|
||||
{
|
||||
if ( $this->value !== TRUE )
|
||||
{
|
||||
throw new \InvalidArgumentException( 'form_bad_captcha' );
|
||||
throw new InvalidArgumentException( 'form_bad_captcha' );
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
|
||||
Reference in new issue
Block a user