Chinaunix首页 | 论坛 | 博客
  • 博客访问: 449350
  • 博文数量: 141
  • 博客积分: 211
  • 博客等级: 入伍新兵
  • 技术积分: 1049
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-17 16:25
个人简介

如此经年,望尽千帆。

文章分类

全部博文(141)

文章存档

2014年(73)

2013年(65)

2012年(3)

我的朋友

分类: PHP

2013-10-10 16:05:06

php 批量替换程序的具体实现代码。
  1. 代码如下:
  2. <?php
  3. /***************************************************************************
  4. batch-replace, v1.1
  5. ***************************************************************************
  6. file: batch-replace_utf8.php
  7. functionality: 本程序可以扫描指定目录的所有文件,进行内容替换。可用于被批量挂马的删除以及批量更新页面某些内容。
  8. 本程序适用于对UTF-8的页面进行修改。
  9. /***************************************************************************
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. * edit
  16. ***************************************************************************/
  17. set_time_limit(3600);

  18. if($_POST['Submit']=='开始执行操作'){
  19. $dir = $_POST['searchpath'];
  20. $shortname = $_POST['shortname'];
  21. $isall = $_POST['isall'];
  22. $isreg = $_POST['isreg'];
  23. if (!get_magic_quotes_gpc()) {
  24. $sstr = $_POST['sstr'];
  25. $rpstr = $_POST['rpstr'];
  26. } else {
  27. $sstr = stripslashes($_POST['sstr']);
  28. $rpstr = stripslashes($_POST['rpstr']);
  29. }

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

  32. if (!is_dir($dir)) return;
  33. if ($sstr == '') return;
  34. //把末尾的/去掉
  35. if(substr($dir,-1)=='/') $dir = substr($dir,0,strrpos($dir,"/"));
  36. //罗列所有目录
  37. if ($isall == 1){
  38. hx_dirtree($dir);
  39. }else{
  40. hx_dealdir($dir);
  41. }
  42. exit();
  43. }

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

  46. $d = dir($path);
  47. while(false !== ($v = $d->read())) {
  48. if($v == "." || $v == "..") continue;
  49. $file = $d->path."/".$v;
  50. if(is_dir($file)) {
  51. echo "

    $v

  52. "; hx_dirtree($file);
  53. }else{
  54. $ext=substr(strrchr($v,"."), 1);
  55. if( in_array($ext , $arrext) ){
  56. echo "
  57. $file ";
  58. $body = file_get_contents($file);
  59. if($isreg == 1){
  60. $body2 = preg_replace($sstr, $rpstr, $body);
  61. }else{
  62. $body2 = str_replace($sstr, $rpstr, $body);
  63. }
  64. if($body != $body2 && $body2 != ''){
  65. tofile($file,$body2);
  66. echo ' OK';
  67. }else{
  68. echo ' NO';
  69. }
  70. echo '
  71. ';
  72. }
  73. }
  74. }
  75. $d->close();
  76. }
  77. function hx_dealdir($dir){
  78. global $sstr,$rpstr,$isreg,$arrext;
  79. if ($dh = opendir($dir)) {
  80. while (false !== ($file = readdir($dh))) {
  81. if(filetype($dir.'/'.$file)=='file'){
  82. $ext=substr(strrchr($file,"."), 1);
  83. if( in_array($ext , $arrext) ){
  84. echo "
  85. $file ";
  86. $body = file_get_contents($dir.'/'.$file);
  87. if($isreg == 1){
  88. $body2 = preg_replace($sstr, $rpstr, $body);
  89. }else{
  90. $body2 = str_replace($sstr, $rpstr, $body);
  91. }
  92. if($body != $body2 && $body2 != ''){
  93. tofile($dir.'/'.$file,$body2);
  94. echo ' OK';
  95. }else{
  96. echo ' NO';
  97. }
  98. echo '
  99. ';
  100. }
  101. }
  102. }
  103. closedir($dh);
  104. }
  105. }
  106. //把生成文件的过程写出函数
  107. function tofile($file_name,$file_content){
  108. if (is_file ($file_name)){
  109. @unlink ($file_name);
  110. }
  111. $handle = fopen ($file_name,"w");
  112. if (!is_writable ($file_name)){
  113. return false;
  114. }
  115. if (!fwrite ($handle,$file_content)){
  116. return false;
  117. }
  118. fclose ($handle); //关闭指针
  119. return $file_name;
  120. }
  121. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
  122. <html xmlns="">
  123. <head>
  124. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  125. <title>批量替换程序|木马批量删除_www.jbxue.com</title>
  126. <style type="text/css">
  127. body{background:#FFFFFF;color:#000;font-size:12px;}
  128. #top{text-align:center;}
  129. h1,p,form{margin:0;padding:0;}
  130. h1{font-size;14px;}
  131. </style>
  132. </head>
  133. <body>
  134. <div id="top">
  135. <h1>批量替换程序(UTF-8版)</h1>
  136. <div>本程序可以扫描指定目录的所有文件,进行<strong>内容替换</strong>。可用于被批量挂马的删除以及批量更新页面某些内容。<br/>
  137. 在文件数量非常多的情况下,本操作比较占用服务器资源,请确脚本超时限制时间允许更改,否则可能无法完成操作。


  138. <form action="" name="form1" target="stafrm" method="post">
  139. <table width="95%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#666666">
  140. <tr>
  141. <td width="10%" bgcolor="#FFFFFF"><strong> 起始根路径:</strong></td>
  142. <td width="90%" bgcolor="#FFFFFF"><input name="searchpath" type="text" id="searchpath" value="./test" size="20" />
  143. 点表示当前目录,末尾不要加/ <input type="checkbox" name="isall" value="1" />包含此目录下所有目录</td>
  144. </tr>
  145. <tr>
  146. <td bgcolor="#FFFFFF"><strong> 文件扩展名:</strong></td>
  147. <td bgcolor="#FFFFFF"><input name="shortname" type="text" id="shortname" size="20" value="php|htm" />
  148. 多个请用|隔开</td>
  149. </tr>
  150. <tr id="rpct">
  151. <td height="64" colspan="2" bgcolor="#FFFFFF"><table width="100%" border="0" cellspacing="1" cellpadding="1">
  152. <tr bgcolor="#EDFCE2">
  153. <td colspan="4"><strong>内容替换选项:</strong> <input type="checkbox" name="isreg" value="1" />使用正则表达式</td>
  154. </tr>
  155. <tr>
  156. <td colspan="4">替换内容类默认使用字符串替换,也可以使用正则表达式(需勾选)"替换为"不填写的话,就表示删除"替换内容"</td>
  157. </tr>
  158. <tr>
  159. <td width="10%"> 替换内容:</td>
  160. <td width="36%"><textarea name="sstr" id="sstr" style="width:90%;height:45px"></textarea></td>
  161. <td width="10%">替 换 为:</td>
  162. <td><textarea name="rpstr" id="rpstr" style="width:90%;height:45px"></textarea></td>
  163. </tr>
  164. </table></td>
  165. </tr>
  166. <tr>
  167. <td colspan="2" height="20" align="center" bgcolor="#E2F5BC"><input type="submit" name="Submit" value="开始执行操作" class="inputbut" /></td>
  168. </tr>
  169. </table>
  170. </form>
  171. <table width="95%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#666666">
  172. <tr bgcolor="#FFFFFF">
  173. <td id="mtd">
  174. <div id='mdv' style='width:100%;height:100;'>
  175. <iframe name="stafrm" frameborder="0" id="stafrm" width="100%" height="100%"></iframe>

  176. <script type="text/javascript">
  177. document.all.mdv.style.pixelHeight = screen.height - 450;
  178. </script> </td>
  179. </tr>
  180. </table>
  181. </body>
  182. </html>

阅读(728) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~