forked from jtl-software/connector-shopware5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectorBackend.php
147 lines (128 loc) · 3.69 KB
/
ConnectorBackend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
use Doctrine\ORM\Mapping as ORM;
use Shopware\Components\CSRFWhitelistAware;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
/**
* @ORM\Embedded
*/
class Shopware_Controllers_Backend_Jtlconnector extends Enlight_Controller_Action implements CSRFWhitelistAware
{
/**
* @return string[]
*/
public function getWhitelistedCSRFActions()
{
return [
'checkLogs',
'downloadLogs',
'deleteLogs',
];
}
/**
*
*/
public function preDispatch()
{
if (!defined('CONNECTOR_DIR')) {
define('CONNECTOR_DIR', __DIR__);
}
if (in_array($this->Request()->getActionName(), ['deleteLogs', 'checkLogs', 'downloadLogs'])) {
Shopware()->Plugins()->Controller()->ViewRenderer()->setNoRender();
}
}
/**
*
*/
public function deleteLogsAction(): void
{
foreach ($this->getLogFiles() as $logFile) {
unlink($logFile);
}
$this->sendJsonResponse(['message' => 'Log files have been deleted.'], 200);
}
/**
* @throws Exception
*/
public function checkLogsAction(): void
{
$message = [];
$status = 200;
if ($this->hasLogFiles() === false) {
$status = 404;
$message['message'] = 'No log files found.';
}
$this->sendJsonResponse($message, $status);
}
/**
* @throws Exception
*/
public function downloadLogsAction(): void
{
$zipFilepath = tempnam(sys_get_temp_dir(), 'jtl-connector-logs');
$noLogFilesMessage = sprintf('There are no log files in %s. Cannot create zip archive.', $this->getLogDir());
$response = new Response($noLogFilesMessage, 404);
if ($this->hasLogFiles()) {
$this->compressLogFiles($zipFilepath);
$response = $this->createDownloadResponse($zipFilepath)->setStatusCode(200);
$response->headers->set('Content-Type', 'application/zip');
}
$response->send();
if(is_file($zipFilepath)) {
unlink($zipFilepath);
}
}
/**
* @param string $zipFilepath
* @throws Exception
*/
protected function compressLogFiles(string $zipFilepath): void
{
$logDirectory = $this->getLogDir();
$zip = new ZipArchive();
$zip->open($zipFilepath, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addGlob(sprintf('%s*.log', $logDirectory), 0, ['remove_all_path' => true]);
$zip->close();
}
/**
* @return array
*/
protected function getLogFiles(): array
{
return glob(sprintf('%s*.log', $this->getLogDir()));
}
/**
* @return bool
*/
protected function hasLogFiles(): bool
{
return count($this->getLogFiles()) > 0;
}
/**
* @return string
*/
protected function getLogDir(): string
{
return sprintf('%s/logs/', CONNECTOR_DIR);
}
/**
* @param array $data
* @param int $status
*/
protected function sendJsonResponse(array $data = [], int $status = 200): void
{
(new JsonResponse($data, $status))->send();
}
/**
* @param string $tmpFile
* @return Response
*/
protected function createDownloadResponse(string $tmpFile): Response
{
return (new BinaryFileResponse(new File($tmpFile)))
->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'shopware5-connector-logs.zip');
}
}