AgenteController.php
10.1 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
* Created by PhpStorm.
* User: eduardo
* Date: 18/10/14
* Time: 23:49
*/
namespace Cacic\CommonBundle\Controller;
use Cacic\CommonBundle\Form\Type\AgenteType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Finder\Finder;
use PharData;
use Symfony\Component\Security\Acl\Exception\Exception;
use ZipArchive;
class AgenteController extends Controller {
public function indexAction(Request $request) {
$logger = $this->get('logger');
// Cria diretório dos agentes se não existir
$rootDir = $this->container->get('kernel')->getRootDir();
$webDir = $rootDir . "/../web/";
$downloadsDir = $webDir . "downloads/";
if (!is_dir($downloadsDir)) {
mkdir($downloadsDir);
}
$cacicDir = $downloadsDir . "cacic/";
if (!is_dir($cacicDir)) {
mkdir($cacicDir);
}
$linuxDir = $cacicDir . "linux/";
if (!is_dir($linuxDir)) {
mkdir($linuxDir);
}
$windowsDir = $cacicDir . "windows/";
if (!is_dir($windowsDir)) {
mkdir($windowsDir);
}
$outrosDir = $downloadsDir . "outros/";
if (!is_dir($outrosDir)) {
mkdir($outrosDir);
}
$form = $this->createForm( new AgenteType() );
$locale = $request->getLocale();
// Constrói array de arquivos e hashes
$finder = new Finder();
$agentes = new Finder();
$saida = array();
// Primeiro tratamos agentes Linux
// A regra é que o agente mais atual estará na pasta current
$finder->directories()->in($linuxDir);
$saida['linux']['versions'] = array();
foreach($finder as $version) {
if ($version->getFileName() == 'current') {
continue;
}
$saida['linux']['versions'][$version->getFileName()] = array();
$agentes->files()->in($version->getRealPath());
foreach ($agentes as $file) {
array_push($saida['linux']['versions'][$version->getFileName()], array(
'name' => $file->getFileName(),
'download_url' => 'downloads/linux/windows/' . $version->getFileName() . '/' . $file->getFileName(),
'hash' => md5_file($file->getRealPath()),
'size' => $file->getSize(),
'filename' => $windowsDir . $version->getFileName() . '/' . $file->getFileName()
));
}
}
// Get latest version
$current = basename(readlink($cacicDir."current"));
$saida['linux']['live_version'] = $current;
// Aí tratamos Windows
$finder->directories()->in($windowsDir);
$saida['windows']['versions'] = array();
foreach($finder as $version) {
if ($version->getFileName() == 'current') {
continue;
}
$saida['windows']['versions'][$version->getFileName()] = array();
$agentes->files()->in($version->getRealPath());
foreach ($agentes as $file) {
array_push($saida['windows']['versions'][$version->getFileName()], array(
'name' => $file->getFileName(),
'download_url' => 'downloads/cacic/windows/' . $version->getFileName() . '/' . $file->getFileName(),
'hash' => md5_file($file->getRealPath()),
'size' => $file->getSize(),
'filename' => 'windows/' . $version->getFileName() . '/' . $file->getFileName()
));
}
}
// Get latest version
$current = basename(readlink($windowsDir."current"));
$saida['windows']['live_version'] = $current;
//$logger->debug("4444444444444444444444444444444444 ".print_r($saida, true));
if ( $request->isMethod('POST') )
{
// Aqui vamos fazer o tratamento dos agentes
$data = $form->getData();
$data['windows_version'] = $request->get('agentes')['windows_version'];
$data['linux_version'] = $request->get('agentes')['linux_version'];
$files = $request->files->get('agentes');
//$logger->debug("99999999999999999999999999999999999 ".print_r($data, true));
if (!empty($files['windows'])) {
//$logger->debug("88888888888888888888888888888888888888 ".print_r($files['windows'], true));
if (empty($data['windows_version'])) {
$logger->error("O parâmetro versão é obrigatório");
$this->get('session')->getFlashBag()->add('error', 'O parâmetro versão é obrigatório');
} else {
$versionDir = $windowsDir . $data['windows_version'];
$result = $this->uploadPackage($files['windows'], $versionDir);
if (!$result) {
$logger->error("Erro na atualização dos Agentes Windows");
$this->get('session')->getFlashBag()->add('error', 'Erro na atualização dos agentes Windows');
} else {
// Make this version current
$logger->debug("Agentes atualizados com sucesso");
symlink($versionDir, "$windowsDir"."current");
$this->get('session')->getFlashBag()->add('success', 'Agentes atualizados com sucesso!');
}
}
}
if (!empty($files['linux'])) {
if (empty($data['linux_version'])) {
$logger->error("O parâmetro versão é obrigatório");
$this->get('session')->getFlashBag()->add('error', 'O parâmetro versão é obrigatório');
} else {
$versionDir = $linuxDir . $data['linux_version'];
$result = $this->uploadPackage($files['linux'], $versionDir);
if (!$result) {
$logger->error("Erro na atualização dos Agentes Linux");
$this->get('session')->getFlashBag()->add('error', 'Erro na atualização dos agentes Linux');
} else {
// Make this version current
symlink($versionDir, $linuxDir."current");
$this->get('session')->getFlashBag()->add('success', 'Agentes atualizados com sucesso!');
}
}
}
}
return $this->render( 'CacicCommonBundle:Agente:index.html.twig',
array(
'local'=>$locale,
'saida' => $saida,
'form' => $form->createView()
)
);
}
public function uploadPackage($file, $version) {
$logger = $this->get('logger');
if (!$file->isValid()) {
$logger->error("Erro no upload do arquivo. Arquivo inválido\n".$file->getErrorMessage());
$this->get('session')->getFlashBag()->add('error', "Erro no upload do arquivo. Arquivo inválido\n".$file->getErrorMessage());
return false;
}
$result = false;
mkdir($version);
//$logger->debug("66666666666666666666666666666666666 ".print_r($file, true));
$extension = $file->getClientOriginalExtension();
//$logger->debug("00000000000000000000000000000000000000000 $extension | $version");
if ($extension == 'zip') {
$zip = new ZipArchive;
if ($zip->open($file) === TRUE) {
$zip->extractTo($version);
$zip->close();
$logger->debug("Arquivo .zip descompactado com sucesso ". $file->getClientOriginalName());
$result = true;
} else {
$logger->error("Erro na descompactação do arquivo .zip ". $file->getClientOriginalName());
$this->get('session')->getFlashBag()->add('error', "Erro na descompatcação do arquivo .zip\n".$file->getErrorMessage());
$result = false;
}
} elseif ($extension == 'gz') {
try {
// decompress from gz
$tar = $version.$file->getClientOriginalName();
$p = new PharData($tar, 0, $file);
$p->decompress();
// Now unarchive from tar
$phar = new PharData($tar);
$phar->extractTo($version);
// Remove file
unlink($tar);
$result = true;
} catch (Exception $e) {
$logger->error("Erro na extração do arquivo .gz \n".$e->getMessage());
$this->get('session')->getFlashBag()->add('error', "Erro na extração do arquivo .gz\n".$file->getErrorMessage());
$result = false;
}
} else {
$logger->error("Extensão inválida para upload dos agentes ".$extension);
$this->get('session')->getFlashBag()->add('error', "Extensão inválida para upload dos agentes ".$extension);
$result = false;
}
return $result;
}
public function excluirAction(Request $request) {
if ( ! $request->isXmlHttpRequest() )
throw $this->createNotFoundException( 'Página não encontrada' );
$rootDir = $this->container->get('kernel')->getRootDir();
$webDir = $rootDir . "/../web/";
$downloadsDir = $webDir . "downloads/";
$cacicDir = $downloadsDir . "cacic/";
$filepath = $cacicDir . $request->get('id');
$this->get('logger')->debug("Excluindo arquivo de agente ".$filepath);
$result = unlink($filepath);
if ($result) {
$response = new Response( json_encode( array('status' => 'ok') ) );
$response->headers->set('Content-Type', 'application/json');
} else {
$response = new Response( json_encode( array('status' => 'error') ) );
$response->headers->set('Content-Type', 'application/json');
}
return $response;
}
}