php 批量替换程序的具体实现代码

 更新时间:2013年10月4日 23:36  点击:2427

代码如下:

复制代码 代码如下:

<?php
/***************************************************************************
batch-replace, v1.1
***************************************************************************
file: batch-replace_utf8.php
functionality: 本程序可以扫描指定目录的所有文件,进行内容替换。可用于被批量挂马的删除以及批量更新页面某些内容。
本程序适用于对UTF-8的页面进行修改。

 

/***************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
***************************************************************************/

set_time_limit(3600);


if($_POST['Submit']=='开始执行操作'){
$dir = $_POST['searchpath'];
$shortname = $_POST['shortname'];
$isall = $_POST['isall'];
$isreg = $_POST['isreg'];

if (!get_magic_quotes_gpc()) {
$sstr = $_POST['sstr'];
$rpstr = $_POST['rpstr'];
} else {
$sstr = stripslashes($_POST['sstr']);
$rpstr = stripslashes($_POST['rpstr']);
}


//分析shortname
$arrext = explode ("|",$shortname);


if (!is_dir($dir)) return;
if ($sstr == '') return;

//把末尾的/去掉
if(substr($dir,-1)=='/') $dir = substr($dir,0,strrpos($dir,"/"));

//罗列所有目录
if ($isall == 1){
hx_dirtree($dir);
}else{
hx_dealdir($dir);

}

exit();
}


function hx_dirtree($path="."){
global $sstr,$rpstr,$isreg,$arrext;


$d = dir($path);
while(false !== ($v = $d->read())) {
if($v == "." || $v == "..") continue;
$file = $d->path."/".$v;
if(is_dir($file)) {
echo "<p>$v</p>"; hx_dirtree($file);
}else{
$ext=substr(strrchr($v,"."), 1);
if( in_array($ext , $arrext) ){
echo "<li>$file ";
$body = file_get_contents($file);
if($isreg == 1){
$body2 = preg_replace($sstr, $rpstr, $body);
}else{
$body2 = str_replace($sstr, $rpstr, $body);
}
if($body != $body2 && $body2 != ''){
tofile($file,$body2);
echo ' OK';
}else{
echo ' NO';
}
echo '</li>';
}
}
}
$d->close();
}

function hx_dealdir($dir){
global $sstr,$rpstr,$isreg,$arrext;
if ($dh = opendir($dir)) {
while (false !== ($file = readdir($dh))) {
if(filetype($dir.'/'.$file)=='file'){

$ext=substr(strrchr($file,"."), 1);
if( in_array($ext , $arrext) ){

echo "<li>$file ";
$body = file_get_contents($dir.'/'.$file);
if($isreg == 1){
$body2 = preg_replace($sstr, $rpstr, $body);
}else{
$body2 = str_replace($sstr, $rpstr, $body);
}
if($body != $body2 && $body2 != ''){
tofile($dir.'/'.$file,$body2);
echo ' OK';
}else{
echo ' NO';
}
echo '</li>';
}
}
}
closedir($dh);
}

}
//把生成文件的过程写出函数
function tofile($file_name,$file_content){
if (is_file ($file_name)){
@unlink ($file_name);
}
$handle = fopen ($file_name,"w");
if (!is_writable ($file_name)){
return false;
}
if (!fwrite ($handle,$file_content)){
return false;
}
fclose ($handle); //关闭指针
return $file_name;
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>批量替换程序|木马批量删除_www.itlearner.com</title>
<style type="text/css">
body{background:#FFFFFF;color:#000;font-size:12px;}
#top{text-align:center;}
h1,p,form{margin:0;padding:0;}
h1{font-size;14px;}
</style>
</head>
<body>
<div id="top">
<h1>批量替换程序(UTF-8版)</h1>
<div>本程序可以扫描指定目录的所有文件,进行<strong>内容替换</strong>。可用于被批量挂马的删除以及批量更新页面某些内容。<br/>
在文件数量非常多的情况下,本操作比较占用服务器资源,请确脚本超时限制时间允许更改,否则可能无法完成操作。</div>
</div>


<form action="<?=$_SERVER['SCRIPT_NAME']?>" name="form1" target="stafrm" method="post">
<table width="95%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#666666">
<tr>
<td width="10%" bgcolor="#FFFFFF"><strong> 起始根路径:</strong></td>
<td width="90%" bgcolor="#FFFFFF"><input name="searchpath" type="text" id="searchpath" value="./test" size="20" />
点表示当前目录,末尾不要加/ <input type="checkbox" name="isall" value="1" />包含此目录下所有目录</td>
</tr>
<tr>
<td bgcolor="#FFFFFF"><strong> 文件扩展名:</strong></td>
<td bgcolor="#FFFFFF"><input name="shortname" type="text" id="shortname" size="20" value="php|htm" />
多个请用|隔开</td>
</tr>
<tr id="rpct">
<td height="64" colspan="2" bgcolor="#FFFFFF"><table width="100%" border="0" cellspacing="1" cellpadding="1">
<tr bgcolor="#EDFCE2">
<td colspan="4"><strong>内容替换选项:</strong> <input type="checkbox" name="isreg" value="1" />使用正则表达式</td>
</tr>
<tr>
<td colspan="4">替换内容类默认使用字符串替换,也可以使用正则表达式(需勾选)。"替换为"不填写的话,就表示删除"替换内容"。</td>
</tr>
<tr>
<td width="10%"> 替换内容:</td>
<td width="36%"><textarea name="sstr" id="sstr" style="width:90%;height:45px"></textarea></td>
<td width="10%">替 换 为:</td>
<td><textarea name="rpstr" id="rpstr" style="width:90%;height:45px"></textarea></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="2" height="20" align="center" bgcolor="#E2F5BC"><input type="submit" name="Submit" value="开始执行操作" class="inputbut" /></td>
</tr>
</table>
</form>
<table width="95%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#666666">
<tr bgcolor="#FFFFFF">
<td id="mtd">
<div id='mdv' style='width:100%;height:100;'>
<iframe name="stafrm" frameborder="0" id="stafrm" width="100%" height="100%"></iframe>
</div>
<script type="text/javascript">
document.all.mdv.style.pixelHeight = screen.height - 450;
</script> </td>
</tr>
</table>

</body>
</html>

[!--infotagslink--]

相关文章

  • 不打开网页直接查看网站的源代码

      有一种方法,可以不打开网站而直接查看到这个网站的源代码..   这样可以有效地防止误入恶意网站...   在浏览器地址栏输入:   view-source:http://...2016-09-20
  • php 调用goolge地图代码

    <?php require('path.inc.php'); header('content-Type: text/html; charset=utf-8'); $borough_id = intval($_GET['id']); if(!$borough_id){ echo ' ...2016-11-25
  • JS基于Mootools实现的个性菜单效果代码

    本文实例讲述了JS基于Mootools实现的个性菜单效果代码。分享给大家供大家参考,具体如下:这里演示基于Mootools做的带动画的垂直型菜单,是一个初学者写的,用来学习Mootools的使用有帮助,下载时请注意要将外部引用的mootools...2015-10-23
  • JS+CSS实现分类动态选择及移动功能效果代码

    本文实例讲述了JS+CSS实现分类动态选择及移动功能效果代码。分享给大家供大家参考,具体如下:这是一个类似选项卡功能的选择插件,与普通的TAb区别是加入了动画效果,多用于商品类网站,用作商品分类功能,不过其它网站也可以用,...2015-10-21
  • JS实现自定义简单网页软键盘效果代码

    本文实例讲述了JS实现自定义简单网页软键盘效果。分享给大家供大家参考,具体如下:这是一款自定义的简单点的网页软键盘,没有使用任何控件,仅是为了练习JavaScript编写水平,安全性方面没有过多考虑,有顾虑的可以不用,目的是学...2015-11-08
  • php 取除连续空格与换行代码

    php 取除连续空格与换行代码,这些我们都用到str_replace与正则函数 第一种: $content=str_replace("n","",$content); echo $content; 第二种: $content=preg_replac...2016-11-25
  • php简单用户登陆程序代码

    php简单用户登陆程序代码 这些教程很对初学者来讲是很有用的哦,这款就下面这一点点代码了哦。 <center> <p>&nbsp;</p> <p>&nbsp;</p> <form name="form1...2016-11-25
  • php批量替换内容或指定目录下所有文件内容

    要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
  • PHP实现清除wordpress里恶意代码

    公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。恶意代码示例...2015-10-23
  • js识别uc浏览器的代码

    其实挺简单的就是if(navigator.userAgent.indexOf('UCBrowser') > -1) {alert("uc浏览器");}else{//不是uc浏览器执行的操作}如果想测试某个浏览器的特征可以通过如下方法获取JS获取浏览器信息 浏览器代码名称:navigator...2015-11-08
  • JS实现双击屏幕滚动效果代码

    本文实例讲述了JS实现双击屏幕滚动效果代码。分享给大家供大家参考,具体如下:这里演示双击滚屏效果代码的实现方法,不知道有觉得有用处的没,现在网上还有很多还在用这个特效的呢,代码分享给大家吧。运行效果截图如下:在线演...2015-10-30
  • JS日期加减,日期运算代码

    一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
  • PHP开发微信支付的代码分享

    微信支付,即便交了保证金,你还是处理测试阶段,不能正式发布。必须到你通过程序测试提交订单、发货通知等数据到微信的系统中,才能申请发布。然后,因为在微信中是通过JS方式调用API,必须在微信后台设置支付授权目录,而且要到...2014-05-31
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • php怎么用拼音 简单的php中文转拼音的实现代码

    小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
  • php导出csv格式数据并将数字转换成文本的思路以及代码分享

    php导出csv格式数据实现:先定义一个字符串 存储内容,例如 $exportdata = '规则111,规则222,审222,规222,服2222,规则1,规则2,规则3,匹配字符,设置时间,有效期'."/n";然后对需要保存csv的数组进行foreach循环,例如复制代...2014-06-07
  • ecshop商品无限级分类代码

    ecshop商品无限级分类代码 function cat_options($spec_cat_id, $arr) { static $cat_options = array(); if (isset($cat_options[$spec_cat_id]))...2016-11-25
  • 几种延迟加载JS代码的方法加快网页的访问速度

    本文介绍了如何延迟javascript代码的加载,加快网页的访问速度。 当一个网站有很多js代码要加载,js代码放置的位置在一定程度上将会影像网页的加载速度,为了让我们的网页加载速度更快,本文总结了一下几个注意点...2013-10-13
  • jQuery实现滑动页面固定顶部显示(可根据显示位置消失与替换)

    本文实例讲述了jQuery实现滑动页面固定顶部显示(可根据显示位置消失与替换)。分享给大家供大家参考,具体如下:这里介绍的jQuery拉动页面固定顶部显示,及自动消失效果,可能ie浏览器下有问题,不过火狐什么的都可以运行看效果...2015-10-30
  • vue项目,代码提交至码云,iconfont的用法说明

    这篇文章主要介绍了vue项目,代码提交至码云,iconfont的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-30