From 4bdb4cfeb4f0bfc318f10040c9862f64d8c520b1 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Wed, 8 Apr 2020 22:04:03 +0200 Subject: [PATCH] Log viewer: download all (filtered) items. closes https://github.com/opnsense/core/issues/4026 - replace grid export with download link generation - add endpoint /api/diagnostics/log/{{module}}/{{scope}}/export?searchPhrase={{filter}} --- .../Diagnostics/Api/LogController.php | 25 +++++++++++++----- .../app/views/OPNsense/Diagnostics/log.volt | 26 +++++-------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/Api/LogController.php b/src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/Api/LogController.php index 1f1221fab..026350a07 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/Api/LogController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/Api/LogController.php @@ -43,25 +43,23 @@ class LogController extends ApiControllerBase $module = substr($name, 0, strlen($name) - 6); $scope = count($arguments) > 0 ? $arguments[0] : ""; $action = count($arguments) > 1 ? $arguments[1] : ""; + $searchPhrase = ''; + // create filter to sanitize input data + $filter = new Filter(); + $filter->add('query', new QueryFilter()); + $backend = new Backend(); if ($this->request->isPost() && substr($name, -6) == 'Action') { $this->sessionClose(); - $backend = new Backend(); if ($action == "clear") { $backend->configdpRun("system clear log", array($module, $scope)); return ["status" => "ok"]; } else { - // create filter to sanitize input data - $filter = new Filter(); - $filter->add('query', new QueryFilter()); - // fetch query parameters (limit results to prevent out of memory issues) $itemsPerPage = $this->request->getPost('rowCount', 'int', 9999); $currentPage = $this->request->getPost('current', 'int', 1); if ($this->request->getPost('searchPhrase', 'string', '') != "") { $searchPhrase = $filter->sanitize($this->request->getPost('searchPhrase'), "query"); - } else { - $searchPhrase = ''; } $response = $backend->configdpRun("system diag log", array($itemsPerPage, @@ -74,6 +72,19 @@ class LogController extends ApiControllerBase return $result; } } + } elseif ($this->request->isGet() && substr($name, -6) == 'Action') { + if ($action == "export") { + if ($this->request->get('searchPhrase', 'string', '') != "") { + $searchPhrase = $filter->sanitize($this->request->get('searchPhrase'), "query"); + } + $response = $backend->configdpRun("system diag log", array(0, 0, $searchPhrase, $module, $scope)); + $this->response->setRawHeader("Content-Type: text/csv"); + $this->response->setRawHeader("Content-Disposition: attachment; filename=".$scope.".log"); + foreach (json_decode($response, true)['rows'] as $row) { + printf("%s\t%s\n", $row['timestamp'], $row['line']); + } + return; + } } return array(); } diff --git a/src/opnsense/mvc/app/views/OPNsense/Diagnostics/log.volt b/src/opnsense/mvc/app/views/OPNsense/Diagnostics/log.volt index 99545b140..cc9de35ed 100644 --- a/src/opnsense/mvc/app/views/OPNsense/Diagnostics/log.volt +++ b/src/opnsense/mvc/app/views/OPNsense/Diagnostics/log.volt @@ -57,27 +57,13 @@ }] }); }); - // download visible items + // download (filtered) items $("#exportbtn").click(function(event){ - let records = []; - $("#grid-log > tbody > tr").each(function(){ - let fields = []; - $(this).find("td").each(function(){fields.push($(this).text().trim())}); - records.push(fields.join("\t")); - }); - let output_data = records.join("\n"); - $('').attr('id','downloadFile') - .attr('href','data:text/csv;charset=utf8,' + encodeURIComponent(output_data)) - .attr('download','{{scope}}.log') - .appendTo('body'); - $('#downloadFile').ready(function() { - if ( window.navigator.msSaveOrOpenBlob && window.Blob ) { - var blob = new Blob( [ output_data ], { type: "text/csv" } ); - navigator.msSaveOrOpenBlob( blob, '{{scope}}.log' ); - } else { - $('#downloadFile').get(0).click(); - } - }); + let download_link = "/api/diagnostics/log/{{module}}/{{scope}}/export"; + if ($("input.search-field").val() !== "") { + download_link = download_link + "?searchPhrase=" + encodeURIComponent($("input.search-field").val()); + } + $('').attr('href',download_link).get(0).click(); }); });