zmr.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. declare(strict_types=1);
  3. if (isset($_GET['dir'])) { //config the basedir
  4. $basedir = $_GET['dir'];
  5. } else {
  6. $basedir = '.';
  7. }
  8. $auto = 1;
  9. checkdir($basedir);
  10. function checkdir($basedir)
  11. {
  12. if ($dh = opendir($basedir))
  13. {
  14. while (($file = readdir($dh)) !== false)
  15. {
  16. if ($file != '.' && $file != '..')
  17. {
  18. if (!is_dir($basedir . "/" . $file)) {
  19. echo "filename: $basedir/$file " . checkBOM("$basedir/$file") . " <br>";
  20. } else {
  21. $dirname = $basedir . "/" . $file;
  22. checkdir($dirname);
  23. }
  24. }
  25. }
  26. closedir($dh);
  27. }
  28. }
  29. function checkBOM($filename)
  30. {
  31. global $auto;
  32. $contents = file_get_contents($filename);
  33. $charset[1] = substr($contents, 0, 1);
  34. $charset[2] = substr($contents, 1, 1);
  35. $charset[3] = substr($contents, 2, 1);
  36. if (ord($charset[1]) == 239 && ord($charset[2]) == 187 &&
  37. ord($charset[3]) == 191) {
  38. if ($auto == 1) {
  39. $rest = substr($contents, 3);
  40. rewrite($filename, $rest);
  41. return ("<font color=red>BOM found, automatically removed.</font>");
  42. } else {
  43. return ("<font color=red>BOM found.</font>");
  44. }
  45. } else return ("BOM Not Found.");
  46. }
  47. function rewrite($filename, $data)
  48. {
  49. $filenum = fopen($filename, "w");
  50. flock($filenum, LOCK_EX);
  51. fwrite($filenum, $data);
  52. fclose($filenum);
  53. }
  54. ?>