2024-10-30 21:13:41 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exports\ZipExports;
|
|
|
|
|
2024-10-30 23:26:23 +08:00
|
|
|
use BookStack\Exports\ZipExports\Models\ZipExportBook;
|
|
|
|
use BookStack\Exports\ZipExports\Models\ZipExportChapter;
|
|
|
|
use BookStack\Exports\ZipExports\Models\ZipExportPage;
|
2024-10-30 21:13:41 +08:00
|
|
|
use ZipArchive;
|
|
|
|
|
|
|
|
class ZipExportValidator
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
protected string $zipPath,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2024-10-30 23:26:23 +08:00
|
|
|
public function validate(): array
|
2024-10-30 21:13:41 +08:00
|
|
|
{
|
|
|
|
// Validate file exists
|
|
|
|
if (!file_exists($this->zipPath) || !is_readable($this->zipPath)) {
|
2024-11-02 22:51:04 +08:00
|
|
|
return ['format' => trans('errors.import_zip_cant_read')];
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate file is valid zip
|
|
|
|
$zip = new \ZipArchive();
|
|
|
|
$opened = $zip->open($this->zipPath, ZipArchive::RDONLY);
|
|
|
|
if ($opened !== true) {
|
2024-11-02 22:51:04 +08:00
|
|
|
return ['format' => trans('errors.import_zip_cant_read')];
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate json data exists, including metadata
|
|
|
|
$jsonData = $zip->getFromName('data.json') ?: '';
|
|
|
|
$importData = json_decode($jsonData, true);
|
|
|
|
if (!$importData) {
|
2024-11-02 22:51:04 +08:00
|
|
|
return ['format' => trans('errors.import_zip_cant_decode_data')];
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
2024-10-30 23:26:23 +08:00
|
|
|
$helper = new ZipValidationHelper($zip);
|
|
|
|
|
2024-10-30 21:13:41 +08:00
|
|
|
if (isset($importData['book'])) {
|
2024-10-30 23:26:23 +08:00
|
|
|
$modelErrors = ZipExportBook::validate($helper, $importData['book']);
|
|
|
|
$keyPrefix = 'book';
|
2024-10-30 21:13:41 +08:00
|
|
|
} else if (isset($importData['chapter'])) {
|
2024-10-30 23:26:23 +08:00
|
|
|
$modelErrors = ZipExportChapter::validate($helper, $importData['chapter']);
|
|
|
|
$keyPrefix = 'chapter';
|
2024-10-30 21:13:41 +08:00
|
|
|
} else if (isset($importData['page'])) {
|
2024-10-30 23:26:23 +08:00
|
|
|
$modelErrors = ZipExportPage::validate($helper, $importData['page']);
|
|
|
|
$keyPrefix = 'page';
|
2024-10-30 21:13:41 +08:00
|
|
|
} else {
|
2024-11-02 22:51:04 +08:00
|
|
|
return ['format' => trans('errors.import_zip_no_data')];
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
2024-10-30 23:26:23 +08:00
|
|
|
|
2024-11-02 22:51:04 +08:00
|
|
|
|
2024-10-30 23:26:23 +08:00
|
|
|
return $this->flattenModelErrors($modelErrors, $keyPrefix);
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
2024-10-30 23:26:23 +08:00
|
|
|
protected function flattenModelErrors(array $errors, string $keyPrefix): array
|
2024-10-30 21:13:41 +08:00
|
|
|
{
|
2024-10-30 23:26:23 +08:00
|
|
|
$flattened = [];
|
|
|
|
|
|
|
|
foreach ($errors as $key => $error) {
|
|
|
|
if (is_array($error)) {
|
|
|
|
$flattened = array_merge($flattened, $this->flattenModelErrors($error, $keyPrefix . '.' . $key));
|
|
|
|
} else {
|
|
|
|
$flattened[$keyPrefix . '.' . $key] = $error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $flattened;
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
|
|
|
}
|