From a5da6a09c5efecf62654f604524470e390756c57 Mon Sep 17 00:00:00 2001 From: Greg Zemskov Date: Tue, 25 Mar 2014 12:16:37 +0400 Subject: [PATCH 1/6] Added comparator feature --- src/analyzer.php | 113 ++++++++++++++---- src/classes/Comparator.inc.php | 89 ++++++++++++++ src/static/lang/en.php | 8 +- src/static/lang/ru.php | 8 +- src/static/templates/analyzer_upload_form.tpl | 21 +++- src/static/templates/comparator_result.tpl | 70 +++++++++++ 6 files changed, 283 insertions(+), 26 deletions(-) create mode 100644 src/classes/Comparator.inc.php create mode 100644 src/static/templates/comparator_result.tpl diff --git a/src/analyzer.php b/src/analyzer.php index 166fadf..442f014 100644 --- a/src/analyzer.php +++ b/src/analyzer.php @@ -12,29 +12,20 @@ require_once("classes/Template.inc.php"); require_once("classes/View.inc.php"); require_once("classes/Analyzer.inc.php"); +require_once("classes/Comparator.inc.php"); require_once("classes/Archiver.inc.php"); /////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['a'])) { + +if ($_POST['a'] == 'show') { + $wlf = array(); $wl_files = array(); $type = (int)$_POST['filter']; - $report = Utils::get_uploaded_file("report"); - - if ($tmp_fh = fopen($report, "r")) { - $sig = fread($tmp_fh, 2); - if ($sig == 'PK') { - $archiver = new Archiver($report, "r"); - $folder = $archiver->extract_files(); - $report = $folder . '/' . 'scan_log.xml'; - $archiver->close(); - } - - fclose($tmp_fh); - } - + $report = getReportFile('report'); for ($i = 1; $i <= MAX_SUPPORTED_WLFILES; $i++) { $wl = Utils::get_uploaded_file("wl" . $i); @@ -60,18 +51,19 @@ $displayed = array(); $row = new Template("static/templates/analyzer_table_row.tpl"); + $table_content = ''; + foreach ($report_files as $item) { $row->prepare(); $row->set('name', $item['path']); - $row->set('snippet', $item['snippet']); - $row->set('pos', $item['pos']); + $row->set('snippet', @$item['snippet']); + $row->set('pos', @$item['pos']); $row->set('size', $item['size'] > -1 ? $item['size'] : '[Folder]'); $row->set('created', date('d/m/Y H:i:s', $item['ctime'])); $row->set('modified', date('d/m/Y H:i:s', $item['mtime'])); - $row->set('evenodd', $i % 2); $flag = ''; - switch ($item['detected']) { + switch (@$item['detected']) { case 'c': $flag = ''; break; case 'w': $flag = '(!)'; break; } @@ -85,7 +77,7 @@ $i++; - $displayed[] = $item['crc32']; + $displayed[] = $item['md5']; } @@ -105,13 +97,88 @@ $content = $templ->get(); -} else { - $templ = new Template("static/templates/analyzer_upload_form.tpl"); - $content = $templ->get(); -} + } else + if (isset($_POST['a']) == 'compare') { + $type = (int)$_POST['filter']; + if ($type) { + $compare_by = 'md5'; + } else { + $compare_by = 'size'; + } + + $report1 = getReportFile('report1'); + $report2 = getReportFile('report2'); + $comparator = new Comparator($report1, $report2, $compare_by); + $diff = $comparator->getDiff(); + + $templ = new Template("static/templates/comparator_result.tpl"); + $loop = array('m', 'd', 'a'); + + $row = new Template("static/templates/analyzer_table_row.tpl"); + + foreach($loop as $index) { + $table_content = ''; + + foreach ($diff[$index] as $item) { + + $row->prepare(); + $row->set('name', $item['path']); + $row->set('snippet', @$item['snippet']); + $row->set('pos', @$item['pos']); + $row->set('size', $item['size'] > -1 ? $item['size'] : '[Folder]'); + $row->set('created', date('d/m/Y H:i:s', $item['ctime'])); + $row->set('modified', date('d/m/Y H:i:s', $item['mtime'])); + + $flag = ''; + switch (@$item['detected']) { + case 'c': $flag = ''; break; + case 'w': $flag = '(!)'; break; + } + + $row->set('flagged', $flag); + $row->set('uid', md5($item['path'])); + $row->set('group', $item['group'] != '' ? $item['group'] : '—'); + $row->set('owner', $item['owner'] != '' ? $item['owner'] : '—'); + $row->set('md5', $item['md5']); + $table_content .= $row->get(); + } + + $templ->set('table_content_' . $index, $table_content); + } + + $content = $templ->get(); + } +} else + { + $templ = new Template("static/templates/analyzer_upload_form.tpl"); + $content = $templ->get(); + } template_output($content); +/////////////////////////////////////////////////////////////////////////////////////////////////////// +function getReportFile($var) { + $new_report = Utils::get_uploaded_file($var); + + if ($tmp_fh = fopen($new_report, "r")) { + $sig = fread($tmp_fh, 2); + if ($sig == 'PK') { + $archiver = new Archiver($new_report, "r"); + $folder = $archiver->extract_files(); + $old_report = $folder . '/' . 'scan_log.xml'; + $new_report = $folder . '/' . 'scan_log.' . rand(1000, 9999) . '.xml'; + rename($old_report, $new_report); + + $archiver->close(); + } + + fclose($tmp_fh); + } + + return $new_report; +} + + /////////////////////////////////////////////////////////////////////////////////////////////////////// function template_output($content) { $view = new View("/static/templates/"); diff --git a/src/classes/Comparator.inc.php b/src/classes/Comparator.inc.php new file mode 100644 index 0000000..4a59bf0 --- /dev/null +++ b/src/classes/Comparator.inc.php @@ -0,0 +1,89 @@ +compare_key = $type; + $this->report1 = $this->parse_xml($report_file1); + $this->report2 = $this->parse_xml($report_file2); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////// +function parse_xml($filename) { + $doc = new DOMDocument(); + + // parse xml file + $validator = new XmlValidator(); + if (!$validator->validate(implode('', file($filename)), 'static/xsd/report.xsd')) { + echo "
"; + die(PS_ERR_BROKEN_XML); + } + + + $doc = new DOMDocument(); + $doc->load($filename); + + return $this->parse_xml_filelist($doc); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////// +private function parse_xml_filelist($doc) { + $files = array(); + + $params = $doc->getElementsByTagName('file'); + foreach ($params as $file_info) { + unset($f); + + foreach ($file_info->childNodes as $file) { + if ($file->nodeName == '#text') continue; + $f[$file->nodeName] = $file->nodeValue; + } + + $files[$f['path']] = $f; + } + + return $files; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////// +function getDiff() { + $result = array(); + $second_keys = array_keys($this->report2); + for ($i = 0; $i < count($second_keys); $i++) + { + $item = $second_keys[$i]; + + // modified? + if (isset($this->report1[$item])) { + if ($this->report1[$item][$this->compare_key] != $this->report2[$item][$this->compare_key]) { + $result['m'][] = $this->report1[$item]; + } + + unset($this->report1[$item]); + unset($this->report2[$item]); + } + } + + // deleted + foreach ($this->report1 as $item) { + $result['d'][] = $item; + } + + // added + foreach ($this->report2 as $item) { + $result['a'][] = $item; + } + + return $result; + +} + + +} // end of class \ No newline at end of file diff --git a/src/static/lang/en.php b/src/static/lang/en.php index 2777038..8b8510d 100644 --- a/src/static/lang/en.php +++ b/src/static/lang/en.php @@ -4,11 +4,17 @@ define('PS_UPLOAD_XML_WHITELIST', 'Upload XML Whitelist'); define('PS_ANALYZE_BUTTON', 'ANALYZE'); define('PS_WHITELIST_BY', 'Whitelist by'); +define('PS_COMPARE_BY', 'Compare by'); define('PS_CRC', 'CRC'); +define('PS_FILESIZE', 'Filesize'); define('PS_FILENAME_CRC', 'Filename + Size'); define('PS_FILENAME', 'Filename'); -define('PS_SCRIPT_HEADER', 'Analyzer v0.6'); +define('PS_CHANGED_FILES', 'Changed files'); +define('PS_ADDED_FILES', 'Added files'); +define('PS_DELETED_FILES', 'Deleted files'); + +define('PS_SCRIPT_HEADER', 'Analyzer v0.7'); define('PS_GO_TO_RECIPE', 'Go to recipe form'); define('PS_RECIPE_RESULT_HEADER', 'Result XML Recipe'); define('PS_ENVIRONMENT_HEADER', 'Environment Variables'); diff --git a/src/static/lang/ru.php b/src/static/lang/ru.php index 93bd09f..3152e80 100644 --- a/src/static/lang/ru.php +++ b/src/static/lang/ru.php @@ -4,11 +4,17 @@ define('PS_UPLOAD_XML_WHITELIST', 'Выбрать whitelist'); define('PS_ANALYZE_BUTTON', 'АНАЛИЗИРОВАТЬ!'); define('PS_WHITELIST_BY', 'Фильтровать по'); +define('PS_COMPARE_BY', 'Сравнивать по'); define('PS_CRC', 'CRC'); +define('PS_FILESIZE', 'по размеру'); define('PS_FILENAME_CRC', 'имени файла + размеру'); define('PS_FILENAME', 'имени файла'); -define('PS_SCRIPT_HEADER', 'Анализатор v0.6'); +define('PS_CHANGED_FILES', 'Измененные файлы'); +define('PS_ADDED_FILES', 'Новые файлы'); +define('PS_DELETED_FILES', 'Удаленные файлы'); + +define('PS_SCRIPT_HEADER', 'Анализатор v0.7'); define('PS_GO_TO_RECIPE', 'Перейти к форме предписаний'); define('PS_RECIPE_RESULT_HEADER', 'XML предписание'); define('PS_ENVIRONMENT_HEADER', 'Переменные окружения'); diff --git a/src/static/templates/analyzer_upload_form.tpl b/src/static/templates/analyzer_upload_form.tpl index d2f2cf2..bc86bb7 100644 --- a/src/static/templates/analyzer_upload_form.tpl +++ b/src/static/templates/analyzer_upload_form.tpl @@ -1,3 +1,5 @@ +
+

View

{PS_UPLOAD_XML_REPORT}:
@@ -13,4 +15,21 @@ {PS_FILENAME}
- \ No newline at end of file + + + +
+

Compare

+
+ +
{PS_UPLOAD_XML_REPORT} 1:
+{PS_UPLOAD_XML_REPORT} 2:
+ +
+{PS_COMPARE_BY}: + {PS_FILESIZE} + {PS_CRC} +
+ +
+
\ No newline at end of file diff --git a/src/static/templates/comparator_result.tpl b/src/static/templates/comparator_result.tpl new file mode 100644 index 0000000..fa434b0 --- /dev/null +++ b/src/static/templates/comparator_result.tpl @@ -0,0 +1,70 @@ + + + +
+ +
+

{PS_CHANGED_FILES}

+ + + + + + + @@table_content_m@@ + +
{PS_TH_FLAG}{PS_TH_FILENAME}{PS_TH_SIZE}{PS_TH_CREATED}{PS_TH_MODIFIED}{PS_TH_OWNER}{PS_TH_GROUP}{PS_TH_ACTION}
+
+ +

{PS_ADDED_FILES}

+ + + + + + + @@table_content_a@@ + +
{PS_TH_FLAG}{PS_TH_FILENAME}{PS_TH_SIZE}{PS_TH_CREATED}{PS_TH_MODIFIED}{PS_TH_OWNER}{PS_TH_GROUP}{PS_TH_ACTION}
+
+ +

{PS_DELETED_FILES}

+ + + + + + + @@table_content_d@@ + +
{PS_TH_FLAG}{PS_TH_FILENAME}{PS_TH_SIZE}{PS_TH_CREATED}{PS_TH_MODIFIED}{PS_TH_OWNER}{PS_TH_GROUP}{PS_TH_ACTION}
+ + + + + + + + + + \ No newline at end of file From 7961344b3c53da0895ae4eab88cab2a8cdf39ff3 Mon Sep 17 00:00:00 2001 From: Greg Zemskov Date: Tue, 8 Apr 2014 11:04:00 +0400 Subject: [PATCH 2/6] Bugfixes and new compare criteria --- src/analyzer.php | 32 +++++++++++++++--- src/classes/Analyzer.inc.php | 3 -- src/classes/Comparator.inc.php | 8 ++++- src/static/css/analyzer.css | 2 +- src/static/lang/en.php | 5 +++ src/static/lang/ru.php | 7 +++- src/static/templates/analyzer_result.tpl | 4 +-- src/static/templates/analyzer_upload_form.tpl | 6 ++-- src/static/templates/comparator_result.tpl | 33 +++++++++++-------- src/static/templates/comparator_table_row.tpl | 5 +++ 10 files changed, 76 insertions(+), 29 deletions(-) create mode 100644 src/static/templates/comparator_table_row.tpl diff --git a/src/analyzer.php b/src/analyzer.php index 442f014..b57b756 100644 --- a/src/analyzer.php +++ b/src/analyzer.php @@ -54,6 +54,11 @@ $table_content = ''; foreach ($report_files as $item) { + if (isset($item['snippet'])) { + $item['snippet'] = base64_decode($item['snippet']); + $item['snippet'] = preg_replace('|@_MARKER_@|', '^', $item['snippet']); + } + $row->prepare(); $row->set('name', $item['path']); $row->set('snippet', @$item['snippet']); @@ -100,10 +105,18 @@ } else if (isset($_POST['a']) == 'compare') { $type = (int)$_POST['filter']; - if ($type) { - $compare_by = 'md5'; - } else { - $compare_by = 'size'; + switch ($type) { + case 1: + $compare_by = 'md5'; + break; + case 2: + $compare_by = 'owner'; + break; + case 3: + $compare_by = 'mtime'; + break; + default: + $compare_by = 'size'; } $report1 = getReportFile('report1'); @@ -114,12 +127,17 @@ $templ = new Template("static/templates/comparator_result.tpl"); $loop = array('m', 'd', 'a'); - $row = new Template("static/templates/analyzer_table_row.tpl"); + $row = new Template("static/templates/comparator_table_row.tpl"); foreach($loop as $index) { $table_content = ''; + if (!isset($diff[$index])) continue; foreach ($diff[$index] as $item) { + if (isset($item['snippet'])) { + $item['snippet'] = base64_decode($item['snippet']); + $item['snippet'] = preg_replace('|@_MARKER_@|', '^', $item['snippet']); + } $row->prepare(); $row->set('name', $item['path']); @@ -146,6 +164,10 @@ $templ->set('table_content_' . $index, $table_content); } + foreach($loop as $index) { + $templ->set('table_content_' . $index, ''); + } + $content = $templ->get(); } } else diff --git a/src/classes/Analyzer.inc.php b/src/classes/Analyzer.inc.php index 06959f1..0d51963 100644 --- a/src/classes/Analyzer.inc.php +++ b/src/classes/Analyzer.inc.php @@ -102,9 +102,6 @@ private function parse_xml_filelist($doc) { if ($file_info->hasAttribute('detected')) { $f['detected'] = $file_info->getAttribute('detected'); $f['snippet'] = $file_info->getAttribute('snippet'); - - $f['snippet'] = str_replace('@_MARKER_@', '|', $f['snippet']); - $f['pos'] = $file_info->getAttribute('pos'); } diff --git a/src/classes/Comparator.inc.php b/src/classes/Comparator.inc.php index 4a59bf0..2602e50 100644 --- a/src/classes/Comparator.inc.php +++ b/src/classes/Comparator.inc.php @@ -44,6 +44,13 @@ private function parse_xml_filelist($doc) { foreach ($file_info->childNodes as $file) { if ($file->nodeName == '#text') continue; $f[$file->nodeName] = $file->nodeValue; + + if ($file_info->hasAttribute('detected')) { + $f['detected'] = $file_info->getAttribute('detected'); + $f['snippet'] = $file_info->getAttribute('snippet'); + $f['pos'] = $file_info->getAttribute('pos'); + } + } $files[$f['path']] = $f; @@ -82,7 +89,6 @@ function getDiff() { } return $result; - } diff --git a/src/static/css/analyzer.css b/src/static/css/analyzer.css index e3fda6a..29dfa65 100644 --- a/src/static/css/analyzer.css +++ b/src/static/css/analyzer.css @@ -93,7 +93,7 @@ h2 { .md5 { - color: #E0E0E0; + color: #9090E0; font-size: 11px; } diff --git a/src/static/lang/en.php b/src/static/lang/en.php index 8b8510d..c616325 100644 --- a/src/static/lang/en.php +++ b/src/static/lang/en.php @@ -1,5 +1,8 @@
-

File List

+

{PS_FILELIST}

{PS_GO_TO_RECIPE} @@ -112,7 +112,7 @@ $(document).ready(function(){ $('#report_table').dataTable({ "aLengthMenu": [[100 , 500, -1], [100, 500, "All"]], - "iDisplayLength": 100 + "iDisplayLength": 500 } ); }); diff --git a/src/static/templates/analyzer_upload_form.tpl b/src/static/templates/analyzer_upload_form.tpl index bc86bb7..b0b7a36 100644 --- a/src/static/templates/analyzer_upload_form.tpl +++ b/src/static/templates/analyzer_upload_form.tpl @@ -1,5 +1,5 @@
-

View

+

{PS_TITLE_VIEW}

{PS_UPLOAD_XML_REPORT}:
@@ -19,7 +19,7 @@
-

Compare

+

{PS_TITLE_COMPARE}

{PS_UPLOAD_XML_REPORT} 1:
@@ -29,6 +29,8 @@ {PS_COMPARE_BY}: {PS_FILESIZE} {PS_CRC} + {PS_OWNER} + {PS_MTIME}
diff --git a/src/static/templates/comparator_result.tpl b/src/static/templates/comparator_result.tpl index fa434b0..1bfc9dd 100644 --- a/src/static/templates/comparator_result.tpl +++ b/src/static/templates/comparator_result.tpl @@ -8,7 +8,7 @@ - + @@table_content_m@@ @@ -16,30 +16,33 @@
{PS_TH_FLAG}{PS_TH_FILENAME}{PS_TH_SIZE}{PS_TH_CREATED}{PS_TH_MODIFIED}{PS_TH_OWNER}{PS_TH_GROUP}{PS_TH_ACTION}
{PS_TH_FLAG}{PS_TH_FILENAME}{PS_TH_SIZE}{PS_TH_CREATED}{PS_TH_MODIFIED}{PS_TH_OWNER}{PS_TH_GROUP}
-

{PS_ADDED_FILES}

+

{PS_DELETED_FILES}

- +
- + - @@table_content_a@@ + @@table_content_d@@
{PS_TH_FLAG}{PS_TH_FILENAME}{PS_TH_SIZE}{PS_TH_CREATED}{PS_TH_MODIFIED}{PS_TH_OWNER}{PS_TH_GROUP}{PS_TH_ACTION}
{PS_TH_FLAG}{PS_TH_FILENAME}{PS_TH_SIZE}{PS_TH_CREATED}{PS_TH_MODIFIED}{PS_TH_OWNER}{PS_TH_GROUP}
-

{PS_DELETED_FILES}

+
+

{PS_ADDED_FILES}

- +
- + - @@table_content_d@@ + @@table_content_a@@
{PS_TH_FLAG}{PS_TH_FILENAME}{PS_TH_SIZE}{PS_TH_CREATED}{PS_TH_MODIFIED}{PS_TH_OWNER}{PS_TH_GROUP}{PS_TH_ACTION}
{PS_TH_FLAG}{PS_TH_FILENAME}{PS_TH_SIZE}{PS_TH_CREATED}{PS_TH_MODIFIED}{PS_TH_OWNER}{PS_TH_GROUP}
+ +