Platon
Новичок
Зачем нужен этот клас? При его тестировании он не выполняет никаких действий над именем пользователя, которое я ввожу в форму. То есть если я ввожу имя с различными html - тегами или с другими не допустимыми символами (?&><']{"/\ и т.д), фильтрация не выполняется
Вот форма: index.php
Вот сам класс: my.class.php
Вот форма: index.php
PHP:
<?php
require 'my.class.php';
$parse = new ParseFilter();
$name = $parse->process( htmlspecialchars( trim( $_POST['name'])));
$name = preg_replace('#\s+#i', ' ', $name);
echo $name;
?>
<form method="post">
<input type="text" name="name" >
<input type="submit" value="GO" >
</form>
PHP:
<?php
class ParseFilter{
var $xssAuto;
var $wysiwyg = false;
var $safe_mode = false;
var $allow_code = true;
var $allow_url = true;
var $allow_image = true;
var $edit_mode = true;
var $code_text = array ();
var $code_count = 0;
var $tagsArray;
var $tagsMethod;
var $tagBlacklist = array ('applet', 'body', 'bgsound', 'base', 'basefont', 'frame', 'frameset', 'head', 'html', 'id', 'iframe', 'ilayer', 'layer', 'link', 'meta', 'name', 'script', 'style', 'title', 'xml' );
var $attrArray;
var $attrMethod;
var $attrBlacklist = array ('action', 'background', 'codebase', 'dynsrc', 'lowsrc' );
function process($source){
//Если функция get_magic_quotes_gpc определена (true) и если magic_quotes включен ( то все ' (одинарные кавычки), " (двойные кавычки), \ (обратный слеш) и NUL экранируются обратным слешем автоматически), то ...
//stripslashes — Удаляет экранирование символов (вместо обратных слэшей будут удаляться двойные одинарные кавычки.)
if( function_exists( "get_magic_quotes_gpc" ) && get_magic_quotes_gpc() ) $source = stripslashes( $source );
$source = $this->remove( $this->decode( $source ));
if( $this->code_count ){
foreach ( $this->code_text as $key_find => $key_replace ){
$find[] = $key_find;
$replace[] = $key_replace;
}
$source = str_replace( $find, $replace, $source );
}
$this->code_count = 0;
$this->code_text = array();
$source = preg_replace( "#\{include#i", "{include", $source );
$source = preg_replace( "#<iframe#i", "<iframe", $source );
$source = preg_replace( "#<script#i", "<script", $source );
$source = str_replace( "<?", "<?", $source );
$source = str_replace( "?>", "?>", $source );
$source = addslashes( $source );
return $source;
}
function remove($source){
$loopCounter = 0;
while ( $source != $this->filterTags( $source )){
$source = $this->filterTags( $source );
$loopCounter ++;
}
return $source;
}
function decode($source){
if ($this->allow_code)
$source = preg_replace( "#\[code\](.+?)\[/code\]#ies", "\$this->code_tag( '\\1' )", $source );
if ($this->safe_mode AND !$this->wysiwyg){
$source = htmlspecialchars( $source, ENT_QUOTES );
$source = str_replace( '&', '&', $source );
}else{
$source = str_replace( "<>", "<>", str_replace( ">>", ">>", str_replace( "<<", "<<", $source )));
$source = str_replace( "<!--", "<!--", $source );
}
return $source;
}
function code_tag($txt = ""){
if ( $txt == "" ){
return;
}
$this->code_count ++;
if ( $this->edit_mode ){
$txt = str_replace( "&", "&", $txt );
$txt = str_replace( "'", "'", $txt );
$txt = str_replace( "<", "<", $txt );
$txt = str_replace( ">", ">", $txt );
$txt = str_replace( """, """, $txt );
$txt = str_replace( "\\\"", """, $txt );
$txt = str_replace( ":", ":", $txt );
$txt = str_replace( "[", "[", $txt );
$txt = str_replace( "]", "]", $txt );
$txt = str_replace( "\r", "__CODENR__", $txt );
$txt = str_replace( "\n", "__CODENN__", $txt );
}
$p = "[code]{" . $this->code_count . "}[/code]";
$this->code_text[$p] = "[code]{$txt}[/code]";
return $p;
}
function filterTags($source){
$preTag = NULL;
$postTag = $source;
$tagOpen_start = strpos( $source, '<' );
while ($tagOpen_start !== FALSE ){
$preTag .= substr( $postTag, 0, $tagOpen_start );
$postTag = substr( $postTag, $tagOpen_start );
$fromTagOpen = substr( $postTag, 1 );
$tagOpen_end = strpos( $fromTagOpen, '>' );
if ($tagOpen_end === false) break;
$tagOpen_nested = strpos( $fromTagOpen, '<' );
if (($tagOpen_nested !== false) && ($tagOpen_nested < $tagOpen_end)){
$preTag .= substr( $postTag, 0, ($tagOpen_nested + 1));
$postTag = substr( $postTag, ($tagOpen_nested + 1));
$tagOpen_start = strpos( $postTag, '<' );
continue;
}
$tagOpen_nested = (strpos( $fromTagOpen, '<' ) + $tagOpen_start + 1);
$currentTag = substr( $fromTagOpen, 0, $tagOpen_end);
$tagLength = strlen( $currentTag );
if (!$tagOpen_end){
$preTag .= $postTag;
$tagOpen_start = strpos( $postTag, '<' );
}
$tagLeft = $currentTag;
$attrSet = array();
$currentSpace = strpos( $tagLeft, ' ');
if (substr( $currentTag, 0, 1 ) == "/" ){
$isCloseTag = TRUE;
list ( $tagName ) = explode( ' ', $currentTag);
$tagName = substr( $tagName, 1);
}else{
$isCloseTag = FALSE;
list ($tagName) = explode(' ', $currentTag);
}
if ((!preg_match("/^[a-z][a-z0-9]*$/i", $tagName)) || (!$tagName) || ((in_array( strtolower($tagName), $this->tagBlacklist)) && ($this->xssAuto))){
$postTag = substr( $postTag, ($tagLength + 2));
$tagOpen_start = strpos( $postTag, '<' );
continue;
}
while ($currentSpace !== FALSE ){
$fromSpace = substr( $tagLeft, ($currentSpace + 1));
$nextSpace = strpos( $fromSpace, ' ' );
$openQuotes = strpos( $fromSpace, '"' );
$closeQuotes = strpos( substr( $fromSpace, ($openQuotes + 1) ), '"' ) + $openQuotes + 1;
if (strpos($fromSpace, '=' ) !== FALSE ){
if (($openQuotes !== FALSE) && (strpos( substr($fromSpace, ($openQuotes + 1)), '"' )!== FALSE) ) $attr = substr($fromSpace, 0, ($closeQuotes + 1));
else $attr = substr( $fromSpace, 0, $nextSpace);
}else $attr = substr( $fromSpace, 0, $nextSpace);
if (! $attr ) $attr = $fromSpace;
$attrSet[] = $attr;
$tagLeft = substr( $fromSpace, strlen($attr ));
$currentSpace = strpos( $tagLeft, ' ');
}
$tagFound = in_array( strtolower( $tagName ), $this->tagsArray);
if ((!$tagFound && $this->tagsMethod) || ($tagFound && ! $this->tagsMethod)){
if (!$isCloseTag){
$attrSet = $this->filterAttr($attrSet, strtolower( $tagName ));
$preTag .= '<' . $tagName;
for ($i = 0; $i < count( $attrSet ); $i ++)
$preTag .= ' ' . $attrSet[$i];
if (strpos( $fromTagOpen, "</" . $tagName ) ) $preTag .= '>';
else $preTag .= ' />';
}else $preTag .= '</' . $tagName . '>';
}
$postTag = substr( $postTag, ($tagLength + 2));
$tagOpen_start = strpos( $postTag, '<' );
}
$preTag .= $postTag;
return $preTag;
}
function filterAttr($attrSet, $tagName){
global $config;
$newSet = array();
for ($i = 0; $i < count( $attrSet ); $i ++){
if (! $attrSet[$i]) continue;
$attrSet[$i] = trim( $attrSet[$i]);
$exp = strpos( $attrSet[$i], '=' );
if ($exp === false) $attrSubSet = Array ($attrSet[$i]);
else{
$attrSubSet = Array();
$attrSubSet[] = substr( $attrSet[$i], 0, $exp );
$attrSubSet[] = substr( $attrSet[$i], $exp + 1 );
}
$attrSubSet[1] = stripslashes( $attrSubSet[1] );
list ($attrSubSet[0] ) = explode( ' ', $attrSubSet[0]);
$attrSubSet[0] = strtolower( $attrSubSet[0] );
if ((! preg_match( "/^[a-z]*$/i", $attrSubSet[0] )) || (($this->xssAuto) && ((in_array( $attrSubSet[0], $this->attrBlacklist )) || (substr( $attrSubSet[0], 0, 2 ) == 'on')))) continue;
if ($attrSubSet[1]){
$attrSubSet[1] = str_replace( '&#', '', $attrSubSet[1]);
if (strtolower($config['charset']) == "utf-8") $attrSubSet[1] = preg_replace( '/\s+/u', ' ', $attrSubSet[1]);
else $attrSubSet[1] = preg_replace('/\s+/', ' ', $attrSubSet[1]);
$attrSubSet[1] = str_replace( '"', '', $attrSubSet[1]);
if ((substr( $attrSubSet[1], 0, 1 ) == "'") && (substr( $attrSubSet[1], (strlen( $attrSubSet[1] ) - 1), 1 ) == "'") ) $attrSubSet[1] = substr($attrSubSet[1], 1, (strlen( $attrSubSet[1] ) - 2));
}
if (((strpos( strtolower( $attrSubSet[1] ), 'expression' ) !== false) && ($attrSubSet[0] == 'style')) || (strpos( strtolower( $attrSubSet[1] ), 'javascript:' ) !== false) || (strpos( strtolower( $attrSubSet[1] ), 'behaviour:' ) !== false) || (strpos( strtolower( $attrSubSet[1] ), 'vbscript:' ) !== false) || (strpos( strtolower( $attrSubSet[1] ), 'mocha:' ) !== false) || (strpos( strtolower( $attrSubSet[1] ), 'data:' ) !== false and $attrSubSet[0] == "href") || (strpos( strtolower( $attrSubSet[1] ), 'data:' ) !== false and $attrSubSet[0] == "data") || (strpos( strtolower( $attrSubSet[1] ), 'data:' ) !== false and $attrSubSet[0] == "src") || ($attrSubSet[0] == "href" and @strpos( strtolower( $attrSubSet[1] ), $config['admin_path'] ) !== false and preg_match( "/[?&%<\[\]]/", $attrSubSet[1] )) || (strpos( strtolower( $attrSubSet[1] ), 'livescript:' ) !== false) ) continue;
$attrFound = in_array( $attrSubSet[0], $this->attrArray);
if ((! $attrFound && $this->attrMethod) || ($attrFound && ! $this->attrMethod)){
if ($attrSubSet[1]) $newSet[] = $attrSubSet[0] . '="' . $attrSubSet[1] . '"';
elseif( $attrSubSet[1] == "0" ) $newSet[] = $attrSubSet[0] . '="0"';
else $newSet[] = $attrSubSet[0] . '=""';
}
}
;
return $newSet;
}
}
?>