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.
Every function here is checked against the real PHP function by
bin/compat fuzz php-std (operations filter.*).
Contents
2 submodules, and what the module defines itself.
use php_std::filter::Error;pub struct ErrorAn exception filter_var() throws (ValueError, TypeError,
Filter\FilterFailedException), or Unsupported for what this port
does not implement.
Display.use php_std::filter::Filtered;pub struct FilteredWhat filter_var() returned, and the warning it emitted, if any.
use php_std::filter::NoCallables;pub struct NoCallablesNo callable at all: FILTER_CALLBACK always throws TypeError.
use php_std::filter::Options;pub enum OptionsThe third argument of filter_var().
use php_std::Value;pub enum ValueA PHP value given to or returned by filter_var().
use php_std::filter::Callables;pub trait CallablesThe PHP callables FILTER_CALLBACK can call, named by its "options"
entry.
use 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 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 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.
pub const FILTER_CALLBACK: i64 = 0x0400Calls the callable in the options entry, as PHP's FILTER_CALLBACK.
pub const FILTER_DEFAULT: i64 = 0x0204The default filter, which leaves the string as is, as PHP's FILTER_DEFAULT.
pub const FILTER_FLAG_ALLOW_FRACTION: i64 = 0x1000Keeps the decimal separator when sanitizing floats, as PHP's FILTER_FLAG_ALLOW_FRACTION.
pub const FILTER_FLAG_ALLOW_HEX: i64 = 0x0002Accepts hexadecimal integers (0x prefix), as PHP's FILTER_FLAG_ALLOW_HEX.
pub const FILTER_FLAG_ALLOW_OCTAL: i64 = 0x0001Accepts octal integers (0 or 0o prefix), as PHP's FILTER_FLAG_ALLOW_OCTAL.
pub const FILTER_FLAG_ALLOW_SCIENTIFIC: i64 = 0x4000Keeps e and E when sanitizing floats, as PHP's FILTER_FLAG_ALLOW_SCIENTIFIC.
pub const FILTER_FLAG_ALLOW_THOUSAND: i64 = 0x2000Accepts or keeps thousand separators, as PHP's FILTER_FLAG_ALLOW_THOUSAND.
pub const FILTER_FLAG_EMAIL_UNICODE: i64 = 0x0010_0000Accepts Unicode in an email's local part, as PHP's FILTER_FLAG_EMAIL_UNICODE.
pub const FILTER_FLAG_EMPTY_STRING_NULL: i64 = 0x0100Turns an empty string into null, as PHP's FILTER_FLAG_EMPTY_STRING_NULL.
pub const FILTER_FLAG_ENCODE_AMP: i64 = 0x0040Encodes & as &, as PHP's FILTER_FLAG_ENCODE_AMP.
pub const FILTER_FLAG_ENCODE_HIGH: i64 = 0x0020Encodes bytes 127 and above as &#N;, as PHP's FILTER_FLAG_ENCODE_HIGH.
pub const FILTER_FLAG_ENCODE_LOW: i64 = 0x0010Encodes bytes below 32 as &#N;, as PHP's FILTER_FLAG_ENCODE_LOW.
pub const FILTER_FLAG_GLOBAL_RANGE: i64 = 0x2000_0000Accepts globally routable IP addresses only, as PHP's FILTER_FLAG_GLOBAL_RANGE.
pub const FILTER_FLAG_HOSTNAME: i64 = 0x0010_0000Requires a valid host name for FILTER_VALIDATE_DOMAIN, as PHP's FILTER_FLAG_HOSTNAME.
pub const FILTER_FLAG_IPV4: i64 = 0x0010_0000Accepts IPv4 addresses only, as PHP's FILTER_FLAG_IPV4.
pub const FILTER_FLAG_IPV6: i64 = 0x0020_0000Accepts IPv6 addresses only, as PHP's FILTER_FLAG_IPV6.
pub const FILTER_FLAG_NO_ENCODE_QUOTES: i64 = 0x0080Leaves ' and " unencoded, as PHP's FILTER_FLAG_NO_ENCODE_QUOTES.
pub const FILTER_FLAG_NO_PRIV_RANGE: i64 = 0x0080_0000Rejects private IP ranges, as PHP's FILTER_FLAG_NO_PRIV_RANGE.
pub const FILTER_FLAG_NO_RES_RANGE: i64 = 0x0040_0000Rejects reserved IP ranges, as PHP's FILTER_FLAG_NO_RES_RANGE.
pub const FILTER_FLAG_NONE: i64 = 0x0000No flags, as PHP's FILTER_FLAG_NONE.
pub const FILTER_FLAG_PATH_REQUIRED: i64 = 0x04_0000Requires a path in a validated URL, as PHP's FILTER_FLAG_PATH_REQUIRED.
pub const FILTER_FLAG_QUERY_REQUIRED: i64 = 0x08_0000Requires a query string in a validated URL, as PHP's FILTER_FLAG_QUERY_REQUIRED.
pub const FILTER_FLAG_STRIP_BACKTICK: i64 = 0x0200Strips backticks, as PHP's FILTER_FLAG_STRIP_BACKTICK.
pub const FILTER_FLAG_STRIP_HIGH: i64 = 0x0008Strips bytes 127 and above, as PHP's FILTER_FLAG_STRIP_HIGH.
pub const FILTER_FLAG_STRIP_LOW: i64 = 0x0004Strips bytes below 32, as PHP's FILTER_FLAG_STRIP_LOW.
pub const FILTER_FORCE_ARRAY: i64 = 0x0400_0000Wraps a scalar result in an array, as PHP's FILTER_FORCE_ARRAY.
pub const FILTER_NULL_ON_FAILURE: i64 = 0x0800_0000Returns null instead of false on failure, as PHP's FILTER_NULL_ON_FAILURE.
pub const FILTER_REQUIRE_ARRAY: i64 = 0x0100_0000Requires an array and filters each element, as PHP's FILTER_REQUIRE_ARRAY.
pub const FILTER_REQUIRE_SCALAR: i64 = 0x0200_0000Requires a scalar value, as PHP's FILTER_REQUIRE_SCALAR.
pub const FILTER_SANITIZE_ADD_SLASHES: i64 = 0x020bEscapes as addslashes(), as PHP's FILTER_SANITIZE_ADD_SLASHES.
pub const FILTER_SANITIZE_EMAIL: i64 = 0x0205Removes the bytes an email address cannot hold, as PHP's FILTER_SANITIZE_EMAIL.
pub const FILTER_SANITIZE_ENCODED: i64 = 0x0202URL-encodes the string, as PHP's FILTER_SANITIZE_ENCODED.
pub const FILTER_SANITIZE_FULL_SPECIAL_CHARS: i64 = 0x020aEncodes as htmlentities() with ENT_QUOTES, as PHP's FILTER_SANITIZE_FULL_SPECIAL_CHARS.
pub const FILTER_SANITIZE_NUMBER_FLOAT: i64 = 0x0208Keeps only digits, signs and the allowed float characters, as PHP's FILTER_SANITIZE_NUMBER_FLOAT.
pub const FILTER_SANITIZE_NUMBER_INT: i64 = 0x0207Keeps only digits and signs, as PHP's FILTER_SANITIZE_NUMBER_INT.
pub const FILTER_SANITIZE_SPECIAL_CHARS: i64 = 0x0203HTML-encodes '"<>& and bytes below 32, as PHP's FILTER_SANITIZE_SPECIAL_CHARS.
pub const FILTER_SANITIZE_STRING: i64 = 0x0201PHP deprecates the constant (8.1), not the filter: the ID runs without a notice.
pub const FILTER_SANITIZE_STRIPPED: i64 = 0x0201An alias of FILTER_SANITIZE_STRING (also a deprecated constant).
pub const FILTER_SANITIZE_URL: i64 = 0x0206Removes the bytes a URL cannot hold, as PHP's FILTER_SANITIZE_URL.
pub const FILTER_THROW_ON_FAILURE: i64 = 0x1000_0000Throws Filter\FilterFailedException on failure, as PHP's FILTER_THROW_ON_FAILURE.
pub const FILTER_UNSAFE_RAW: i64 = 0x0204Leaves the string as is, optionally stripping or encoding bytes, as PHP's FILTER_UNSAFE_RAW.
pub const FILTER_VALIDATE_BOOL: i64 = 0x0102Validates a boolean ("1", "true", "on", "yes" and their opposites), as PHP's FILTER_VALIDATE_BOOL.
pub const FILTER_VALIDATE_DOMAIN: i64 = 0x0115Validates a domain name, as PHP's FILTER_VALIDATE_DOMAIN.
pub const FILTER_VALIDATE_EMAIL: i64 = 0x0112Validates an email address, as PHP's FILTER_VALIDATE_EMAIL.
pub const FILTER_VALIDATE_FLOAT: i64 = 0x0103Validates a float, as PHP's FILTER_VALIDATE_FLOAT.
pub const FILTER_VALIDATE_INT: i64 = 0x0101Validates an integer, as PHP's FILTER_VALIDATE_INT.
pub const FILTER_VALIDATE_IP: i64 = 0x0113Validates an IPv4 or IPv6 address, as PHP's FILTER_VALIDATE_IP.
pub const FILTER_VALIDATE_MAC: i64 = 0x0114Validates a MAC address, as PHP's FILTER_VALIDATE_MAC.
pub const FILTER_VALIDATE_REGEXP: i64 = 0x0110Validates against the regexp option, as PHP's FILTER_VALIDATE_REGEXP.
pub const FILTER_VALIDATE_URL: i64 = 0x0111Validates a URL, as PHP's FILTER_VALIDATE_URL.
filter::email
Own pageFILTER_VALIDATE_EMAIL without its regular expression.
php_filter_validate_email() matches the address (at most 320 bytes)
against one of two fixed regular expressions (EMAIL, or
EMAIL_UNICODE with FILTER_FLAG_EMAIL_UNICODE). Every part of them
is deterministic except two lookaheads, so this module checks the same
language directly, one character at a time (a byte, or a code point for
the /u expression):
(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})and the same with{65,}@: the units of these lookaheads are a character other than\and", or\and a character up to\x7E, each with an optional"on both sides. Where each unit starts is forced, so the address is refused when 255 units follow each other from the start, or 65 or more are followed by@(units_refuse).- the local part: dot-separated words, each an atom or a quoted string.
- the domain: dot-separated labels of letters, digits and inner hyphens,
none of 64 characters or more, ending in one starting with a letter;
or an address literal (
[IPv4],[IPv6:...]with at most 6 groups around::,[IPv6:...:IPv4]with at most 4).
The expressions are case-insensitive: with /u, PCRE2 also matches K
with U+212A KELVIN SIGN and S with U+017F LATIN SMALL LETTER LONG S, and
\pL/\pN follow PCRE2's Unicode 15.0.0 tables.
The one thing a direct check cannot see is PCRE2 running out of
pcre.backtrack_limit, which fails the validation. Only the lookaheads
backtrack enough to get there, by trying both sides for each " between
two units: an address with more than MAX_QUOTES double quotes runs
the regular expression itself (see validate).
filter::sanitize
Own pageThe sanitizing filters of ext/filter/sanitizing_filters.c (PHP 8.5),
with the parts of ext/standard they call: php_strip_tags_ex()
(string.c), php_escape_html_entities_ex() (html.c, as
FILTER_SANITIZE_FULL_SPECIAL_CHARS calls it: every HTML 4.01 entity,
UTF-8, no double encoding) and php_addslashes().
Every function takes the value already converted to a string and returns the filtered value; none of them can fail.