| [c455d7b] | 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (C) 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 4 |
|
|---|
| 5 | This program is free software: you can redistribute it and/or modify
|
|---|
| 6 | it under the terms of the GNU General Public License as published by
|
|---|
| 7 | the Free Software Foundation, either version 3 of the License, or
|
|---|
| 8 | (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | This program is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | GNU General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU General Public License
|
|---|
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | namespace GnuBoat;
|
|---|
| 20 |
|
|---|
| 21 | require_once(__DIR__ ."/../autoloader.php");
|
|---|
| 22 |
|
|---|
| 23 | class ServiceLogFiles extends Service
|
|---|
| 24 | {
|
|---|
| 25 | function __construct()
|
|---|
| 26 | {
|
|---|
| 27 | parent::__construct();
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| [1ba85cf] | 30 | protected function process()
|
|---|
| [c455d7b] | 31 | {
|
|---|
| 32 | $debug = $this->debug();
|
|---|
| 33 | $config = new Config();
|
|---|
| 34 |
|
|---|
| 35 | if(!array_key_exists("logfilesSelected", $_POST)){
|
|---|
| 36 | // return list with available log files
|
|---|
| 37 | $files = glob($config->logDirectory() ."*.log");
|
|---|
| 38 | array_walk($files, 'GnuBoat\ServiceLogFiles::stripLogDirectory');
|
|---|
| 39 |
|
|---|
| 40 | header('Content-Type: application/json');
|
|---|
| 41 | if($debug > 0){
|
|---|
| 42 | echo json_encode($files, JSON_PRETTY_PRINT) ."\n";
|
|---|
| 43 | }
|
|---|
| 44 | else{
|
|---|
| 45 | echo json_encode($files) ."\n";
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 | else{
|
|---|
| 49 | // prepare a download of available log files
|
|---|
| 50 | $files = $_POST["logfilesSelected"];
|
|---|
| 51 | if(count($files) < 1){
|
|---|
| 52 | return;
|
|---|
| 53 | }
|
|---|
| 54 | elseif(count($files) == 1){
|
|---|
| 55 | $content = file_get_contents($config->logDirectory() . $files[0]);
|
|---|
| 56 | if($content === false){
|
|---|
| 57 | echo "<html><head></head><body>Failed to read logfile ". $files[0] ."</body></html>";
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | header('Content-Type: text/plain');
|
|---|
| 61 | header('Content-disposition: attachment; filename="'. $files[0] .'"');
|
|---|
| 62 | header('Content-Length: ' . filesize($config->logDirectory() . $files[0]));
|
|---|
| 63 | echo $content;
|
|---|
| 64 | }
|
|---|
| 65 | else{
|
|---|
| 66 | $date = new \DateTime('NOW');
|
|---|
| 67 | $archiveName = "gnuboat-logs-". $date->format("Y-m-d");
|
|---|
| 68 |
|
|---|
| 69 | // provide files as zip file
|
|---|
| 70 | $tmpArchive = tempnam(sys_get_temp_dir(), $archiveName);
|
|---|
| 71 | $zip = new \ZipArchive();
|
|---|
| 72 | if($zip->open($tmpArchive, \ZipArchive::CREATE) !== TRUE){
|
|---|
| 73 | echo "<html><head></head><body>Failed to create zip file</body></html>";
|
|---|
| 74 | return;
|
|---|
| 75 | }
|
|---|
| 76 | foreach($files as $file){
|
|---|
| 77 | if($zip->addFile($config->logDirectory() . $file, $archiveName ."/". $file) !== true){
|
|---|
| 78 | $zip->close();
|
|---|
| 79 | echo "<html><head></head><body>Failed to add logfile ". $file ." to the zip file</body></html>";
|
|---|
| 80 | return;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | if($zip->close() !== true){
|
|---|
| 84 | echo "<html><head></head><body>Failed to save zip file</body></html>";
|
|---|
| 85 | return;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | echo "<html><head></head><body>Failed to add logfile ". $file ." to the zip file</body></html>";
|
|---|
| 89 |
|
|---|
| 90 | //Send zip folder
|
|---|
| 91 | while (ob_get_level()) {
|
|---|
| 92 | ob_end_clean();
|
|---|
| 93 | }
|
|---|
| 94 | ob_start();
|
|---|
| 95 | header('Content-Type: application/zip');
|
|---|
| 96 | header('Content-Transfer-Encoding: Binary');
|
|---|
| 97 | header('Content-disposition: attachment; filename=' . $archiveName . '.zip');
|
|---|
| 98 | header('Content-Length: ' . filesize($tmpArchive));
|
|---|
| 99 | ob_flush();
|
|---|
| 100 | ob_clean();
|
|---|
| 101 | readfile($tmpArchive);
|
|---|
| 102 | unlink($tmpArchive);
|
|---|
| 103 | return;
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | public static function stripLogDirectory(&$item, $key)
|
|---|
| 109 | {
|
|---|
| 110 | $config = new Config();
|
|---|
| 111 | $item = str_replace($config->logDirectory(), "", $item);
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | ?>
|
|---|