1<?php 2 3/** 4 * webtrees: online genealogy 5 * 'Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\Exceptions\FileUploadException; 23use Fisharebest\Webtrees\FlashMessages; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Registry; 26use Fisharebest\Webtrees\Services\TreeService; 27use Fisharebest\Webtrees\Validator; 28use League\Flysystem\FilesystemException; 29use League\Flysystem\UnableToReadFile; 30use Psr\Http\Message\ResponseInterface; 31use Psr\Http\Message\ServerRequestInterface; 32use Psr\Http\Message\StreamFactoryInterface; 33use Psr\Http\Server\RequestHandlerInterface; 34 35use function basename; 36use function redirect; 37use function route; 38 39use const UPLOAD_ERR_NO_FILE; 40use const UPLOAD_ERR_OK; 41 42/** 43 * Import a GEDCOM file into a tree. 44 */ 45class ImportGedcomAction implements RequestHandlerInterface 46{ 47 private StreamFactoryInterface $stream_factory; 48 49 private TreeService $tree_service; 50 51 /** 52 * @param StreamFactoryInterface $stream_factory 53 * @param TreeService $tree_service 54 */ 55 public function __construct(StreamFactoryInterface $stream_factory, TreeService $tree_service) 56 { 57 $this->tree_service = $tree_service; 58 $this->stream_factory = $stream_factory; 59 } 60 61 /** 62 * @param ServerRequestInterface $request 63 * 64 * @return ResponseInterface 65 * @throws FilesystemException 66 * @throws UnableToReadFile 67 */ 68 public function handle(ServerRequestInterface $request): ResponseInterface 69 { 70 $tree = Validator::attributes($request)->tree(); 71 $keep_media = Validator::parsedBody($request)->boolean('keep_media', false); 72 $word_wrapped_notes = Validator::parsedBody($request)->boolean('WORD_WRAPPED_NOTES', false); 73 $gedcom_media_path = Validator::parsedBody($request)->string('GEDCOM_MEDIA_PATH'); 74 $encodings = ['' => ''] + Registry::encodingFactory()->list(); 75 $encoding = Validator::parsedBody($request)->isInArrayKeys($encodings)->string('encoding'); 76 $source = Validator::parsedBody($request)->isInArray(['client', 'server'])->string('source'); 77 78 // Save these choices as defaults 79 $tree->setPreference('keep_media', $keep_media ? '1' : '0'); 80 $tree->setPreference('WORD_WRAPPED_NOTES', $word_wrapped_notes ? '1' : '0'); 81 $tree->setPreference('GEDCOM_MEDIA_PATH', $gedcom_media_path); 82 83 if ($source === 'client') { 84 $client_file = $request->getUploadedFiles()['client_file'] ?? null; 85 86 if ($client_file === null || $client_file->getError() === UPLOAD_ERR_NO_FILE) { 87 FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger'); 88 89 return redirect(route(ImportGedcomPage::class, ['tree' => $tree->name()])); 90 } 91 92 if ($client_file->getError() !== UPLOAD_ERR_OK) { 93 throw new FileUploadException($client_file); 94 } 95 96 $this->tree_service->importGedcomFile($tree, $client_file->getStream(), basename($client_file->getClientFilename()), $encoding); 97 } 98 99 if ($source === 'server') { 100 $server_file = Validator::parsedBody($request)->string('server_file'); 101 102 if ($server_file === '') { 103 FlashMessages::addMessage(I18N::translate('No GEDCOM file was received.'), 'danger'); 104 105 return redirect(route(ImportGedcomPage::class, ['tree' => $tree->name()])); 106 } 107 108 $resource = Registry::filesystem()->data()->readStream($server_file); 109 $stream = $this->stream_factory->createStreamFromResource($resource); 110 $this->tree_service->importGedcomFile($tree, $stream, $server_file, $encoding); 111 } 112 113 return redirect(route(ManageTrees::class, ['tree' => $tree->name()])); 114 } 115} 116