| Total Complexity | 61 |
| Total Lines | 377 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SyspassImport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SyspassImport, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | final class SyspassImport extends XmlImportBase implements ImportInterface |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * Iniciar la importación desde sysPass. |
||
| 54 | * |
||
| 55 | * @return ImportInterface |
||
| 56 | * @throws ImportException |
||
| 57 | */ |
||
| 58 | public function doImport() |
||
| 59 | { |
||
| 60 | try { |
||
| 61 | $this->eventDispatcher->notifyEvent('run.import.syspass', |
||
| 62 | new Event($this, EventMessage::factory() |
||
| 63 | ->addDescription(__u('sysPass XML Import'))) |
||
| 64 | ); |
||
| 65 | |||
| 66 | if ($this->importParams->getImportMasterPwd() !== '') { |
||
| 67 | $this->mPassValidHash = Hash::checkHashKey($this->importParams->getImportMasterPwd(), $this->configService->getByParam('masterPwd')); |
||
| 68 | } |
||
| 69 | |||
| 70 | $this->version = $this->getXmlVersion(); |
||
| 71 | |||
| 72 | if ($this->detectEncrypted()) { |
||
| 73 | if ($this->importParams->getImportPwd() === '') { |
||
| 74 | throw new ImportException(__u('Encryption password not set'), ImportException::INFO); |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->processEncrypted(); |
||
| 78 | } |
||
| 79 | |||
| 80 | $this->checkIntegrity(); |
||
| 81 | |||
| 82 | $this->processCategories(); |
||
| 83 | |||
| 84 | if ($this->version >= VersionUtil::versionToInteger('3.0.0')) { |
||
| 85 | $this->processClients(); |
||
| 86 | } else { |
||
| 87 | $this->processCustomers(); |
||
|
|
|||
| 88 | } |
||
| 89 | |||
| 90 | $this->processTags(); |
||
| 91 | $this->processAccounts(); |
||
| 92 | |||
| 93 | return $this; |
||
| 94 | } catch (ImportException $e) { |
||
| 95 | throw $e; |
||
| 96 | } catch (Exception $e) { |
||
| 97 | throw new ImportException($e->getMessage(), ImportException::CRITICAL); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Obtener la versión del XML |
||
| 103 | */ |
||
| 104 | protected function getXmlVersion() |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Verificar si existen datos encriptados |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | protected function detectEncrypted() |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Procesar los datos encriptados y añadirlos al árbol DOM desencriptados |
||
| 121 | * |
||
| 122 | * @throws ImportException |
||
| 123 | */ |
||
| 124 | protected function processEncrypted() |
||
| 125 | { |
||
| 126 | $hash = $this->xmlDOM->getElementsByTagName('Encrypted')->item(0)->getAttribute('hash'); |
||
| 127 | |||
| 128 | if (!empty($hash) && !Hash::checkHashKey($this->importParams->getImportPwd(), $hash)) { |
||
| 129 | throw new ImportException(__u('Wrong encryption password')); |
||
| 130 | } |
||
| 131 | |||
| 132 | foreach ($this->xmlDOM->getElementsByTagName('Data') as $node) { |
||
| 133 | /** @var $node DOMElement */ |
||
| 134 | $data = base64_decode($node->nodeValue); |
||
| 135 | |||
| 136 | try { |
||
| 137 | if ($this->version >= 210) { |
||
| 138 | $xmlDecrypted = Crypt::decrypt($data, |
||
| 139 | $node->getAttribute('key'), |
||
| 140 | $this->importParams->getImportPwd()); |
||
| 141 | } else { |
||
| 142 | $xmlDecrypted = OldCrypt::getDecrypt($data, |
||
| 143 | base64_decode($node->getAttribute('iv')), |
||
| 144 | $this->importParams->getImportPwd()); |
||
| 145 | } |
||
| 146 | } catch (CryptoException $e) { |
||
| 147 | processException($e); |
||
| 148 | |||
| 149 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 150 | |||
| 151 | continue; |
||
| 152 | } |
||
| 153 | |||
| 154 | $newXmlData = new DOMDocument(); |
||
| 155 | |||
| 156 | if ($newXmlData->loadXML($xmlDecrypted) === false) { |
||
| 157 | throw new ImportException(__u('Wrong encryption password')); |
||
| 158 | } |
||
| 159 | |||
| 160 | $this->xmlDOM->documentElement->appendChild($this->xmlDOM->importNode($newXmlData->documentElement, TRUE)); |
||
| 161 | } |
||
| 162 | |||
| 163 | // Eliminar los datos encriptados tras desencriptar los mismos |
||
| 164 | if ($this->xmlDOM->getElementsByTagName('Data')->length > 0) { |
||
| 165 | $nodeData = $this->xmlDOM->getElementsByTagName('Encrypted')->item(0); |
||
| 166 | $nodeData->parentNode->removeChild($nodeData); |
||
| 167 | } |
||
| 168 | |||
| 169 | $this->eventDispatcher->notifyEvent('run.import.syspass.process.decryption', |
||
| 170 | new Event($this, EventMessage::factory() |
||
| 171 | ->addDescription(__u('Data unencrypted'))) |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Checks XML file's data integrity using the signed hash |
||
| 177 | */ |
||
| 178 | protected function checkIntegrity() |
||
| 179 | { |
||
| 180 | $key = $this->importParams->getImportPwd() ?: sha1($this->configData->getPasswordSalt()); |
||
| 181 | |||
| 182 | if (!XmlVerifyService::checkXmlHash($this->xmlDOM, $key)) { |
||
| 183 | $this->eventDispatcher->notifyEvent('run.import.syspass.process.verify', |
||
| 184 | new Event($this, EventMessage::factory() |
||
| 185 | ->addDescription(__u('Error while checking integrity hash')) |
||
| 186 | ->addDescription(__u('If you are importing an exported file from the same origin, the data could be compromised.'))) |
||
| 187 | ); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Obtener las categorías y añadirlas a sysPass. |
||
| 193 | * |
||
| 194 | * @throws ImportException |
||
| 195 | */ |
||
| 196 | protected function processCategories() |
||
| 226 | } |
||
| 227 | }); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Obtener los clientes y añadirlos a sysPass. |
||
| 232 | * |
||
| 233 | * @throws ImportException |
||
| 234 | */ |
||
| 235 | protected function processClients() |
||
| 236 | { |
||
| 237 | $this->getNodesData('Clients', 'Client', |
||
| 238 | function (DOMElement $client) { |
||
| 239 | $clientData = new ClientData(); |
||
| 240 | |||
| 241 | foreach ($client->childNodes as $node) { |
||
| 242 | if (isset($node->tagName)) { |
||
| 243 | switch ($node->tagName) { |
||
| 244 | case 'name': |
||
| 245 | $clientData->setName($node->nodeValue); |
||
| 246 | break; |
||
| 247 | case 'description': |
||
| 248 | $clientData->setDescription($node->nodeValue); |
||
| 249 | break; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | try { |
||
| 255 | $this->addWorkingItem('client', (int)$client->getAttribute('id'), $this->addClient($clientData)); |
||
| 256 | |||
| 257 | $this->eventDispatcher->notifyEvent('run.import.syspass.process.client', |
||
| 258 | new Event($this, EventMessage::factory() |
||
| 259 | ->addDetail(__u('Client imported'), $clientData->getName())) |
||
| 260 | ); |
||
| 261 | } catch (Exception $e) { |
||
| 262 | processException($e); |
||
| 263 | |||
| 264 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 265 | } |
||
| 266 | }); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Obtener los clientes y añadirlos a sysPass. |
||
| 271 | * |
||
| 272 | * @throws ImportException |
||
| 273 | * @deprecated |
||
| 274 | */ |
||
| 275 | protected function processCustomers() |
||
| 276 | { |
||
| 277 | $this->getNodesData('Customers', 'Customer', |
||
| 278 | function (DOMElement $client) { |
||
| 279 | $clientData = new ClientData(); |
||
| 280 | |||
| 281 | foreach ($client->childNodes as $node) { |
||
| 282 | if (isset($node->tagName)) { |
||
| 283 | switch ($node->tagName) { |
||
| 284 | case 'name': |
||
| 285 | $clientData->setName($node->nodeValue); |
||
| 286 | break; |
||
| 287 | case 'description': |
||
| 288 | $clientData->setDescription($node->nodeValue); |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | try { |
||
| 295 | $this->addWorkingItem('client', (int)$client->getAttribute('id'), $this->addClient($clientData)); |
||
| 296 | |||
| 297 | $this->eventDispatcher->notifyEvent('run.import.syspass.process.customer', |
||
| 298 | new Event($this, EventMessage::factory() |
||
| 299 | ->addDetail(__u('Client imported'), $clientData->getName())) |
||
| 300 | ); |
||
| 301 | } catch (Exception $e) { |
||
| 302 | processException($e); |
||
| 303 | |||
| 304 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 305 | } |
||
| 306 | }); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Obtener las etiquetas y añadirlas a sysPass. |
||
| 311 | * |
||
| 312 | * @throws ImportException |
||
| 313 | */ |
||
| 314 | protected function processTags() |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Obtener los datos de las cuentas de sysPass y crearlas. |
||
| 347 | * |
||
| 348 | * @throws ImportException |
||
| 349 | */ |
||
| 350 | protected function processAccounts() |
||
| 351 | { |
||
| 352 | $this->getNodesData('Accounts', 'Account', |
||
| 353 | function (DOMElement $account) { |
||
| 354 | $accountRequest = new AccountRequest(); |
||
| 355 | |||
| 356 | /** @var DOMElement $node */ |
||
| 357 | foreach ($account->childNodes as $node) { |
||
| 358 | if (isset($node->tagName)) { |
||
| 359 | switch ($node->tagName) { |
||
| 360 | case 'name'; |
||
| 361 | $accountRequest->name = $node->nodeValue; |
||
| 362 | break; |
||
| 363 | case 'login'; |
||
| 364 | $accountRequest->login = $node->nodeValue; |
||
| 365 | break; |
||
| 366 | case 'categoryId'; |
||
| 367 | $accountRequest->categoryId = $this->getWorkingItem('category', (int)$node->nodeValue); |
||
| 368 | break; |
||
| 369 | case 'clientId'; |
||
| 370 | case 'customerId'; |
||
| 371 | $accountRequest->clientId = $this->getWorkingItem('client', (int)$node->nodeValue); |
||
| 372 | break; |
||
| 373 | case 'url'; |
||
| 374 | $accountRequest->url = $node->nodeValue; |
||
| 375 | break; |
||
| 376 | case 'pass'; |
||
| 377 | $accountRequest->pass = $node->nodeValue; |
||
| 378 | break; |
||
| 379 | case 'key'; |
||
| 380 | $accountRequest->key = $node->nodeValue; |
||
| 381 | break; |
||
| 382 | case 'notes'; |
||
| 383 | $accountRequest->notes = $node->nodeValue; |
||
| 384 | break; |
||
| 385 | case 'tags': |
||
| 386 | $accountRequest->tags = $this->processAccountTags($node->childNodes); |
||
| 387 | } |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | try { |
||
| 392 | $this->addAccount($accountRequest); |
||
| 393 | |||
| 394 | $this->eventDispatcher->notifyEvent('run.import.syspass.process.account', |
||
| 395 | new Event($this, EventMessage::factory() |
||
| 396 | ->addDetail(__u('Account imported'), $accountRequest->name)) |
||
| 397 | ); |
||
| 398 | } catch (Exception $e) { |
||
| 399 | processException($e); |
||
| 400 | |||
| 401 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||
| 402 | } |
||
| 403 | }); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Procesar las etiquetas de la cuenta |
||
| 408 | * |
||
| 409 | * @param DOMNodeList $nodes |
||
| 410 | * |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | protected function processAccountTags(DOMNodeList $nodes) |
||
| 427 | } |
||
| 428 | } |