There's like a million Q&A that explain the options like FILTER_FLAG_STRIP_LOW
, but what does FILTER_SANITIZE_STRING
do on its own, without any options? Does it just filter tags?
Answers
mysql_real_escape_string()
will escape any malicious characters. In addition, you can use a regex like /^[A-Za-z]{4}d{3}$/
to make sure that the user indeed entered a valid input.
From the documentation, it seems like the :inverse_of
option is a method for avoiding SQL queries, not generating them. It's a hint to ActiveRecord to use already loaded data instead of fetching it again through a relationship.
Their example:
class Dungeon < ActiveRecord::Base
has_many :traps, :inverse_of => :dungeon
has_one :evil_wizard, :inverse_of => :dungeon
end
class Trap < ActiveRecord::Base
belongs_to :dungeon, :inverse_of => :traps
end
class EvilWizard < ActiveRecord::Base
belongs_to :dungeon, :inverse_of => :evil_wizard
end
In this case, calling dungeon.traps.first.dungeon
should return the original dungeon
object instead of loading a new one as would be the case by default.
Here's an excellent article with illustrations by a Google engineer:
http://ssp.impulsetrain.com/porterduff.html
PorterDuff is described as a way of combining images as if they were "irregular shaped pieces of cardboard" overlayed on each other, as well as a scheme for blending the overlapping parts.
The default Android way of composing images is PorterDuff.Mode.SRC_OVER, which equates to drawing the source image/color over the target image. In other words, it does what you would expect and draws the source image (the one you're drawing) on top of the destination image (the canvas) with the destination image showing through to the degree defined by the source image's alpha.
You can use the key below to understand the algebra that the Android docs use to describe the other modes (see the article for a fuller desription with similar terms).
- Sa Source alpha
- Sc Source color
- Da Destination alpha
- Dc Destination color
Where alpha is a value [0..1]
, and color is substituted once per channel (so use the formula once for each of red, green and blue)
The resulting values are specified as a pair in square braces as follows.
[<alpha-value>,<color-value>]
Where alpha-value
and color-value
are formulas for generating the resulting alpha chanel and each color chanel respectively.
According to PHP Manual:
According to W3Schools:
Now, that doesn't tell us much. Let's go see some PHP sources.
ext/filter/filter.c
:Now, let's go see how
php_filter_string
is defined.ext/filter/sanitizing_filters.c
:I'll skip commenting flags since they're already explained on the Internet, like you said, and focus on what is always performed instead, which is not so well documented.
First -
php_filter_strip
. It doesn't do much, just takes the flags you pass to the function and processes them accordingly. It does the well-documented stuff.Then we construct some kind of map and call
php_filter_encode_html
. It's more interesting: it converts stuff like"
,'
,&
and chars with their ASCII codes lower than 32 and higher than 127 to HTML entities, so&
in your string becomes&
. Again, it uses flags for this.Then we get call to
php_strip_tags_ex
, which just strips HTML, XML and PHP tags (according to its definition in/ext/standard/string.c
) and removes NULL bytes, like the comment says.The code that follows it is used for internal string management and doesn't really do any sanitization. Well, not exactly - passing undocumented flag
FILTER_FLAG_EMPTY_STRING_NULL
will returnNULL
if the sanitized string is empty, instead of returning just an empty string, but it's not really that much useful. An example:?
There isn't much more going on, so the manual was fairly correct - to sum it up:
FILTER_FLAG_NO_ENCODE_QUOTES
- This flag does not encode quotes.FILTER_FLAG_STRIP_LOW
- Strip characters with ASCII value below 32.FILTER_FLAG_STRIP_HIGH
- Strip characters with ASCII value above 127.FILTER_FLAG_ENCODE_LOW
- Encode characters with ASCII value below 32.FILTER_FLAG_ENCODE_HIGH
- Encode characters with ASCII value above 127.FILTER_FLAG_ENCODE_AMP
- Encode the & character to&
(not&
).FILTER_FLAG_EMPTY_STRING_NULL
- ReturnNULL
instead of empty strings.