过滤html在线编辑器产生有危害代码

巴扎黑
发布: 2017-03-30 15:01:21
原创
2308人浏览过

部分强大的在线编辑器,已经包含了代码整理,过滤功能,但js处理的能被很轻易的饶过,服务端必须要再次过滤一次,这几天花了点时间,写了部分,希望对大家有点用处,本人能力有限,还请有能力的朋友补全它。



/*不需要过滤的数组*/
$htm_on=array(
"<acronym","acronym>",
"<basefont","basefont>",
"<button","button>",
"<caption","caption>",
"<clientinformation","clientinformation>",
"<font","font>",
"<implementation","implementation>",
"<button","button>",
"<location","location>",
"<option","option>",
"<selection","selection>",
"<strong","strong>",
"font");

$htm_on_uper=array(
"<acronym","acronym>",
"<basefont","basefont>",
"<button","button>",
"<caption","caption>",
"<clientinformation","clientinformation>",
"<font","font>",
"<implementation","implementation>",
"<button","button>",
"<location","location>",
"<option","option>",
"<selection","selection>",
"<strong","strong>",
"font");

/*字符格式*/
$str=strtolower($str);
$str=preg_replace("//s+/", " ", $str);//过滤回车
$str=preg_replace("/ +/", " ", $str);//过滤多个空格

/*过滤/替换几种形式的js*/
$str=preg_replace("/<(script.*?)>(.*?)<(//script.*?)>/si","",$str);//删除<script>。。。</script>格式,
//$str=preg_replace("/<(script.*?)>(.*?)<(//script.*?)>/si","<//1>//2<//3>",$str);//替换为可以显示的,

$str=preg_replace("/<(script.*?)>/si","",$str);//删除<script>未封闭
//$str=preg_replace("/<(script.*?)>/si","<//1>",$str);//替换未封闭

/*删除/替换表单*/
$str=preg_replace("/<(//?form.*?)>/si","",$str);//删除表单
//$str=preg_replace("/<(//?form.*?)>/si","<//1>",$str);//替换表单

$str=preg_replace("/<(i?frame.*?)>(.*?)<(//i?frame.*?)>/si","",$str);//删除框架
//$str=preg_replace("/<(i?frame.*?)>(.*?)<(//i?frame.*?)>/si","<//1>//2<//3>",$str);//替换框架

/*过滤on事件*/
$str=preg_replace("/href=(.+?)([/"|/'| |>])/ie","'href='.strtoupper('//1').'//2'",$str);//把href=涉及到的on转换为大写。
$str=str_replace($htm_on,$htm_on_uper,$str);//把<font,font>换为大写,dhtml标签字符,正则判断太烦琐,采用转换办法。
$str=preg_replace("/(on[^ /.<>]+?)([ |>])/s","//2",$str);//取掉on事件

/*过滤超级连接的js*/
$str=preg_replace("/(href|src|background|url|dynsrc|expression|codebase)[=:/(]([ /"/']*?/w+/..*?|javascript|vbscript:[^>]*?)(/)?)([ >//])/si","//1='#' //3//4",$str);//取掉href=javascript:

//返回小写字符
$str=strtolower($str);
$str=str_replace("&","&",$str);

以上就是过滤html在线编辑器产生有危害代码 的详细内容,更多请关注php中文网其它相关文章!

相关标签:
HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号