function sync_files( $remote, $local )
{
if ( ! is_file( $local ) ) { return copy( $remote, $local ); }
$handle = fopen( $remote, 'r' );
if ( ! $handle ) { die( "Could not open {$remote}." ); }
$meta = stream_get_meta_data( $handle );
fclose( $handle );
foreach( $meta['wrapper_data'] as $response )
{
// Redirection
if ( substr( strtolower( $response ), 0, 10 ) === 'location: ' ) {
return sync_files( substr( $response, 10 ), $local );
}
// Compare sizes
if ( substr( strtolower( $response ), 0, 16 ) === 'content-length: ' )
{
if ( (int) filesize( $local ) !== (int) substr( $response, 16 ) ) {
return copy( $remote, $local );
}
continue;
}
// Compare dates
if ( substr( strtolower( $response ), 0, 15 ) === 'last-modified: ' )
{
if ( (int) filemtime( $local ) < (int) strtotime( substr( $response, 15 ) ) ) {
return copy( $remote, $local );
}
continue;
}
}
return false;
}
Remove Nodes (Comments, Script Tags) from $html String
function remove_nodes( $html, array $selectors = array( '//comment()', '//script' ) )
{
$dom = new DOMDocument;
$dom->loadHtml( strval( $html ) );
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$xpath = new DOMXPath( $dom );
foreach( $selectors as $selector ) {
while ( $node = $xpath->query( $selector )->item( 0 ) ) {
// Remove selected tag from html string
$node->parentNode->removeChild( $node );
}
}
return $dom->saveHTML();
}
Sanitize Key : Lowercase Accentless $string, Special Characters $replacement
function sanitize_key( $string, $replacement = '_' )
{
// Lowercase and remove accents
$string = htmlentities( trim( strtolower( strval( $string ) ) ), ENT_NOQUOTES );
$string = preg_replace( '/&([a-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);/', '$1', $string );
$string = preg_replace( '/&([a-z]{2})(?:lig);/', '$1', $string );
$string = preg_replace( '/&[^;]+;/', $replacement, $string );
// Replace non-alphanumeric characters
return preg_replace( '/[^a-z0-9]+/', $replacement, $string );
}
Sanitize $value According to its $type
function sanitize( $value, $type = null )
{
// Recursive sanitation for arrays and objects
if ( is_array( $value ) || is_object( $value ) ) {
$output = array();
foreach ( (array) $value as $k => $v ) {
$k = sanitize_key( $k );
$output[$k] = sanitize( $v, $type );
}
return $output;
}
// Default to value's actual type
if ( ! $type ) { $type = gettype( $value ); }
switch( strtolower( $type ) )
{
case 'boolean':
return boolval( $value );
case 'integer':
case 'number':
return intval( $value );
case 'double':
case 'float':
return floatval( $value );
case 'null':
return null;
case 'resource':
return $value;
case 'url':
return filter_var( rtrim( $value, '/' ), FILTER_SANITIZE_URL );
case 'email':
return filter_var( substr( $value, 0, 254 ), FILTER_SANITIZE_EMAIL );
default:
// String
return filter_var( $value, FILTER_SANITIZE_STRING );
}
}
Is the Request Made with Ajax ?
function is_ajax_request() {
return ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] )
&& strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) === 'xmlhttprequest';
}
Is the Browser Older Than Internet Explorer $version ?
function is_lt_ie( $version = 8 )
{
preg_match( '/MSIE (.*?);/', $_SERVER['HTTP_USER_AGENT'], $matches );
if ( ! isset( $matches[1] ) ) {
preg_match( '/Trident/d{1,2}.d{1,2}; rv:([0-9]*)/', $_SERVER['HTTP_USER_AGENT'], $matches );
}
return ( isset( $matches[1] ) && $matches[1] < $version );
}