@@ -29,71 +29,71 @@ |
||
29 | 29 | */ |
30 | 30 | class AdminConfigAction implements RequestHandlerInterface |
31 | 31 | { |
32 | - /** |
|
33 | - * @var CertificatesModule|null $module |
|
34 | - */ |
|
35 | - private $module; |
|
36 | - |
|
37 | - /** |
|
38 | - * Constructor for Admin Config Action request handler |
|
39 | - * |
|
40 | - * @param ModuleService $module_service |
|
41 | - */ |
|
42 | - public function __construct(ModuleService $module_service) |
|
43 | - { |
|
44 | - $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * {@inheritDoc} |
|
49 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
50 | - */ |
|
51 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
52 | - { |
|
53 | - $tree = $request->getAttribute('tree'); |
|
54 | - assert($tree instanceof Tree); |
|
55 | - |
|
56 | - $admin_config_route = route(AdminConfigPage::class, ['tree' => $tree->name()]); |
|
57 | - |
|
58 | - if ($this->module === null) { |
|
59 | - FlashMessages::addMessage( |
|
60 | - I18N::translate('The attached module could not be found.'), |
|
61 | - 'danger' |
|
62 | - ); |
|
63 | - return redirect($admin_config_route); |
|
64 | - } |
|
65 | - |
|
66 | - $params = (array) $request->getParsedBody(); |
|
67 | - |
|
68 | - $tree->setPreference('MAJ_CERTIF_SHOW_CERT', $params['MAJ_CERTIF_SHOW_CERT'] ?? (string) Auth::PRIV_HIDE); |
|
69 | - $tree->setPreference( |
|
70 | - 'MAJ_CERTIF_SHOW_NO_WATERMARK', |
|
71 | - $params['MAJ_CERTIF_SHOW_NO_WATERMARK'] ?? (string) Auth::PRIV_HIDE |
|
72 | - ); |
|
73 | - $tree->setPreference('MAJ_CERTIF_WM_DEFAULT', $params['MAJ_CERTIF_WM_DEFAULT'] ?? ''); |
|
74 | - |
|
75 | - $watermark_font_size = $params['MAJ_CERTIF_WM_FONT_MAXSIZE'] ?? ''; |
|
76 | - if (is_numeric($watermark_font_size) && $watermark_font_size > 0) { |
|
77 | - $tree->setPreference('MAJ_CERTIF_WM_FONT_MAXSIZE', $params['MAJ_CERTIF_WM_FONT_MAXSIZE']); |
|
78 | - } |
|
79 | - |
|
80 | - // Only accept valid color for MAJ_WM_FONT_COLOR |
|
81 | - $watermark_color = $params['MAJ_CERTIF_WM_FONT_COLOR'] ?? ''; |
|
82 | - if (preg_match('/#([a-fA-F0-9]{3}){1,2}/', $watermark_color) === 1) { |
|
83 | - $tree->setPreference('MAJ_CERTIF_WM_FONT_COLOR', $watermark_color); |
|
84 | - } |
|
85 | - |
|
86 | - // Only accept valid folders for MAJ_CERT_ROOTDIR |
|
87 | - $cert_root_dir = $params['MAJ_CERTIF_ROOTDIR'] ?? ''; |
|
88 | - $cert_root_dir = preg_replace('/[:\/\\\\]+/', '/', $cert_root_dir); |
|
89 | - $cert_root_dir = trim($cert_root_dir, '/') . '/'; |
|
90 | - $tree->setPreference('MAJ_CERTIF_ROOTDIR', $cert_root_dir); |
|
91 | - |
|
92 | - FlashMessages::addMessage( |
|
93 | - I18N::translate('The preferences for the module “%s” have been updated.', $this->module->title()), |
|
94 | - 'success' |
|
95 | - ); |
|
96 | - |
|
97 | - return redirect($admin_config_route); |
|
98 | - } |
|
32 | + /** |
|
33 | + * @var CertificatesModule|null $module |
|
34 | + */ |
|
35 | + private $module; |
|
36 | + |
|
37 | + /** |
|
38 | + * Constructor for Admin Config Action request handler |
|
39 | + * |
|
40 | + * @param ModuleService $module_service |
|
41 | + */ |
|
42 | + public function __construct(ModuleService $module_service) |
|
43 | + { |
|
44 | + $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * {@inheritDoc} |
|
49 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
50 | + */ |
|
51 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
52 | + { |
|
53 | + $tree = $request->getAttribute('tree'); |
|
54 | + assert($tree instanceof Tree); |
|
55 | + |
|
56 | + $admin_config_route = route(AdminConfigPage::class, ['tree' => $tree->name()]); |
|
57 | + |
|
58 | + if ($this->module === null) { |
|
59 | + FlashMessages::addMessage( |
|
60 | + I18N::translate('The attached module could not be found.'), |
|
61 | + 'danger' |
|
62 | + ); |
|
63 | + return redirect($admin_config_route); |
|
64 | + } |
|
65 | + |
|
66 | + $params = (array) $request->getParsedBody(); |
|
67 | + |
|
68 | + $tree->setPreference('MAJ_CERTIF_SHOW_CERT', $params['MAJ_CERTIF_SHOW_CERT'] ?? (string) Auth::PRIV_HIDE); |
|
69 | + $tree->setPreference( |
|
70 | + 'MAJ_CERTIF_SHOW_NO_WATERMARK', |
|
71 | + $params['MAJ_CERTIF_SHOW_NO_WATERMARK'] ?? (string) Auth::PRIV_HIDE |
|
72 | + ); |
|
73 | + $tree->setPreference('MAJ_CERTIF_WM_DEFAULT', $params['MAJ_CERTIF_WM_DEFAULT'] ?? ''); |
|
74 | + |
|
75 | + $watermark_font_size = $params['MAJ_CERTIF_WM_FONT_MAXSIZE'] ?? ''; |
|
76 | + if (is_numeric($watermark_font_size) && $watermark_font_size > 0) { |
|
77 | + $tree->setPreference('MAJ_CERTIF_WM_FONT_MAXSIZE', $params['MAJ_CERTIF_WM_FONT_MAXSIZE']); |
|
78 | + } |
|
79 | + |
|
80 | + // Only accept valid color for MAJ_WM_FONT_COLOR |
|
81 | + $watermark_color = $params['MAJ_CERTIF_WM_FONT_COLOR'] ?? ''; |
|
82 | + if (preg_match('/#([a-fA-F0-9]{3}){1,2}/', $watermark_color) === 1) { |
|
83 | + $tree->setPreference('MAJ_CERTIF_WM_FONT_COLOR', $watermark_color); |
|
84 | + } |
|
85 | + |
|
86 | + // Only accept valid folders for MAJ_CERT_ROOTDIR |
|
87 | + $cert_root_dir = $params['MAJ_CERTIF_ROOTDIR'] ?? ''; |
|
88 | + $cert_root_dir = preg_replace('/[:\/\\\\]+/', '/', $cert_root_dir); |
|
89 | + $cert_root_dir = trim($cert_root_dir, '/') . '/'; |
|
90 | + $tree->setPreference('MAJ_CERTIF_ROOTDIR', $cert_root_dir); |
|
91 | + |
|
92 | + FlashMessages::addMessage( |
|
93 | + I18N::translate('The preferences for the module “%s” have been updated.', $this->module->title()), |
|
94 | + 'success' |
|
95 | + ); |
|
96 | + |
|
97 | + return redirect($admin_config_route); |
|
98 | + } |
|
99 | 99 | } |
@@ -37,128 +37,128 @@ |
||
37 | 37 | */ |
38 | 38 | class CertificateImage implements RequestHandlerInterface |
39 | 39 | { |
40 | - /** |
|
41 | - * @var CertificatesModule|null $module |
|
42 | - */ |
|
43 | - private $module; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var CertificateFilesystemService $certif_filesystem |
|
47 | - */ |
|
48 | - private $certif_filesystem; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var CertificateImageFactory $certif_image_factory |
|
52 | - */ |
|
53 | - private $certif_image_factory; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var CertificateDataService $certif_data_service |
|
57 | - */ |
|
58 | - private $certif_data_service; |
|
59 | - |
|
60 | - /** |
|
61 | - * @var UrlObfuscatorService $url_obfuscator_service |
|
62 | - */ |
|
63 | - private $url_obfuscator_service; |
|
64 | - |
|
65 | - /** |
|
66 | - * Constructor for Certificate Image Request Handler |
|
67 | - * |
|
68 | - * @param ModuleService $module_service |
|
69 | - */ |
|
70 | - public function __construct( |
|
71 | - ModuleService $module_service, |
|
72 | - CertificateFilesystemService $certif_filesystem, |
|
73 | - CertificateDataService $certif_data_service, |
|
74 | - UrlObfuscatorService $url_obfuscator_service |
|
75 | - ) { |
|
76 | - $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
77 | - $this->certif_filesystem = $certif_filesystem; |
|
78 | - $this->certif_image_factory = new CertificateImageFactory($this->certif_filesystem); |
|
79 | - $this->certif_data_service = $certif_data_service; |
|
80 | - $this->url_obfuscator_service = $url_obfuscator_service; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * {@inheritDoc} |
|
85 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
86 | - */ |
|
87 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
88 | - { |
|
89 | - if ($this->module === null) { |
|
90 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
91 | - } |
|
92 | - |
|
93 | - $tree = $request->getAttribute('tree'); |
|
94 | - assert($tree instanceof Tree); |
|
95 | - |
|
96 | - $user = $request->getAttribute('user'); |
|
97 | - assert($user instanceof UserInterface); |
|
98 | - |
|
99 | - $certif_path = $request->getAttribute('cid'); |
|
100 | - $certificate = null; |
|
101 | - if ($certif_path !== '' && $this->url_obfuscator_service->tryDeobfuscate($certif_path)) { |
|
102 | - $certificate = $this->certif_filesystem->certificate($tree, $certif_path); |
|
103 | - } |
|
104 | - |
|
105 | - if ($certificate === null) { |
|
106 | - return $this->certif_image_factory |
|
107 | - ->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND) |
|
108 | - ->withHeader('X-Image-Exception', I18N::translate('The certificate was not found in this family tree.')) |
|
109 | - ; |
|
110 | - } |
|
111 | - |
|
112 | - $use_watermark = $this->certif_image_factory->certificateNeedsWatermark($certificate, $user); |
|
113 | - $watermark = $use_watermark ? $this->watermark($request, $certificate) : null; |
|
114 | - |
|
115 | - return $this->certif_image_factory->certificateFileResponse( |
|
116 | - $certificate, |
|
117 | - $use_watermark, |
|
118 | - $watermark |
|
119 | - ); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Get watermark data for a certificate. |
|
124 | - * |
|
125 | - * @param ServerRequestInterface $request |
|
126 | - * @param Certificate $certificate |
|
127 | - * @return Watermark |
|
128 | - */ |
|
129 | - private function watermark(ServerRequestInterface $request, Certificate $certificate): Watermark |
|
130 | - { |
|
131 | - $color = $certificate->tree()->getPreference('MAJ_CERTIF_WM_FONT_COLOR', Watermark::DEFAULT_COLOR); |
|
132 | - $size = $certificate->tree()->getPreference('MAJ_CERTIF_WM_FONT_MAXSIZE'); |
|
133 | - $text = $this->watermarkText($request, $certificate); |
|
134 | - |
|
135 | - return new Watermark($text, $color, is_numeric($size) ? (int) $size : Watermark::DEFAULT_SIZE); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Get the text to be watermarked for a certificate. |
|
140 | - * |
|
141 | - * @param ServerRequestInterface $request |
|
142 | - * @param Certificate $certificate |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - private function watermarkText(ServerRequestInterface $request, Certificate $certificate): string |
|
146 | - { |
|
147 | - $sid = $request->getQueryParams()['sid'] ?? ''; |
|
148 | - if ($sid !== '') { |
|
149 | - $source = Registry::sourceFactory()->make($sid, $certificate->tree()); |
|
150 | - } else { |
|
151 | - $source = $this->certif_data_service->oneLinkedSource($certificate); |
|
152 | - } |
|
153 | - |
|
154 | - if ($source !== null && $source->canShowName()) { |
|
155 | - $repo = $source->facts(['REPO'])->first(); |
|
156 | - if ($repo !== null && ($repo = $repo->target()) !== null && $repo->canShowName()) { |
|
157 | - return I18N::translate('© %s - %s', strip_tags($repo->fullName()), strip_tags($source->fullName())); |
|
158 | - } |
|
159 | - return $source->fullName(); |
|
160 | - } |
|
161 | - $default_text = $certificate->tree()->getPreference('MAJ_CERTIF_WM_DEFAULT'); |
|
162 | - return $default_text !== '' ? $default_text : I18N::translate('This image is protected under copyright law.'); |
|
163 | - } |
|
40 | + /** |
|
41 | + * @var CertificatesModule|null $module |
|
42 | + */ |
|
43 | + private $module; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var CertificateFilesystemService $certif_filesystem |
|
47 | + */ |
|
48 | + private $certif_filesystem; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var CertificateImageFactory $certif_image_factory |
|
52 | + */ |
|
53 | + private $certif_image_factory; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var CertificateDataService $certif_data_service |
|
57 | + */ |
|
58 | + private $certif_data_service; |
|
59 | + |
|
60 | + /** |
|
61 | + * @var UrlObfuscatorService $url_obfuscator_service |
|
62 | + */ |
|
63 | + private $url_obfuscator_service; |
|
64 | + |
|
65 | + /** |
|
66 | + * Constructor for Certificate Image Request Handler |
|
67 | + * |
|
68 | + * @param ModuleService $module_service |
|
69 | + */ |
|
70 | + public function __construct( |
|
71 | + ModuleService $module_service, |
|
72 | + CertificateFilesystemService $certif_filesystem, |
|
73 | + CertificateDataService $certif_data_service, |
|
74 | + UrlObfuscatorService $url_obfuscator_service |
|
75 | + ) { |
|
76 | + $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
77 | + $this->certif_filesystem = $certif_filesystem; |
|
78 | + $this->certif_image_factory = new CertificateImageFactory($this->certif_filesystem); |
|
79 | + $this->certif_data_service = $certif_data_service; |
|
80 | + $this->url_obfuscator_service = $url_obfuscator_service; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * {@inheritDoc} |
|
85 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
86 | + */ |
|
87 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
88 | + { |
|
89 | + if ($this->module === null) { |
|
90 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
91 | + } |
|
92 | + |
|
93 | + $tree = $request->getAttribute('tree'); |
|
94 | + assert($tree instanceof Tree); |
|
95 | + |
|
96 | + $user = $request->getAttribute('user'); |
|
97 | + assert($user instanceof UserInterface); |
|
98 | + |
|
99 | + $certif_path = $request->getAttribute('cid'); |
|
100 | + $certificate = null; |
|
101 | + if ($certif_path !== '' && $this->url_obfuscator_service->tryDeobfuscate($certif_path)) { |
|
102 | + $certificate = $this->certif_filesystem->certificate($tree, $certif_path); |
|
103 | + } |
|
104 | + |
|
105 | + if ($certificate === null) { |
|
106 | + return $this->certif_image_factory |
|
107 | + ->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND) |
|
108 | + ->withHeader('X-Image-Exception', I18N::translate('The certificate was not found in this family tree.')) |
|
109 | + ; |
|
110 | + } |
|
111 | + |
|
112 | + $use_watermark = $this->certif_image_factory->certificateNeedsWatermark($certificate, $user); |
|
113 | + $watermark = $use_watermark ? $this->watermark($request, $certificate) : null; |
|
114 | + |
|
115 | + return $this->certif_image_factory->certificateFileResponse( |
|
116 | + $certificate, |
|
117 | + $use_watermark, |
|
118 | + $watermark |
|
119 | + ); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Get watermark data for a certificate. |
|
124 | + * |
|
125 | + * @param ServerRequestInterface $request |
|
126 | + * @param Certificate $certificate |
|
127 | + * @return Watermark |
|
128 | + */ |
|
129 | + private function watermark(ServerRequestInterface $request, Certificate $certificate): Watermark |
|
130 | + { |
|
131 | + $color = $certificate->tree()->getPreference('MAJ_CERTIF_WM_FONT_COLOR', Watermark::DEFAULT_COLOR); |
|
132 | + $size = $certificate->tree()->getPreference('MAJ_CERTIF_WM_FONT_MAXSIZE'); |
|
133 | + $text = $this->watermarkText($request, $certificate); |
|
134 | + |
|
135 | + return new Watermark($text, $color, is_numeric($size) ? (int) $size : Watermark::DEFAULT_SIZE); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Get the text to be watermarked for a certificate. |
|
140 | + * |
|
141 | + * @param ServerRequestInterface $request |
|
142 | + * @param Certificate $certificate |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + private function watermarkText(ServerRequestInterface $request, Certificate $certificate): string |
|
146 | + { |
|
147 | + $sid = $request->getQueryParams()['sid'] ?? ''; |
|
148 | + if ($sid !== '') { |
|
149 | + $source = Registry::sourceFactory()->make($sid, $certificate->tree()); |
|
150 | + } else { |
|
151 | + $source = $this->certif_data_service->oneLinkedSource($certificate); |
|
152 | + } |
|
153 | + |
|
154 | + if ($source !== null && $source->canShowName()) { |
|
155 | + $repo = $source->facts(['REPO'])->first(); |
|
156 | + if ($repo !== null && ($repo = $repo->target()) !== null && $repo->canShowName()) { |
|
157 | + return I18N::translate('© %s - %s', strip_tags($repo->fullName()), strip_tags($source->fullName())); |
|
158 | + } |
|
159 | + return $source->fullName(); |
|
160 | + } |
|
161 | + $default_text = $certificate->tree()->getPreference('MAJ_CERTIF_WM_DEFAULT'); |
|
162 | + return $default_text !== '' ? $default_text : I18N::translate('This image is protected under copyright law.'); |
|
163 | + } |
|
164 | 164 | } |
@@ -31,73 +31,73 @@ |
||
31 | 31 | */ |
32 | 32 | class CertificatesList implements RequestHandlerInterface |
33 | 33 | { |
34 | - use ViewResponseTrait; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var CertificatesModule|null $module |
|
38 | - */ |
|
39 | - private $module; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var CertificateFilesystemService $certif_filesystem |
|
43 | - */ |
|
44 | - private $certif_filesystem; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var UrlObfuscatorService $url_obfuscator_service |
|
48 | - */ |
|
49 | - private $url_obfuscator_service; |
|
50 | - |
|
51 | - |
|
52 | - /** |
|
53 | - * Constructor for CertificatesList Request Handler |
|
54 | - * |
|
55 | - * @param ModuleService $module_service |
|
56 | - */ |
|
57 | - public function __construct( |
|
58 | - ModuleService $module_service, |
|
59 | - CertificateFilesystemService $certif_filesystem, |
|
60 | - UrlObfuscatorService $url_obfuscator_service |
|
61 | - ) { |
|
62 | - $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
63 | - $this->certif_filesystem = $certif_filesystem; |
|
64 | - $this->url_obfuscator_service = $url_obfuscator_service; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * {@inheritDoc} |
|
69 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
70 | - */ |
|
71 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
72 | - { |
|
73 | - if ($this->module === null) { |
|
74 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
75 | - } |
|
76 | - |
|
77 | - $tree = $request->getAttribute('tree'); |
|
78 | - assert($tree instanceof Tree); |
|
79 | - |
|
80 | - $title = I18N::translate('Certificates'); |
|
81 | - |
|
82 | - $cities = array_map(function (string $item): array { |
|
83 | - return [$this->url_obfuscator_service->obfuscate($item), $item]; |
|
84 | - }, $this->certif_filesystem->cities($tree)); |
|
85 | - |
|
86 | - $city = $request->getQueryParams()['cityobf'] ?? $request->getAttribute('cityobf') ?? ''; |
|
87 | - |
|
88 | - if ($city !== '' && $this->url_obfuscator_service->tryDeobfuscate($city)) { |
|
89 | - $title = I18N::translate('Certificates for %s', $city); |
|
90 | - $certificates = $this->certif_filesystem->certificatesForCity($tree, $city); |
|
91 | - } |
|
92 | - |
|
93 | - return $this->viewResponse($this->module->name() . '::certificates-list', [ |
|
94 | - 'title' => $title, |
|
95 | - 'tree' => $tree, |
|
96 | - 'module_name' => $this->module->name(), |
|
97 | - 'cities' => $cities, |
|
98 | - 'selected_city' => $city, |
|
99 | - 'certificates_list' => $certificates ?? collect(), |
|
100 | - 'url_obfuscator_service' => $this->url_obfuscator_service |
|
101 | - ]); |
|
102 | - } |
|
34 | + use ViewResponseTrait; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var CertificatesModule|null $module |
|
38 | + */ |
|
39 | + private $module; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var CertificateFilesystemService $certif_filesystem |
|
43 | + */ |
|
44 | + private $certif_filesystem; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var UrlObfuscatorService $url_obfuscator_service |
|
48 | + */ |
|
49 | + private $url_obfuscator_service; |
|
50 | + |
|
51 | + |
|
52 | + /** |
|
53 | + * Constructor for CertificatesList Request Handler |
|
54 | + * |
|
55 | + * @param ModuleService $module_service |
|
56 | + */ |
|
57 | + public function __construct( |
|
58 | + ModuleService $module_service, |
|
59 | + CertificateFilesystemService $certif_filesystem, |
|
60 | + UrlObfuscatorService $url_obfuscator_service |
|
61 | + ) { |
|
62 | + $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
63 | + $this->certif_filesystem = $certif_filesystem; |
|
64 | + $this->url_obfuscator_service = $url_obfuscator_service; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * {@inheritDoc} |
|
69 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
70 | + */ |
|
71 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
72 | + { |
|
73 | + if ($this->module === null) { |
|
74 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
75 | + } |
|
76 | + |
|
77 | + $tree = $request->getAttribute('tree'); |
|
78 | + assert($tree instanceof Tree); |
|
79 | + |
|
80 | + $title = I18N::translate('Certificates'); |
|
81 | + |
|
82 | + $cities = array_map(function (string $item): array { |
|
83 | + return [$this->url_obfuscator_service->obfuscate($item), $item]; |
|
84 | + }, $this->certif_filesystem->cities($tree)); |
|
85 | + |
|
86 | + $city = $request->getQueryParams()['cityobf'] ?? $request->getAttribute('cityobf') ?? ''; |
|
87 | + |
|
88 | + if ($city !== '' && $this->url_obfuscator_service->tryDeobfuscate($city)) { |
|
89 | + $title = I18N::translate('Certificates for %s', $city); |
|
90 | + $certificates = $this->certif_filesystem->certificatesForCity($tree, $city); |
|
91 | + } |
|
92 | + |
|
93 | + return $this->viewResponse($this->module->name() . '::certificates-list', [ |
|
94 | + 'title' => $title, |
|
95 | + 'tree' => $tree, |
|
96 | + 'module_name' => $this->module->name(), |
|
97 | + 'cities' => $cities, |
|
98 | + 'selected_city' => $city, |
|
99 | + 'certificates_list' => $certificates ?? collect(), |
|
100 | + 'url_obfuscator_service' => $this->url_obfuscator_service |
|
101 | + ]); |
|
102 | + } |
|
103 | 103 | } |
@@ -34,58 +34,58 @@ |
||
34 | 34 | */ |
35 | 35 | class AdminConfigPage implements RequestHandlerInterface |
36 | 36 | { |
37 | - use ViewResponseTrait; |
|
37 | + use ViewResponseTrait; |
|
38 | 38 | |
39 | - private ?CertificatesModule $module; |
|
40 | - private TreeService $tree_service; |
|
39 | + private ?CertificatesModule $module; |
|
40 | + private TreeService $tree_service; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Constructor for Admin Config page request handler |
|
44 | - * |
|
45 | - * @param ModuleService $module_service |
|
46 | - */ |
|
47 | - public function __construct(ModuleService $module_service, TreeService $tree_service) |
|
48 | - { |
|
49 | - $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
50 | - $this->tree_service = $tree_service; |
|
51 | - } |
|
42 | + /** |
|
43 | + * Constructor for Admin Config page request handler |
|
44 | + * |
|
45 | + * @param ModuleService $module_service |
|
46 | + */ |
|
47 | + public function __construct(ModuleService $module_service, TreeService $tree_service) |
|
48 | + { |
|
49 | + $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
50 | + $this->tree_service = $tree_service; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * {@inheritDoc} |
|
55 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
56 | - */ |
|
57 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
58 | - { |
|
59 | - $this->layout = 'layouts/administration'; |
|
53 | + /** |
|
54 | + * {@inheritDoc} |
|
55 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
56 | + */ |
|
57 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
58 | + { |
|
59 | + $this->layout = 'layouts/administration'; |
|
60 | 60 | |
61 | - if ($this->module === null) { |
|
62 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
63 | - } |
|
61 | + if ($this->module === null) { |
|
62 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
63 | + } |
|
64 | 64 | |
65 | - $user = $request->getAttribute('user'); |
|
66 | - assert($user instanceof UserInterface); |
|
65 | + $user = $request->getAttribute('user'); |
|
66 | + assert($user instanceof UserInterface); |
|
67 | 67 | |
68 | - $all_trees = $this->tree_service->all()->filter(fn(Tree $tree) => Auth::isManager($tree, $user)); |
|
69 | - if ($all_trees->count() === 0) { |
|
70 | - throw new HttpAccessDeniedException(); |
|
71 | - } |
|
68 | + $all_trees = $this->tree_service->all()->filter(fn(Tree $tree) => Auth::isManager($tree, $user)); |
|
69 | + if ($all_trees->count() === 0) { |
|
70 | + throw new HttpAccessDeniedException(); |
|
71 | + } |
|
72 | 72 | |
73 | - $tree = $request->getAttribute('tree') ?? $all_trees->first(); |
|
74 | - assert($tree instanceof Tree); |
|
73 | + $tree = $request->getAttribute('tree') ?? $all_trees->first(); |
|
74 | + assert($tree instanceof Tree); |
|
75 | 75 | |
76 | - $data_folder = Registry::filesystem()->dataName(); |
|
76 | + $data_folder = Registry::filesystem()->dataName(); |
|
77 | 77 | |
78 | - $same_tree = fn(Tree $tree_collection): bool => $tree->id() === $tree_collection->id(); |
|
79 | - if (!$all_trees->contains($same_tree)) { |
|
80 | - throw new HttpAccessDeniedException(); |
|
81 | - } |
|
78 | + $same_tree = fn(Tree $tree_collection): bool => $tree->id() === $tree_collection->id(); |
|
79 | + if (!$all_trees->contains($same_tree)) { |
|
80 | + throw new HttpAccessDeniedException(); |
|
81 | + } |
|
82 | 82 | |
83 | - return $this->viewResponse($this->module->name() . '::admin/config', [ |
|
84 | - 'module_name' => $this->module->name(), |
|
85 | - 'title' => $this->module->title(), |
|
86 | - 'tree' => $tree, |
|
87 | - 'other_trees' => $all_trees->reject($same_tree), |
|
88 | - 'data_folder' => $data_folder |
|
89 | - ]); |
|
90 | - } |
|
83 | + return $this->viewResponse($this->module->name() . '::admin/config', [ |
|
84 | + 'module_name' => $this->module->name(), |
|
85 | + 'title' => $this->module->title(), |
|
86 | + 'tree' => $tree, |
|
87 | + 'other_trees' => $all_trees->reject($same_tree), |
|
88 | + 'data_folder' => $data_folder |
|
89 | + ]); |
|
90 | + } |
|
91 | 91 | } |
@@ -34,80 +34,80 @@ |
||
34 | 34 | */ |
35 | 35 | class CertificatePage implements RequestHandlerInterface |
36 | 36 | { |
37 | - use ViewResponseTrait; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var CertificatesModule|null $module |
|
41 | - */ |
|
42 | - private $module; |
|
43 | - |
|
44 | - /** |
|
45 | - * @var CertificateFilesystemService $certif_filesystem |
|
46 | - */ |
|
47 | - private $certif_filesystem; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var CertificateDataService $certif_data_service |
|
51 | - */ |
|
52 | - private $certif_data_service; |
|
53 | - |
|
54 | - /** |
|
55 | - * @var UrlObfuscatorService $url_obfuscator_service |
|
56 | - */ |
|
57 | - private $url_obfuscator_service; |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * Constructor for CertificatePage Request Handler |
|
62 | - * |
|
63 | - * @param ModuleService $module_service |
|
64 | - * @param CertificateFilesystemService $certif_filesystem |
|
65 | - * @param CertificateDataService $certif_data_service |
|
66 | - * @param UrlObfuscatorService $url_obfuscator_service |
|
67 | - */ |
|
68 | - public function __construct( |
|
69 | - ModuleService $module_service, |
|
70 | - CertificateFilesystemService $certif_filesystem, |
|
71 | - CertificateDataService $certif_data_service, |
|
72 | - UrlObfuscatorService $url_obfuscator_service |
|
73 | - ) { |
|
74 | - $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
75 | - $this->certif_filesystem = $certif_filesystem; |
|
76 | - $this->certif_data_service = $certif_data_service; |
|
77 | - $this->url_obfuscator_service = $url_obfuscator_service; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * {@inheritDoc} |
|
82 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
83 | - */ |
|
84 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
85 | - { |
|
86 | - if ($this->module === null) { |
|
87 | - throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
88 | - } |
|
89 | - |
|
90 | - $tree = $request->getAttribute('tree'); |
|
91 | - assert($tree instanceof Tree); |
|
92 | - |
|
93 | - $certif_path = $request->getAttribute('cid'); |
|
94 | - if ($certif_path !== '' && $this->url_obfuscator_service->tryDeobfuscate($certif_path)) { |
|
95 | - $certificate = $this->certif_filesystem->certificate($tree, $certif_path); |
|
96 | - } |
|
97 | - |
|
98 | - if (!isset($certificate)) { |
|
99 | - FlashMessages::addMessage('The requested certificate is not valid.'); |
|
100 | - return redirect(route(TreePage::class, ['tree' => $tree->name()])); |
|
101 | - } |
|
102 | - |
|
103 | - return $this->viewResponse($this->module->name() . '::certificate-page', [ |
|
104 | - 'title' => I18N::translate('Certificate - %s', $certificate->name()), |
|
105 | - 'tree' => $tree, |
|
106 | - 'module_name' => $this->module->name(), |
|
107 | - 'certificate' => $certificate, |
|
108 | - 'url_obfuscator_service' => $this->url_obfuscator_service, |
|
109 | - 'linked_individuals' => $this->certif_data_service->linkedIndividuals($certificate), |
|
110 | - 'linked_families' => $this->certif_data_service->linkedFamilies($certificate) |
|
111 | - ]); |
|
112 | - } |
|
37 | + use ViewResponseTrait; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var CertificatesModule|null $module |
|
41 | + */ |
|
42 | + private $module; |
|
43 | + |
|
44 | + /** |
|
45 | + * @var CertificateFilesystemService $certif_filesystem |
|
46 | + */ |
|
47 | + private $certif_filesystem; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var CertificateDataService $certif_data_service |
|
51 | + */ |
|
52 | + private $certif_data_service; |
|
53 | + |
|
54 | + /** |
|
55 | + * @var UrlObfuscatorService $url_obfuscator_service |
|
56 | + */ |
|
57 | + private $url_obfuscator_service; |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * Constructor for CertificatePage Request Handler |
|
62 | + * |
|
63 | + * @param ModuleService $module_service |
|
64 | + * @param CertificateFilesystemService $certif_filesystem |
|
65 | + * @param CertificateDataService $certif_data_service |
|
66 | + * @param UrlObfuscatorService $url_obfuscator_service |
|
67 | + */ |
|
68 | + public function __construct( |
|
69 | + ModuleService $module_service, |
|
70 | + CertificateFilesystemService $certif_filesystem, |
|
71 | + CertificateDataService $certif_data_service, |
|
72 | + UrlObfuscatorService $url_obfuscator_service |
|
73 | + ) { |
|
74 | + $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
75 | + $this->certif_filesystem = $certif_filesystem; |
|
76 | + $this->certif_data_service = $certif_data_service; |
|
77 | + $this->url_obfuscator_service = $url_obfuscator_service; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * {@inheritDoc} |
|
82 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
83 | + */ |
|
84 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
85 | + { |
|
86 | + if ($this->module === null) { |
|
87 | + throw new HttpNotFoundException(I18N::translate('The attached module could not be found.')); |
|
88 | + } |
|
89 | + |
|
90 | + $tree = $request->getAttribute('tree'); |
|
91 | + assert($tree instanceof Tree); |
|
92 | + |
|
93 | + $certif_path = $request->getAttribute('cid'); |
|
94 | + if ($certif_path !== '' && $this->url_obfuscator_service->tryDeobfuscate($certif_path)) { |
|
95 | + $certificate = $this->certif_filesystem->certificate($tree, $certif_path); |
|
96 | + } |
|
97 | + |
|
98 | + if (!isset($certificate)) { |
|
99 | + FlashMessages::addMessage('The requested certificate is not valid.'); |
|
100 | + return redirect(route(TreePage::class, ['tree' => $tree->name()])); |
|
101 | + } |
|
102 | + |
|
103 | + return $this->viewResponse($this->module->name() . '::certificate-page', [ |
|
104 | + 'title' => I18N::translate('Certificate - %s', $certificate->name()), |
|
105 | + 'tree' => $tree, |
|
106 | + 'module_name' => $this->module->name(), |
|
107 | + 'certificate' => $certificate, |
|
108 | + 'url_obfuscator_service' => $this->url_obfuscator_service, |
|
109 | + 'linked_individuals' => $this->certif_data_service->linkedIndividuals($certificate), |
|
110 | + 'linked_families' => $this->certif_data_service->linkedFamilies($certificate) |
|
111 | + ]); |
|
112 | + } |
|
113 | 113 | } |
@@ -30,55 +30,55 @@ |
||
30 | 30 | */ |
31 | 31 | class AutoCompleteFile extends AbstractAutocompleteHandler |
32 | 32 | { |
33 | - // Tell the browser to cache the results |
|
34 | - protected const CACHE_LIFE = 10; |
|
33 | + // Tell the browser to cache the results |
|
34 | + protected const CACHE_LIFE = 10; |
|
35 | 35 | |
36 | - private ?CertificatesModule $module; |
|
37 | - private CertificateFilesystemService $certif_filesystem; |
|
38 | - private UrlObfuscatorService $url_obfuscator_service; |
|
36 | + private ?CertificatesModule $module; |
|
37 | + private CertificateFilesystemService $certif_filesystem; |
|
38 | + private UrlObfuscatorService $url_obfuscator_service; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Constructor for AutoCompleteFile Request Handler |
|
42 | - * |
|
43 | - * @param ModuleService $module_service |
|
44 | - * @param CertificateFilesystemService $certif_filesystem |
|
45 | - * @param UrlObfuscatorService $url_obfuscator_service |
|
46 | - * @param SearchService $search_service |
|
47 | - */ |
|
48 | - public function __construct( |
|
49 | - ModuleService $module_service, |
|
50 | - CertificateFilesystemService $certif_filesystem, |
|
51 | - UrlObfuscatorService $url_obfuscator_service, |
|
52 | - SearchService $search_service |
|
53 | - ) { |
|
54 | - parent::__construct($search_service); |
|
55 | - $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
56 | - $this->certif_filesystem = $certif_filesystem; |
|
57 | - $this->url_obfuscator_service = $url_obfuscator_service; |
|
58 | - } |
|
40 | + /** |
|
41 | + * Constructor for AutoCompleteFile Request Handler |
|
42 | + * |
|
43 | + * @param ModuleService $module_service |
|
44 | + * @param CertificateFilesystemService $certif_filesystem |
|
45 | + * @param UrlObfuscatorService $url_obfuscator_service |
|
46 | + * @param SearchService $search_service |
|
47 | + */ |
|
48 | + public function __construct( |
|
49 | + ModuleService $module_service, |
|
50 | + CertificateFilesystemService $certif_filesystem, |
|
51 | + UrlObfuscatorService $url_obfuscator_service, |
|
52 | + SearchService $search_service |
|
53 | + ) { |
|
54 | + parent::__construct($search_service); |
|
55 | + $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
56 | + $this->certif_filesystem = $certif_filesystem; |
|
57 | + $this->url_obfuscator_service = $url_obfuscator_service; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * {@inheritDoc} |
|
62 | - * @see \Fisharebest\Webtrees\Http\RequestHandlers\AbstractAutocompleteHandler::search() |
|
63 | - */ |
|
64 | - protected function search(ServerRequestInterface $request): Collection |
|
65 | - { |
|
66 | - $tree = $request->getAttribute('tree'); |
|
67 | - assert($tree instanceof Tree); |
|
60 | + /** |
|
61 | + * {@inheritDoc} |
|
62 | + * @see \Fisharebest\Webtrees\Http\RequestHandlers\AbstractAutocompleteHandler::search() |
|
63 | + */ |
|
64 | + protected function search(ServerRequestInterface $request): Collection |
|
65 | + { |
|
66 | + $tree = $request->getAttribute('tree'); |
|
67 | + assert($tree instanceof Tree); |
|
68 | 68 | |
69 | - $city = $request->getQueryParams()['cityobf'] ?? ''; |
|
69 | + $city = $request->getQueryParams()['cityobf'] ?? ''; |
|
70 | 70 | |
71 | - if ($this->module === null || $city === '' || !$this->url_obfuscator_service->tryDeobfuscate($city)) { |
|
72 | - return collect(); |
|
73 | - } |
|
71 | + if ($this->module === null || $city === '' || !$this->url_obfuscator_service->tryDeobfuscate($city)) { |
|
72 | + return collect(); |
|
73 | + } |
|
74 | 74 | |
75 | - $query = $request->getAttribute('query') ?? ''; |
|
75 | + $query = $request->getAttribute('query') ?? ''; |
|
76 | 76 | |
77 | - /** @var Collection<string> $results */ |
|
78 | - $results = $this->certif_filesystem |
|
79 | - ->certificatesForCityContaining($tree, $city, $query) |
|
80 | - ->map(fn(Certificate $certificate): string => $certificate->filename()); |
|
77 | + /** @var Collection<string> $results */ |
|
78 | + $results = $this->certif_filesystem |
|
79 | + ->certificatesForCityContaining($tree, $city, $query) |
|
80 | + ->map(fn(Certificate $certificate): string => $certificate->filename()); |
|
81 | 81 | |
82 | - return $results; |
|
83 | - } |
|
82 | + return $results; |
|
83 | + } |
|
84 | 84 | } |
@@ -23,121 +23,121 @@ |
||
23 | 23 | */ |
24 | 24 | class UrlObfuscatorService |
25 | 25 | { |
26 | - /** |
|
27 | - * @var string|null $encryption_key |
|
28 | - */ |
|
29 | - private $encryption_key; |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * Return (and generate) the key to be used for the encryption step |
|
34 | - * |
|
35 | - * @return string Encryption key |
|
36 | - */ |
|
37 | - protected function encryptionKey(): string |
|
38 | - { |
|
39 | - if ($this->encryption_key === null) { |
|
40 | - /** @var ServerRequestInterface $request **/ |
|
41 | - $request = app(ServerRequestInterface::class); |
|
42 | - $server_name = $request->getServerParams()['SERVER_NAME'] ?? ''; |
|
43 | - $server_software = $request->getServerParams()['SERVER_SOFTWARE'] ?? ''; |
|
44 | - $this->encryption_key = str_pad(md5( |
|
45 | - $server_name !== '' && $server_software !== '' ? |
|
46 | - $server_name . $server_software : |
|
47 | - 'STANDARDKEYIFNOSERVER' |
|
48 | - ), SODIUM_CRYPTO_SECRETBOX_KEYBYTES, "1234567890ABCDEF"); |
|
49 | - } |
|
50 | - return $this->encryption_key; |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Obfuscate a clear text, with a combination of encryption and base64 encoding. |
|
55 | - * The return string is URL-safe. |
|
56 | - * |
|
57 | - * @param string $cleartext Text to obfuscate |
|
58 | - * @param string $key |
|
59 | - * @param string $nonce |
|
60 | - * @return string |
|
61 | - */ |
|
62 | - public function obfuscate(string $cleartext, string $key = '', string $nonce = ''): string |
|
63 | - { |
|
64 | - if ($nonce === '') { |
|
65 | - $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); |
|
66 | - } |
|
67 | - if ($key === '') { |
|
68 | - $key = $this->encryptionKey(); |
|
69 | - } |
|
70 | - |
|
71 | - if (strlen($nonce) !== SODIUM_CRYPTO_SECRETBOX_NONCEBYTES) { |
|
72 | - throw new InvalidArgumentException('The nonce needs to be SODIUM_CRYPTO_SECRETBOX_NONCEBYTES long'); |
|
73 | - } |
|
74 | - |
|
75 | - if (strlen($key) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { |
|
76 | - throw new InvalidArgumentException('The key needs to be SODIUM_CRYPTO_SECRETBOX_KEYBYTES long'); |
|
77 | - } |
|
78 | - |
|
79 | - \Fisharebest\Webtrees\DebugBar::addMessage($key); |
|
80 | - $encryted = sodium_crypto_secretbox($cleartext, $nonce, $key); |
|
81 | - return strtr(base64_encode($nonce . $encryted), '+/=', '._-'); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Deobfuscate a string from an URL to a clear text. |
|
86 | - * |
|
87 | - * @param string $obfuscated Text to deobfuscate |
|
88 | - * @param string $key |
|
89 | - * @throws InvalidArgumentException |
|
90 | - * @return string |
|
91 | - */ |
|
92 | - public function deobfuscate(string $obfuscated, string $key = ''): string |
|
93 | - { |
|
94 | - $obfuscated = strtr($obfuscated, '._-', '+/='); |
|
95 | - if ($key === '') { |
|
96 | - $key = $this->encryptionKey(); |
|
97 | - } |
|
98 | - |
|
99 | - if (strlen($key) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { |
|
100 | - throw new InvalidArgumentException('The key needs to be SODIUM_CRYPTO_SECRETBOX_KEYBYTES long'); |
|
101 | - } |
|
102 | - |
|
103 | - $encrypted = base64_decode($obfuscated, true); |
|
104 | - if ($encrypted === false) { |
|
105 | - throw new InvalidArgumentException('The encrypted value is not in correct base64 format.'); |
|
106 | - } |
|
107 | - |
|
108 | - if (mb_strlen($encrypted, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) { |
|
109 | - throw new InvalidArgumentException('The encrypted value does not contain enough characters for the key.'); |
|
110 | - } |
|
111 | - |
|
112 | - $nonce = mb_substr($encrypted, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit'); |
|
113 | - $ciphertext = mb_substr($encrypted, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit'); |
|
114 | - |
|
115 | - $decrypted = sodium_crypto_secretbox_open($ciphertext, $nonce, $key); |
|
116 | - |
|
117 | - if ($decrypted === false) { |
|
118 | - throw new InvalidArgumentException('The message has been tampered with in transit.'); |
|
119 | - } |
|
120 | - |
|
121 | - //sodium_memzero($ciphertext); // sodium_compat cannot handle it, only through libsodium |
|
122 | - |
|
123 | - /** @var string $decrypted - Psalm detect as string|true otherwise */ |
|
124 | - return $decrypted; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Try to deobfuscate a string from an URL to a clear text, returning whether the operation is a success. |
|
129 | - * |
|
130 | - * @param string $obfuscated Text to deobfuscate |
|
131 | - * @param string $key |
|
132 | - * @return bool |
|
133 | - */ |
|
134 | - public function tryDeobfuscate(string &$obfuscated, string $key = ''): bool |
|
135 | - { |
|
136 | - try { |
|
137 | - $obfuscated = $this->deobfuscate($obfuscated, $key); |
|
138 | - return true; |
|
139 | - } catch (InvalidArgumentException $ex) { |
|
140 | - } |
|
141 | - return false; |
|
142 | - } |
|
26 | + /** |
|
27 | + * @var string|null $encryption_key |
|
28 | + */ |
|
29 | + private $encryption_key; |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * Return (and generate) the key to be used for the encryption step |
|
34 | + * |
|
35 | + * @return string Encryption key |
|
36 | + */ |
|
37 | + protected function encryptionKey(): string |
|
38 | + { |
|
39 | + if ($this->encryption_key === null) { |
|
40 | + /** @var ServerRequestInterface $request **/ |
|
41 | + $request = app(ServerRequestInterface::class); |
|
42 | + $server_name = $request->getServerParams()['SERVER_NAME'] ?? ''; |
|
43 | + $server_software = $request->getServerParams()['SERVER_SOFTWARE'] ?? ''; |
|
44 | + $this->encryption_key = str_pad(md5( |
|
45 | + $server_name !== '' && $server_software !== '' ? |
|
46 | + $server_name . $server_software : |
|
47 | + 'STANDARDKEYIFNOSERVER' |
|
48 | + ), SODIUM_CRYPTO_SECRETBOX_KEYBYTES, "1234567890ABCDEF"); |
|
49 | + } |
|
50 | + return $this->encryption_key; |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Obfuscate a clear text, with a combination of encryption and base64 encoding. |
|
55 | + * The return string is URL-safe. |
|
56 | + * |
|
57 | + * @param string $cleartext Text to obfuscate |
|
58 | + * @param string $key |
|
59 | + * @param string $nonce |
|
60 | + * @return string |
|
61 | + */ |
|
62 | + public function obfuscate(string $cleartext, string $key = '', string $nonce = ''): string |
|
63 | + { |
|
64 | + if ($nonce === '') { |
|
65 | + $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); |
|
66 | + } |
|
67 | + if ($key === '') { |
|
68 | + $key = $this->encryptionKey(); |
|
69 | + } |
|
70 | + |
|
71 | + if (strlen($nonce) !== SODIUM_CRYPTO_SECRETBOX_NONCEBYTES) { |
|
72 | + throw new InvalidArgumentException('The nonce needs to be SODIUM_CRYPTO_SECRETBOX_NONCEBYTES long'); |
|
73 | + } |
|
74 | + |
|
75 | + if (strlen($key) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { |
|
76 | + throw new InvalidArgumentException('The key needs to be SODIUM_CRYPTO_SECRETBOX_KEYBYTES long'); |
|
77 | + } |
|
78 | + |
|
79 | + \Fisharebest\Webtrees\DebugBar::addMessage($key); |
|
80 | + $encryted = sodium_crypto_secretbox($cleartext, $nonce, $key); |
|
81 | + return strtr(base64_encode($nonce . $encryted), '+/=', '._-'); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Deobfuscate a string from an URL to a clear text. |
|
86 | + * |
|
87 | + * @param string $obfuscated Text to deobfuscate |
|
88 | + * @param string $key |
|
89 | + * @throws InvalidArgumentException |
|
90 | + * @return string |
|
91 | + */ |
|
92 | + public function deobfuscate(string $obfuscated, string $key = ''): string |
|
93 | + { |
|
94 | + $obfuscated = strtr($obfuscated, '._-', '+/='); |
|
95 | + if ($key === '') { |
|
96 | + $key = $this->encryptionKey(); |
|
97 | + } |
|
98 | + |
|
99 | + if (strlen($key) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { |
|
100 | + throw new InvalidArgumentException('The key needs to be SODIUM_CRYPTO_SECRETBOX_KEYBYTES long'); |
|
101 | + } |
|
102 | + |
|
103 | + $encrypted = base64_decode($obfuscated, true); |
|
104 | + if ($encrypted === false) { |
|
105 | + throw new InvalidArgumentException('The encrypted value is not in correct base64 format.'); |
|
106 | + } |
|
107 | + |
|
108 | + if (mb_strlen($encrypted, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) { |
|
109 | + throw new InvalidArgumentException('The encrypted value does not contain enough characters for the key.'); |
|
110 | + } |
|
111 | + |
|
112 | + $nonce = mb_substr($encrypted, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit'); |
|
113 | + $ciphertext = mb_substr($encrypted, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit'); |
|
114 | + |
|
115 | + $decrypted = sodium_crypto_secretbox_open($ciphertext, $nonce, $key); |
|
116 | + |
|
117 | + if ($decrypted === false) { |
|
118 | + throw new InvalidArgumentException('The message has been tampered with in transit.'); |
|
119 | + } |
|
120 | + |
|
121 | + //sodium_memzero($ciphertext); // sodium_compat cannot handle it, only through libsodium |
|
122 | + |
|
123 | + /** @var string $decrypted - Psalm detect as string|true otherwise */ |
|
124 | + return $decrypted; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Try to deobfuscate a string from an URL to a clear text, returning whether the operation is a success. |
|
129 | + * |
|
130 | + * @param string $obfuscated Text to deobfuscate |
|
131 | + * @param string $key |
|
132 | + * @return bool |
|
133 | + */ |
|
134 | + public function tryDeobfuscate(string &$obfuscated, string $key = ''): bool |
|
135 | + { |
|
136 | + try { |
|
137 | + $obfuscated = $this->deobfuscate($obfuscated, $key); |
|
138 | + return true; |
|
139 | + } catch (InvalidArgumentException $ex) { |
|
140 | + } |
|
141 | + return false; |
|
142 | + } |
|
143 | 143 | } |
@@ -39,163 +39,163 @@ |
||
39 | 39 | */ |
40 | 40 | class CertificateImageFactory extends ImageFactory implements ImageFactoryInterface |
41 | 41 | { |
42 | - /** |
|
43 | - * @var CertificateFilesystemService $filesystem_service |
|
44 | - */ |
|
45 | - private $filesystem_service; |
|
46 | - |
|
47 | - /** |
|
48 | - * Constructor for the Certificate Image Factory |
|
49 | - * |
|
50 | - * @param CertificateFilesystemService $filesystem_service |
|
51 | - */ |
|
52 | - public function __construct(CertificateFilesystemService $filesystem_service) |
|
53 | - { |
|
54 | - $this->filesystem_service = $filesystem_service; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Check is a file MIME type is supported by the system. |
|
59 | - * |
|
60 | - * @param string $mime |
|
61 | - * @return bool |
|
62 | - */ |
|
63 | - public function isMimeTypeSupported(string $mime): bool |
|
64 | - { |
|
65 | - return array_key_exists($mime, self::INTERVENTION_FORMATS); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Create a full-size version of a certificate. |
|
70 | - * |
|
71 | - * @param Certificate $certificate |
|
72 | - * @param bool $add_watermark |
|
73 | - * @param Watermark $watermark |
|
74 | - * @throws InvalidArgumentException |
|
75 | - * @return ResponseInterface |
|
76 | - */ |
|
77 | - public function certificateFileResponse( |
|
78 | - Certificate $certificate, |
|
79 | - bool $add_watermark = false, |
|
80 | - Watermark $watermark = null |
|
81 | - ): ResponseInterface { |
|
82 | - $filesystem = $this->filesystem_service->filesystem($certificate->tree()); |
|
83 | - $filename = $certificate->path(); |
|
84 | - |
|
85 | - if (!$add_watermark) { |
|
86 | - return $this->fileResponse($filesystem, $filename, false); |
|
87 | - } |
|
88 | - |
|
89 | - try { |
|
90 | - $image = $this->imageManager()->make($filesystem->readStream($filename)); |
|
91 | - $image = $this->autorotateImage($image); |
|
92 | - |
|
93 | - if ($watermark == null) { |
|
94 | - throw new InvalidArgumentException('Watermark data not defined'); |
|
95 | - } |
|
96 | - |
|
97 | - $width = $image->width(); |
|
98 | - $height = $image->height(); |
|
99 | - |
|
100 | - $watermark->adjustSize($width); |
|
101 | - $watermark_x = (int) ceil($watermark->textLengthEstimate() * 1.5); |
|
102 | - $watermark_y = $watermark->size() * 12 + 1; |
|
103 | - |
|
104 | - $font_definition = function (AbstractFont $font) use ($watermark): void { |
|
105 | - $font->file(Webtrees::ROOT_DIR . 'resources/fonts/DejaVuSans.ttf'); |
|
106 | - $font->color($watermark->color()); |
|
107 | - $font->size($watermark->size()); |
|
108 | - $font->valign('top'); |
|
109 | - }; |
|
110 | - |
|
111 | - for ($i = min((int) ceil($width * 0.1), $watermark_x); $i < $width; $i += $watermark_x) { |
|
112 | - for ($j = min((int) ceil($height * 0.1), $watermark_y); $j < $height; $j += $watermark_y) { |
|
113 | - $image = $image->text($watermark->text(), $i, $j, $font_definition); |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - $format = static::INTERVENTION_FORMATS[$image->mime()] ?? 'jpg'; |
|
118 | - $quality = $this->extractImageQuality($image, static::GD_DEFAULT_IMAGE_QUALITY); |
|
119 | - $data = (string) $image->encode($format, $quality); |
|
120 | - |
|
121 | - return $this->imageResponse($data, $image->mime(), ''); |
|
122 | - } catch (NotReadableException $ex) { |
|
123 | - return $this->replacementImageResponse(pathinfo($filename, PATHINFO_EXTENSION)) |
|
124 | - ->withHeader('X-Image-Exception', $ex->getMessage()); |
|
125 | - } catch (FilesystemException | UnableToReadFile $ex) { |
|
126 | - return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND); |
|
127 | - } catch (Throwable $ex) { |
|
128 | - return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR) |
|
129 | - ->withHeader('X-Image-Exception', $ex->getMessage()); |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Does a full-sized certificate need a watermark? |
|
135 | - * |
|
136 | - * @param Certificate $certificate |
|
137 | - * @param UserInterface $user |
|
138 | - * @return bool |
|
139 | - */ |
|
140 | - public function certificateNeedsWatermark(Certificate $certificate, UserInterface $user): bool |
|
141 | - { |
|
142 | - $tree = $certificate->tree(); |
|
143 | - |
|
144 | - return Auth::accessLevel($tree, $user) > $tree->getPreference('MAJ_CERTIF_SHOW_NO_WATERMARK'); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Neutralise the methods associated with MediaFile. |
|
149 | - */ |
|
150 | - |
|
151 | - /** |
|
152 | - * {@inheritDoc} |
|
153 | - * @see \Fisharebest\Webtrees\Factories\ImageFactory::mediaFileResponse() |
|
154 | - */ |
|
155 | - public function mediaFileResponse(MediaFile $media_file, bool $add_watermark, bool $download): ResponseInterface |
|
156 | - { |
|
157 | - throw new BadMethodCallException("Invalid method for Certificates"); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * {@inheritDoc} |
|
162 | - * @see \Fisharebest\Webtrees\Factories\ImageFactory::mediaFileThumbnailResponse() |
|
163 | - */ |
|
164 | - public function mediaFileThumbnailResponse( |
|
165 | - MediaFile $media_file, |
|
166 | - int $width, |
|
167 | - int $height, |
|
168 | - string $fit, |
|
169 | - bool $add_watermark |
|
170 | - ): ResponseInterface { |
|
171 | - throw new BadMethodCallException("Invalid method for Certificates"); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * {@inheritDoc} |
|
176 | - * @see \Fisharebest\Webtrees\Factories\ImageFactory::createWatermark() |
|
177 | - */ |
|
178 | - public function createWatermark(int $width, int $height, MediaFile $media_file): Image |
|
179 | - { |
|
180 | - |
|
181 | - throw new BadMethodCallException("Invalid method for Certificates"); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * {@inheritDoc} |
|
186 | - * @see \Fisharebest\Webtrees\Factories\ImageFactory::fileNeedsWatermark() |
|
187 | - */ |
|
188 | - public function fileNeedsWatermark(MediaFile $media_file, UserInterface $user): bool |
|
189 | - { |
|
190 | - throw new BadMethodCallException("Invalid method for Certificates"); |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * {@inheritDoc} |
|
195 | - * @see \Fisharebest\Webtrees\Factories\ImageFactory::thumbnailNeedsWatermark() |
|
196 | - */ |
|
197 | - public function thumbnailNeedsWatermark(MediaFile $media_file, UserInterface $user): bool |
|
198 | - { |
|
199 | - throw new BadMethodCallException("Invalid method for Certificates"); |
|
200 | - } |
|
42 | + /** |
|
43 | + * @var CertificateFilesystemService $filesystem_service |
|
44 | + */ |
|
45 | + private $filesystem_service; |
|
46 | + |
|
47 | + /** |
|
48 | + * Constructor for the Certificate Image Factory |
|
49 | + * |
|
50 | + * @param CertificateFilesystemService $filesystem_service |
|
51 | + */ |
|
52 | + public function __construct(CertificateFilesystemService $filesystem_service) |
|
53 | + { |
|
54 | + $this->filesystem_service = $filesystem_service; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Check is a file MIME type is supported by the system. |
|
59 | + * |
|
60 | + * @param string $mime |
|
61 | + * @return bool |
|
62 | + */ |
|
63 | + public function isMimeTypeSupported(string $mime): bool |
|
64 | + { |
|
65 | + return array_key_exists($mime, self::INTERVENTION_FORMATS); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Create a full-size version of a certificate. |
|
70 | + * |
|
71 | + * @param Certificate $certificate |
|
72 | + * @param bool $add_watermark |
|
73 | + * @param Watermark $watermark |
|
74 | + * @throws InvalidArgumentException |
|
75 | + * @return ResponseInterface |
|
76 | + */ |
|
77 | + public function certificateFileResponse( |
|
78 | + Certificate $certificate, |
|
79 | + bool $add_watermark = false, |
|
80 | + Watermark $watermark = null |
|
81 | + ): ResponseInterface { |
|
82 | + $filesystem = $this->filesystem_service->filesystem($certificate->tree()); |
|
83 | + $filename = $certificate->path(); |
|
84 | + |
|
85 | + if (!$add_watermark) { |
|
86 | + return $this->fileResponse($filesystem, $filename, false); |
|
87 | + } |
|
88 | + |
|
89 | + try { |
|
90 | + $image = $this->imageManager()->make($filesystem->readStream($filename)); |
|
91 | + $image = $this->autorotateImage($image); |
|
92 | + |
|
93 | + if ($watermark == null) { |
|
94 | + throw new InvalidArgumentException('Watermark data not defined'); |
|
95 | + } |
|
96 | + |
|
97 | + $width = $image->width(); |
|
98 | + $height = $image->height(); |
|
99 | + |
|
100 | + $watermark->adjustSize($width); |
|
101 | + $watermark_x = (int) ceil($watermark->textLengthEstimate() * 1.5); |
|
102 | + $watermark_y = $watermark->size() * 12 + 1; |
|
103 | + |
|
104 | + $font_definition = function (AbstractFont $font) use ($watermark): void { |
|
105 | + $font->file(Webtrees::ROOT_DIR . 'resources/fonts/DejaVuSans.ttf'); |
|
106 | + $font->color($watermark->color()); |
|
107 | + $font->size($watermark->size()); |
|
108 | + $font->valign('top'); |
|
109 | + }; |
|
110 | + |
|
111 | + for ($i = min((int) ceil($width * 0.1), $watermark_x); $i < $width; $i += $watermark_x) { |
|
112 | + for ($j = min((int) ceil($height * 0.1), $watermark_y); $j < $height; $j += $watermark_y) { |
|
113 | + $image = $image->text($watermark->text(), $i, $j, $font_definition); |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + $format = static::INTERVENTION_FORMATS[$image->mime()] ?? 'jpg'; |
|
118 | + $quality = $this->extractImageQuality($image, static::GD_DEFAULT_IMAGE_QUALITY); |
|
119 | + $data = (string) $image->encode($format, $quality); |
|
120 | + |
|
121 | + return $this->imageResponse($data, $image->mime(), ''); |
|
122 | + } catch (NotReadableException $ex) { |
|
123 | + return $this->replacementImageResponse(pathinfo($filename, PATHINFO_EXTENSION)) |
|
124 | + ->withHeader('X-Image-Exception', $ex->getMessage()); |
|
125 | + } catch (FilesystemException | UnableToReadFile $ex) { |
|
126 | + return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND); |
|
127 | + } catch (Throwable $ex) { |
|
128 | + return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR) |
|
129 | + ->withHeader('X-Image-Exception', $ex->getMessage()); |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Does a full-sized certificate need a watermark? |
|
135 | + * |
|
136 | + * @param Certificate $certificate |
|
137 | + * @param UserInterface $user |
|
138 | + * @return bool |
|
139 | + */ |
|
140 | + public function certificateNeedsWatermark(Certificate $certificate, UserInterface $user): bool |
|
141 | + { |
|
142 | + $tree = $certificate->tree(); |
|
143 | + |
|
144 | + return Auth::accessLevel($tree, $user) > $tree->getPreference('MAJ_CERTIF_SHOW_NO_WATERMARK'); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Neutralise the methods associated with MediaFile. |
|
149 | + */ |
|
150 | + |
|
151 | + /** |
|
152 | + * {@inheritDoc} |
|
153 | + * @see \Fisharebest\Webtrees\Factories\ImageFactory::mediaFileResponse() |
|
154 | + */ |
|
155 | + public function mediaFileResponse(MediaFile $media_file, bool $add_watermark, bool $download): ResponseInterface |
|
156 | + { |
|
157 | + throw new BadMethodCallException("Invalid method for Certificates"); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * {@inheritDoc} |
|
162 | + * @see \Fisharebest\Webtrees\Factories\ImageFactory::mediaFileThumbnailResponse() |
|
163 | + */ |
|
164 | + public function mediaFileThumbnailResponse( |
|
165 | + MediaFile $media_file, |
|
166 | + int $width, |
|
167 | + int $height, |
|
168 | + string $fit, |
|
169 | + bool $add_watermark |
|
170 | + ): ResponseInterface { |
|
171 | + throw new BadMethodCallException("Invalid method for Certificates"); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * {@inheritDoc} |
|
176 | + * @see \Fisharebest\Webtrees\Factories\ImageFactory::createWatermark() |
|
177 | + */ |
|
178 | + public function createWatermark(int $width, int $height, MediaFile $media_file): Image |
|
179 | + { |
|
180 | + |
|
181 | + throw new BadMethodCallException("Invalid method for Certificates"); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * {@inheritDoc} |
|
186 | + * @see \Fisharebest\Webtrees\Factories\ImageFactory::fileNeedsWatermark() |
|
187 | + */ |
|
188 | + public function fileNeedsWatermark(MediaFile $media_file, UserInterface $user): bool |
|
189 | + { |
|
190 | + throw new BadMethodCallException("Invalid method for Certificates"); |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * {@inheritDoc} |
|
195 | + * @see \Fisharebest\Webtrees\Factories\ImageFactory::thumbnailNeedsWatermark() |
|
196 | + */ |
|
197 | + public function thumbnailNeedsWatermark(MediaFile $media_file, UserInterface $user): bool |
|
198 | + { |
|
199 | + throw new BadMethodCallException("Invalid method for Certificates"); |
|
200 | + } |
|
201 | 201 | } |
@@ -19,112 +19,112 @@ |
||
19 | 19 | */ |
20 | 20 | class Watermark |
21 | 21 | { |
22 | - /** |
|
23 | - * Default font color for watermarks |
|
24 | - * @var string DEFAULT_COLOR |
|
25 | - * */ |
|
26 | - public const DEFAULT_COLOR = '#4D6DF3'; |
|
27 | - |
|
28 | - /** |
|
29 | - * Default maximum font size for watermarks |
|
30 | - * @var string DEFAULT_SIZE |
|
31 | - * */ |
|
32 | - public const DEFAULT_SIZE = 18; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var string $text |
|
36 | - */ |
|
37 | - private $text; |
|
38 | - |
|
39 | - /** |
|
40 | - * @var string $color; |
|
41 | - */ |
|
42 | - private $color; |
|
43 | - |
|
44 | - |
|
45 | - /** |
|
46 | - * @var int $size |
|
47 | - */ |
|
48 | - private $size; |
|
49 | - |
|
50 | - /** |
|
51 | - * Constructor for Watermark data class |
|
52 | - * |
|
53 | - * @param string $text |
|
54 | - * @param string $color |
|
55 | - * @param int $size |
|
56 | - */ |
|
57 | - public function __construct(string $text, string $color, int $size) |
|
58 | - { |
|
59 | - $this->text = $text; |
|
60 | - $this->color = $color; |
|
61 | - $this->size = $size; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Get the watermark text. |
|
66 | - * |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public function text(): string |
|
70 | - { |
|
71 | - return $this->text; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Get the watermark font color. |
|
76 | - * |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - public function color(): string |
|
80 | - { |
|
81 | - return $this->color; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Get the watermark maximum font size. |
|
86 | - * @return int |
|
87 | - */ |
|
88 | - public function size(): int |
|
89 | - { |
|
90 | - return $this->size; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Return an estimate of the size in pixels of the watermark text length. |
|
95 | - * |
|
96 | - * @return int |
|
97 | - */ |
|
98 | - public function textLengthEstimate(): int |
|
99 | - { |
|
100 | - return $this->stringLengthEstimate(mb_strlen($this->text), $this->size); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Decrease the font size if necessary, based on the image width. |
|
105 | - * |
|
106 | - * @param int $width |
|
107 | - */ |
|
108 | - public function adjustSize(int $width): void |
|
109 | - { |
|
110 | - $len = mb_strlen($this->text); |
|
111 | - while ($this->stringLengthEstimate($len, $this->size) > 0.9 * $width) { |
|
112 | - $this->size--; |
|
113 | - if ($this->size == 2) { |
|
114 | - return; |
|
115 | - } |
|
116 | - } |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Return an estimate of the size in pixels of a text in a specified font size. |
|
121 | - * |
|
122 | - * @param int $text_length |
|
123 | - * @param int $font_size |
|
124 | - * @return int |
|
125 | - */ |
|
126 | - private function stringLengthEstimate(int $text_length, int $font_size): int |
|
127 | - { |
|
128 | - return $text_length * (int) ceil(($font_size + 2) * 0.5); |
|
129 | - } |
|
22 | + /** |
|
23 | + * Default font color for watermarks |
|
24 | + * @var string DEFAULT_COLOR |
|
25 | + * */ |
|
26 | + public const DEFAULT_COLOR = '#4D6DF3'; |
|
27 | + |
|
28 | + /** |
|
29 | + * Default maximum font size for watermarks |
|
30 | + * @var string DEFAULT_SIZE |
|
31 | + * */ |
|
32 | + public const DEFAULT_SIZE = 18; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var string $text |
|
36 | + */ |
|
37 | + private $text; |
|
38 | + |
|
39 | + /** |
|
40 | + * @var string $color; |
|
41 | + */ |
|
42 | + private $color; |
|
43 | + |
|
44 | + |
|
45 | + /** |
|
46 | + * @var int $size |
|
47 | + */ |
|
48 | + private $size; |
|
49 | + |
|
50 | + /** |
|
51 | + * Constructor for Watermark data class |
|
52 | + * |
|
53 | + * @param string $text |
|
54 | + * @param string $color |
|
55 | + * @param int $size |
|
56 | + */ |
|
57 | + public function __construct(string $text, string $color, int $size) |
|
58 | + { |
|
59 | + $this->text = $text; |
|
60 | + $this->color = $color; |
|
61 | + $this->size = $size; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Get the watermark text. |
|
66 | + * |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public function text(): string |
|
70 | + { |
|
71 | + return $this->text; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Get the watermark font color. |
|
76 | + * |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + public function color(): string |
|
80 | + { |
|
81 | + return $this->color; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Get the watermark maximum font size. |
|
86 | + * @return int |
|
87 | + */ |
|
88 | + public function size(): int |
|
89 | + { |
|
90 | + return $this->size; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Return an estimate of the size in pixels of the watermark text length. |
|
95 | + * |
|
96 | + * @return int |
|
97 | + */ |
|
98 | + public function textLengthEstimate(): int |
|
99 | + { |
|
100 | + return $this->stringLengthEstimate(mb_strlen($this->text), $this->size); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Decrease the font size if necessary, based on the image width. |
|
105 | + * |
|
106 | + * @param int $width |
|
107 | + */ |
|
108 | + public function adjustSize(int $width): void |
|
109 | + { |
|
110 | + $len = mb_strlen($this->text); |
|
111 | + while ($this->stringLengthEstimate($len, $this->size) > 0.9 * $width) { |
|
112 | + $this->size--; |
|
113 | + if ($this->size == 2) { |
|
114 | + return; |
|
115 | + } |
|
116 | + } |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Return an estimate of the size in pixels of a text in a specified font size. |
|
121 | + * |
|
122 | + * @param int $text_length |
|
123 | + * @param int $font_size |
|
124 | + * @return int |
|
125 | + */ |
|
126 | + private function stringLengthEstimate(int $text_length, int $font_size): int |
|
127 | + { |
|
128 | + return $text_length * (int) ceil(($font_size + 2) * 0.5); |
|
129 | + } |
|
130 | 130 | } |