diff --git a/src/MonarcFO/Service/AnrAssetService.php b/src/MonarcFO/Service/AnrAssetService.php index 0e7659de..bed5b8c9 100755 --- a/src/MonarcFO/Service/AnrAssetService.php +++ b/src/MonarcFO/Service/AnrAssetService.php @@ -45,9 +45,6 @@ class AnrAssetService extends \MonarcCore\Service\AbstractService */ public function importFromFile($anrId, $data) { - // Ensure we either have a password, or an empty string (and not 'null') - $key = empty($data['password']) ? '' : $data['password']; - // We can have multiple files imported with the same password (we'll emit warnings if the password mismatches) if (empty($data['file'])) { throw new \MonarcCore\Exception\Exception('File missing', 412); @@ -58,7 +55,21 @@ public function importFromFile($anrId, $data) foreach ($data['file'] as $f) { if (isset($f['error']) && $f['error'] === UPLOAD_ERR_OK && file_exists($f['tmp_name'])) { - $file = json_decode(trim($this->decrypt(base64_decode(file_get_contents($f['tmp_name'])), $key)), true); + $file = []; + if (empty($data['password'])) { + $file = json_decode(trim(file_get_contents($f['tmp_name'])), true); + if ($file == false) { // support legacy export which were base64 encoded + $file = json_decode(trim($this->decrypt(base64_decode(file_get_contents($f['tmp_name'])), '')), true); + } + } else { + // Decrypt the file and store the JSON data as an array in memory + $key = $data['password']; + $file = json_decode(trim($this->decrypt(file_get_contents($f['tmp_name']), $key)), true); + if ($file == false) { // support legacy export which were base64 encoded + $file = json_decode(trim($this->decrypt(base64_decode(file_get_contents($f['tmp_name'])), $key)), true); + } + } + if ($file !== false && ($id = $this->get('objectExportService')->importFromArray($file, $anr)) !== false) { $ids[] = $id; } else { @@ -306,4 +317,4 @@ public function importFromArray($data, $anr, &$objectsCache = []) } return false; } -} \ No newline at end of file +} diff --git a/src/MonarcFO/Service/AnrObjectService.php b/src/MonarcFO/Service/AnrObjectService.php index b4cee672..a07f6cdf 100755 --- a/src/MonarcFO/Service/AnrObjectService.php +++ b/src/MonarcFO/Service/AnrObjectService.php @@ -31,9 +31,6 @@ class AnrObjectService extends \MonarcCore\Service\ObjectService */ public function importFromFile($anrId, $data) { - // Ensure we either have a password, or an empty string (and not 'null') - $key = empty($data['password']) ? '' : $data['password']; - // Mode may either be 'merge' or 'duplicate' $mode = empty($data['mode']) ? 'merge' : $data['mode']; @@ -48,8 +45,20 @@ public function importFromFile($anrId, $data) foreach ($data['file'] as $f) { // Ensure the file has been uploaded properly, silently skip the files that are erroneous if (isset($f['error']) && $f['error'] === UPLOAD_ERR_OK && file_exists($f['tmp_name'])) { - // Decrypt the file and store the JSON data as an array in memory - $file = json_decode(trim($this->decrypt(base64_decode(file_get_contents($f['tmp_name'])), $key)), true); + $file = []; + if (empty($data['password'])) { + $file = json_decode(trim(file_get_contents($f['tmp_name'])), true); + if ($file == false) { // support legacy export which were base64 encoded + $file = json_decode(trim($this->decrypt(base64_decode(file_get_contents($f['tmp_name'])), '')), true); + } + } else { + // Decrypt the file and store the JSON data as an array in memory + $key = $data['password']; + $file = json_decode(trim($this->decrypt(file_get_contents($f['tmp_name']), $key)), true); + if ($file == false) { // support legacy export which were base64 encoded + $file = json_decode(trim($this->decrypt(base64_decode(file_get_contents($f['tmp_name'])), $key)), true); + } + } if ($file !== false && ($id = $this->get('objectExportService')->importFromArray($file, $anr, $mode)) !== false) { // Import was successful, store the ID @@ -151,4 +160,4 @@ public function importFromCommon($id, $data) throw new \MonarcCore\Exception\Exception('Object not found', 412); } } -} \ No newline at end of file +}