xref: /webtrees/app/Http/RequestHandlers/SiteLogsDownload.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
157bfa969SGreg Roach<?php
257bfa969SGreg Roach
357bfa969SGreg Roach/**
457bfa969SGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
657bfa969SGreg Roach * This program is free software: you can redistribute it and/or modify
757bfa969SGreg Roach * it under the terms of the GNU General Public License as published by
857bfa969SGreg Roach * the Free Software Foundation, either version 3 of the License, or
957bfa969SGreg Roach * (at your option) any later version.
1057bfa969SGreg Roach * This program is distributed in the hope that it will be useful,
1157bfa969SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1257bfa969SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1357bfa969SGreg Roach * GNU General Public License for more details.
1457bfa969SGreg Roach * You should have received a copy of the GNU General Public License
15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1657bfa969SGreg Roach */
1757bfa969SGreg Roach
1857bfa969SGreg Roachdeclare(strict_types=1);
1957bfa969SGreg Roach
2057bfa969SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2157bfa969SGreg Roach
2257bfa969SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
2357bfa969SGreg Roachuse Fisharebest\Webtrees\Services\SiteLogsService;
2457bfa969SGreg Roachuse Psr\Http\Message\ResponseInterface;
2557bfa969SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
2657bfa969SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
2757bfa969SGreg Roachuse stdClass;
2857bfa969SGreg Roach
2957bfa969SGreg Roachuse function response;
3057bfa969SGreg Roachuse function str_replace;
3157bfa969SGreg Roach
3257bfa969SGreg Roach/**
3357bfa969SGreg Roach * Download logs.
3457bfa969SGreg Roach */
3557bfa969SGreg Roachclass SiteLogsDownload implements RequestHandlerInterface
3657bfa969SGreg Roach{
3757bfa969SGreg Roach    /** @var SiteLogsService */
3857bfa969SGreg Roach    private $site_logs_service;
3957bfa969SGreg Roach
4057bfa969SGreg Roach    /**
4157bfa969SGreg Roach     * @param SiteLogsService $site_logs_service
4257bfa969SGreg Roach     */
4357bfa969SGreg Roach    public function __construct(SiteLogsService $site_logs_service)
4457bfa969SGreg Roach    {
4557bfa969SGreg Roach        $this->site_logs_service = $site_logs_service;
4657bfa969SGreg Roach    }
4757bfa969SGreg Roach
4857bfa969SGreg Roach    /**
4957bfa969SGreg Roach     * @param ServerRequestInterface $request
5057bfa969SGreg Roach     *
5157bfa969SGreg Roach     * @return ResponseInterface
5257bfa969SGreg Roach     */
5357bfa969SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
5457bfa969SGreg Roach    {
5557bfa969SGreg Roach        $content = $this->site_logs_service->logsQuery($request->getQueryParams())
5657bfa969SGreg Roach            ->orderBy('log_id')
5757bfa969SGreg Roach            ->get()
5857bfa969SGreg Roach            ->map(static function (stdClass $row): string {
5957bfa969SGreg Roach                return
6057bfa969SGreg Roach                    '"' . $row->log_time . '",' .
6157bfa969SGreg Roach                    '"' . $row->log_type . '",' .
6257bfa969SGreg Roach                    '"' . str_replace('"', '""', $row->log_message) . '",' .
6357bfa969SGreg Roach                    '"' . $row->ip_address . '",' .
6457bfa969SGreg Roach                    '"' . str_replace('"', '""', $row->user_name) . '",' .
6557bfa969SGreg Roach                    '"' . str_replace('"', '""', $row->gedcom_name) . '"' .
6657bfa969SGreg Roach                    "\n";
6757bfa969SGreg Roach            })
6857bfa969SGreg Roach            ->implode('');
6957bfa969SGreg Roach
7057bfa969SGreg Roach        return response($content, StatusCodeInterface::STATUS_OK, [
71a0e7c429SGreg Roach            'Content-Type'        => 'text/csv; charset=UTF-8',
7257bfa969SGreg Roach            'Content-Disposition' => 'attachment; filename="webtrees-logs.csv"',
7357bfa969SGreg Roach        ]);
7457bfa969SGreg Roach    }
7557bfa969SGreg Roach}
76