source: src/webapp/webservice/inc/GnuBoat/ServiceLogFiles.php@ 008bce9

0.5 debian/bullseye debian/jessie
Last change on this file since 008bce9 was 008bce9, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago

First more or less working version of visualising logfiles on the map

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[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
19namespace GnuBoat;
20
21require_once(__DIR__ ."/../autoloader.php");
22
23class 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();
[008bce9]34 if($debug > 1)
35 print "Log directory: ". $config->logDirectory() ."\n";
[c455d7b]36
37 if(!array_key_exists("logfilesSelected", $_POST)){
38 // return list with available log files
39 $files = glob($config->logDirectory() ."*.log");
40 array_walk($files, 'GnuBoat\ServiceLogFiles::stripLogDirectory');
41
42 header('Content-Type: application/json');
43 if($debug > 0){
44 echo json_encode($files, JSON_PRETTY_PRINT) ."\n";
45 }
46 else{
47 echo json_encode($files) ."\n";
48 }
49 }
50 else{
51 // prepare a download of available log files
52 $files = $_POST["logfilesSelected"];
53 if(count($files) < 1){
54 return;
55 }
56 elseif(count($files) == 1){
57 $content = file_get_contents($config->logDirectory() . $files[0]);
58 if($content === false){
59 echo "<html><head></head><body>Failed to read logfile ". $files[0] ."</body></html>";
60 }
61
62 header('Content-Type: text/plain');
63 header('Content-disposition: attachment; filename="'. $files[0] .'"');
64 header('Content-Length: ' . filesize($config->logDirectory() . $files[0]));
65 echo $content;
66 }
67 else{
68 $date = new \DateTime('NOW');
69 $archiveName = "gnuboat-logs-". $date->format("Y-m-d");
70
71 // provide files as zip file
72 $tmpArchive = tempnam(sys_get_temp_dir(), $archiveName);
73 $zip = new \ZipArchive();
74 if($zip->open($tmpArchive, \ZipArchive::CREATE) !== TRUE){
75 echo "<html><head></head><body>Failed to create zip file</body></html>";
76 return;
77 }
78 foreach($files as $file){
79 if($zip->addFile($config->logDirectory() . $file, $archiveName ."/". $file) !== true){
80 $zip->close();
81 echo "<html><head></head><body>Failed to add logfile ". $file ." to the zip file</body></html>";
82 return;
83 }
84 }
85 if($zip->close() !== true){
86 echo "<html><head></head><body>Failed to save zip file</body></html>";
87 return;
88 }
89
90 echo "<html><head></head><body>Failed to add logfile ". $file ." to the zip file</body></html>";
91
92 //Send zip folder
93 while (ob_get_level()) {
94 ob_end_clean();
95 }
96 ob_start();
97 header('Content-Type: application/zip');
98 header('Content-Transfer-Encoding: Binary');
99 header('Content-disposition: attachment; filename=' . $archiveName . '.zip');
100 header('Content-Length: ' . filesize($tmpArchive));
101 ob_flush();
102 ob_clean();
103 readfile($tmpArchive);
104 unlink($tmpArchive);
105 return;
106 }
107 }
108 }
109
110 public static function stripLogDirectory(&$item, $key)
111 {
112 $config = new Config();
113 $item = str_replace($config->logDirectory(), "", $item);
114 }
115}
116
117?>
Note: See TracBrowser for help on using the repository browser.