diff options
| author | Greg Roach <fisharebest@webtrees.net> | 2019-11-01 22:57:26 +0000 |
|---|---|---|
| committer | Greg Roach <fisharebest@webtrees.net> | 2019-11-01 23:12:31 +0000 |
| commit | 6d5769063576eccb94961415641dc91233b05baa (patch) | |
| tree | 0bcc72d5365b0af340f39d884305feaec8308067 /app/Http/RequestHandlers/ExportGedcomServer.php | |
| parent | a24f7142802ba8dbf1a0dd85d42358c815e25a60 (diff) | |
| download | webtrees-6d5769063576eccb94961415641dc91233b05baa.tar.gz webtrees-6d5769063576eccb94961415641dc91233b05baa.tar.bz2 webtrees-6d5769063576eccb94961415641dc91233b05baa.zip | |
Fix: #2709 - download GEDCOM using ugly URLs
Diffstat (limited to 'app/Http/RequestHandlers/ExportGedcomServer.php')
| -rw-r--r-- | app/Http/RequestHandlers/ExportGedcomServer.php | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/app/Http/RequestHandlers/ExportGedcomServer.php b/app/Http/RequestHandlers/ExportGedcomServer.php new file mode 100644 index 0000000000..d5a4110bc8 --- /dev/null +++ b/app/Http/RequestHandlers/ExportGedcomServer.php @@ -0,0 +1,101 @@ +<?php + +/** + * webtrees: online genealogy + * Copyright (C) 2019 webtrees development team + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +declare(strict_types=1); + +namespace Fisharebest\Webtrees\Http\RequestHandlers; + +use Fisharebest\Webtrees\FlashMessages; +use Fisharebest\Webtrees\Html; +use Fisharebest\Webtrees\Http\ViewResponseTrait; +use Fisharebest\Webtrees\I18N; +use Fisharebest\Webtrees\Tree; +use League\Flysystem\FilesystemInterface; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\RequestHandlerInterface; +use Throwable; + +use function assert; +use function fclose; +use function fopen; +use function pathinfo; +use function redirect; +use function rewind; +use function route; +use function strtolower; + +use const PATHINFO_EXTENSION; + +/** + * Save a GEDCOM file on the server. + */ +class ExportGedcomServer implements RequestHandlerInterface +{ + use ViewResponseTrait; + + /** @var FilesystemInterface */ + private $filesystem; + + /** + * ExportGedcomServer constructor. + * + * @param FilesystemInterface $filesystem + */ + public function __construct(FilesystemInterface $filesystem) + { + $this->filesystem = $filesystem; + } + + /** + * @param ServerRequestInterface $request + * + * @return ResponseInterface + */ + public function handle(ServerRequestInterface $request): ResponseInterface + { + $tree = $request->getAttribute('tree'); + assert($tree instanceof Tree); + + $filename = $tree->name(); + + // Force a ".ged" suffix + if (strtolower(pathinfo($filename, PATHINFO_EXTENSION)) !== 'ged') { + $filename .= '.ged'; + } + + try { + $stream = fopen('php://temp', 'wb+'); + $tree->exportGedcom($stream); + rewind($stream); + $this->filesystem->putStream($filename, $stream); + fclose($stream); + + /* I18N: %s is a filename */ + FlashMessages::addMessage(I18N::translate('The family tree has been exported to %s.', Html::filename($filename)), 'success'); + } catch (Throwable $ex) { + FlashMessages::addMessage( + I18N::translate('The file %s could not be created.', Html::filename($filename)) . '<hr><samp dir="ltr">' . $ex->getMessage() . '</samp>', + 'danger' + ); + } + + $url = route('manage-trees', ['tree' => $tree->name()]); + + return redirect($url); + } +} |
