Kullanıcı PHP HTML sunulan izin

6 Cevap php

Ben kullanıcı bir çok kullanıcı profilleri için html sunulan izin vermek istiyorum, ben şu anda istemiyorum ne filtrelemek için çalışın ama ben şimdi bir beyaz liste yaklaşım değiştirmek ve kullanmak isteyen duyuyorum.

İşte benim geçerli olmayan-beyaz liste yaklaşım

function FilterHTML($string) {
    if (get_magic_quotes_gpc()) {
    	$string = stripslashes($string);
    }
    $string = html_entity_decode($string, ENT_QUOTES, "ISO-8859-1");
    // convert decimal
    $string = preg_replace('/&#(\d+)/me', "chr(\\1)", $string); // decimal notation
    // convert hex
    $string = preg_replace('/&#x([a-f0-9]+)/mei', "chr(0x\\1)", $string); // hex notation
    //$string = html_entity_decode($string, ENT_COMPAT, "UTF-8");
    $string = preg_replace('#(&\#*\w+)[\x00-\x20]+;#U', "$1;", $string);
    $string = preg_replace('#(<[^>]+[\s\r\n\"\'])(on|xmlns)[^>]*>#iU', "$1>", $string);
    //$string = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $string); //bad line
    $string = preg_replace('#/*\*()[^>]*\*/#i', "", $string); // REMOVE /**/
    $string = preg_replace('#([a-z]*)[\x00-\x20]*([\`\'\"]*)[\\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iU', '...', $string); //JAVASCRIPT
    $string = preg_replace('#([a-z]*)([\'\"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iU', '...', $string); //VBSCRIPT
    $string = preg_replace('#([a-z]*)[\x00-\x20]*([\\\]*)[\\x00-\x20]*@([\\\]*)[\x00-\x20]*i([\\\]*)[\x00-\x20]*m([\\\]*)[\x00-\x20]*p([\\\]*)[\x00-\x20]*o([\\\]*)[\x00-\x20]*r([\\\]*)[\x00-\x20]*t#iU', '...', $string); //@IMPORT
    $string = preg_replace('#([a-z]*)[\x00-\x20]*e[\x00-\x20]*x[\x00-\x20]*p[\x00-\x20]*r[\x00-\x20]*e[\x00-\x20]*s[\x00-\x20]*s[\x00-\x20]*i[\x00-\x20]*o[\x00-\x20]*n#iU', '...', $string); //EXPRESSION
    $string = preg_replace('#</*\w+:\w[^>]*>#i', "", $string);
    $string = preg_replace('#</?t(able|r|d)(\s[^>]*)?>#i', '', $string); // strip out tables
    $string = preg_replace('/(potspace|pot space|rateuser|marquee)/i', '...', $string); // filter some words
    //$string = str_replace('left:0px; top: 0px;','',$string);
    do {
    	$oldstring = $string;
    	//bgsound|
    	$string = preg_replace('#</*(applet|meta|xml|blink|link|script|iframe|frame|frameset|ilayer|layer|title|base|body|xml|AllowScriptAccess|big)[^>]*>#i', "...", $string);
    } while ($oldstring != $string);
    return addslashes($string);
}

The above works pretty well, I have never had any problems after 2 years of use with it but for a whitelist approach is there anything similars to stackoverflows C# method but in PHP? http://refactormycode.com/codes/333-sanitize-html

6 Cevap

Maybe it is safer to use DOMDocument to analyze it correctly, remove disallowed tags with removeChild() and then get the result. It is not always safe to filter stuff with regular expressions, specially if things start to get such complexity. Hackers can find a way to cheat your filters, forums and social networks do know that very well.

Örneğin, tarayıcılar

HTML Purifier Orada iyi HTML ayrıştırıcı / temizleyicidir.

Aslında elde etmek için oldukça basit bir amacı var - sadece beyaz listeye etiketlerin listeden bazı etiketler DEĞİLDİR şey için kontrol ve kaynaktan bunları kaldırmak gerekir. Bu bir regex ile oldukça kolayca yapılabilir.

function sanitize($html) {
  $whitelist = array(
    'b', 'i', 'u', 'strong', 'em', 'a'
  );

  return preg_replace("/<(^".implode("|", $whitelist).")(.*)>(.*)<\/(^".implode("|", $whitelist).")>/", "", $html);
}

Ben bu test değil, ve oralarda bir yerde bir hata muhtemelen var ama bunu nasıl çalıştığını yüreğin olsun. Ayrıca, Tekstil veya Markdown gibi bir biçimlendirme dili kullanarak bakmak isteyebilirsiniz.

Jamie

Sadece strip_tags () işlevini kullanabilirsiniz

Fonksiyon olarak tanımlanır yana

string strip_tags  ( string $str  [, string $allowable_tags  ] )

Bunu yapabilirsiniz:

$html = $_POST['content'];
$html = strip_tags($html, '<b><a><i><u><span>');

Ama sayisinda strip_tags kullanırken, özelliklerini kapalı filtre mümkün olmayacaktır dikkat çekmek. örneğin

<a href="javascript:alert('haha caught cha!');">link</a>

Aşağıda bu fonksiyonu "getCleanHTML" deneyin Beyaz liste etiketi adıyla elemanlarının istisnalar dışında elemanları metin içeriğini ayıklayın. Bu kod, temiz ve kolay anlaşılır ve hata ayıklama olduğunu.

<?php

$TagWhiteList = array(
    'b', 'i', 'u', 'strong', 'em', 'a', 'img'
);

function getHTMLCode($Node) {
    $Document = new DOMDocument();    
    $Document->appendChild($Document->importNode($Node, true));
    return $Document->saveHTML();
}
function getCleanHTML($Node, $Text = "") {
    global $TagWhiteList;

    $TextName = $Node->tagName;
    if ($TextName == null)
        return $Text.$Node->textContent;

    if (in_array($TextName, $TagWhiteList)) 
        return $Text.getHTMLCode($Node);

    $Node = $Node->firstChild;
    if ($Node != null)
        $Text = getCleanHTML($Node, $Text);

    while($Node->nextSibling != null) {
        $Text = getCleanHTML($Node->nextSibling, $Text);
        $Node = $Node->nextSibling;
    }
    return $Text;
}

$Doc = new DOMDocument();
$Doc->loadHTMLFile("Test.html");
echo getCleanHTML($Doc->documentElement)."\n";

?>

Umarım bu yardımcı olur.

Sayisinda strip_tags kullanarak sadece düşündüren o sizin için ... farkında olmak: strip_tags o olacak da karışıklık etiket niteliklerini ve kırık etiketleri yukarı dışarı şerit GEÇMEZ.

Manuel sayfasından:

Warning Çünkü strip_tags () aslında kısmi, HTML doğrulamak değil, ya da kırık etiketleri beklenenden daha fazla metin / veri kaldırılmasına neden olabilir.

Warning This function does not modify any attributes on the tags that you allow using allowable_tags , including the style and onmouseover attributes that a mischievous user may abuse when posting text that will be shown to other users.

Sadece bu bir çözüm güvenemez.