A Trap with get_html_translation_table()
You may know unhtmlspecialchars() has implemented into PHP.
With former versions of PHP, we had made such a custom function using get_html_translation_table() like this:
function my_unhtmlspecialchars( $text , $quotes = ENT_QUOTES )
{
return strtr( $text , array_flip( get_html_translation_table( HTML_SPECIALCHARS , $quotes ) ) ) ;
}
<?php
var_dump( htmlspecialchars( '"\'<>&' , ENT_QUOTES ) ) ;
var_dump( get_html_translation_table( HTML_SPECIALCHARS , ENT_QUOTES ) ) ;
?>
string(25) "& quot;& #039;& lt;& gt;& amp;"
array(5) {
["""]=>
string(6) "& quot;"
["'"]=>
string(5) "& #39;"
["<"]=>
string(4) "& lt;"
[">"]=>
string(4) "& gt;"
["& "]=>
string(5) "& amp;"
}
WYSIWYG Editors require "allow HTML" for the system.
But it must invite "Script Insertion" attacks easily.
kentauls told me HTMLPurifier.
http://htmlpurifier.org/
It looks great especially smoketest for XSS.
You should know HTMLPurifier can work with PHP5 only though the documentation tells us that it can work with PHP>=4.3.2.
Anyway, I've included this library into Protector.
You can try "postcommon_post_htmlpurify4guest.php" as protector's filter plugin.
But, it is just a sample.
I'll modify my modules can use HTMLPurifier as necessary by config.
HTMLPurifier allows us "WYSIWYG forum" etc.
There are important difference in order of functions for shutdown and output-buffering between PHP4.x/5.0.x and PHP5.1
<?php
register_shutdown_function( 'sf' ) ;
ob_start( 'ob' ) ;
echo 'main ' ;
function ob( $s )
{
return $s.'ob ' ;
}
function sf()
{
echo 'sf ' ;
}
?>
main ob sf
main sf ob
<?php
register_shutdown_function( 'sf' ) ;
ob_start( 'ob' ) ;
echo 'main ' ;
function ob( $s )
{
return $s.'ob ' ;
}
function sf()
{
while( @ob_end_flush() ) ; // here
echo 'sf ' ;
}
?>
array_map() the built-in function of PHP is fast and useful one.
eg1) fetching an array of integer from a textbox. [1,2,3,4]
You can get it by just a line.
$myarray = array_map( 'intval' , explode( ',' , $post_data ) ) ;
$merged_array = array_map( 'unserialize' , array_unique( array_map( 'serialize' , array_merge( $array1 , $array2 ) ) ) ) ;
Index specified by a boolean, a double, ant an int will be integer index via intval()
exception: out of the range of 32bit integer
Index specified by a string will be two type of index by the condition
/^[-]?[1-9][0-9]*$/