Version 4.5.0
This commit is contained in:
1 parent
1a0c7fd3c2
commit
f79dcf067a
3791 files changed
+248032
-76372
No files matched your search
@@ -11,7 +11,7 @@
|
||||
namespace IPS\Helpers\Form;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
@@ -50,8 +50,9 @@ class _Text extends TextArea
|
||||
'nullLang' => 'no_value', // If provided, an "or X" checkbox will appear with X being the value of this language key. When checked, NULL will be returned as the value.
|
||||
'accountUsername' => TRUE, // If TRUE or an \IPS\Member, additional checks will be performed to ensure provided value is acceptable for use as a username. Pass an \IPS\Member object to exclude that member from the duplicate checks
|
||||
'trim' => TRUE, // If TRUE (which is the default), whitespace will be stripped from the start and end of the value
|
||||
'bypassProfanity' => FALSE, // If TRUE, profanity filter will be bypassed when it is not appropriate to do so (ex: on the login form). Defaults to FALSE.
|
||||
);
|
||||
'bypassProfanity' => 0, // Profanity filter bypass, see BYPASS_PROFANITY_* constants below.
|
||||
'htmlAutocomplete' => NULL, // an HTML autocomplete attribute If Null no autocomplete is set otherwise use a value from https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-autocomplete-url
|
||||
);
|
||||
* @endcode
|
||||
*/
|
||||
protected $defaultOptions = array(
|
||||
@@ -65,7 +66,8 @@ class _Text extends TextArea
|
||||
'nullLang' => NULL,
|
||||
'accountUsername' => FALSE,
|
||||
'trim' => TRUE,
|
||||
'bypassProfanity' => FALSE,
|
||||
'bypassProfanity' => 0,
|
||||
'htmlAutocomplete' => NULL,
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -77,34 +79,49 @@ class _Text extends TextArea
|
||||
* @brief Form type
|
||||
*/
|
||||
public $formType = 'text';
|
||||
|
||||
/**
|
||||
* @brief Do not bypass profanity filters
|
||||
*/
|
||||
const BYPASS_PROFANITY_NONE = 0;
|
||||
|
||||
/**
|
||||
* @brief Perform profanity swaps only
|
||||
*/
|
||||
const BYPASS_PROFANITY_SWAP = 1;
|
||||
|
||||
/**
|
||||
* @brief Bypass profanity entirely
|
||||
*/
|
||||
const BYPASS_PROFANITY_ALL = 2;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* Sets that the field is required if there is a minimum length and vice-versa
|
||||
*
|
||||
* @see \IPS\Helpers\Form\FormAbstract::__construct
|
||||
* @param string $name Name
|
||||
* @param mixed $defaultValue Default value
|
||||
* @param bool|NULL $required Required? (NULL for not required, but appears to be so)
|
||||
* @param array $options Type-specific options
|
||||
* @param callback $customValidationCode Custom validation code
|
||||
* @param string $prefix HTML to show before input field
|
||||
* @param string $suffix HTML to show after input field
|
||||
* @param string $id The ID to add to the row
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct( $name, $defaultValue=NULL, $required=FALSE, $options=array(), $customValidationCode=NULL, $prefix=NULL, $suffix=NULL, $id=NULL )
|
||||
{
|
||||
/* Pull in default options from child class */
|
||||
$this->defaultOptions = array_merge( $this->defaultOptions, $this->childDefaultOptions );
|
||||
|
||||
/* Set username regex */
|
||||
$args = func_get_args();
|
||||
if ( isset( $args[3]['accountUsername'] ) and $args[3]['accountUsername'] !== FALSE )
|
||||
if ( isset( $options['accountUsername'] ) and $options['accountUsername'] !== FALSE )
|
||||
{
|
||||
$args[3]['minLength'] = \IPS\Settings::i()->min_user_name_length;
|
||||
$args[3]['maxLength'] = \IPS\Settings::i()->max_user_name_length;
|
||||
|
||||
if ( \IPS\Settings::i()->username_characters )
|
||||
{
|
||||
$args[3]['regex'] = '/^[' . str_replace( '\-', '-', preg_quote( \IPS\Settings::i()->username_characters, '/' ) ) . ']*$/iu';
|
||||
}
|
||||
$options['minLength'] = \IPS\Settings::i()->min_user_name_length;
|
||||
$options['maxLength'] = \IPS\Settings::i()->max_user_name_length;
|
||||
}
|
||||
|
||||
/* Call parent constructor */
|
||||
call_user_func_array( 'parent::__construct', $args );
|
||||
parent::__construct( $name, $defaultValue, $required, $options, $customValidationCode, $prefix, $suffix, $id );
|
||||
|
||||
/* Add JS */
|
||||
if ( isset( $this->options['autocomplete']['prefix'] ) and $this->options['autocomplete']['prefix'] )
|
||||
@@ -113,7 +130,7 @@ class _Text extends TextArea
|
||||
}
|
||||
|
||||
/* Set the form type */
|
||||
$this->formType = mb_strtolower( mb_substr( get_called_class(), mb_strrpos( get_called_class(), '\\' ) + 1 ) );
|
||||
$this->formType = mb_strtolower( mb_substr( \get_called_class(), mb_strrpos( \get_called_class(), '\\' ) + 1 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,7 +145,7 @@ class _Text extends TextArea
|
||||
{
|
||||
/* 10/19/15 - adding htmlspecialchars around value if autocomplete is enabled so that html tag characters can be used (e.g. for members) */
|
||||
/* This value is decoded by the JS widget before use */
|
||||
if( $this->options['autocomplete'] and !empty( $this->value ) and is_array( $this->value ) )
|
||||
if( $this->options['autocomplete'] and !empty( $this->value ) and \is_array( $this->value ) )
|
||||
{
|
||||
foreach( $this->value as $key => $value )
|
||||
{
|
||||
@@ -136,7 +153,7 @@ class _Text extends TextArea
|
||||
}
|
||||
}
|
||||
|
||||
return \IPS\Theme::i()->getTemplate( 'forms', 'core', 'global' )->text( $this->name, $this->formType, $this->value, $this->required, $this->options['maxLength'], $this->options['size'], $this->options['disabled'], $this->options['autocomplete'], $this->options['placeholder'], NULL, $this->options['nullLang'], $this->htmlId );
|
||||
return \IPS\Theme::i()->getTemplate( 'forms', 'core', 'global' )->text( $this->name, $this->formType, $this->value, $this->required, $this->options['maxLength'], $this->options['size'], $this->options['disabled'], $this->options['autocomplete'], $this->options['placeholder'], NULL, $this->options['nullLang'], $this->htmlId, FALSE, $this->options['htmlAutocomplete'] );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,7 +175,7 @@ class _Text extends TextArea
|
||||
|
||||
if ( $this->options['trim'] )
|
||||
{
|
||||
if ( is_array( $return ) )
|
||||
if ( \is_array( $return ) )
|
||||
{
|
||||
$return = array_map( 'trim', $return );
|
||||
}
|
||||
@@ -170,19 +187,27 @@ class _Text extends TextArea
|
||||
|
||||
if ( isset( $this->options['autocomplete'] ) )
|
||||
{
|
||||
$return = htmlspecialchars_decode( $return, ENT_QUOTES );
|
||||
|
||||
if ( !is_array( $return ) and ( !isset( $this->options['autocomplete']['maxItems'] ) or $this->options['autocomplete']['maxItems'] != 1 ) )
|
||||
if ( \is_array( $return ) )
|
||||
{
|
||||
$return = array_map( function( $val ) {
|
||||
return htmlspecialchars_decode( $val, ENT_QUOTES );
|
||||
}, $return );
|
||||
}
|
||||
else
|
||||
{
|
||||
$return = htmlspecialchars_decode( $return, ENT_QUOTES );
|
||||
}
|
||||
|
||||
if ( !\is_array( $return ) and ( !isset( $this->options['autocomplete']['maxItems'] ) or $this->options['autocomplete']['maxItems'] != 1 ) )
|
||||
{
|
||||
$return = array_filter( array_map( 'trim', explode( "\n", $return ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Remove all invisible characters if this is a username */
|
||||
if( $this->options['accountUsername'] )
|
||||
{
|
||||
if ( !is_array( $return ) )
|
||||
if ( !\is_array( $return ) )
|
||||
{
|
||||
$return = preg_replace( '/\p{C}+/u', '', $return );
|
||||
}
|
||||
@@ -197,8 +222,8 @@ class _Text extends TextArea
|
||||
|
||||
if ( isset( $this->options['autocomplete']['prefix'] ) and $this->options['autocomplete']['prefix'] )
|
||||
{
|
||||
$return = is_array( $return ) ? $return : ( $return ? array( $return ) : array() );
|
||||
|
||||
$return = \is_array( $return ) ? $return : ( $return ? array( $return ) : array() );
|
||||
|
||||
$firstAsPrefix = $this->name . '_first_as_prefix';
|
||||
$freechoicePrefixCheckbox = $this->name . '_freechoice_prefix';
|
||||
$freechoicePrefix = $this->name . '_prefix';
|
||||
@@ -237,13 +262,23 @@ class _Text extends TextArea
|
||||
*/
|
||||
public function formatValue()
|
||||
{
|
||||
if ( $this->options['autocomplete'] !== NULL and ( !isset( $this->options['autocomplete']['maxItems'] ) or $this->options['autocomplete']['maxItems'] != 1 ) and !is_array( $this->value ) and $this->value !== NULL )
|
||||
if ( $this->options['autocomplete'] !== NULL and ( !isset( $this->options['autocomplete']['maxItems'] ) or $this->options['autocomplete']['maxItems'] != 1 ) and !\is_array( $this->value ) and $this->value !== NULL )
|
||||
{
|
||||
return array_filter( array_map( 'trim', explode( "\n", $this->value ) ) );
|
||||
}
|
||||
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value to use in the label 'for' attribute
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLabelForAttribute()
|
||||
{
|
||||
return 'elInput_' . parent::getLabelForAttribute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate
|
||||
@@ -257,7 +292,7 @@ class _Text extends TextArea
|
||||
parent::validate();
|
||||
|
||||
/* Check it isn't just invisible characters (we don't strip them, because things like zero-width-joiners when in between other characters have a special meaning */
|
||||
if ( $this->required and is_string( $this->value ) and mb_strlen( preg_replace( '/\p{C}+/u', '', $this->value ) ) === 0 )
|
||||
if ( $this->required and \is_string( $this->value ) and mb_strlen( preg_replace( '/\p{C}+/u', '', $this->value ) ) === 0 )
|
||||
{
|
||||
throw new \DomainException('form_required');
|
||||
}
|
||||
@@ -293,7 +328,7 @@ class _Text extends TextArea
|
||||
|
||||
/* Construct break points */
|
||||
$exactProfanityBreakpoints = array();
|
||||
if( count( $exactProfanity ) )
|
||||
if( \count( $exactProfanity ) )
|
||||
{
|
||||
$array = array();
|
||||
foreach( array_keys( $exactProfanity ) as $entry )
|
||||
@@ -314,6 +349,12 @@ class _Text extends TextArea
|
||||
/* Username */
|
||||
if ( $this->options['accountUsername'] )
|
||||
{
|
||||
/* Check it is valid */
|
||||
if ( !\IPS\Login::usernameIsAllowed( $this->value ) )
|
||||
{
|
||||
throw new \DomainException( 'form_name_banned' );
|
||||
}
|
||||
|
||||
/* Check if it exists */
|
||||
if ( !( $this->options['accountUsername'] instanceof \IPS\Member ) or mb_strtolower( $this->options['accountUsername']->name ) !== mb_strtolower( $this->value ) )
|
||||
{
|
||||
@@ -338,18 +379,18 @@ class _Text extends TextArea
|
||||
{
|
||||
if( $this->value )
|
||||
{
|
||||
$values = ( is_array( $this->value ) ) ? $this->value : array( $this->value );
|
||||
$values = ( \is_array( $this->value ) ) ? $this->value : array( $this->value );
|
||||
}
|
||||
else
|
||||
{
|
||||
$values = array();
|
||||
}
|
||||
|
||||
if ( isset( $this->options['autocomplete']['maxItems'] ) and count( $values ) > $this->options['autocomplete']['maxItems'] )
|
||||
if ( isset( $this->options['autocomplete']['maxItems'] ) and \count( $values ) > $this->options['autocomplete']['maxItems'] )
|
||||
{
|
||||
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack( 'form_tags_max', FALSE, array( 'pluralize' => array( $this->options['autocomplete']['maxItems'] ) ) ) );
|
||||
}
|
||||
if ( isset( $this->options['autocomplete']['minItems'] ) and ( $this->required or count( $values ) > 0 ) and count( $values ) < $this->options['autocomplete']['minItems'] )
|
||||
if ( isset( $this->options['autocomplete']['minItems'] ) and ( $this->required or \count( $values ) > 0 ) and \count( $values ) < $this->options['autocomplete']['minItems'] )
|
||||
{
|
||||
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack( 'form_tags_min', FALSE, array( 'pluralize' => array( $this->options['autocomplete']['minItems'] ) ) ) );
|
||||
}
|
||||
@@ -374,7 +415,7 @@ class _Text extends TextArea
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( isset( $this->options['autocomplete']['filterProfanity'] ) AND is_array( $this->value ) AND count( $this->value ) and \IPS\Dispatcher::i() instanceof \IPS\Dispatcher\Front and !\IPS\Member::loggedIn()->group['g_bypass_badwords'] )
|
||||
if ( isset( $this->options['autocomplete']['filterProfanity'] ) AND \is_array( $this->value ) AND \count( $this->value ) and \IPS\Dispatcher::i() instanceof \IPS\Dispatcher\Front and !\IPS\Member::loggedIn()->group['g_bypass_badwords'] )
|
||||
{
|
||||
foreach ( $values as $k => $v )
|
||||
{
|
||||
@@ -413,7 +454,7 @@ class _Text extends TextArea
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $this->options['autocomplete']['unique'] ) AND $this->options['autocomplete']['unique'] AND is_array( $this->value ) AND count( $this->value ) )
|
||||
if ( isset( $this->options['autocomplete']['unique'] ) AND $this->options['autocomplete']['unique'] AND \is_array( $this->value ) AND \count( $this->value ) )
|
||||
{
|
||||
foreach ( $this->value as $v )
|
||||
{
|
||||
@@ -425,7 +466,7 @@ class _Text extends TextArea
|
||||
}
|
||||
}
|
||||
|
||||
if ( is_array ( $this->value ) AND isset( $this->options['autocomplete']['source'] ) AND is_array( $this->options['autocomplete']['source'] ) AND ( !isset( $this->options['autocomplete']['freeChoice'] ) OR !$this->options['autocomplete']['freeChoice'] ) )
|
||||
if ( \is_array( $this->value ) AND isset( $this->options['autocomplete']['source'] ) AND \is_array( $this->options['autocomplete']['source'] ) AND ( !isset( $this->options['autocomplete']['freeChoice'] ) OR !$this->options['autocomplete']['freeChoice'] ) )
|
||||
{
|
||||
if( isset( $this->options['autocomplete']['forceLower'] ) AND $this->options['autocomplete']['forceLower'] )
|
||||
{
|
||||
@@ -439,15 +480,15 @@ class _Text extends TextArea
|
||||
}
|
||||
|
||||
/* Split on profanity */
|
||||
if( $this->options['bypassProfanity'] === FALSE and is_string( $this->value ) and \IPS\Dispatcher::i() instanceof \IPS\Dispatcher\Front and !\IPS\Member::loggedIn()->group['g_bypass_badwords'] AND !$this->options['accountUsername'] )
|
||||
if( \in_array( $this->options['bypassProfanity'], array( static::BYPASS_PROFANITY_NONE, static::BYPASS_PROFANITY_SWAP ) ) and \is_string( $this->value ) and \IPS\Dispatcher::i() instanceof \IPS\Dispatcher\Front and !\IPS\Member::loggedIn()->group['g_bypass_badwords'] AND !$this->options['accountUsername'] )
|
||||
{
|
||||
$newVal = NULL;
|
||||
if ( count( $exactProfanityBreakpoints ) )
|
||||
if ( \count( $exactProfanityBreakpoints ) )
|
||||
{
|
||||
/* preg_split can return boolean false*/
|
||||
$split = preg_split( '/' . implode( '|', $exactProfanityBreakpoints ) . '/iu', $this->value, null, PREG_SPLIT_DELIM_CAPTURE );
|
||||
|
||||
if( is_array( $split ) )
|
||||
if( \is_array( $split ) )
|
||||
{
|
||||
foreach ( $split as $section )
|
||||
{
|
||||
@@ -459,7 +500,10 @@ class _Text extends TextArea
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack( 'form_tags_not_allowed', FALSE, array( 'sprintf' => array( $section ) ) ) );
|
||||
if ( $this->options['bypassProfanity'] === static::BYPASS_PROFANITY_NONE )
|
||||
{
|
||||
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack( 'form_tags_not_allowed', FALSE, array( 'sprintf' => array( $section ) ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -472,7 +516,7 @@ class _Text extends TextArea
|
||||
|
||||
$value = $newVal ?: $this->value;
|
||||
|
||||
if ( count( $looseProfanity ) )
|
||||
if ( \count( $looseProfanity ) )
|
||||
{
|
||||
$swaps = array();
|
||||
foreach( $looseProfanity AS $type => $row )
|
||||
@@ -485,7 +529,10 @@ class _Text extends TextArea
|
||||
{
|
||||
if ( mb_stristr( $value, $type ) )
|
||||
{
|
||||
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack( 'form_tags_not_allowed', FALSE, array( 'sprintf' => array( $row->type ) ) ) );
|
||||
if ( $this->options['bypassProfanity'] === static::BYPASS_PROFANITY_NONE )
|
||||
{
|
||||
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack( 'form_tags_not_allowed', FALSE, array( 'sprintf' => array( $row->type ) ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user