filter
Modulefilter_var(): the validation filters (FILTER_VALIDATE_INT, BOOL,
FLOAT, REGEXP, URL, EMAIL, IP, MAC, DOMAIN), the sanitizing
filters (FILTER_DEFAULT/FILTER_UNSAFE_RAW, FILTER_SANITIZE_*) and
FILTER_CALLBACK, with their flags and options, ported from ext/filter
(logical_filters.c, sanitizing_filters.c, callback_filter.c,
filter.c) of PHP 8.5.
| PHP | Rust |
|---|---|
filter_var($value, $filter, $options) | filter_var |
filter_var($value, FILTER_CALLBACK, ['options' => $callable]) | filter_var_with |
PHP values are Values (strings are bytes). The third argument is
either flags or an options array (Options), read the way
php_filter_call() reads it: "filter", "flags" and "options" keys,
with zval_get_long()/zval_get_double() conversions of min_range and
max_range, and the "default" option replacing a false (or, with
FILTER_NULL_ON_FAILURE, null) result, even a valid false from
FILTER_VALIDATE_BOOL.
FILTER_VALIDATE_REGEXP runs its regular expression on crate::pcre,
through PHP's compiled-pattern cache like PHP does.
FILTER_VALIDATE_EMAIL checks the language of PHP's fixed expressions
directly (filter/email.rs), and runs the expression itself only for
the addresses where PCRE2's backtracking limit could decide the result.
FILTER_VALIDATE_URL parses with crate::url::parse_url, or
with crate::url::rfc3986 for "uri_parser_class" => Uri\Rfc3986\Uri.
FILTER_CALLBACK calls a PHP callable; filter_var_with takes the
Callables that stand for them: whether the "options" entry is
callable (PHP throws TypeError when it is not), and calling it with the
value converted to a string. filter_var knows no callable.
Like PHP, filtering an array goes on after an element throws; the exception reported is the last one thrown (a failed validation does not throw while another exception is pending, and callables are not called).
FILTER_SANITIZE_FULL_SPECIAL_CHARS encodes as htmlentities() with the
default charset, which this port fixes at PHP's default, UTF-8
(default_charset/internal_encoding are not consulted).
Not supported (an Error with class Unsupported): the WHATWG URL
parser (Uri\WhatWg\Url) for FILTER_VALIDATE_URL.
Contents
use utopia_php_std::filter::Error;pub struct Error {
// private fields
}An exception filter_var() throws (ValueError, TypeError,
Filter\FilterFailedException), or Unsupported for what this port
does not implement.
Display.use utopia_php_std::filter::Filtered;pub struct Filtered {
pub value: Value,
pub warning: Option<Vec<u8>>,
}What filter_var() returned, and the warning it emitted, if any.
use utopia_php_std::filter::NoCallables;pub struct NoCallables;No callable at all: FILTER_CALLBACK always throws TypeError.
use utopia_php_std::filter::Options;pub enum Options {
Flags(i64),
Array(Vec<(Key, Value)>),
}The third argument of filter_var().
use utopia_php_std::filter::Value;pub enum Value {
Null,
Bool(bool),
Int(i64),
Float(f64),
Str(Vec<u8>),
Array(Vec<(Key, Value)>),
Object,
}A PHP value given to or returned by filter_var().
use utopia_php_std::filter::Callables;pub trait CallablesThe PHP callables FILTER_CALLBACK can call, named by its "options"
entry.
use utopia_php_std::filter::filter_var;pub fn filter_var(value: &Value, filter: i64, options: &Options) -> Result<Filtered, Error>filter_var($value, $filter, $options), without callables (see
filter_var_with).
use utopia_php_std::filter::filter_var_with;pub fn filter_var_with(
value: &Value,
filter: i64,
options: &Options,
callables: &mut dyn Callables,
) -> Result<Filtered, Error>filter_var($value, $filter, $options) where callables resolves and
calls the callable of FILTER_CALLBACK (the "options" entry).
use utopia_php_std::filter::validate_domain;pub fn validate_domain(d: &[u8], hostname: bool) -> boolphp_filter_validate_domain_ex(): whether a non-empty string passes
filter_var($d, FILTER_VALIDATE_DOMAIN), with FILTER_FLAG_HOSTNAME
when hostname is set, without building the filter's result.
use utopia_php_std::filter::validate_ip;pub fn validate_ip(s: &[u8], flags: i64) -> boolphp_filter_validate_ip(): whether filter_var($s, FILTER_VALIDATE_IP, $flags) of a
string succeeds, without the copy of the string it returns.
use utopia_php_std::filter::validate_url;pub fn validate_url(s: &[u8], flags: i64, parser: Option<&[u8]>) -> Result<bool, Error>php_filter_validate_url(): filter_var($s, FILTER_VALIDATE_URL, $flags) of a string,
without the copy of the string it returns; Err for an invalid uri_parser_class.
pub const FILTER_CALLBACK: i64 = 1_024i64;Calls the callable in the options entry, as PHP's FILTER_CALLBACK.
pub const FILTER_DEFAULT: i64 = 516i64;The default filter, which leaves the string as is, as PHP's FILTER_DEFAULT.
pub const FILTER_FLAG_ALLOW_FRACTION: i64 = 4_096i64;Keeps the decimal separator when sanitizing floats, as PHP's FILTER_FLAG_ALLOW_FRACTION.
pub const FILTER_FLAG_ALLOW_HEX: i64 = 2i64;Accepts hexadecimal integers (0x prefix), as PHP's FILTER_FLAG_ALLOW_HEX.
pub const FILTER_FLAG_ALLOW_OCTAL: i64 = 1i64;Accepts octal integers (0 or 0o prefix), as PHP's FILTER_FLAG_ALLOW_OCTAL.
pub const FILTER_FLAG_ALLOW_SCIENTIFIC: i64 = 16_384i64;Keeps e and E when sanitizing floats, as PHP's FILTER_FLAG_ALLOW_SCIENTIFIC.
pub const FILTER_FLAG_ALLOW_THOUSAND: i64 = 8_192i64;Accepts or keeps thousand separators, as PHP's FILTER_FLAG_ALLOW_THOUSAND.
pub const FILTER_FLAG_EMAIL_UNICODE: i64 = 1_048_576i64;Accepts Unicode in an email's local part, as PHP's FILTER_FLAG_EMAIL_UNICODE.
pub const FILTER_FLAG_EMPTY_STRING_NULL: i64 = 256i64;Turns an empty string into null, as PHP's FILTER_FLAG_EMPTY_STRING_NULL.
pub const FILTER_FLAG_ENCODE_AMP: i64 = 64i64;Encodes & as &, as PHP's FILTER_FLAG_ENCODE_AMP.
pub const FILTER_FLAG_ENCODE_HIGH: i64 = 32i64;Encodes bytes 127 and above as &#N;, as PHP's FILTER_FLAG_ENCODE_HIGH.
pub const FILTER_FLAG_ENCODE_LOW: i64 = 16i64;Encodes bytes below 32 as &#N;, as PHP's FILTER_FLAG_ENCODE_LOW.
pub const FILTER_FLAG_GLOBAL_RANGE: i64 = 536_870_912i64;Accepts globally routable IP addresses only, as PHP's FILTER_FLAG_GLOBAL_RANGE.
pub const FILTER_FLAG_HOSTNAME: i64 = 1_048_576i64;Requires a valid host name for FILTER_VALIDATE_DOMAIN, as PHP's FILTER_FLAG_HOSTNAME.
pub const FILTER_FLAG_IPV4: i64 = 1_048_576i64;Accepts IPv4 addresses only, as PHP's FILTER_FLAG_IPV4.
pub const FILTER_FLAG_IPV6: i64 = 2_097_152i64;Accepts IPv6 addresses only, as PHP's FILTER_FLAG_IPV6.
pub const FILTER_FLAG_NO_ENCODE_QUOTES: i64 = 128i64;Leaves ' and " unencoded, as PHP's FILTER_FLAG_NO_ENCODE_QUOTES.
pub const FILTER_FLAG_NO_PRIV_RANGE: i64 = 8_388_608i64;Rejects private IP ranges, as PHP's FILTER_FLAG_NO_PRIV_RANGE.
pub const FILTER_FLAG_NO_RES_RANGE: i64 = 4_194_304i64;Rejects reserved IP ranges, as PHP's FILTER_FLAG_NO_RES_RANGE.
pub const FILTER_FLAG_NONE: i64 = 0i64;No flags, as PHP's FILTER_FLAG_NONE.
pub const FILTER_FLAG_PATH_REQUIRED: i64 = 262_144i64;Requires a path in a validated URL, as PHP's FILTER_FLAG_PATH_REQUIRED.
pub const FILTER_FLAG_QUERY_REQUIRED: i64 = 524_288i64;Requires a query string in a validated URL, as PHP's FILTER_FLAG_QUERY_REQUIRED.
pub const FILTER_FLAG_STRIP_BACKTICK: i64 = 512i64;Strips backticks, as PHP's FILTER_FLAG_STRIP_BACKTICK.
pub const FILTER_FLAG_STRIP_HIGH: i64 = 8i64;Strips bytes 127 and above, as PHP's FILTER_FLAG_STRIP_HIGH.
pub const FILTER_FLAG_STRIP_LOW: i64 = 4i64;Strips bytes below 32, as PHP's FILTER_FLAG_STRIP_LOW.
pub const FILTER_FORCE_ARRAY: i64 = 67_108_864i64;Wraps a scalar result in an array, as PHP's FILTER_FORCE_ARRAY.
pub const FILTER_NULL_ON_FAILURE: i64 = 134_217_728i64;Returns null instead of false on failure, as PHP's FILTER_NULL_ON_FAILURE.
pub const FILTER_REQUIRE_ARRAY: i64 = 16_777_216i64;Requires an array and filters each element, as PHP's FILTER_REQUIRE_ARRAY.
pub const FILTER_REQUIRE_SCALAR: i64 = 33_554_432i64;Requires a scalar value, as PHP's FILTER_REQUIRE_SCALAR.
pub const FILTER_SANITIZE_ADD_SLASHES: i64 = 523i64;Escapes as addslashes(), as PHP's FILTER_SANITIZE_ADD_SLASHES.
pub const FILTER_SANITIZE_EMAIL: i64 = 517i64;Removes the bytes an email address cannot hold, as PHP's FILTER_SANITIZE_EMAIL.
pub const FILTER_SANITIZE_ENCODED: i64 = 514i64;URL-encodes the string, as PHP's FILTER_SANITIZE_ENCODED.
pub const FILTER_SANITIZE_FULL_SPECIAL_CHARS: i64 = 522i64;Encodes as htmlentities() with ENT_QUOTES, as PHP's FILTER_SANITIZE_FULL_SPECIAL_CHARS.
pub const FILTER_SANITIZE_NUMBER_FLOAT: i64 = 520i64;Keeps only digits, signs and the allowed float characters, as PHP's FILTER_SANITIZE_NUMBER_FLOAT.
pub const FILTER_SANITIZE_NUMBER_INT: i64 = 519i64;Keeps only digits and signs, as PHP's FILTER_SANITIZE_NUMBER_INT.
pub const FILTER_SANITIZE_SPECIAL_CHARS: i64 = 515i64;HTML-encodes '"<>& and bytes below 32, as PHP's FILTER_SANITIZE_SPECIAL_CHARS.
pub const FILTER_SANITIZE_STRING: i64 = 513i64;PHP deprecates the constant (8.1), not the filter: the ID runs without a notice.
pub const FILTER_SANITIZE_STRIPPED: i64 = 513i64;An alias of FILTER_SANITIZE_STRING (also a deprecated constant).
pub const FILTER_SANITIZE_URL: i64 = 518i64;Removes the bytes a URL cannot hold, as PHP's FILTER_SANITIZE_URL.
pub const FILTER_THROW_ON_FAILURE: i64 = 268_435_456i64;Throws Filter\FilterFailedException on failure, as PHP's FILTER_THROW_ON_FAILURE.
pub const FILTER_UNSAFE_RAW: i64 = 516i64;Leaves the string as is, optionally stripping or encoding bytes, as PHP's FILTER_UNSAFE_RAW.
pub const FILTER_VALIDATE_BOOL: i64 = 258i64;Validates a boolean ("1", "true", "on", "yes" and their opposites), as PHP's FILTER_VALIDATE_BOOL.
pub const FILTER_VALIDATE_DOMAIN: i64 = 277i64;Validates a domain name, as PHP's FILTER_VALIDATE_DOMAIN.
pub const FILTER_VALIDATE_EMAIL: i64 = 274i64;Validates an email address, as PHP's FILTER_VALIDATE_EMAIL.
pub const FILTER_VALIDATE_FLOAT: i64 = 259i64;Validates a float, as PHP's FILTER_VALIDATE_FLOAT.
pub const FILTER_VALIDATE_INT: i64 = 257i64;Validates an integer, as PHP's FILTER_VALIDATE_INT.
pub const FILTER_VALIDATE_IP: i64 = 275i64;Validates an IPv4 or IPv6 address, as PHP's FILTER_VALIDATE_IP.
pub const FILTER_VALIDATE_MAC: i64 = 276i64;Validates a MAC address, as PHP's FILTER_VALIDATE_MAC.
pub const FILTER_VALIDATE_REGEXP: i64 = 272i64;Validates against the regexp option, as PHP's FILTER_VALIDATE_REGEXP.
pub const FILTER_VALIDATE_URL: i64 = 273i64;Validates a URL, as PHP's FILTER_VALIDATE_URL.