PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/NarniaGuardian.php

https://gitlab.com/kidaa/Narnia-Guardian
PHP | 107 lines | 96 code | 9 blank | 2 comment | 8 complexity | 048d44e920fdd94f30f55fe9ffc149bd MD5 | raw file
Possible License(s): Unlicense
  1. <?php
  2. Class NarniaGD
  3. {
  4. protected $selfpath = null;
  5. protected $blacklist = null;
  6. protected $blackfilelist = null;
  7. protected $searchStart = null;
  8. protected $searchEnd = null;
  9. protected $uniquelist = array();
  10. function __construct() {
  11. date_default_timezone_set('Europe/Riga');
  12. echo '<pre>Narnia Guardian<br>';
  13. $this->selfpath = realpath(dirname(__FILE__));
  14. $this->blacklist = file($this->selfpath.'/blacklist.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  15. $this->blackfilelist = file($this->selfpath.'/blackfilelist.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  16. $this->uniquelist = file($this->selfpath.'/uniquelist.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  17. $this->searchStart = '<?php';
  18. $this->searchEnd = '?>';
  19. // print_r($this->uniquelist);
  20. // echo '<br><br>';
  21. }
  22. function logSuccess($root, $string){
  23. $escaped = preg_replace('/[^A-Za-z0-9_\-]/', '_', $root);
  24. file_put_contents($this->selfpath.'/logs/main.log',date('Y-m-d G:i').' '.$string.PHP_EOL, FILE_APPEND);
  25. file_put_contents($this->selfpath.'/logs/root-'.$escaped.'.log',date('Y-m-d G:i').' '.$string.PHP_EOL, FILE_APPEND);
  26. }
  27. function cleanMess($curContent){
  28. foreach ($this->blacklist as $bad){
  29. $pos = strpos($curContent,$bad);
  30. if($pos){
  31. $start=strrpos(substr($curContent,0,$pos), $this->searchStart);
  32. $end=$pos+strpos(substr($curContent,$pos), $this->searchEnd)+strlen($this->searchEnd);
  33. $lenght=$end-$start;
  34. if ($lenght<($pos+strlen($this->searchEnd)+10)){
  35. $this->logSuccess('error-'.$root,'This is messed up with PHP tags '.$path);
  36. }
  37. echo $bad.' pos '.$pos.' start '.$start.' end '.$end.' len '.$lenght.'<br>';
  38. if (($start < $end) && ($end < strlen($curContent))){
  39. $curContent=substr_replace($curContent,"",$start,$lenght);
  40. }else {
  41. $this->logSuccess('error-'.$root,'This is messed up with PHP tags '.$path);
  42. }
  43. }
  44. }
  45. return $curContent;
  46. }
  47. function getUnique($string, $path){
  48. $strings = explode("\n", $string);
  49. if (!in_array($strings[0], $this->uniquelist)) {
  50. $this->uniquelist[]=$strings[0];
  51. echo $path.'<br>';
  52. echo $strings[0].'<br>';
  53. }
  54. }
  55. public function cleanFiles($root){
  56. $time_start = (float) array_sum(explode(' ',microtime()));
  57. $iter = new RecursiveIteratorIterator(
  58. new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS),
  59. RecursiveIteratorIterator::SELF_FIRST,
  60. RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
  61. );
  62. foreach ($iter as $path) {
  63. if ($path->getExtension()=='php') {
  64. $dirty=file_get_contents($path);
  65. $clean = $this->cleanMess($dirty);
  66. if ($dirty<>$clean){
  67. $savethis = file_put_contents($path,$clean);
  68. if ($savethis){
  69. $this->logSuccess($root,'Cleaned up '.$path);
  70. }
  71. $savethis = null;
  72. }
  73. $count = substr_count($clean,'\\');
  74. if ($count>1000){
  75. $this->logSuccess('error-'.$root,'This is BAD FILE '.$count.' '.$path);
  76. }
  77. $this->getUnique($clean, $path);
  78. }
  79. }
  80. $time_end = (float) array_sum(explode(' ',microtime()));
  81. $time_diff = "Processing $root time: ". sprintf("%.4f", ($time_end-$time_start))." seconds";
  82. $this->logSuccess($root, $time_diff);
  83. file_put_contents($this->selfpath.'/logs/main-scripttime.log',date('Y-m-d G:i').' '.$time_diff.PHP_EOL, FILE_APPEND);
  84. }
  85. function __destruct() {
  86. echo 'Lets go sleep';
  87. $output = null;
  88. foreach ($this->uniquelist as $line){
  89. $output=$output.$line.PHP_EOL;
  90. }
  91. file_put_contents($this->selfpath.'/logs/uniquelist.txt',$output);
  92. // print_r($this->uniquelist);
  93. }
  94. }