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

debian/bullseye
Last change on this file since ec8d5d0 was 0662b2d, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago

Improved webservice class to use TrackItem in ServiceData.php and added some tests

  • Property mode set to 100644
File size: 3.3 KB
Line 
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
30 protected function process()
31 {
32 $debug = $this->debug();
33 $config = new Config();
34 if($debug > 1)
35 print "Log directory: ". $config->logDirectory() ."\n";
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 return $this->output($files);
42 }
43
44 // prepare a download of available log files
45 $files = $_POST["logfilesSelected"];
46 if(count($files) < 1)
47 return $this->error("No logfile selected");
48 elseif(count($files) == 1){
49 $content = file_get_contents($config->logDirectory() . $files[0]);
50 if($content === false){
51 echo "<html><head></head><body>Failed to read logfile ". $files[0] ."</body></html>";
52 }
53
54 header('Content-Type: text/plain');
55 header('Content-disposition: attachment; filename="'. $files[0] .'"');
56 header('Content-Length: ' . filesize($config->logDirectory() . $files[0]));
57 echo $content;
58 }
59 else{
60 $date = new \DateTime('NOW');
61 $archiveName = "gnuboat-logs-". $date->format("Y-m-d");
62
63 // provide files as zip file
64 $tmpArchive = tempnam(sys_get_temp_dir(), $archiveName);
65 $zip = new \ZipArchive();
66 if($zip->open($tmpArchive, \ZipArchive::CREATE) !== TRUE){
67 echo "<html><head></head><body>Failed to create zip file</body></html>";
68 return;
69 }
70 foreach($files as $file){
71 if($zip->addFile($config->logDirectory() . $file, $archiveName ."/". $file) !== true){
72 $zip->close();
73 echo "<html><head></head><body>Failed to add logfile ". $file ." to the zip file</body></html>";
74 return;
75 }
76 }
77 if($zip->close() !== true){
78 echo "<html><head></head><body>Failed to save zip file</body></html>";
79 return;
80 }
81
82 echo "<html><head></head><body>Failed to add logfile ". $file ." to the zip file</body></html>";
83
84 //Send zip folder
85 while (ob_get_level()) {
86 ob_end_clean();
87 }
88 ob_start();
89 header('Content-Type: application/zip');
90 header('Content-Transfer-Encoding: Binary');
91 header('Content-disposition: attachment; filename=' . $archiveName . '.zip');
92 header('Content-Length: ' . filesize($tmpArchive));
93 ob_flush();
94 ob_clean();
95 readfile($tmpArchive);
96 unlink($tmpArchive);
97 return;
98 }
99 }
100
101 public static function stripLogDirectory(&$item, $key)
102 {
103 $config = new Config();
104 $item = str_replace($config->logDirectory(), "", $item);
105 }
106}
107
108?>
Note: See TracBrowser for help on using the repository browser.