我让用户提交一些文字(包括随机的html图片链接)然后我试图从文本中的图像中创建一个基本的BBCode [img] [/ img]标签。
我目前正在测试的方式如下:
字符串(取自随机论坛):
After a fair few years of doing the usual lowering, fitting wheels etc,when it comes to car modifying, we spent a couple of years doing Minimoto racing all round the country in the Southern British Minimoto Championship winning the 2006 Production Privateer Championship.<br />
<br />
<img src="http://i2.photobucket.com/albums/y18/moo0484/scan0001.jpg" border="0" class="tcattdimglink" onload="NcodeImageResizer.createOn(this);" alt="" /><br />
<br />
<img src="http://i2.photobucket.com/albums/y18/moo0484/01072007065.jpg" border="0" class="tcattdimglink" onload="NcodeImageResizer.createOn(this);" alt="" /><br />
然后我替换任何图像属性/使用函数将图像标签更改为bbcode:
function convert($text) {
$text = preg_replace('/class=".*?"/', '', $text);
$text = preg_replace('/alt=".*?"/', '', $text);
$text = preg_replace('/src="/', '', $text);
$text = preg_replace('/border=".*?"/', '', $text);
$text = preg_replace('/onload=".*?"/', '', $text);
$text = str_replace("<img", "[img]", "$text");
$text = str_replace('">', "[/img]", "$text");
return nl2br($text);
}
如果标签未使用尾部斜杠关闭,则此功能完全正常。我可以添加另一条规则:
$text = str_replace('"/>', "[/img]", "$text");
哪个会起作用,但是我仍然留下了空白区域。
所以我的问题是,我可以从img标签之间删除空格:
<img />
例如,在preg_replace函数中。*?替换&#34;&#34;。
之间的内容我可以使用img标签做类似的事情并删除它们之间的空白区域吗?
我显然不能跑:
$text = preg_replace('/\s+/', '', $text);
因为我需要文本中的空格等。
谢谢!
答案 0 :(得分:0)
你应该删除任何空白和rouge属性所以几乎所有属性,尤其是on *事件属性,如onClick,onBlur。有很多方法可以将XSS攻击添加到HTML中。如果你想让用户输入HTML使用htmlpurifier,那么将它们全部删除的东西是不可维护的。它很容易初始化为您的代码,并有很多选项。
一个简单的替代方法是只提取img的src然后删除属性并将src放回并生成一串图像,然后使用strip_tags()删除所有HTML,然后将图像连接到文本上。它缺乏图像的定位。
类似于:
<?php
$html = <<<DEMO
After a fair <script>alert('XSS');</script>few ...
winning the 2006 Production Privateer Championship.<br />
<div style="background-image: url(javascript:alert('XSS'))"></div>
<br />
<img src="http://i2.photobucket.com/albums/y18/moo0484/scan0001.jpg" border="0" class="tcattdimglink" onload="NcodeImageResizer.createOn(this);" alt="" /><br />
<br />
text here
<img src="http://i2.photobucket.com/albums/y18/moo0484/01072007065.jpg" border="0" class="tcattdimglink" onload="NcodeImageResizer.createOn(this);" alt="" /><br />
more txt here
DEMO;
$dom = new DOMDocument;
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
if (false === ($elements = $xpath->query("//*"))) die('Error');
foreach ($elements as $element) {
//remove script tags
if($element->nodeName=='script'){
$element->parentNode->removeChild($element);
}
//remove empty tags but not images
if (!$element->hasChildNodes() || $element->nodeValue == '') {
if($element->nodeName != 'img'){
$element->parentNode->removeChild($element);
}
}
//remove all attributes except links and imgs
for ($i = $element->attributes->length; --$i >= 0;) {
$name = $element->attributes->item($i)->name;
if (('img' === $element->nodeName && 'src' === $name) || ('a' === $element->nodeName && 'href' === $name)){
continue;
}
$element->removeAttribute($name);
}
}
//put dom together and remove the document body
echo preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $dom->saveHTML());
/*
<p>After a fair few ...
winning the 2006 Production Privateer Championship.</p>
<img src="http://i2.photobucket.com/albums/y18/moo0484/scan0001.jpg">
text here
<img src="http://i2.photobucket.com/albums/y18/moo0484/01072007065.jpg">
more txt here
*/
虽然只是考虑使用htmlpurifier,但是1990年代他们也在呼唤他们希望BBCODE重新使用降价。 ; P
祝你好运