[ 'class' => TypeDef\BooleanDef::class ], 'checkbox' => [ 'class' => TypeDef\PresenceBooleanDef::class ], 'integer' => [ 'class' => TypeDef\IntegerDef::class ], 'limit' => [ 'class' => TypeDef\LimitDef::class ], 'float' => [ 'class' => TypeDef\FloatDef::class ], 'double' => [ 'class' => TypeDef\FloatDef::class ], 'string' => [ 'class' => TypeDef\StringDef::class ], 'password' => [ 'class' => TypeDef\PasswordDef::class ], 'NULL' => [ 'class' => TypeDef\StringDef::class, 'args' => [ [ 'allowEmptyWhenRequired' => true, ] ], ], 'timestamp' => [ 'class' => TypeDef\TimestampDef::class ], 'upload' => [ 'class' => TypeDef\UploadDef::class ], 'enum' => [ 'class' => TypeDef\EnumDef::class ], ]; /** @var Callbacks */ private $callbacks; /** @var ObjectFactory */ private $objectFactory; /** @var (TypeDef|array)[] Map parameter type names to TypeDef objects or ObjectFactory specs */ private $typeDefs = []; /** @var int Default values for PARAM_ISMULTI_LIMIT1 */ private $ismultiLimit1; /** @var int Default values for PARAM_ISMULTI_LIMIT2 */ private $ismultiLimit2; /** * @param Callbacks $callbacks * @param ObjectFactory $objectFactory To turn specs into TypeDef objects * @param array $options Associative array of additional settings * - 'typeDefs': (array) As for addTypeDefs(). If omitted, self::$STANDARD_TYPES will be used. * Pass an empty array if you want to start with no registered types. * - 'ismultiLimits': (int[]) Two ints, being the default values for PARAM_ISMULTI_LIMIT1 and * PARAM_ISMULTI_LIMIT2. If not given, defaults to `[ 50, 500 ]`. */ public function __construct( Callbacks $callbacks, ObjectFactory $objectFactory, array $options = [] ) { $this->callbacks = $callbacks; $this->objectFactory = $objectFactory; $this->addTypeDefs( $options['typeDefs'] ?? self::$STANDARD_TYPES ); $this->ismultiLimit1 = $options['ismultiLimits'][0] ?? 50; $this->ismultiLimit2 = $options['ismultiLimits'][1] ?? 500; } /** * List known type names * @return string[] */ public function knownTypes() { return array_keys( $this->typeDefs ); } /** * Register multiple type handlers * * @see addTypeDef() * @param array $typeDefs Associative array mapping `$name` to `$typeDef`. */ public function addTypeDefs( array $typeDefs ) { foreach ( $typeDefs as $name => $def ) { $this->addTypeDef( $name, $def ); } } /** * Register a type handler * * To allow code to omit PARAM_TYPE in settings arrays to derive the type * from PARAM_DEFAULT, it is strongly recommended that the following types be * registered: "boolean", "integer", "double", "string", "NULL", and "enum". * * When using ObjectFactory specs, the following extra arguments are passed: * - The Callbacks object for this ParamValidator instance. * * @param string $name Type name * @param TypeDef|array $typeDef Type handler or ObjectFactory spec to create one. */ public function addTypeDef( $name, $typeDef ) { Assert::parameterType( implode( '|', [ TypeDef::class, 'array' ] ), $typeDef, '$typeDef' ); if ( isset( $this->typeDefs[$name] ) ) { throw new InvalidArgumentException( "Type '$name' is already registered" ); } $this->typeDefs[$name] = $typeDef; } /** * Register a type handler, overriding any existing handler * @see addTypeDef * @param string $name Type name * @param TypeDef|array|null $typeDef As for addTypeDef, or null to unregister a type. */ public function overrideTypeDef( $name, $typeDef ) { Assert::parameterType( implode( '|', [ TypeDef::class, 'array', 'null' ] ), $typeDef, '$typeDef' ); if ( $typeDef === null ) { unset( $this->typeDefs[$name] ); } else { $this->typeDefs[$name] = $typeDef; } } /** * Test if a type is registered * @param string $name Type name * @return bool */ public function hasTypeDef( $name ) { return isset( $this->typeDefs[$name] ); } /** * Get the TypeDef for a type * @param string|array $type Any array is considered equivalent to the string "enum". * @return TypeDef|null */ public function getTypeDef( $type ) { if ( is_array( $type ) ) { $type = 'enum'; } if ( !isset( $this->typeDefs[$type] ) ) { return null; } $def = $this->typeDefs[$type]; if ( !$def instanceof TypeDef ) { $def = $this->objectFactory->createObject( $def, [ 'extraArgs' => [ $this->callbacks ], 'assertClass' => TypeDef::class, ] ); $this->typeDefs[$type] = $def; } return $def; } /** * Normalize a parameter settings array * @param array|mixed $settings Default value or an array of settings * using PARAM_* constants. * @return array */ public function normalizeSettings( $settings ) { // Shorthand if ( !is_array( $settings ) ) { $settings = [ self::PARAM_DEFAULT => $settings, ]; } // When type is not given, determine it from the type of the PARAM_DEFAULT if ( !isset( $settings[self::PARAM_TYPE] ) ) { $settings[self::PARAM_TYPE] = gettype( $settings[self::PARAM_DEFAULT] ?? null ); } $typeDef = $this->getTypeDef( $settings[self::PARAM_TYPE] ); if ( $typeDef ) { $settings = $typeDef->normalizeSettings( $settings ); } return $settings; } /** * Fetch and valiate a parameter value using a settings array * * @param string $name Parameter name * @param array|mixed $settings Default value or an array of settings * using PARAM_* constants. * @param array $options Options array, passed through to the TypeDef and Callbacks. * @return mixed Validated parameter value * @throws ValidationException if the value is invalid */ public function getValue( $name, $settings, array $options = [] ) { $settings = $this->normalizeSettings( $settings ); $typeDef = $this->getTypeDef( $settings[self::PARAM_TYPE] ); if ( !$typeDef ) { throw new DomainException( "Param $name's type is unknown - {$settings[self::PARAM_TYPE]}" ); } $value = $typeDef->getValue( $name, $settings, $options ); if ( $value !== null ) { if ( !empty( $settings[self::PARAM_SENSITIVE] ) ) { $this->callbacks->recordCondition( DataMessageValue::new( 'paramvalidator-param-sensitive', [], 'param-sensitive' ) ->plaintextParams( $name, $value ), $name, $value, $settings, $options ); } // Set a warning if a deprecated parameter has been passed if ( !empty( $settings[self::PARAM_DEPRECATED] ) ) { $this->callbacks->recordCondition( DataMessageValue::new( 'paramvalidator-param-deprecated', [], 'param-deprecated' ) ->plaintextParams( $name, $value ), $name, $value, $settings, $options ); } } elseif ( isset( $settings[self::PARAM_DEFAULT] ) ) { $value = $settings[self::PARAM_DEFAULT]; } return $this->validateValue( $name, $value, $settings, $options ); } /** * Valiate a parameter value using a settings array * * @param string $name Parameter name * @param null|mixed $value Parameter value * @param array|mixed $settings Default value or an array of settings * using PARAM_* constants. * @param array $options Options array, passed through to the TypeDef and Callbacks. * - An additional option, 'values-list', will be set when processing the * values of a multi-valued parameter. * @return mixed Validated parameter value(s) * @throws ValidationException if the value is invalid */ public function validateValue( $name, $value, $settings, array $options = [] ) { $settings = $this->normalizeSettings( $settings ); $typeDef = $this->getTypeDef( $settings[self::PARAM_TYPE] ); if ( !$typeDef ) { throw new DomainException( "Param $name's type is unknown - {$settings[self::PARAM_TYPE]}" ); } if ( $value === null ) { if ( !empty( $settings[self::PARAM_REQUIRED] ) ) { throw new ValidationException( DataMessageValue::new( 'paramvalidator-missingparam', [], 'missingparam' ) ->plaintextParams( $name ), $name, $value, $settings ); } return null; } // Non-multi if ( empty( $settings[self::PARAM_ISMULTI] ) ) { if ( substr( $value, 0, 1 ) === "\x1f" ) { throw new ValidationException( DataMessageValue::new( 'paramvalidator-notmulti', [], 'badvalue' ) ->plaintextParams( $name, $value ), $name, $value, $settings ); } return $typeDef->validate( $name, $value, $settings, $options ); } // Split the multi-value and validate each parameter $limit1 = $settings[self::PARAM_ISMULTI_LIMIT1] ?? $this->ismultiLimit1; $limit2 = max( $limit1, $settings[self::PARAM_ISMULTI_LIMIT2] ?? $this->ismultiLimit2 ); $valuesList = is_array( $value ) ? $value : self::explodeMultiValue( $value, $limit2 + 1 ); // Handle PARAM_ALL $enumValues = $typeDef->getEnumValues( $name, $settings, $options ); if ( is_array( $enumValues ) && isset( $settings[self::PARAM_ALL] ) && count( $valuesList ) === 1 ) { $allValue = is_string( $settings[self::PARAM_ALL] ) ? $settings[self::PARAM_ALL] : self::ALL_DEFAULT_STRING; if ( $valuesList[0] === $allValue ) { return $enumValues; } } // Avoid checking useHighLimits() unless it's actually necessary $sizeLimit = ( $limit2 > $limit1 && count( $valuesList ) > $limit1 && $this->callbacks->useHighLimits( $options ) ) ? $limit2 : $limit1; if ( count( $valuesList ) > $sizeLimit ) { if ( is_array( $value ) ) { $value = self::implodeMultiValue( $value ); } throw new ValidationException( DataMessageValue::new( 'paramvalidator-toomanyvalues', [], 'toomanyvalues', [ 'limit' => $sizeLimit, 'lowlimit' => $limit1, 'highlimit' => $limit2, ] )->plaintextParams( $name, $value )->numParams( $sizeLimit ), $name, $valuesList, $settings ); } $options['values-list'] = $valuesList; $validValues = []; $invalidValues = []; foreach ( $valuesList as $v ) { try { $validValues[] = $typeDef->validate( $name, $v, $settings, $options ); } catch ( ValidationException $ex ) { if ( empty( $settings[self::PARAM_IGNORE_INVALID_VALUES] ) ) { throw $ex; } $invalidValues[] = $v; } } if ( $invalidValues ) { $this->callbacks->recordCondition( DataMessageValue::new( 'paramvalidator-unrecognizedvalues', [], 'unrecognizedvalues', [ 'values' => $invalidValues, ] ) ->plaintextParams( $name, $value ) ->commaListParams( array_map( function ( $v ) { return new ScalarParam( ParamType::PLAINTEXT, $v ); }, $invalidValues ) ) ->numParams( count( $invalidValues ) ), $name, $value, $settings, $options ); } // Throw out duplicates if requested if ( empty( $settings[self::PARAM_ALLOW_DUPLICATES] ) ) { $validValues = array_values( array_unique( $validValues ) ); } return $validValues; } /** * Describe parameter settings in a machine-readable format. * * @param string $name Parameter name. * @param array|mixed $settings Default value or an array of settings * using PARAM_* constants. * @param array $options Options array. * @return array */ public function getParamInfo( $name, $settings, array $options ) { $settings = $this->normalizeSettings( $settings ); $typeDef = $this->getTypeDef( $settings[self::PARAM_TYPE] ); $info = []; $info['type'] = $settings[self::PARAM_TYPE]; $info['required'] = !empty( $settings[self::PARAM_REQUIRED] ); if ( !empty( $settings[self::PARAM_DEPRECATED] ) ) { $info['deprecated'] = true; } if ( !empty( $settings[self::PARAM_SENSITIVE] ) ) { $info['sensitive'] = true; } if ( isset( $settings[self::PARAM_DEFAULT] ) ) { $info['default'] = $settings[self::PARAM_DEFAULT]; } $info['multi'] = !empty( $settings[self::PARAM_ISMULTI] ); if ( $info['multi'] ) { $info['lowlimit'] = $settings[self::PARAM_ISMULTI_LIMIT1] ?? $this->ismultiLimit1; $info['highlimit'] = max( $info['lowlimit'], $settings[self::PARAM_ISMULTI_LIMIT2] ?? $this->ismultiLimit2 ); $info['limit'] = $info['highlimit'] > $info['lowlimit'] && $this->callbacks->useHighLimits( $options ) ? $info['highlimit'] : $info['lowlimit']; if ( !empty( $settings[self::PARAM_ALLOW_DUPLICATES] ) ) { $info['allowsduplicates'] = true; } $allSpecifier = $settings[self::PARAM_ALL] ?? false; if ( $allSpecifier !== false ) { if ( !is_string( $allSpecifier ) ) { $allSpecifier = self::ALL_DEFAULT_STRING; } $info['allspecifier'] = $allSpecifier; } } if ( $typeDef ) { $info = array_merge( $info, $typeDef->getParamInfo( $name, $settings, $options ) ); } // Filter out nulls (strictly) return array_filter( $info, function ( $v ) { return $v !== null; } ); } /** * Describe parameter settings in human-readable format * * @param string $name Parameter name being described. * @param array|mixed $settings Default value or an array of settings * using PARAM_* constants. * @param array $options Options array. * @return MessageValue[] */ public function getHelpInfo( $name, $settings, array $options ) { $settings = $this->normalizeSettings( $settings ); $typeDef = $this->getTypeDef( $settings[self::PARAM_TYPE] ); // Define ordering. Some are overwritten below, some expected from the TypeDef $info = [ self::PARAM_DEPRECATED => null, self::PARAM_REQUIRED => null, self::PARAM_SENSITIVE => null, self::PARAM_TYPE => null, self::PARAM_ISMULTI => null, self::PARAM_ISMULTI_LIMIT1 => null, self::PARAM_ALL => null, self::PARAM_DEFAULT => null, ]; if ( !empty( $settings[self::PARAM_DEPRECATED] ) ) { $info[self::PARAM_DEPRECATED] = MessageValue::new( 'paramvalidator-help-deprecated' ); } if ( !empty( $settings[self::PARAM_REQUIRED] ) ) { $info[self::PARAM_REQUIRED] = MessageValue::new( 'paramvalidator-help-required' ); } if ( !empty( $settings[self::PARAM_ISMULTI] ) ) { $info[self::PARAM_ISMULTI] = MessageValue::new( 'paramvalidator-help-multi-sep' ); $lowcount = $settings[self::PARAM_ISMULTI_LIMIT1] ?? $this->ismultiLimit1; $highcount = max( $lowcount, $settings[self::PARAM_ISMULTI_LIMIT2] ?? $this->ismultiLimit2 ); $values = $typeDef ? $typeDef->getEnumValues( $name, $settings, $options ) : null; if ( // Only mention the limits if they're likely to matter. $values === null || count( $values ) > $lowcount || !empty( $settings[self::PARAM_ALLOW_DUPLICATES] ) ) { if ( $highcount > $lowcount ) { $info[self::PARAM_ISMULTI_LIMIT1] = MessageValue::new( 'paramvalidator-help-multi-max' ) ->numParams( $lowcount, $highcount ); } else { $info[self::PARAM_ISMULTI_LIMIT1] = MessageValue::new( 'paramvalidator-help-multi-max-simple' ) ->numParams( $lowcount ); } } $allSpecifier = $settings[self::PARAM_ALL] ?? false; if ( $allSpecifier !== false ) { if ( !is_string( $allSpecifier ) ) { $allSpecifier = self::ALL_DEFAULT_STRING; } $info[self::PARAM_ALL] = MessageValue::new( 'paramvalidator-help-multi-all' ) ->plaintextParams( $allSpecifier ); } } if ( isset( $settings[self::PARAM_DEFAULT] ) && $typeDef ) { $value = $typeDef->stringifyValue( $name, $settings[self::PARAM_DEFAULT], $settings, $options ); if ( $value === '' ) { $info[self::PARAM_DEFAULT] = MessageValue::new( 'paramvalidator-param-default-empty' ); } elseif ( $value !== null ) { $info[self::PARAM_DEFAULT] = MessageValue::new( 'paramvalidator-param-default' ) ->plaintextParams( $value ); } } if ( $typeDef ) { $info = array_merge( $info, $typeDef->getHelpInfo( $name, $settings, $options ) ); } // Put the default at the very end (the TypeDef may have added extra messages) $default = $info[self::PARAM_DEFAULT]; unset( $info[self::PARAM_DEFAULT] ); $info[self::PARAM_DEFAULT] = $default; // Filter out nulls return array_filter( $info ); } /** * Split a multi-valued parameter string, like explode() * * Note that, unlike explode(), this will return an empty array when given * an empty string. * * @param string $value * @param int $limit * @return string[] */ public static function explodeMultiValue( $value, $limit ) { if ( $value === '' || $value === "\x1f" ) { return []; } if ( substr( $value, 0, 1 ) === "\x1f" ) { $sep = "\x1f"; $value = substr( $value, 1 ); } else { $sep = '|'; } return explode( $sep, $value, $limit ); } /** * Implode an array as a multi-valued parameter string, like implode() * * @param array $value * @return string */ public static function implodeMultiValue( array $value ) { if ( $value === [ '' ] ) { // There's no value that actually returns a single empty string. // Best we can do is this that returns two, which will be deduplicated to one. return '|'; } foreach ( $value as $v ) { if ( strpos( $v, '|' ) !== false ) { return "\x1f" . implode( "\x1f", $value ); } } return implode( '|', $value ); } }