xref: /webtrees/app/Http/RequestHandlers/EmptyClipboard.php (revision 551ad4afbcef2a72a6cf6461f1747762180b12c5)
19927f70fSGreg Roach<?php
29927f70fSGreg Roach
39927f70fSGreg Roach/**
49927f70fSGreg Roach * webtrees: online genealogy
59927f70fSGreg Roach * Copyright (C) 2021 webtrees development team
69927f70fSGreg Roach * This program is free software: you can redistribute it and/or modify
79927f70fSGreg Roach * it under the terms of the GNU General Public License as published by
89927f70fSGreg Roach * the Free Software Foundation, either version 3 of the License, or
99927f70fSGreg Roach * (at your option) any later version.
109927f70fSGreg Roach * This program is distributed in the hope that it will be useful,
119927f70fSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
129927f70fSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
139927f70fSGreg Roach * GNU General Public License for more details.
149927f70fSGreg Roach * You should have received a copy of the GNU General Public License
159927f70fSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
169927f70fSGreg Roach */
179927f70fSGreg Roach
189927f70fSGreg Roachdeclare(strict_types=1);
199927f70fSGreg Roach
209927f70fSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
219927f70fSGreg Roach
229927f70fSGreg Roachuse Fisharebest\Webtrees\Auth;
239927f70fSGreg Roachuse Fisharebest\Webtrees\Registry;
249927f70fSGreg Roachuse Fisharebest\Webtrees\Services\ClipboardService;
259927f70fSGreg Roachuse Fisharebest\Webtrees\Tree;
269927f70fSGreg Roachuse Psr\Http\Message\ResponseInterface;
279927f70fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
289927f70fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
299927f70fSGreg Roach
309927f70fSGreg Roachuse function assert;
319927f70fSGreg Roachuse function is_string;
329927f70fSGreg Roachuse function redirect;
339927f70fSGreg Roach
349927f70fSGreg Roach/**
359927f70fSGreg Roach * Empty the clipboard.
369927f70fSGreg Roach */
379927f70fSGreg Roachclass EmptyClipboard implements RequestHandlerInterface
389927f70fSGreg Roach{
399927f70fSGreg Roach    private ClipboardService $clipboard_service;
409927f70fSGreg Roach
419927f70fSGreg Roach    /**
429927f70fSGreg Roach     * PasteFact constructor.
439927f70fSGreg Roach     *
449927f70fSGreg Roach     * @param ClipboardService $clipboard_service
459927f70fSGreg Roach     */
469927f70fSGreg Roach    public function __construct(ClipboardService $clipboard_service)
479927f70fSGreg Roach    {
489927f70fSGreg Roach        $this->clipboard_service = $clipboard_service;
499927f70fSGreg Roach    }
509927f70fSGreg Roach
519927f70fSGreg Roach    /**
529927f70fSGreg Roach     * Paste a fact from the clipboard into a record.
539927f70fSGreg Roach     *
549927f70fSGreg Roach     * @param ServerRequestInterface $request
559927f70fSGreg Roach     *
569927f70fSGreg Roach     * @return ResponseInterface
579927f70fSGreg Roach     */
589927f70fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
599927f70fSGreg Roach    {
609927f70fSGreg Roach        $params = (array) $request->getParsedBody();
619927f70fSGreg Roach
629927f70fSGreg Roach        $this->clipboard_service->emptyClipboard();
639927f70fSGreg Roach
64*551ad4afSGreg Roach        $base_url = $request->getAttribute('base_url');
65*551ad4afSGreg Roach        $url      = str_starts_with($params['url'], $base_url) ? $params['url'] : $request->getHeaderLine('Referer');
66*551ad4afSGreg Roach
679927f70fSGreg Roach        return redirect($url);
689927f70fSGreg Roach    }
699927f70fSGreg Roach}
70