1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees-lib: MyArtJaub library for webtrees |
5
|
|
|
* |
6
|
|
|
* @package MyArtJaub\Webtrees |
7
|
|
|
* @subpackage Certificates |
8
|
|
|
* @author Jonathan Jaubart <[email protected]> |
9
|
|
|
* @copyright Copyright (c) 2009-2022, Jonathan Jaubart |
10
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace MyArtJaub\Webtrees\Module\Certificates\Http\RequestHandlers; |
16
|
|
|
|
17
|
|
|
use Fig\Http\Message\StatusCodeInterface; |
18
|
|
|
use Fisharebest\Webtrees\I18N; |
19
|
|
|
use Fisharebest\Webtrees\Registry; |
20
|
|
|
use Fisharebest\Webtrees\Tree; |
21
|
|
|
use Fisharebest\Webtrees\Validator; |
22
|
|
|
use Fisharebest\Webtrees\Contracts\UserInterface; |
23
|
|
|
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; |
24
|
|
|
use Fisharebest\Webtrees\Services\ModuleService; |
25
|
|
|
use MyArtJaub\Webtrees\Module\Certificates\CertificatesModule; |
26
|
|
|
use MyArtJaub\Webtrees\Module\Certificates\Factories\CertificateImageFactory; |
27
|
|
|
use MyArtJaub\Webtrees\Module\Certificates\Model\Certificate; |
28
|
|
|
use MyArtJaub\Webtrees\Module\Certificates\Model\Watermark; |
29
|
|
|
use MyArtJaub\Webtrees\Module\Certificates\Services\CertificateDataService; |
30
|
|
|
use MyArtJaub\Webtrees\Module\Certificates\Services\CertificateFilesystemService; |
31
|
|
|
use MyArtJaub\Webtrees\Module\Certificates\Services\UrlObfuscatorService; |
32
|
|
|
use Psr\Http\Message\ResponseInterface; |
33
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
34
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Request handler for providing an image of a certificate. |
38
|
|
|
*/ |
39
|
|
|
class CertificateImage implements RequestHandlerInterface |
40
|
|
|
{ |
41
|
|
|
private ?CertificatesModule $module; |
42
|
|
|
private CertificateFilesystemService $certif_filesystem; |
43
|
|
|
private CertificateImageFactory $certif_image_factory; |
44
|
|
|
private CertificateDataService $certif_data_service; |
45
|
|
|
private UrlObfuscatorService $url_obfuscator_service; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructor for Certificate Image Request Handler |
49
|
|
|
* |
50
|
|
|
* @param ModuleService $module_service |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
ModuleService $module_service, |
54
|
|
|
CertificateFilesystemService $certif_filesystem, |
55
|
|
|
CertificateDataService $certif_data_service, |
56
|
|
|
UrlObfuscatorService $url_obfuscator_service |
57
|
|
|
) { |
58
|
|
|
$this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
59
|
|
|
$this->certif_filesystem = $certif_filesystem; |
60
|
|
|
$this->certif_image_factory = new CertificateImageFactory($this->certif_filesystem); |
61
|
|
|
$this->certif_data_service = $certif_data_service; |
62
|
|
|
$this->url_obfuscator_service = $url_obfuscator_service; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
* @see \Psr\Http\Server\RequestHandlerInterface::handle() |
68
|
|
|
*/ |
69
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
70
|
|
|
{ |
71
|
|
|
if ($this->module === null) { |
72
|
|
|
throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$tree = Validator::attributes($request)->tree(); |
76
|
|
|
$user = Validator::attributes($request)->user(); |
77
|
|
|
|
78
|
|
|
$certif_path = Validator::attributes($request)->string('cid', ''); |
79
|
|
|
$certificate = null; |
80
|
|
|
if ($certif_path !== '' && $this->url_obfuscator_service->tryDeobfuscate($certif_path)) { |
81
|
|
|
$certificate = $this->certif_filesystem->certificate($tree, $certif_path); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($certificate === null) { |
85
|
|
|
return $this->certif_image_factory |
86
|
|
|
->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND) |
87
|
|
|
->withHeader('X-Image-Exception', I18N::translate('The certificate was not found in this family tree.')) |
88
|
|
|
; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$use_watermark = $this->certif_image_factory->certificateNeedsWatermark($certificate, $user); |
92
|
|
|
$watermark = $use_watermark ? $this->watermark($request, $certificate) : null; |
93
|
|
|
|
94
|
|
|
return $this->certif_image_factory->certificateFileResponse( |
95
|
|
|
$certificate, |
96
|
|
|
$use_watermark, |
97
|
|
|
$watermark |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get watermark data for a certificate. |
103
|
|
|
* |
104
|
|
|
* @param ServerRequestInterface $request |
105
|
|
|
* @param Certificate $certificate |
106
|
|
|
* @return Watermark |
107
|
|
|
*/ |
108
|
|
|
private function watermark(ServerRequestInterface $request, Certificate $certificate): Watermark |
109
|
|
|
{ |
110
|
|
|
$color = $certificate->tree()->getPreference('MAJ_CERTIF_WM_FONT_COLOR', Watermark::DEFAULT_COLOR); |
111
|
|
|
$size = $certificate->tree()->getPreference('MAJ_CERTIF_WM_FONT_MAXSIZE'); |
112
|
|
|
$text = $this->watermarkText($request, $certificate); |
113
|
|
|
|
114
|
|
|
return new Watermark($text, $color, is_numeric($size) ? (int) $size : Watermark::DEFAULT_SIZE); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the text to be watermarked for a certificate. |
119
|
|
|
* |
120
|
|
|
* @param ServerRequestInterface $request |
121
|
|
|
* @param Certificate $certificate |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
private function watermarkText(ServerRequestInterface $request, Certificate $certificate): string |
125
|
|
|
{ |
126
|
|
|
$sid = Validator::queryParams($request)->isXref()->string('sid', ''); |
127
|
|
|
if ($sid !== '') { |
128
|
|
|
$source = Registry::sourceFactory()->make($sid, $certificate->tree()); |
129
|
|
|
} else { |
130
|
|
|
$source = $this->certif_data_service->oneLinkedSource($certificate); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($source !== null && $source->canShowName()) { |
134
|
|
|
$repo = $source->facts(['REPO'])->first(); |
135
|
|
|
if ($repo !== null && ($repo = $repo->target()) !== null && $repo->canShowName()) { |
136
|
|
|
return I18N::translate('© %s - %s', strip_tags($repo->fullName()), strip_tags($source->fullName())); |
137
|
|
|
} |
138
|
|
|
return strip_tags($source->fullName()); |
139
|
|
|
} |
140
|
|
|
$default_text = $certificate->tree()->getPreference('MAJ_CERTIF_WM_DEFAULT'); |
141
|
|
|
return $default_text !== '' ? $default_text : I18N::translate('This image is protected under copyright law.'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|