PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/public/manul/classes/FileList.inc.php

https://gitlab.com/tetrapak07/vimm-me
PHP | 209 lines | 159 code | 45 blank | 5 comment | 24 complexity | dfa00fa5cc82239d745e03254a1bb890 MD5 | raw file
  1. <?php
  2. ob_start();
  3. require_once('XmlValidator.inc.php');
  4. require_once('FileInfo.inc.php');
  5. require_once('Writer.inc.php');
  6. ob_end_clean();
  7. class FileList
  8. {
  9. function __construct()
  10. {
  11. global $projectTmpDir;
  12. $this->DIRLIST_TMP_FILENAME = $projectTmpDir . '/dirlist.manul.tmp.txt';
  13. $this->FILELIST_OFFSET_FILENAME = $projectTmpDir . '/queue_offset.manul.tmp.txt';
  14. $this->logFilename = $projectTmpDir . '/scan_log.xml';
  15. $this->AJAX_HEADER_DIRS = 'DIRS';
  16. $this->AJAX_HEADER_ERROR = 'ERR';
  17. $this->AJAX_TMP_FILE = $projectTmpDir . '/ajax_scnnr_tbnj.manul.tmp';
  18. $this->MAX_EXECUTION_DURATION = 5;
  19. $this->TYPE_ANY = 0;
  20. $this->TYPE_FOLDER = 1;
  21. $this->TYPE_FILE = 2;
  22. $this->ACTION_SKIP = 0;
  23. $this->ACTION_PROCESS = 1;
  24. $this->scanSkipPathWildcard = array(); // wildcards to exclude from scanning
  25. $this->filesFound = 0;
  26. $this->filesNode = null;
  27. $this->xmlResult = '';
  28. $this->SCRIPT_START = time();
  29. $this->dom = new DOMDocument('1.0', 'utf-8');
  30. $this->dom->formatOutput = true;
  31. $this->filesNode = $this->dom->createElement('files');
  32. $this->dom->appendChild($this->filesNode);
  33. $this->homedir = dirname(dirname(__FILE__));
  34. #For creating temprorary queue for further antimalware/whitelist scan
  35. $this->GENERATE_FILE_QUEUE = true;
  36. $this->tmpQueueFilename = $projectTmpDir . '/scan_queue.manul.tmp.txt';
  37. }
  38. private function throwTimeout()
  39. {
  40. echo $this->AJAX_HEADER_ERROR . "\n";
  41. echo 'File listing timeout. Try to increase an interval in settings.' . "\n";
  42. exit;
  43. }
  44. private function fileExecutor($filePath, $type, $actionType)
  45. {
  46. if ($actionType === $this->ACTION_PROCESS) {
  47. $fileinfo = new FileInfo($filePath);
  48. $fileinfoNode = $fileinfo->getXMLNode();
  49. if ($this->GENERATE_FILE_QUEUE && is_file($fileinfo->absoluteName)) {
  50. $queue_entry = $fileinfo->absoluteName . ' ' . $fileinfo->md5 . PHP_EOL;
  51. file_put_contents2($this->tmpQueueFilename, $queue_entry, 'a');
  52. }
  53. $this->dom->documentElement->appendChild($this->dom->importNode($fileinfoNode, true));
  54. $this->filesFound++;
  55. } else if ($actionType === $this->ACTION_SKIP) {
  56. // TODO: Do something with skipped item
  57. // fputs($file_handle, "* SKIPPED *************************************** " . $file_path);
  58. }
  59. }
  60. public function getXMLFilelist()
  61. {
  62. $result = implode('', file($this->AJAX_TMP_FILE));
  63. $this->cleanUp();
  64. return $result;
  65. }
  66. private function finalizeRound()
  67. {
  68. global $php_errormsg;
  69. if ($fHandle = fopen($this->AJAX_TMP_FILE, 'a')) {
  70. $nodeList = $this->filesNode->childNodes;
  71. $num = $nodeList->length;
  72. for ($i = 0; $i < $num; $i++) {
  73. fputs($fHandle, $this->dom->saveXML($nodeList->item($i)));
  74. }
  75. fclose($fHandle);
  76. $response['meta'] = array('type' => 'getFileList', 'status' => 'inProcess', 'phpError' => $php_errormsg);
  77. $response['data'] = array();
  78. $report = json_encode($response);
  79. return $report;
  80. } else {
  81. ob_end_clean();
  82. // output result for ajax processing
  83. $response['meta'] = array('type' => 'error', 'phpError' => $php_errormsg);
  84. $response['data'] = array('Cannot write to file ' . $this->logFilename);
  85. $report = json_encode($response);
  86. return $report;
  87. }
  88. }
  89. private function cleanUp()
  90. {
  91. @unlink($this->DIRLIST_TMP_FILENAME);
  92. @unlink($this->FILELIST_OFFSET_FILENAME);
  93. @unlink($this->AJAX_TMP_FILE);
  94. }
  95. function setUp()
  96. {
  97. }
  98. public function performScanning()
  99. {
  100. global $php_errormsg;
  101. $dirs = '.';
  102. if (file_exists($this->DIRLIST_TMP_FILENAME))
  103. $dirs = file_get_contents($this->DIRLIST_TMP_FILENAME);
  104. $dirList = explode("\n", $dirs);
  105. $startTime = time();
  106. while (true) {
  107. $dirList = array_merge($this->folderWalker(array_shift($dirList), $this->filesFound), $dirList);
  108. $currentTime = time();
  109. if (($currentTime - $startTime >= $this->MAX_EXECUTION_DURATION) || (count($dirList) < 1)) break;
  110. }
  111. $result = $this->finalizeRound();
  112. if (!$this->filesFound) {
  113. $response['meta'] = array('type' => 'getFileList', 'status' => 'finished', 'phpError' => $php_errormsg);
  114. $response['data'] = array();
  115. $report = json_encode($response);
  116. return $report;
  117. }
  118. file_put_contents2($this->DIRLIST_TMP_FILENAME, implode("\n", $dirList));
  119. return $result;
  120. }
  121. public function getInterval()
  122. {
  123. return $this->MAX_EXECUTION_DURATION;
  124. }
  125. public function setInterval($val)
  126. {
  127. $this->MAX_EXECUTION_DURATION = $val;
  128. }
  129. private function folderWalker($path, &$files_found)
  130. {
  131. if ($path === '.')
  132. $path = $_SERVER['DOCUMENT_ROOT'];
  133. $dirList = array();
  134. if ($currentDir = opendir($path)) {
  135. while ($file = readdir($currentDir)) {
  136. if ($file === '.' || $file === '..' || is_link($path) || $file === basename($this->homedir)) continue;
  137. $name = $file;
  138. $file = $path . '/' . $file;
  139. // skip path entries from the list
  140. foreach ($this->scanSkipPathWildcard as $item) {
  141. if (preg_match('|' . $item . '|i', $file, $found)) {
  142. foreach ($this->scan_iterator_func as $callback) {
  143. $callback($file, TYPE_ANY, ACTION_SKIP);
  144. }
  145. continue;
  146. }
  147. }
  148. $fileType = $this->TYPE_FILE;
  149. if (is_dir($file)) {
  150. $dirList[] = $file;
  151. $fileType = $this->TYPE_FOLDER;
  152. }
  153. $this->fileExecutor($file, $fileType, $this->ACTION_PROCESS);
  154. }
  155. closedir($currentDir);
  156. }
  157. if (!is_file($this->tmpQueueFilename) && count($dirList) === 0) {
  158. $response['meta'] = array('type' => 'getFileList', 'status' => 'finished', 'phpError' => PS_ERR_NO_FILES_IN_WEB_ROOT);
  159. $report = json_encode($response);
  160. die($report);
  161. }
  162. return $dirList;
  163. }
  164. } // End of class