Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
system_virus_scan.php
1 #!/usr/bin/php
2 <?php
25 error_reporting(E_ALL);
26 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
27 
28 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
29 if (empty($SYSTEM_ROOT)) {
30  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
31  exit();
32 }
33 
34 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
35  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
36  exit();
37 }
38 
39 require_once $SYSTEM_ROOT.'/core/include/init.inc';
40 require_once(SQ_FUDGE_PATH.'/general/file_system.inc');
41 require_once(SQ_FUDGE_PATH.'/antivirus/antivirus.inc');
42 
43 // Scan the files for viruii
44 $report = '';
45 $recursive = TRUE;
46 $result = Antivirus::scan_file($SYSTEM_ROOT.'/data', $report, $recursive);
47 
48 // If a virus is found, scan the file for the sucker!
49 if (!$result) {
50  $virus_count = 0;
51  $count = 0;
52  foreach ($report as $line_no => $line) {
53  // Scan each line
54  if (preg_match('/FOUND/', $line)) {
55  list($file, $virus_info) = explode(':', $line);
56  }//end if
57  if (preg_match('/Infection/', $line)) {
58  list($file, $virus_info) = explode('Infection', $line);
59  if (!empty($file)) {
60  if (strpos($file, '->') !== FALSE) {
61  // Virus was found in an archive
62  $file_parts = explode('->', $file);
63  $file = $file_parts[0];
64  }//end if
65  }//end if
66  }//end if
67  if (preg_match('/^Infected/', $line)) {
68  list($info, $infected_files) = explode(':', $line);
69  if (!empty($infected_files)) {
70  $virus_count = $infected_files;
71  }//end if
72  }//end if
73 
74  // Found a virus on this file, let's operate on it!
75  if (isset($file)) {
76  $count++;
77  if (is_file_versioning($file)) {
78  $fv = $GLOBALS['SQ_SYSTEM']->getFileVersioning();
79  $fv->remove($file);
80  } else {
81  // Don't think this is part of file versioning
82  // So we must delete this file (after all it is infected)
83  unlink($file);
84  }//end if
85 
86  // All finished, clear it
87  unset($file);
88  }//end if
89  }//end foreach
90 
91  // Display summary
92  if ($count == $virus_count) {
93  echo 'Found '.$virus_count." Infected files\n";
94  } else {
95  echo "An error occurred\n";
96  }//end if
97 }//end if
98 
99 
108 function is_file_versioning($real_file)
109 {
110  $ffv_dir = dirname($real_file).'/.FFV';
111  if (!is_dir($ffv_dir)) return FALSE;
112 
113  $ffv_file = $ffv_dir.'/'.basename($real_file);
114  if (!is_file($ffv_file)) {
115  return FALSE;
116  }
117 
118  return TRUE;
119 }//end is_file_versioning()
120 
121 ?>