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


Posted in PHP onOctober 04, 2013

代码如下:

<?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>
PHP 相关文章推荐
Email+URL的判断和自动转换函数
Oct 09 PHP
Zend Studio for Eclipse的java.lang.NullPointerException错误的解决方法
Dec 06 PHP
php.ini中date.timezone设置分析
Jul 29 PHP
php操作JSON格式数据的实现代码
Dec 24 PHP
php empty()与isset()区别的详细介绍
Jun 17 PHP
PHP遍历并打印指定目录下所有文件实例
Feb 10 PHP
采用thinkphp自带方法生成静态html文件详解
Jun 13 PHP
PHP获取MySql新增记录ID值的3种方法
Jun 24 PHP
PHP 正则表达式常用函数
Aug 17 PHP
33道php常见面试题及答案
Jul 06 PHP
PHP实现一维数组与二维数组去重功能示例
May 24 PHP
Laravel等框架模型关联的可用性浅析
Dec 15 PHP
php5.5中类级别的常量使用介绍
Oct 02 #PHP
php mysql_real_escape_string函数用法与实例教程
Sep 30 #PHP
PHP文件上传主要代码讲解
Sep 30 #PHP
php中利用str_pad函数生成数字递增形式的产品编号
Sep 30 #PHP
PHP中func_get_args(),func_get_arg(),func_num_args()的区别
Sep 30 #PHP
PHP设置一边执行一边输出结果的代码
Sep 30 #PHP
PHP file_get_contents设置超时处理方法
Sep 30 #PHP
You might like
PHP概述.
2006/10/09 PHP
php的access操作类
2008/04/09 PHP
CodeIgniter php mvc框架 中国网站
2008/05/26 PHP
如何取得中文字符串中出现次数最多的子串
2013/08/08 PHP
PHP调试函数和日志记录函数分享
2015/01/31 PHP
php+ajax实现无刷新动态加载数据技术
2015/04/28 PHP
php判断表是否存在的方法
2015/06/18 PHP
jQuery 图片切换插件(代码比较少)
2012/05/07 Javascript
基于jquery的DIV随滚动条滚动而滚动的代码
2012/07/20 Javascript
asp.net刷新本页面的六种方法总结
2014/01/07 Javascript
谈谈我对JavaScript原型和闭包系列理解(随手笔记8)
2015/12/24 Javascript
Bootstrap网格系统详解
2016/04/26 Javascript
JS打印组合功能
2016/08/04 Javascript
jQuery插件fullPage.js实现全屏滚动效果
2016/12/02 Javascript
用vue和node写的简易购物车实现
2017/04/25 Javascript
JS解析url查询参数的简单代码
2017/08/06 Javascript
深入浅析JavaScript中的RegExp对象
2017/09/18 Javascript
node通过express搭建自己的服务器
2017/09/30 Javascript
MUI顶部选项卡的用法(tab-top-webview-main)详解
2017/10/08 Javascript
深入理解js A*寻路算法原理与具体实现过程
2018/12/13 Javascript
vue缓存的keepalive页面刷新数据的方法
2019/04/23 Javascript
微信小程序之导航滑块视图容器功能的实现代码(简单两步)
2020/06/19 Javascript
[01:09]模型精美,特效酷炫!TI9不朽宝藏Ⅰ鉴赏
2019/05/10 DOTA
PyQt打开保存对话框的方法和使用详解
2019/02/27 Python
详解Django将秒转换为xx天xx时xx分
2019/09/27 Python
详解Anaconda安装tensorflow报错问题解决方法
2020/11/01 Python
canvas小画板之平滑曲线的实现
2020/08/12 HTML / CSS
婚鞋、新娘鞋、礼服鞋、童鞋:Nina Shoes
2019/09/04 全球购物
建筑毕业生自我鉴定
2013/10/18 职场文书
办公室前台岗位职责范本
2013/12/10 职场文书
自我鉴定四大框架
2014/01/17 职场文书
节约用水倡议书
2014/04/16 职场文书
入党政审材料范文
2014/12/24 职场文书
幼儿园教师节感谢信
2015/01/23 职场文书
宾馆安全管理制度
2015/08/06 职场文书
导游词之广西漓江
2019/11/02 职场文书