This function scrubbs your user input data shiny and clean.
It ensures: the data has a given type, maximum length, and syntax. E.g. if the data comes out of an input-field use this function with the argument $escape set to YANA_ESCAPE_LINEBREAK, to enforce the input does not have any unexpected line breaks.
Valid values for parameter $type:
- int, integer
- float, double
- boolean, bool
- array, set
- string
- object
- time = the input is an unix time code
- mail = the input is a mail adress
- ip = the input is an IP adress
- select = the input is taken from a select field (treated as "string")
- text = the input is taken from a textarea field (treated as "string")
- upload = the input is the index of the uploaded file in the $_FILES-array
Note: type "upload" will return the path to the uploaded file on success and an integer error constant on error. See the PHP manual for details on these codes.
Valid values for parameter $escape:
- YANA_ESCAPE_NONE = leave special chars alone (default)
- YANA_ESCAPE_SLASHED = apply addslashes()
- YANA_ESCAPE_TOKEN = replace template delimiters with html-entities
- YANA_ESCAPE_CODED = convert all characters to html-entities
- YANA_ESCAPE_LINEBREAK = revert all white-space to spaces
(for security reasons you should ALWAYS use this setting if you
expect data from any other field than textarea) - YANA_ESCAPE_USERTEXT = treat full-text message from an textarea element,
prevents flooding by removing doubled elements
These constants can be combined! Examples of usage:
- YANA_ESCAPE_SLASHED = just slashes
- YANA_ESCAPE_SLASHED | YANA_ESCAPE_TOKEN = slashes and token
- YANA_ESCAPE_ALL & ~YANA_ESCAPE_USERTEXT = all but usertext
Interpretation of the $length parameter depends on the $type argument given.
- no type = interpreted as maximum length of characters (implicit string conversion)
- string = maximum length of characters
- integer = maximum number of digits
- float = maximum number of digits (without fraction) - this may be combined with argument $precision
- upload = maximum size of file in bytes
For type float and integer, if the number of digits exceeds the maximum, the maximum number allowed will be returned instead.
For type integer see the following examples:
$value=-3, $length=1 : return -3
$value=3.2, $length=1 : return 3
$value=3.4, $length=1 : return 3
$value=3.5, $length=1 : return 4
$value=3.6, $length=1 : return 4
$value=9.9, $length=1 : return 9
$value=11.11, $length=2 : return 11
$value=111.11, $length=2 : return 99
$value=10, $length=1 : return 9
The argument $precision is the maximum number of digits for the decimal fraction of a number. This argument applies only to types float and double.
For type float see the following examples:
$value=-3.1, $length=1, $precision 0: return -3
$value=3.4, $length=1, $precision 0: return 3
$value=3.5, $length=1, $precision 0: return 4
$value=3.21, $length=1, $precision 1: return 3.2
$value=13.5, $length=1, $precision 1: return 9.9
$value=11.11, $length=2, $precision 1: return 11.1
$value=111.11, $length=2, $precision 1: return 99.9
$value=0.115, $length=0, $precision 2: return .12
$value=5.115, $length=1, $precision 2: return 5.12
Note on compatibility: The argument $precision was introduced in version 2.9.7. This changes the interpretation of the argument $length. Versions BEFORE 2.9.7 interpreted float values as numeric strings. Thus $length was understood as the maximum length in characters (including fraction). This has changed in version 2.9.7, where float values are treated as numbers - as shown in the examples above. The argument $length is now interpreted as the maximum length of the full decimal number (excluding fraction).
For type "upload" see the following example:
switch ($filename)
{
case UPLOAD_ERR_INI_SIZE:
exit('File exceeds upload_max_filesize in php.ini');
break;
case UPLOAD_ERR_FORM_SIZE:
exit('File bigger than 1000000 bytes');
break;
case UPLOAD_ERR_PARTIAL:
exit('File was only partially uploaded');
break;
case UPLOAD_ERR_NO_FILE:
exit('No file was uploaded');
break;
case UPLOAD_ERR_NO_TMP_DIR:
exit('Missing a temporary folder');
break;
case UPLOAD_ERR_CANT_WRITE:
exit('Failed to write file to disk');
break;
case UPLOAD_ERR_EXTENSION:
exit('File upload stopped by extension');
break;
}
} else {
}
Note: type "image" is only treated as string here. There is a specific function for this job in the
Image See the function
Image::uploadFile() for more details.
For type "text" see the following example:
// this example will untaint text taken from a HTML form
// input taken from field 'message'
$unsaveInput = $_GET['message'];
// type of data
$type = 'text';
// max. number of characters
$length = 1000;
// escape input
// untaint input
$saveInput =
untaintInput($unsaveInput, $type, $length, $escape);