11cfe16bdSGreg Roach<?php 21cfe16bdSGreg Roach 31cfe16bdSGreg Roach/** 41cfe16bdSGreg Roach * webtrees: online genealogy 51cfe16bdSGreg Roach * Copyright (C) 2019 webtrees development team 61cfe16bdSGreg Roach * This program is free software: you can redistribute it and/or modify 71cfe16bdSGreg Roach * it under the terms of the GNU General Public License as published by 81cfe16bdSGreg Roach * the Free Software Foundation, either version 3 of the License, or 91cfe16bdSGreg Roach * (at your option) any later version. 101cfe16bdSGreg Roach * This program is distributed in the hope that it will be useful, 111cfe16bdSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 121cfe16bdSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131cfe16bdSGreg Roach * GNU General Public License for more details. 141cfe16bdSGreg Roach * You should have received a copy of the GNU General Public License 151cfe16bdSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 161cfe16bdSGreg Roach */ 171cfe16bdSGreg Roach 181cfe16bdSGreg Roachdeclare(strict_types=1); 191cfe16bdSGreg Roach 201cfe16bdSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 211cfe16bdSGreg Roach 221cfe16bdSGreg Roachuse Fig\Http\Message\StatusCodeInterface; 231cfe16bdSGreg Roachuse Fisharebest\Webtrees\Auth; 241cfe16bdSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 251cfe16bdSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 261cfe16bdSGreg Roachuse Fisharebest\Webtrees\I18N; 271cfe16bdSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface; 281cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportHtml; 291cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportParserGenerate; 301cfe16bdSGreg Roachuse Fisharebest\Webtrees\Report\ReportPdf; 311cfe16bdSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 321cfe16bdSGreg Roachuse Fisharebest\Webtrees\Tree; 33*a04bb9a2SGreg Roachuse League\Flysystem\FilesystemInterface; 341cfe16bdSGreg Roachuse Psr\Http\Message\ResponseInterface; 351cfe16bdSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 361cfe16bdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 371cfe16bdSGreg Roach 381cfe16bdSGreg Roachuse function addcslashes; 391cfe16bdSGreg Roachuse function assert; 401cfe16bdSGreg Roachuse function ob_get_clean; 411cfe16bdSGreg Roachuse function ob_start; 421cfe16bdSGreg Roachuse function redirect; 431cfe16bdSGreg Roachuse function response; 441cfe16bdSGreg Roachuse function route; 451cfe16bdSGreg Roach 4614981035SGreg Roachuse const PHP_VERSION_ID; 4714981035SGreg Roach 481cfe16bdSGreg Roach/** 491cfe16bdSGreg Roach * Show all available reports. 501cfe16bdSGreg Roach */ 511cfe16bdSGreg Roachclass ReportGenerate implements RequestHandlerInterface 521cfe16bdSGreg Roach{ 531cfe16bdSGreg Roach use ViewResponseTrait; 541cfe16bdSGreg Roach 551cfe16bdSGreg Roach /** 561cfe16bdSGreg Roach * @var ModuleService 571cfe16bdSGreg Roach */ 581cfe16bdSGreg Roach private $module_service; 591cfe16bdSGreg Roach 601cfe16bdSGreg Roach /** 611cfe16bdSGreg Roach * ReportEngineController constructor. 621cfe16bdSGreg Roach * 631cfe16bdSGreg Roach * @param ModuleService $module_service 641cfe16bdSGreg Roach */ 651cfe16bdSGreg Roach public function __construct(ModuleService $module_service) 661cfe16bdSGreg Roach { 671cfe16bdSGreg Roach $this->module_service = $module_service; 681cfe16bdSGreg Roach } 691cfe16bdSGreg Roach 701cfe16bdSGreg Roach /** 711cfe16bdSGreg Roach * A list of available reports. 721cfe16bdSGreg Roach * 731cfe16bdSGreg Roach * @param ServerRequestInterface $request 741cfe16bdSGreg Roach * 751cfe16bdSGreg Roach * @return ResponseInterface 761cfe16bdSGreg Roach */ 771cfe16bdSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 781cfe16bdSGreg Roach { 791cfe16bdSGreg Roach $tree = $request->getAttribute('tree'); 8075964c75SGreg Roach assert($tree instanceof Tree); 811cfe16bdSGreg Roach 821cfe16bdSGreg Roach $user = $request->getAttribute('user'); 8375964c75SGreg Roach assert($user instanceof UserInterface); 841cfe16bdSGreg Roach 85*a04bb9a2SGreg Roach $data_filesystem = $request->getAttribute('filesystem.data'); 86*a04bb9a2SGreg Roach assert($data_filesystem instanceof FilesystemInterface); 87*a04bb9a2SGreg Roach 881cfe16bdSGreg Roach $report = $request->getAttribute('report'); 891cfe16bdSGreg Roach $module = $this->module_service->findByName($report); 901cfe16bdSGreg Roach 911cfe16bdSGreg Roach if (!$module instanceof ModuleReportInterface) { 921cfe16bdSGreg Roach return redirect(route(ReportListPage::class, ['tree' => $tree->name()])); 931cfe16bdSGreg Roach } 941cfe16bdSGreg Roach 951cfe16bdSGreg Roach Auth::checkComponentAccess($module, 'report', $tree, $user); 961cfe16bdSGreg Roach 971cfe16bdSGreg Roach $varnames = $request->getQueryParams()['varnames'] ?? []; 981cfe16bdSGreg Roach $vars = $request->getQueryParams()['vars'] ?? []; 991cfe16bdSGreg Roach $variables = []; 1001cfe16bdSGreg Roach 1011cfe16bdSGreg Roach foreach ($varnames as $name) { 1021cfe16bdSGreg Roach $variables[$name]['id'] = $vars[$name] ?? ''; 1031cfe16bdSGreg Roach } 1041cfe16bdSGreg Roach 1051cfe16bdSGreg Roach $xml_filename = $module->resourcesFolder() . $module->xmlFilename(); 1061cfe16bdSGreg Roach 1071cfe16bdSGreg Roach $format = $request->getQueryParams()['format'] ?? ''; 1081cfe16bdSGreg Roach $destination = $request->getQueryParams()['destination'] ?? ''; 1091cfe16bdSGreg Roach 1101cfe16bdSGreg Roach switch ($format) { 1111cfe16bdSGreg Roach default: 1121cfe16bdSGreg Roach case 'HTML': 1131cfe16bdSGreg Roach ob_start(); 114*a04bb9a2SGreg Roach new ReportParserGenerate($xml_filename, new ReportHtml(), $variables, $tree, $data_filesystem); 1151cfe16bdSGreg Roach $html = ob_get_clean(); 1161cfe16bdSGreg Roach 1171cfe16bdSGreg Roach $this->layout = 'layouts/report'; 1181cfe16bdSGreg Roach 1191cfe16bdSGreg Roach $response = $this->viewResponse('report-page', [ 1201cfe16bdSGreg Roach 'content' => $html, 1211cfe16bdSGreg Roach 'title' => I18N::translate('Report'), 1221cfe16bdSGreg Roach ]); 1231cfe16bdSGreg Roach 1241cfe16bdSGreg Roach if ($destination === 'download') { 1251cfe16bdSGreg Roach $response = $response->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($report, '"') . '.html"'); 1261cfe16bdSGreg Roach } 1271cfe16bdSGreg Roach 1281cfe16bdSGreg Roach return $response; 1291cfe16bdSGreg Roach 1301cfe16bdSGreg Roach case 'PDF': 13114981035SGreg Roach if (PHP_VERSION_ID >= 70400) { 13214981035SGreg Roach $pr = 'https://github.com/tecnickcom/TCPDF/pull/137'; 13314981035SGreg Roach $error = 'PDF reports do not currently work on PHP >= 7.4'; 13414981035SGreg Roach $error .= '<br>'; 13514981035SGreg Roach $error .= 'Waiting for <a href="' . $pr . '" class="alert-link">' . $pr . '</a>'; 13614981035SGreg Roach 13714981035SGreg Roach return $this->viewResponse('errors/unhandled-exception', [ 13814981035SGreg Roach 'error' => $error, 13914981035SGreg Roach 'title' => 'TCPDF error', 14014981035SGreg Roach 'tree' => $tree, 14114981035SGreg Roach ]); 14214981035SGreg Roach } 14314981035SGreg Roach 1441cfe16bdSGreg Roach ob_start(); 145*a04bb9a2SGreg Roach new ReportParserGenerate($xml_filename, new ReportPdf(), $variables, $tree, $data_filesystem); 1461cfe16bdSGreg Roach $pdf = ob_get_clean(); 1471cfe16bdSGreg Roach 1481cfe16bdSGreg Roach $headers = ['Content-Type' => 'application/pdf']; 1491cfe16bdSGreg Roach 1501cfe16bdSGreg Roach if ($destination === 'download') { 1511cfe16bdSGreg Roach $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes($report, '"') . '.pdf"'; 1521cfe16bdSGreg Roach } 1531cfe16bdSGreg Roach 1541cfe16bdSGreg Roach return response($pdf, StatusCodeInterface::STATUS_OK, $headers); 1551cfe16bdSGreg Roach } 1561cfe16bdSGreg Roach } 1571cfe16bdSGreg Roach} 158