From 70029fbaedc75d9f54dcf5b4c0d6e45ef6a6e1a7 Mon Sep 17 00:00:00 2001 From: nuxsmin Date: Tue, 20 Feb 2018 20:11:47 +0100 Subject: [PATCH] * [MOD] Config module. Work in progress * [MOD] Improved import module. Work in progress --- .../Controllers/ConfigImportController.php | 8 +++-- lib/SP/Services/Import/FileImport.php | 14 +++----- lib/SP/Services/Import/ImportService.php | 4 ++- lib/SP/Services/Import/SyspassImport.php | 4 +-- lib/SP/Services/Import/XmlFileImport.php | 35 ++++++++----------- 5 files changed, 30 insertions(+), 35 deletions(-) diff --git a/app/modules/web/Controllers/ConfigImportController.php b/app/modules/web/Controllers/ConfigImportController.php index 52d1620b..34f4a9f5 100644 --- a/app/modules/web/Controllers/ConfigImportController.php +++ b/app/modules/web/Controllers/ConfigImportController.php @@ -62,9 +62,13 @@ class ConfigImportController extends SimpleControllerBase try { $importService = $this->dic->get(ImportService::class); - $importService->doImport($importParams, new FileImport($this->router->request()->files()->get('inFile'))); + $counter = $importService->doImport($importParams, new FileImport($this->router->request()->files()->get('inFile'))); - $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Importación finalizada'), [__u('Revise el registro de eventos para más detalles')]); + if ($counter > 0) { + $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Importación finalizada'), [__u('Revise el registro de eventos para más detalles')]); + } else { + $this->returnJsonResponse(JsonResponse::JSON_WARNING, __u('No se importaron cuentas'), [__u('Revise el registro de eventos para más detalles')]); + } } catch (\Exception $e) { processException($e); diff --git a/lib/SP/Services/Import/FileImport.php b/lib/SP/Services/Import/FileImport.php index 51ad1d7d..c2b775fa 100644 --- a/lib/SP/Services/Import/FileImport.php +++ b/lib/SP/Services/Import/FileImport.php @@ -48,14 +48,14 @@ class FileImport * * @var string */ - protected $tmpFile = ''; + protected $tmpFile; /** * Tipo Mime del archivo * * @var string */ - protected $fileType = ''; + protected $fileType; /** * FileImport constructor. @@ -107,7 +107,7 @@ class FileImport if (!file_exists($this->tmpFile) || !is_readable($this->tmpFile)) { // Registramos el máximo tamaño permitido por PHP - Util::getMaxUpload(); + debugLog('Max. upload size: ' . Util::getMaxUpload()); throw new SPException( __u('Error interno al leer el archivo'), @@ -150,9 +150,7 @@ class FileImport { $this->autodetectEOL(); - $this->fileContent = file($this->tmpFile, FILE_SKIP_EMPTY_LINES); - - if ($this->fileContent === false) { + if (($this->fileContent = file($this->tmpFile, FILE_SKIP_EMPTY_LINES)) === false) { throw new SPException( __u('Error interno al leer el archivo'), SPException::ERROR, @@ -178,9 +176,7 @@ class FileImport { $this->autodetectEOL(); - $this->fileContent = file_get_contents($this->tmpFile); - - if ($this->fileContent === false) { + if (($this->fileContent = file_get_contents($this->tmpFile)) === false) { throw new SPException( __u('Error interno al leer el archivo'), SPException::ERROR, diff --git a/lib/SP/Services/Import/ImportService.php b/lib/SP/Services/Import/ImportService.php index 60218f53..a956f217 100644 --- a/lib/SP/Services/Import/ImportService.php +++ b/lib/SP/Services/Import/ImportService.php @@ -50,6 +50,7 @@ class ImportService extends Service * * @param ImportParams $importParams * @param FileImport $fileImport + * @return int * @throws \Exception * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface @@ -70,12 +71,13 @@ class ImportService extends Service throw new ImportException(__u('No es posible iniciar una transacción')); } - $import->doImport(); + $counter = $import->doImport()->getCounter(); if (!DbWrapper::endTransaction($db)) { throw new ImportException(__u('No es posible finalizar una transacción')); } + return $counter; // $LogMessage->addDetails(__('Cuentas importadas'), $Import->getCounter()); } catch (\Exception $e) { if (DbWrapper::rollbackTransaction($db)) { diff --git a/lib/SP/Services/Import/SyspassImport.php b/lib/SP/Services/Import/SyspassImport.php index 3543d1a6..988d7fa6 100644 --- a/lib/SP/Services/Import/SyspassImport.php +++ b/lib/SP/Services/Import/SyspassImport.php @@ -319,11 +319,11 @@ class SyspassImport extends XmlImportBase implements ImportInterface $accountRequest->login = $node->nodeValue; break; case 'categoryId'; - $accountRequest->categoryId = $this->categories[(int)$node->nodeValue]; + $accountRequest->categoryId = isset($this->categories[(int)$node->nodeValue]) ? $this->categories[(int)$node->nodeValue] : null; break; case 'clientId'; case 'customerId'; - $accountRequest->clientId = $this->clients[(int)$node->nodeValue]; + $accountRequest->clientId = isset($this->clients[(int)$node->nodeValue]) ? $this->clients[(int)$node->nodeValue] : null; break; case 'url'; $accountRequest->url = $node->nodeValue; diff --git a/lib/SP/Services/Import/XmlFileImport.php b/lib/SP/Services/Import/XmlFileImport.php index 04597766..f03a27b1 100644 --- a/lib/SP/Services/Import/XmlFileImport.php +++ b/lib/SP/Services/Import/XmlFileImport.php @@ -59,12 +59,12 @@ class XmlFileImport { $this->readXMLFile(); - $tags = $this->xmlDOM->getElementsByTagName('Generator'); + $nodes = $this->xmlDOM->getElementsByTagName('Generator'); - /** @var \DOMElement[] $tags */ - foreach ($tags as $tag) { - if ($tag->nodeValue === 'KeePass' || $tag->nodeValue === 'sysPass') { - return strtolower($tag->nodeValue); + /** @var \DOMElement[] $nodes */ + foreach ($nodes as $node) { + if ($node->nodeValue === 'KeePass' || $node->nodeValue === 'sysPass') { + return strtolower($node->nodeValue); } } @@ -77,15 +77,13 @@ class XmlFileImport default: break; } - } else { - throw new ImportException( - __u('Archivo XML no soportado'), - ImportException::ERROR, - __u('No es posible detectar la aplicación que exportó los datos') - ); } - return ''; + throw new ImportException( + __u('Archivo XML no soportado'), + ImportException::ERROR, + __u('No es posible detectar la aplicación que exportó los datos') + ); } /** @@ -109,23 +107,18 @@ class XmlFileImport /** * Leer la cabecera del archivo XML y obtener patrones de aplicaciones conocidas. - * - * @return bool */ protected function parseFileHeader() { - $handle = @fopen($this->fileImport->getTmpFile(), 'r'); - $headersRegex = '/(KEEPASSX_DATABASE|revelationdata)/i'; - - if ($handle) { + if (($handle = @fopen($this->fileImport->getTmpFile(), 'r')) !== false) { // No. de líneas a leer como máximo $maxLines = 5; $count = 0; while (($buffer = fgets($handle, 4096)) !== false && $count <= $maxLines) { - if (preg_match($headersRegex, $buffer, $app)) { + if (preg_match('/(?P
KEEPASSX_DATABASE|revelationdata)/i', $buffer, $matches)) { fclose($handle); - return strtolower($app[0]); + return strtolower($matches['header']); } $count++; } @@ -133,7 +126,7 @@ class XmlFileImport fclose($handle); } - return false; + return null; } /**