@@ -24,57 +24,57 @@ |
||
24 | 24 | */ |
25 | 25 | class MapAdapterResult |
26 | 26 | { |
27 | - private GeoAnalysisResult $result; |
|
27 | + private GeoAnalysisResult $result; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @var array<int, \Brick\Geo\IO\GeoJSON\Feature> $features |
|
31 | - */ |
|
32 | - private array $features; |
|
29 | + /** |
|
30 | + * @var array<int, \Brick\Geo\IO\GeoJSON\Feature> $features |
|
31 | + */ |
|
32 | + private array $features; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Constructor for MapAdapterResult |
|
36 | - * |
|
37 | - * @param GeoAnalysisResult $result |
|
38 | - * @param \Brick\Geo\IO\GeoJSON\Feature[] $features |
|
39 | - */ |
|
40 | - final public function __construct(GeoAnalysisResult $result, array $features) |
|
41 | - { |
|
42 | - $this->result = $result; |
|
43 | - $this->features = $features; |
|
44 | - } |
|
34 | + /** |
|
35 | + * Constructor for MapAdapterResult |
|
36 | + * |
|
37 | + * @param GeoAnalysisResult $result |
|
38 | + * @param \Brick\Geo\IO\GeoJSON\Feature[] $features |
|
39 | + */ |
|
40 | + final public function __construct(GeoAnalysisResult $result, array $features) |
|
41 | + { |
|
42 | + $this->result = $result; |
|
43 | + $this->features = $features; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get the GeoAnalysisResult after mapping of the places |
|
48 | - * |
|
49 | - * @return GeoAnalysisResult |
|
50 | - */ |
|
51 | - public function geoAnalysisResult(): GeoAnalysisResult |
|
52 | - { |
|
53 | - return $this->result; |
|
54 | - } |
|
46 | + /** |
|
47 | + * Get the GeoAnalysisResult after mapping of the places |
|
48 | + * |
|
49 | + * @return GeoAnalysisResult |
|
50 | + */ |
|
51 | + public function geoAnalysisResult(): GeoAnalysisResult |
|
52 | + { |
|
53 | + return $this->result; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get the list of features to display on the map |
|
58 | - * |
|
59 | - * @return array<int, \Brick\Geo\IO\GeoJSON\Feature> |
|
60 | - */ |
|
61 | - public function features(): array |
|
62 | - { |
|
63 | - return $this->features; |
|
64 | - } |
|
56 | + /** |
|
57 | + * Get the list of features to display on the map |
|
58 | + * |
|
59 | + * @return array<int, \Brick\Geo\IO\GeoJSON\Feature> |
|
60 | + */ |
|
61 | + public function features(): array |
|
62 | + { |
|
63 | + return $this->features; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Merge the current MapAdapter with another. |
|
68 | - * The current object is modified, not the second one. |
|
69 | - * |
|
70 | - * @param MapAdapterResult $other |
|
71 | - * @return static |
|
72 | - */ |
|
73 | - public function merge(MapAdapterResult $other): self |
|
74 | - { |
|
75 | - return new static( |
|
76 | - $this->result->merge($other->result), |
|
77 | - [...$this->features, ...$other->features] |
|
78 | - ); |
|
79 | - } |
|
66 | + /** |
|
67 | + * Merge the current MapAdapter with another. |
|
68 | + * The current object is modified, not the second one. |
|
69 | + * |
|
70 | + * @param MapAdapterResult $other |
|
71 | + * @return static |
|
72 | + */ |
|
73 | + public function merge(MapAdapterResult $other): self |
|
74 | + { |
|
75 | + return new static( |
|
76 | + $this->result->merge($other->result), |
|
77 | + [...$this->features, ...$other->features] |
|
78 | + ); |
|
79 | + } |
|
80 | 80 | } |
@@ -25,68 +25,68 @@ |
||
25 | 25 | */ |
26 | 26 | class TitlesCardHook implements NameAccordionExtenderInterface |
27 | 27 | { |
28 | - private ModuleInterface $module; |
|
28 | + private ModuleInterface $module; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor for TitlesCardHook |
|
32 | - * |
|
33 | - * @param ModuleInterface $module |
|
34 | - */ |
|
35 | - public function __construct(ModuleInterface $module) |
|
36 | - { |
|
37 | - $this->module = $module; |
|
38 | - } |
|
30 | + /** |
|
31 | + * Constructor for TitlesCardHook |
|
32 | + * |
|
33 | + * @param ModuleInterface $module |
|
34 | + */ |
|
35 | + public function __construct(ModuleInterface $module) |
|
36 | + { |
|
37 | + $this->module = $module; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * {@inheritDoc} |
|
42 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module() |
|
43 | - */ |
|
44 | - public function module(): ModuleInterface |
|
45 | - { |
|
46 | - return $this->module; |
|
47 | - } |
|
40 | + /** |
|
41 | + * {@inheritDoc} |
|
42 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module() |
|
43 | + */ |
|
44 | + public function module(): ModuleInterface |
|
45 | + { |
|
46 | + return $this->module; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * {@inheritDoc} |
|
51 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\NameAccordionExtenderInterface::accordionCard() |
|
52 | - */ |
|
53 | - public function accordionCard(Individual $individual): string |
|
54 | - { |
|
55 | - $title_separator = $this->module->getPreference('MAJ_TITLE_PREFIX'); |
|
56 | - if ($title_separator === '') { |
|
57 | - return ''; |
|
58 | - } |
|
49 | + /** |
|
50 | + * {@inheritDoc} |
|
51 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\NameAccordionExtenderInterface::accordionCard() |
|
52 | + */ |
|
53 | + public function accordionCard(Individual $individual): string |
|
54 | + { |
|
55 | + $title_separator = $this->module->getPreference('MAJ_TITLE_PREFIX'); |
|
56 | + if ($title_separator === '') { |
|
57 | + return ''; |
|
58 | + } |
|
59 | 59 | |
60 | - $titles = $this->individualTitles($individual, '/(.*?) ((' . $title_separator . ')(.*))/i'); |
|
60 | + $titles = $this->individualTitles($individual, '/(.*?) ((' . $title_separator . ')(.*))/i'); |
|
61 | 61 | |
62 | - return count($titles) === 0 ? '' : |
|
63 | - view($this->module()->name() . '::components/accordion-item-titles', [ 'titles' => $titles ]); |
|
64 | - } |
|
62 | + return count($titles) === 0 ? '' : |
|
63 | + view($this->module()->name() . '::components/accordion-item-titles', [ 'titles' => $titles ]); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Extract the individual titles from the TITL tags. |
|
68 | - * Split the title based on a pattern to identify the title and the land it refers to. |
|
69 | - * |
|
70 | - * @param Individual $individual |
|
71 | - * @param string $pattern |
|
72 | - * @return array<string, string[]> |
|
73 | - */ |
|
74 | - protected function individualTitles(Individual $individual, string $pattern): array |
|
75 | - { |
|
76 | - $titles_list = []; |
|
77 | - /** @var \Illuminate\Support\Collection<string> $titles */ |
|
78 | - $titles = $individual->facts(['TITL']) |
|
79 | - ->sortByDesc(fn(Fact $fact) => $fact->date()->julianDay()) |
|
80 | - ->map(fn(Fact $fact) => $fact->value()); |
|
66 | + /** |
|
67 | + * Extract the individual titles from the TITL tags. |
|
68 | + * Split the title based on a pattern to identify the title and the land it refers to. |
|
69 | + * |
|
70 | + * @param Individual $individual |
|
71 | + * @param string $pattern |
|
72 | + * @return array<string, string[]> |
|
73 | + */ |
|
74 | + protected function individualTitles(Individual $individual, string $pattern): array |
|
75 | + { |
|
76 | + $titles_list = []; |
|
77 | + /** @var \Illuminate\Support\Collection<string> $titles */ |
|
78 | + $titles = $individual->facts(['TITL']) |
|
79 | + ->sortByDesc(fn(Fact $fact) => $fact->date()->julianDay()) |
|
80 | + ->map(fn(Fact $fact) => $fact->value()); |
|
81 | 81 | |
82 | - foreach ($titles as $title) { |
|
83 | - if (preg_match($pattern, $title, $match) === 1) { |
|
84 | - /** @var array<int, string> $match */ |
|
85 | - $titles_list[$match[1]][] = trim($match[2]); |
|
86 | - } else { |
|
87 | - $titles_list[$title][] = ''; |
|
88 | - } |
|
89 | - } |
|
90 | - return $titles_list; |
|
91 | - } |
|
82 | + foreach ($titles as $title) { |
|
83 | + if (preg_match($pattern, $title, $match) === 1) { |
|
84 | + /** @var array<int, string> $match */ |
|
85 | + $titles_list[$match[1]][] = trim($match[2]); |
|
86 | + } else { |
|
87 | + $titles_list[$title][] = ''; |
|
88 | + } |
|
89 | + } |
|
90 | + return $titles_list; |
|
91 | + } |
|
92 | 92 | } |
@@ -57,10 +57,9 @@ |
||
57 | 57 | return ''; |
58 | 58 | } |
59 | 59 | |
60 | - $titles = $this->individualTitles($individual, '/(.*?) ((' . $title_separator . ')(.*))/i'); |
|
60 | + $titles = $this->individualTitles($individual, '/(.*?) (('.$title_separator.')(.*))/i'); |
|
61 | 61 | |
62 | - return count($titles) === 0 ? '' : |
|
63 | - view($this->module()->name() . '::components/accordion-item-titles', [ 'titles' => $titles ]); |
|
62 | + return count($titles) === 0 ? '' : view($this->module()->name().'::components/accordion-item-titles', ['titles' => $titles]); |
|
64 | 63 | } |
65 | 64 | |
66 | 65 | /** |
@@ -61,12 +61,12 @@ |
||
61 | 61 | public function factSourcePrepend(Tree $tree, $fact): string |
62 | 62 | { |
63 | 63 | $permission_level = $tree->getPreference('MAJ_CERTIF_SHOW_CERT'); |
64 | - if (is_numeric($permission_level) && Auth::accessLevel($tree) <= (int) $permission_level) { |
|
64 | + if (is_numeric($permission_level) && Auth::accessLevel($tree) <= (int)$permission_level) { |
|
65 | 65 | $path = $this->extractPath($fact); |
66 | 66 | |
67 | 67 | if ($path !== '') { |
68 | 68 | $certificate = new Certificate($tree, $path); |
69 | - return view($this->module->name() . '::components/certificate-icon', [ |
|
69 | + return view($this->module->name().'::components/certificate-icon', [ |
|
70 | 70 | 'module_name' => $this->module->name(), |
71 | 71 | 'certificate' => $certificate, |
72 | 72 | 'url_obfuscator_service' => $this->url_obfuscator_service, |
@@ -30,87 +30,87 @@ |
||
30 | 30 | */ |
31 | 31 | class SourceCertificateIconHook implements FactSourceTextExtenderInterface |
32 | 32 | { |
33 | - private CertificatesModule $module; |
|
34 | - private UrlObfuscatorService $url_obfuscator_service; |
|
33 | + private CertificatesModule $module; |
|
34 | + private UrlObfuscatorService $url_obfuscator_service; |
|
35 | 35 | |
36 | - /** |
|
37 | - * Constructor for SourceCertificateIconHook |
|
38 | - * |
|
39 | - * @param CertificatesModule $module |
|
40 | - * @param UrlObfuscatorService $url_obfuscator_service |
|
41 | - */ |
|
42 | - public function __construct(CertificatesModule $module, UrlObfuscatorService $url_obfuscator_service) |
|
43 | - { |
|
44 | - $this->module = $module; |
|
45 | - $this->url_obfuscator_service = $url_obfuscator_service; |
|
46 | - } |
|
36 | + /** |
|
37 | + * Constructor for SourceCertificateIconHook |
|
38 | + * |
|
39 | + * @param CertificatesModule $module |
|
40 | + * @param UrlObfuscatorService $url_obfuscator_service |
|
41 | + */ |
|
42 | + public function __construct(CertificatesModule $module, UrlObfuscatorService $url_obfuscator_service) |
|
43 | + { |
|
44 | + $this->module = $module; |
|
45 | + $this->url_obfuscator_service = $url_obfuscator_service; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * {@inheritDoc} |
|
50 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module() |
|
51 | - */ |
|
52 | - public function module(): ModuleInterface |
|
53 | - { |
|
54 | - return $this->module; |
|
55 | - } |
|
48 | + /** |
|
49 | + * {@inheritDoc} |
|
50 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module() |
|
51 | + */ |
|
52 | + public function module(): ModuleInterface |
|
53 | + { |
|
54 | + return $this->module; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * {@inheritDoc} |
|
59 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\FactSourceTextExtenderInterface::factSourcePrepend() |
|
60 | - */ |
|
61 | - public function factSourcePrepend(Tree $tree, $fact): string |
|
62 | - { |
|
63 | - $permission_level = $tree->getPreference('MAJ_CERTIF_SHOW_CERT'); |
|
64 | - if (is_numeric($permission_level) && Auth::accessLevel($tree) <= (int) $permission_level) { |
|
65 | - $path = $this->extractPath($fact); |
|
57 | + /** |
|
58 | + * {@inheritDoc} |
|
59 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\FactSourceTextExtenderInterface::factSourcePrepend() |
|
60 | + */ |
|
61 | + public function factSourcePrepend(Tree $tree, $fact): string |
|
62 | + { |
|
63 | + $permission_level = $tree->getPreference('MAJ_CERTIF_SHOW_CERT'); |
|
64 | + if (is_numeric($permission_level) && Auth::accessLevel($tree) <= (int) $permission_level) { |
|
65 | + $path = $this->extractPath($fact); |
|
66 | 66 | |
67 | - if ($path !== '') { |
|
68 | - $certificate = new Certificate($tree, $path); |
|
69 | - return view($this->module->name() . '::components/certificate-icon', [ |
|
70 | - 'module_name' => $this->module->name(), |
|
71 | - 'certificate' => $certificate, |
|
72 | - 'url_obfuscator_service' => $this->url_obfuscator_service, |
|
73 | - 'js_script_url' => $this->module->assetUrl('js/certificates.min.js') |
|
74 | - ]); |
|
75 | - } |
|
76 | - } |
|
77 | - return ''; |
|
78 | - } |
|
67 | + if ($path !== '') { |
|
68 | + $certificate = new Certificate($tree, $path); |
|
69 | + return view($this->module->name() . '::components/certificate-icon', [ |
|
70 | + 'module_name' => $this->module->name(), |
|
71 | + 'certificate' => $certificate, |
|
72 | + 'url_obfuscator_service' => $this->url_obfuscator_service, |
|
73 | + 'js_script_url' => $this->module->assetUrl('js/certificates.min.js') |
|
74 | + ]); |
|
75 | + } |
|
76 | + } |
|
77 | + return ''; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Extract path from the provided fact objet. |
|
82 | - * |
|
83 | - * @param \Fisharebest\Webtrees\Fact|array<array<\Fisharebest\Webtrees\Contracts\ElementInterface|string>> $fact |
|
84 | - * @return string |
|
85 | - * @psalm-suppress RedundantConditionGivenDocblockType |
|
86 | - */ |
|
87 | - private function extractPath($fact): string |
|
88 | - { |
|
89 | - if ($fact instanceof Fact && $fact->target() instanceof Source) { |
|
90 | - return $fact->attribute('_ACT'); |
|
91 | - } elseif ( |
|
92 | - is_array($fact) && count($fact) === 2 |
|
93 | - && null !== ($source_elements = $fact[0]) && is_array($source_elements) // @phpstan-ignore-line |
|
94 | - && null !== ($source_values = $fact[1]) && is_array($source_values) // @phpstan-ignore-line |
|
95 | - ) { |
|
96 | - foreach ($source_elements as $key => $element) { |
|
97 | - if ( |
|
98 | - $element instanceof SourceCertificate |
|
99 | - && isset($source_values[$key]) && is_string($source_values[$key]) |
|
100 | - ) { |
|
101 | - return $element->canonical($source_values[$key]); |
|
102 | - } |
|
103 | - } |
|
104 | - } |
|
105 | - return ''; |
|
106 | - } |
|
80 | + /** |
|
81 | + * Extract path from the provided fact objet. |
|
82 | + * |
|
83 | + * @param \Fisharebest\Webtrees\Fact|array<array<\Fisharebest\Webtrees\Contracts\ElementInterface|string>> $fact |
|
84 | + * @return string |
|
85 | + * @psalm-suppress RedundantConditionGivenDocblockType |
|
86 | + */ |
|
87 | + private function extractPath($fact): string |
|
88 | + { |
|
89 | + if ($fact instanceof Fact && $fact->target() instanceof Source) { |
|
90 | + return $fact->attribute('_ACT'); |
|
91 | + } elseif ( |
|
92 | + is_array($fact) && count($fact) === 2 |
|
93 | + && null !== ($source_elements = $fact[0]) && is_array($source_elements) // @phpstan-ignore-line |
|
94 | + && null !== ($source_values = $fact[1]) && is_array($source_values) // @phpstan-ignore-line |
|
95 | + ) { |
|
96 | + foreach ($source_elements as $key => $element) { |
|
97 | + if ( |
|
98 | + $element instanceof SourceCertificate |
|
99 | + && isset($source_values[$key]) && is_string($source_values[$key]) |
|
100 | + ) { |
|
101 | + return $element->canonical($source_values[$key]); |
|
102 | + } |
|
103 | + } |
|
104 | + } |
|
105 | + return ''; |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * {@inheritDoc} |
|
110 | - * @see \MyArtJaub\Webtrees\Contracts\Hooks\FactSourceTextExtenderInterface::factSourceAppend() |
|
111 | - */ |
|
112 | - public function factSourceAppend(Tree $tree, $fact): string |
|
113 | - { |
|
114 | - return ''; |
|
115 | - } |
|
108 | + /** |
|
109 | + * {@inheritDoc} |
|
110 | + * @see \MyArtJaub\Webtrees\Contracts\Hooks\FactSourceTextExtenderInterface::factSourceAppend() |
|
111 | + */ |
|
112 | + public function factSourceAppend(Tree $tree, $fact): string |
|
113 | + { |
|
114 | + return ''; |
|
115 | + } |
|
116 | 116 | } |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | bool $add_watermark = false, |
80 | 80 | Watermark $watermark = null |
81 | 81 | ): ResponseInterface { |
82 | - $filesystem = $this->filesystem_service->filesystem($certificate->tree()); |
|
82 | + $filesystem = $this->filesystem_service->filesystem($certificate->tree()); |
|
83 | 83 | $filename = $certificate->path(); |
84 | 84 | |
85 | 85 | if (!$add_watermark) { |
@@ -98,34 +98,34 @@ discard block |
||
98 | 98 | $height = $image->height(); |
99 | 99 | |
100 | 100 | $watermark->adjustSize($width); |
101 | - $watermark_x = (int) ceil($watermark->textLengthEstimate() * 1.5); |
|
101 | + $watermark_x = (int)ceil($watermark->textLengthEstimate() * 1.5); |
|
102 | 102 | $watermark_y = $watermark->size() * 12 + 1; |
103 | 103 | |
104 | - $font_definition = function (AbstractFont $font) use ($watermark): void { |
|
105 | - $font->file(Webtrees::ROOT_DIR . 'resources/fonts/DejaVuSans.ttf'); |
|
104 | + $font_definition = function(AbstractFont $font) use ($watermark): void { |
|
105 | + $font->file(Webtrees::ROOT_DIR.'resources/fonts/DejaVuSans.ttf'); |
|
106 | 106 | $font->color($watermark->color()); |
107 | 107 | $font->size($watermark->size()); |
108 | 108 | $font->valign('top'); |
109 | 109 | }; |
110 | 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) { |
|
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 | 113 | $image = $image->text($watermark->text(), $i, $j, $font_definition); |
114 | 114 | } |
115 | 115 | } |
116 | 116 | |
117 | 117 | $format = static::SUPPORTED_FORMATS[$image->mime()] ?? 'jpg'; |
118 | 118 | $quality = $this->extractImageQuality($image, static::GD_DEFAULT_IMAGE_QUALITY); |
119 | - $data = (string) $image->encode($format, $quality); |
|
119 | + $data = (string)$image->encode($format, $quality); |
|
120 | 120 | |
121 | 121 | return $this->imageResponse($data, $image->mime(), ''); |
122 | 122 | } catch (NotReadableException $ex) { |
123 | 123 | return $this->replacementImageResponse(pathinfo($filename, PATHINFO_EXTENSION)) |
124 | 124 | ->withHeader('X-Image-Exception', $ex->getMessage()); |
125 | 125 | } catch (FilesystemException | UnableToReadFile $ex) { |
126 | - return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND); |
|
126 | + return $this->replacementImageResponse((string)StatusCodeInterface::STATUS_NOT_FOUND); |
|
127 | 127 | } catch (Throwable $ex) { |
128 | - return $this->replacementImageResponse((string) StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR) |
|
128 | + return $this->replacementImageResponse((string)StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR) |
|
129 | 129 | ->withHeader('X-Image-Exception', $ex->getMessage()); |
130 | 130 | } |
131 | 131 | } |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | public function certificateNeedsWatermark(Certificate $certificate, UserInterface $user): bool |
141 | 141 | { |
142 | 142 | $tree = $certificate->tree(); |
143 | - $watermark_level = (int) ($tree->getPreference('MAJ_CERTIF_SHOW_NO_WATERMARK', (string) Auth::PRIV_HIDE)); |
|
143 | + $watermark_level = (int)($tree->getPreference('MAJ_CERTIF_SHOW_NO_WATERMARK', (string)Auth::PRIV_HIDE)); |
|
144 | 144 | |
145 | 145 | return Auth::accessLevel($tree, $user) > $watermark_level; |
146 | 146 | } |
@@ -39,164 +39,164 @@ |
||
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::SUPPORTED_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::SUPPORTED_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 | - $watermark_level = (int) ($tree->getPreference('MAJ_CERTIF_SHOW_NO_WATERMARK', (string) Auth::PRIV_HIDE)); |
|
144 | - |
|
145 | - return Auth::accessLevel($tree, $user) > $watermark_level; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Neutralise the methods associated with MediaFile. |
|
150 | - */ |
|
151 | - |
|
152 | - /** |
|
153 | - * {@inheritDoc} |
|
154 | - * @see \Fisharebest\Webtrees\Factories\ImageFactory::mediaFileResponse() |
|
155 | - */ |
|
156 | - public function mediaFileResponse(MediaFile $media_file, bool $add_watermark, bool $download): ResponseInterface |
|
157 | - { |
|
158 | - throw new BadMethodCallException("Invalid method for Certificates"); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * {@inheritDoc} |
|
163 | - * @see \Fisharebest\Webtrees\Factories\ImageFactory::mediaFileThumbnailResponse() |
|
164 | - */ |
|
165 | - public function mediaFileThumbnailResponse( |
|
166 | - MediaFile $media_file, |
|
167 | - int $width, |
|
168 | - int $height, |
|
169 | - string $fit, |
|
170 | - bool $add_watermark |
|
171 | - ): ResponseInterface { |
|
172 | - throw new BadMethodCallException("Invalid method for Certificates"); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * {@inheritDoc} |
|
177 | - * @see \Fisharebest\Webtrees\Factories\ImageFactory::createWatermark() |
|
178 | - */ |
|
179 | - public function createWatermark(int $width, int $height, MediaFile $media_file): Image |
|
180 | - { |
|
181 | - |
|
182 | - throw new BadMethodCallException("Invalid method for Certificates"); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * {@inheritDoc} |
|
187 | - * @see \Fisharebest\Webtrees\Factories\ImageFactory::fileNeedsWatermark() |
|
188 | - */ |
|
189 | - public function fileNeedsWatermark(MediaFile $media_file, UserInterface $user): bool |
|
190 | - { |
|
191 | - throw new BadMethodCallException("Invalid method for Certificates"); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * {@inheritDoc} |
|
196 | - * @see \Fisharebest\Webtrees\Factories\ImageFactory::thumbnailNeedsWatermark() |
|
197 | - */ |
|
198 | - public function thumbnailNeedsWatermark(MediaFile $media_file, UserInterface $user): bool |
|
199 | - { |
|
200 | - throw new BadMethodCallException("Invalid method for Certificates"); |
|
201 | - } |
|
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::SUPPORTED_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::SUPPORTED_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 | + $watermark_level = (int) ($tree->getPreference('MAJ_CERTIF_SHOW_NO_WATERMARK', (string) Auth::PRIV_HIDE)); |
|
144 | + |
|
145 | + return Auth::accessLevel($tree, $user) > $watermark_level; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Neutralise the methods associated with MediaFile. |
|
150 | + */ |
|
151 | + |
|
152 | + /** |
|
153 | + * {@inheritDoc} |
|
154 | + * @see \Fisharebest\Webtrees\Factories\ImageFactory::mediaFileResponse() |
|
155 | + */ |
|
156 | + public function mediaFileResponse(MediaFile $media_file, bool $add_watermark, bool $download): ResponseInterface |
|
157 | + { |
|
158 | + throw new BadMethodCallException("Invalid method for Certificates"); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * {@inheritDoc} |
|
163 | + * @see \Fisharebest\Webtrees\Factories\ImageFactory::mediaFileThumbnailResponse() |
|
164 | + */ |
|
165 | + public function mediaFileThumbnailResponse( |
|
166 | + MediaFile $media_file, |
|
167 | + int $width, |
|
168 | + int $height, |
|
169 | + string $fit, |
|
170 | + bool $add_watermark |
|
171 | + ): ResponseInterface { |
|
172 | + throw new BadMethodCallException("Invalid method for Certificates"); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * {@inheritDoc} |
|
177 | + * @see \Fisharebest\Webtrees\Factories\ImageFactory::createWatermark() |
|
178 | + */ |
|
179 | + public function createWatermark(int $width, int $height, MediaFile $media_file): Image |
|
180 | + { |
|
181 | + |
|
182 | + throw new BadMethodCallException("Invalid method for Certificates"); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * {@inheritDoc} |
|
187 | + * @see \Fisharebest\Webtrees\Factories\ImageFactory::fileNeedsWatermark() |
|
188 | + */ |
|
189 | + public function fileNeedsWatermark(MediaFile $media_file, UserInterface $user): bool |
|
190 | + { |
|
191 | + throw new BadMethodCallException("Invalid method for Certificates"); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * {@inheritDoc} |
|
196 | + * @see \Fisharebest\Webtrees\Factories\ImageFactory::thumbnailNeedsWatermark() |
|
197 | + */ |
|
198 | + public function thumbnailNeedsWatermark(MediaFile $media_file, UserInterface $user): bool |
|
199 | + { |
|
200 | + throw new BadMethodCallException("Invalid method for Certificates"); |
|
201 | + } |
|
202 | 202 | } |
@@ -31,53 +31,53 @@ |
||
31 | 31 | */ |
32 | 32 | class AutoCompleteFile extends AbstractAutocompleteHandler |
33 | 33 | { |
34 | - // Tell the browser to cache the results |
|
35 | - protected const CACHE_LIFE = 10; |
|
34 | + // Tell the browser to cache the results |
|
35 | + protected const CACHE_LIFE = 10; |
|
36 | 36 | |
37 | - private ?CertificatesModule $module; |
|
38 | - private CertificateFilesystemService $certif_filesystem; |
|
39 | - private UrlObfuscatorService $url_obfuscator_service; |
|
37 | + private ?CertificatesModule $module; |
|
38 | + private CertificateFilesystemService $certif_filesystem; |
|
39 | + private UrlObfuscatorService $url_obfuscator_service; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Constructor for AutoCompleteFile Request Handler |
|
43 | - * |
|
44 | - * @param ModuleService $module_service |
|
45 | - * @param CertificateFilesystemService $certif_filesystem |
|
46 | - * @param UrlObfuscatorService $url_obfuscator_service |
|
47 | - * @param SearchService $search_service |
|
48 | - */ |
|
49 | - public function __construct( |
|
50 | - ModuleService $module_service, |
|
51 | - CertificateFilesystemService $certif_filesystem, |
|
52 | - UrlObfuscatorService $url_obfuscator_service, |
|
53 | - SearchService $search_service |
|
54 | - ) { |
|
55 | - parent::__construct($search_service); |
|
56 | - $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
57 | - $this->certif_filesystem = $certif_filesystem; |
|
58 | - $this->url_obfuscator_service = $url_obfuscator_service; |
|
59 | - } |
|
41 | + /** |
|
42 | + * Constructor for AutoCompleteFile Request Handler |
|
43 | + * |
|
44 | + * @param ModuleService $module_service |
|
45 | + * @param CertificateFilesystemService $certif_filesystem |
|
46 | + * @param UrlObfuscatorService $url_obfuscator_service |
|
47 | + * @param SearchService $search_service |
|
48 | + */ |
|
49 | + public function __construct( |
|
50 | + ModuleService $module_service, |
|
51 | + CertificateFilesystemService $certif_filesystem, |
|
52 | + UrlObfuscatorService $url_obfuscator_service, |
|
53 | + SearchService $search_service |
|
54 | + ) { |
|
55 | + parent::__construct($search_service); |
|
56 | + $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
57 | + $this->certif_filesystem = $certif_filesystem; |
|
58 | + $this->url_obfuscator_service = $url_obfuscator_service; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * {@inheritDoc} |
|
63 | - * @see \Fisharebest\Webtrees\Http\RequestHandlers\AbstractAutocompleteHandler::search() |
|
64 | - */ |
|
65 | - protected function search(ServerRequestInterface $request): Collection |
|
66 | - { |
|
67 | - $tree = Validator::attributes($request)->tree(); |
|
68 | - $city = Validator::queryParams($request)->string('cityobf', ''); |
|
61 | + /** |
|
62 | + * {@inheritDoc} |
|
63 | + * @see \Fisharebest\Webtrees\Http\RequestHandlers\AbstractAutocompleteHandler::search() |
|
64 | + */ |
|
65 | + protected function search(ServerRequestInterface $request): Collection |
|
66 | + { |
|
67 | + $tree = Validator::attributes($request)->tree(); |
|
68 | + $city = Validator::queryParams($request)->string('cityobf', ''); |
|
69 | 69 | |
70 | - if ($this->module === null || $city === '' || !$this->url_obfuscator_service->tryDeobfuscate($city)) { |
|
71 | - return collect(); |
|
72 | - } |
|
70 | + if ($this->module === null || $city === '' || !$this->url_obfuscator_service->tryDeobfuscate($city)) { |
|
71 | + return collect(); |
|
72 | + } |
|
73 | 73 | |
74 | - $query = Validator::attributes($request)->string('query', ''); |
|
74 | + $query = Validator::attributes($request)->string('query', ''); |
|
75 | 75 | |
76 | - /** @var Collection<int, string> $results */ |
|
77 | - $results = $this->certif_filesystem |
|
78 | - ->certificatesForCityContaining($tree, $city, $query) |
|
79 | - ->map(fn(Certificate $certificate): string => $certificate->filename()); |
|
76 | + /** @var Collection<int, string> $results */ |
|
77 | + $results = $this->certif_filesystem |
|
78 | + ->certificatesForCityContaining($tree, $city, $query) |
|
79 | + ->map(fn(Certificate $certificate): string => $certificate->filename()); |
|
80 | 80 | |
81 | - return $results; |
|
82 | - } |
|
81 | + return $results; |
|
82 | + } |
|
83 | 83 | } |
@@ -71,10 +71,10 @@ |
||
71 | 71 | return collect(); |
72 | 72 | } |
73 | 73 | |
74 | - $query = Validator::attributes($request)->string('query', ''); |
|
74 | + $query = Validator::attributes($request)->string('query', ''); |
|
75 | 75 | |
76 | 76 | /** @var Collection<int, string> $results */ |
77 | - $results = $this->certif_filesystem |
|
77 | + $results = $this->certif_filesystem |
|
78 | 78 | ->certificatesForCityContaining($tree, $city, $query) |
79 | 79 | ->map(fn(Certificate $certificate): string => $certificate->filename()); |
80 | 80 |
@@ -38,106 +38,106 @@ |
||
38 | 38 | */ |
39 | 39 | class CertificateImage implements RequestHandlerInterface |
40 | 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 | - } |
|
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 | 143 | } |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | |
84 | 84 | if ($certificate === null) { |
85 | 85 | return $this->certif_image_factory |
86 | - ->replacementImageResponse((string) StatusCodeInterface::STATUS_NOT_FOUND) |
|
86 | + ->replacementImageResponse((string)StatusCodeInterface::STATUS_NOT_FOUND) |
|
87 | 87 | ->withHeader('X-Image-Exception', I18N::translate('The certificate was not found in this family tree.')) |
88 | 88 | ; |
89 | 89 | } |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | $size = $certificate->tree()->getPreference('MAJ_CERTIF_WM_FONT_MAXSIZE'); |
112 | 112 | $text = $this->watermarkText($request, $certificate); |
113 | 113 | |
114 | - return new Watermark($text, $color, is_numeric($size) ? (int) $size : Watermark::DEFAULT_SIZE); |
|
114 | + return new Watermark($text, $color, is_numeric($size) ? (int)$size : Watermark::DEFAULT_SIZE); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | /** |
@@ -62,11 +62,11 @@ discard block |
||
62 | 62 | |
63 | 63 | $tree->setPreference( |
64 | 64 | 'MAJ_CERTIF_SHOW_CERT', |
65 | - (string) Validator::parsedBody($request)->integer('MAJ_CERTIF_SHOW_CERT', Auth::PRIV_HIDE) |
|
65 | + (string)Validator::parsedBody($request)->integer('MAJ_CERTIF_SHOW_CERT', Auth::PRIV_HIDE) |
|
66 | 66 | ); |
67 | 67 | $tree->setPreference( |
68 | 68 | 'MAJ_CERTIF_SHOW_NO_WATERMARK', |
69 | - (string) Validator::parsedBody($request)->integer('MAJ_CERTIF_SHOW_NO_WATERMARK', Auth::PRIV_HIDE) |
|
69 | + (string)Validator::parsedBody($request)->integer('MAJ_CERTIF_SHOW_NO_WATERMARK', Auth::PRIV_HIDE) |
|
70 | 70 | ); |
71 | 71 | $tree->setPreference( |
72 | 72 | 'MAJ_CERTIF_WM_DEFAULT', |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | |
76 | 76 | $tree->setPreference( |
77 | 77 | 'MAJ_CERTIF_WM_FONT_MAXSIZE', |
78 | - (string) ( |
|
78 | + (string)( |
|
79 | 79 | Validator::parsedBody($request)->isBetween(0, PHP_INT_MAX)->integer('MAJ_CERTIF_WM_FONT_MAXSIZE', 18) |
80 | 80 | ) |
81 | 81 | ); |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | // Only accept valid folders for MAJ_CERT_ROOTDIR |
90 | 90 | $cert_root_dir = Validator::parsedBody($request)->string('MAJ_CERTIF_ROOTDIR', ''); |
91 | 91 | $cert_root_dir = preg_replace('/[:\/\\\\]+/', '/', $cert_root_dir) ?? ''; |
92 | - $cert_root_dir = trim($cert_root_dir, '/') . '/'; |
|
92 | + $cert_root_dir = trim($cert_root_dir, '/').'/'; |
|
93 | 93 | $tree->setPreference('MAJ_CERTIF_ROOTDIR', $cert_root_dir); |
94 | 94 | |
95 | 95 | FlashMessages::addMessage( |
@@ -30,71 +30,71 @@ |
||
30 | 30 | */ |
31 | 31 | class AdminConfigAction implements RequestHandlerInterface |
32 | 32 | { |
33 | - private ?CertificatesModule $module; |
|
33 | + private ?CertificatesModule $module; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Constructor for Admin Config Action request handler |
|
37 | - * |
|
38 | - * @param ModuleService $module_service |
|
39 | - */ |
|
40 | - public function __construct(ModuleService $module_service) |
|
41 | - { |
|
42 | - $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
43 | - } |
|
35 | + /** |
|
36 | + * Constructor for Admin Config Action request handler |
|
37 | + * |
|
38 | + * @param ModuleService $module_service |
|
39 | + */ |
|
40 | + public function __construct(ModuleService $module_service) |
|
41 | + { |
|
42 | + $this->module = $module_service->findByInterface(CertificatesModule::class)->first(); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * {@inheritDoc} |
|
47 | - * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
48 | - */ |
|
49 | - public function handle(ServerRequestInterface $request): ResponseInterface |
|
50 | - { |
|
51 | - $tree = Validator::attributes($request)->tree(); |
|
45 | + /** |
|
46 | + * {@inheritDoc} |
|
47 | + * @see \Psr\Http\Server\RequestHandlerInterface::handle() |
|
48 | + */ |
|
49 | + public function handle(ServerRequestInterface $request): ResponseInterface |
|
50 | + { |
|
51 | + $tree = Validator::attributes($request)->tree(); |
|
52 | 52 | |
53 | - if ($this->module === null) { |
|
54 | - FlashMessages::addMessage( |
|
55 | - I18N::translate('The attached module could not be found.'), |
|
56 | - 'danger' |
|
57 | - ); |
|
58 | - return Registry::responseFactory()->redirect(AdminConfigPage::class, ['tree' => $tree->name()]); |
|
59 | - } |
|
53 | + if ($this->module === null) { |
|
54 | + FlashMessages::addMessage( |
|
55 | + I18N::translate('The attached module could not be found.'), |
|
56 | + 'danger' |
|
57 | + ); |
|
58 | + return Registry::responseFactory()->redirect(AdminConfigPage::class, ['tree' => $tree->name()]); |
|
59 | + } |
|
60 | 60 | |
61 | - $tree->setPreference( |
|
62 | - 'MAJ_CERTIF_SHOW_CERT', |
|
63 | - (string) Validator::parsedBody($request)->integer('MAJ_CERTIF_SHOW_CERT', Auth::PRIV_HIDE) |
|
64 | - ); |
|
65 | - $tree->setPreference( |
|
66 | - 'MAJ_CERTIF_SHOW_NO_WATERMARK', |
|
67 | - (string) Validator::parsedBody($request)->integer('MAJ_CERTIF_SHOW_NO_WATERMARK', Auth::PRIV_HIDE) |
|
68 | - ); |
|
69 | - $tree->setPreference( |
|
70 | - 'MAJ_CERTIF_WM_DEFAULT', |
|
71 | - Validator::parsedBody($request)->string('MAJ_CERTIF_WM_DEFAULT', '') |
|
72 | - ); |
|
61 | + $tree->setPreference( |
|
62 | + 'MAJ_CERTIF_SHOW_CERT', |
|
63 | + (string) Validator::parsedBody($request)->integer('MAJ_CERTIF_SHOW_CERT', Auth::PRIV_HIDE) |
|
64 | + ); |
|
65 | + $tree->setPreference( |
|
66 | + 'MAJ_CERTIF_SHOW_NO_WATERMARK', |
|
67 | + (string) Validator::parsedBody($request)->integer('MAJ_CERTIF_SHOW_NO_WATERMARK', Auth::PRIV_HIDE) |
|
68 | + ); |
|
69 | + $tree->setPreference( |
|
70 | + 'MAJ_CERTIF_WM_DEFAULT', |
|
71 | + Validator::parsedBody($request)->string('MAJ_CERTIF_WM_DEFAULT', '') |
|
72 | + ); |
|
73 | 73 | |
74 | - $tree->setPreference( |
|
75 | - 'MAJ_CERTIF_WM_FONT_MAXSIZE', |
|
76 | - (string) ( |
|
77 | - Validator::parsedBody($request)->isBetween(0, PHP_INT_MAX)->integer('MAJ_CERTIF_WM_FONT_MAXSIZE', 18) |
|
78 | - ) |
|
79 | - ); |
|
74 | + $tree->setPreference( |
|
75 | + 'MAJ_CERTIF_WM_FONT_MAXSIZE', |
|
76 | + (string) ( |
|
77 | + Validator::parsedBody($request)->isBetween(0, PHP_INT_MAX)->integer('MAJ_CERTIF_WM_FONT_MAXSIZE', 18) |
|
78 | + ) |
|
79 | + ); |
|
80 | 80 | |
81 | - // Only accept valid color for MAJ_WM_FONT_COLOR |
|
82 | - $watermark_color = Validator::parsedBody($request)->string('MAJ_CERTIF_WM_FONT_COLOR', ''); |
|
83 | - if (preg_match('/#([a-fA-F0-9]{3}){1,2}/', $watermark_color) === 1) { |
|
84 | - $tree->setPreference('MAJ_CERTIF_WM_FONT_COLOR', $watermark_color); |
|
85 | - } |
|
81 | + // Only accept valid color for MAJ_WM_FONT_COLOR |
|
82 | + $watermark_color = Validator::parsedBody($request)->string('MAJ_CERTIF_WM_FONT_COLOR', ''); |
|
83 | + if (preg_match('/#([a-fA-F0-9]{3}){1,2}/', $watermark_color) === 1) { |
|
84 | + $tree->setPreference('MAJ_CERTIF_WM_FONT_COLOR', $watermark_color); |
|
85 | + } |
|
86 | 86 | |
87 | - // Only accept valid folders for MAJ_CERT_ROOTDIR |
|
88 | - $cert_root_dir = Validator::parsedBody($request)->string('MAJ_CERTIF_ROOTDIR', ''); |
|
89 | - $cert_root_dir = preg_replace('/[:\/\\\\]+/', '/', $cert_root_dir) ?? ''; |
|
90 | - $cert_root_dir = trim($cert_root_dir, '/') . '/'; |
|
91 | - $tree->setPreference('MAJ_CERTIF_ROOTDIR', $cert_root_dir); |
|
87 | + // Only accept valid folders for MAJ_CERT_ROOTDIR |
|
88 | + $cert_root_dir = Validator::parsedBody($request)->string('MAJ_CERTIF_ROOTDIR', ''); |
|
89 | + $cert_root_dir = preg_replace('/[:\/\\\\]+/', '/', $cert_root_dir) ?? ''; |
|
90 | + $cert_root_dir = trim($cert_root_dir, '/') . '/'; |
|
91 | + $tree->setPreference('MAJ_CERTIF_ROOTDIR', $cert_root_dir); |
|
92 | 92 | |
93 | - FlashMessages::addMessage( |
|
94 | - I18N::translate('The preferences for the module “%s” have been updated.', $this->module->title()), |
|
95 | - 'success' |
|
96 | - ); |
|
93 | + FlashMessages::addMessage( |
|
94 | + I18N::translate('The preferences for the module “%s” have been updated.', $this->module->title()), |
|
95 | + 'success' |
|
96 | + ); |
|
97 | 97 | |
98 | - return Registry::responseFactory()->redirect(AdminConfigPage::class, ['tree' => $tree->name()]); |
|
99 | - } |
|
98 | + return Registry::responseFactory()->redirect(AdminConfigPage::class, ['tree' => $tree->name()]); |
|
99 | + } |
|
100 | 100 | } |
@@ -23,120 +23,120 @@ |
||
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 | - $encryted = sodium_crypto_secretbox($cleartext, $nonce, $key); |
|
80 | - return strtr(base64_encode($nonce . $encryted), '+/=', '._-'); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Deobfuscate a string from an URL to a clear text. |
|
85 | - * |
|
86 | - * @param string $obfuscated Text to deobfuscate |
|
87 | - * @param string $key |
|
88 | - * @throws InvalidArgumentException |
|
89 | - * @return string |
|
90 | - */ |
|
91 | - public function deobfuscate(string $obfuscated, string $key = ''): string |
|
92 | - { |
|
93 | - $obfuscated = strtr($obfuscated, '._-', '+/='); |
|
94 | - if ($key === '') { |
|
95 | - $key = $this->encryptionKey(); |
|
96 | - } |
|
97 | - |
|
98 | - if (strlen($key) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { |
|
99 | - throw new InvalidArgumentException('The key needs to be SODIUM_CRYPTO_SECRETBOX_KEYBYTES long'); |
|
100 | - } |
|
101 | - |
|
102 | - $encrypted = base64_decode($obfuscated, true); |
|
103 | - if ($encrypted === false) { |
|
104 | - throw new InvalidArgumentException('The encrypted value is not in correct base64 format.'); |
|
105 | - } |
|
106 | - |
|
107 | - if (mb_strlen($encrypted, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) { |
|
108 | - throw new InvalidArgumentException('The encrypted value does not contain enough characters for the key.'); |
|
109 | - } |
|
110 | - |
|
111 | - $nonce = mb_substr($encrypted, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit'); |
|
112 | - $ciphertext = mb_substr($encrypted, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit'); |
|
113 | - |
|
114 | - $decrypted = sodium_crypto_secretbox_open($ciphertext, $nonce, $key); |
|
115 | - |
|
116 | - if ($decrypted === false) { |
|
117 | - throw new InvalidArgumentException('The message has been tampered with in transit.'); |
|
118 | - } |
|
119 | - |
|
120 | - //sodium_memzero($ciphertext); // sodium_compat cannot handle it, only through libsodium |
|
121 | - |
|
122 | - /** @var string $decrypted - Psalm detect as string|true otherwise */ |
|
123 | - return $decrypted; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Try to deobfuscate a string from an URL to a clear text, returning whether the operation is a success. |
|
128 | - * |
|
129 | - * @param string $obfuscated Text to deobfuscate |
|
130 | - * @param string $key |
|
131 | - * @return bool |
|
132 | - */ |
|
133 | - public function tryDeobfuscate(string &$obfuscated, string $key = ''): bool |
|
134 | - { |
|
135 | - try { |
|
136 | - $obfuscated = $this->deobfuscate($obfuscated, $key); |
|
137 | - return true; |
|
138 | - } catch (InvalidArgumentException $ex) { |
|
139 | - } |
|
140 | - return false; |
|
141 | - } |
|
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 | + $encryted = sodium_crypto_secretbox($cleartext, $nonce, $key); |
|
80 | + return strtr(base64_encode($nonce . $encryted), '+/=', '._-'); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Deobfuscate a string from an URL to a clear text. |
|
85 | + * |
|
86 | + * @param string $obfuscated Text to deobfuscate |
|
87 | + * @param string $key |
|
88 | + * @throws InvalidArgumentException |
|
89 | + * @return string |
|
90 | + */ |
|
91 | + public function deobfuscate(string $obfuscated, string $key = ''): string |
|
92 | + { |
|
93 | + $obfuscated = strtr($obfuscated, '._-', '+/='); |
|
94 | + if ($key === '') { |
|
95 | + $key = $this->encryptionKey(); |
|
96 | + } |
|
97 | + |
|
98 | + if (strlen($key) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) { |
|
99 | + throw new InvalidArgumentException('The key needs to be SODIUM_CRYPTO_SECRETBOX_KEYBYTES long'); |
|
100 | + } |
|
101 | + |
|
102 | + $encrypted = base64_decode($obfuscated, true); |
|
103 | + if ($encrypted === false) { |
|
104 | + throw new InvalidArgumentException('The encrypted value is not in correct base64 format.'); |
|
105 | + } |
|
106 | + |
|
107 | + if (mb_strlen($encrypted, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) { |
|
108 | + throw new InvalidArgumentException('The encrypted value does not contain enough characters for the key.'); |
|
109 | + } |
|
110 | + |
|
111 | + $nonce = mb_substr($encrypted, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit'); |
|
112 | + $ciphertext = mb_substr($encrypted, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit'); |
|
113 | + |
|
114 | + $decrypted = sodium_crypto_secretbox_open($ciphertext, $nonce, $key); |
|
115 | + |
|
116 | + if ($decrypted === false) { |
|
117 | + throw new InvalidArgumentException('The message has been tampered with in transit.'); |
|
118 | + } |
|
119 | + |
|
120 | + //sodium_memzero($ciphertext); // sodium_compat cannot handle it, only through libsodium |
|
121 | + |
|
122 | + /** @var string $decrypted - Psalm detect as string|true otherwise */ |
|
123 | + return $decrypted; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Try to deobfuscate a string from an URL to a clear text, returning whether the operation is a success. |
|
128 | + * |
|
129 | + * @param string $obfuscated Text to deobfuscate |
|
130 | + * @param string $key |
|
131 | + * @return bool |
|
132 | + */ |
|
133 | + public function tryDeobfuscate(string &$obfuscated, string $key = ''): bool |
|
134 | + { |
|
135 | + try { |
|
136 | + $obfuscated = $this->deobfuscate($obfuscated, $key); |
|
137 | + return true; |
|
138 | + } catch (InvalidArgumentException $ex) { |
|
139 | + } |
|
140 | + return false; |
|
141 | + } |
|
142 | 142 | } |
@@ -43,8 +43,7 @@ discard block |
||
43 | 43 | $server_software = $request->getServerParams()['SERVER_SOFTWARE'] ?? ''; |
44 | 44 | $this->encryption_key = str_pad(md5( |
45 | 45 | $server_name !== '' && $server_software !== '' ? |
46 | - $server_name . $server_software : |
|
47 | - 'STANDARDKEYIFNOSERVER' |
|
46 | + $server_name.$server_software : 'STANDARDKEYIFNOSERVER' |
|
48 | 47 | ), SODIUM_CRYPTO_SECRETBOX_KEYBYTES, "1234567890ABCDEF"); |
49 | 48 | } |
50 | 49 | return $this->encryption_key; |
@@ -77,7 +76,7 @@ discard block |
||
77 | 76 | } |
78 | 77 | |
79 | 78 | $encryted = sodium_crypto_secretbox($cleartext, $nonce, $key); |
80 | - return strtr(base64_encode($nonce . $encryted), '+/=', '._-'); |
|
79 | + return strtr(base64_encode($nonce.$encryted), '+/=', '._-'); |
|
81 | 80 | } |
82 | 81 | |
83 | 82 | /** |
@@ -29,126 +29,126 @@ |
||
29 | 29 | */ |
30 | 30 | class SourceStatusService |
31 | 31 | { |
32 | - /** |
|
33 | - * Maximum timespan between the date of a source and the date of the event to consider the source precise. |
|
34 | - * Arbitrally set to approximately a year around the event date. |
|
35 | - * |
|
36 | - * @var int DATE_PRECISION_MARGIN |
|
37 | - */ |
|
38 | - private const DATE_PRECISION_MARGIN = 180; |
|
39 | - |
|
40 | - /** |
|
41 | - * Return the status of source citations for a fact. |
|
42 | - * |
|
43 | - * @param Fact $fact |
|
44 | - * @return FactSourceStatus |
|
45 | - */ |
|
46 | - public function sourceStatusForFact(Fact $fact): FactSourceStatus |
|
47 | - { |
|
48 | - $source_status = new FactSourceStatus(); |
|
49 | - |
|
50 | - $date = $fact->date(); |
|
51 | - $source_status |
|
52 | - ->setFactHasDate($date->isOK()) |
|
53 | - ->setFactHasPreciseDate($date->qual1 === '' && $date->minimumJulianDay() === $date->maximumJulianDay()); |
|
54 | - |
|
55 | - foreach ($fact->getCitations() as $citation) { |
|
56 | - $source_status |
|
57 | - ->setHasSource(true) |
|
58 | - ->addHasSupportingDocument(preg_match('/\n3 _ACT (?:.*)/', $citation) === 1); |
|
59 | - |
|
60 | - preg_match_all("/\n3 DATA(?:\n[4-9] .*)*\n4 DATE (.*)/", $citation, $date_matches, PREG_SET_ORDER); |
|
61 | - foreach ($date_matches as $date_match) { |
|
62 | - $source_date = new Date($date_match[1]); |
|
63 | - $source_status |
|
64 | - ->addSourceHasDate($source_date->isOK()) |
|
65 | - ->addSourceMatchesFactDate($date->isOK() && $source_date->isOK() |
|
66 | - && abs($source_date->julianDay() - $date->julianDay()) < self::DATE_PRECISION_MARGIN); |
|
67 | - } |
|
68 | - |
|
69 | - if ($source_status->isFullySourced()) { |
|
70 | - return $source_status; |
|
71 | - } |
|
72 | - } |
|
73 | - |
|
74 | - return $source_status; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Return the status of sources for a Gedcom record. |
|
79 | - * |
|
80 | - * @param GedcomRecord $record |
|
81 | - * @return SourceStatus |
|
82 | - */ |
|
83 | - public function sourceStatusForRecord(GedcomRecord $record): SourceStatus |
|
84 | - { |
|
85 | - $source_status = new SourceStatus(); |
|
86 | - |
|
87 | - foreach ($record->facts(['SOUR']) as $source) { |
|
88 | - $source_status |
|
89 | - ->setHasSource(true) |
|
90 | - ->addHasSupportingDocument($source->attribute('_ACT') !== ''); |
|
91 | - |
|
92 | - if ($source_status->isFullySourced()) { |
|
93 | - return $source_status; |
|
94 | - } |
|
95 | - } |
|
96 | - |
|
97 | - return $source_status; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Return the status of source citations for a list of fact types associated with a record. |
|
102 | - * |
|
103 | - * @param GedcomRecord $record |
|
104 | - * @param string[] $tags |
|
105 | - * @return FactSourceStatus |
|
106 | - */ |
|
107 | - public function sourceStatusForFactsWithTags(GedcomRecord $record, array $tags): FactSourceStatus |
|
108 | - { |
|
109 | - $source_status = new NullFactSourceStatus(); |
|
110 | - |
|
111 | - foreach ($record->facts($tags) as $fact) { |
|
112 | - $source_status = $source_status->combineWith($this->sourceStatusForFact($fact)); |
|
113 | - if ($source_status->isFullySourced()) { |
|
114 | - return $source_status; |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - return $source_status; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Return the status of source citations for an individual's birth events. |
|
123 | - * |
|
124 | - * @param Individual $individual |
|
125 | - * @return FactSourceStatus |
|
126 | - */ |
|
127 | - public function sourceStatusForBirth(Individual $individual): FactSourceStatus |
|
128 | - { |
|
129 | - return $this->sourceStatusForFactsWithTags($individual, Gedcom::BIRTH_EVENTS); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Return the status of source citations for an individual's death events. |
|
134 | - * |
|
135 | - * @param Individual $individual |
|
136 | - * @return FactSourceStatus |
|
137 | - */ |
|
138 | - public function sourceStatusForDeath(Individual $individual): FactSourceStatus |
|
139 | - { |
|
140 | - return $this->sourceStatusForFactsWithTags($individual, Gedcom::DEATH_EVENTS); |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Return the status of source citations for a family's marriage events. |
|
145 | - * |
|
146 | - * @param Family $family |
|
147 | - * @return FactSourceStatus |
|
148 | - */ |
|
149 | - public function sourceStatusForMarriage(Family $family): FactSourceStatus |
|
150 | - { |
|
151 | - $marr_events = [...Gedcom::MARRIAGE_EVENTS, 'MARC', 'MARL', 'MARS']; |
|
152 | - return $this->sourceStatusForFactsWithTags($family, $marr_events); |
|
153 | - } |
|
32 | + /** |
|
33 | + * Maximum timespan between the date of a source and the date of the event to consider the source precise. |
|
34 | + * Arbitrally set to approximately a year around the event date. |
|
35 | + * |
|
36 | + * @var int DATE_PRECISION_MARGIN |
|
37 | + */ |
|
38 | + private const DATE_PRECISION_MARGIN = 180; |
|
39 | + |
|
40 | + /** |
|
41 | + * Return the status of source citations for a fact. |
|
42 | + * |
|
43 | + * @param Fact $fact |
|
44 | + * @return FactSourceStatus |
|
45 | + */ |
|
46 | + public function sourceStatusForFact(Fact $fact): FactSourceStatus |
|
47 | + { |
|
48 | + $source_status = new FactSourceStatus(); |
|
49 | + |
|
50 | + $date = $fact->date(); |
|
51 | + $source_status |
|
52 | + ->setFactHasDate($date->isOK()) |
|
53 | + ->setFactHasPreciseDate($date->qual1 === '' && $date->minimumJulianDay() === $date->maximumJulianDay()); |
|
54 | + |
|
55 | + foreach ($fact->getCitations() as $citation) { |
|
56 | + $source_status |
|
57 | + ->setHasSource(true) |
|
58 | + ->addHasSupportingDocument(preg_match('/\n3 _ACT (?:.*)/', $citation) === 1); |
|
59 | + |
|
60 | + preg_match_all("/\n3 DATA(?:\n[4-9] .*)*\n4 DATE (.*)/", $citation, $date_matches, PREG_SET_ORDER); |
|
61 | + foreach ($date_matches as $date_match) { |
|
62 | + $source_date = new Date($date_match[1]); |
|
63 | + $source_status |
|
64 | + ->addSourceHasDate($source_date->isOK()) |
|
65 | + ->addSourceMatchesFactDate($date->isOK() && $source_date->isOK() |
|
66 | + && abs($source_date->julianDay() - $date->julianDay()) < self::DATE_PRECISION_MARGIN); |
|
67 | + } |
|
68 | + |
|
69 | + if ($source_status->isFullySourced()) { |
|
70 | + return $source_status; |
|
71 | + } |
|
72 | + } |
|
73 | + |
|
74 | + return $source_status; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Return the status of sources for a Gedcom record. |
|
79 | + * |
|
80 | + * @param GedcomRecord $record |
|
81 | + * @return SourceStatus |
|
82 | + */ |
|
83 | + public function sourceStatusForRecord(GedcomRecord $record): SourceStatus |
|
84 | + { |
|
85 | + $source_status = new SourceStatus(); |
|
86 | + |
|
87 | + foreach ($record->facts(['SOUR']) as $source) { |
|
88 | + $source_status |
|
89 | + ->setHasSource(true) |
|
90 | + ->addHasSupportingDocument($source->attribute('_ACT') !== ''); |
|
91 | + |
|
92 | + if ($source_status->isFullySourced()) { |
|
93 | + return $source_status; |
|
94 | + } |
|
95 | + } |
|
96 | + |
|
97 | + return $source_status; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Return the status of source citations for a list of fact types associated with a record. |
|
102 | + * |
|
103 | + * @param GedcomRecord $record |
|
104 | + * @param string[] $tags |
|
105 | + * @return FactSourceStatus |
|
106 | + */ |
|
107 | + public function sourceStatusForFactsWithTags(GedcomRecord $record, array $tags): FactSourceStatus |
|
108 | + { |
|
109 | + $source_status = new NullFactSourceStatus(); |
|
110 | + |
|
111 | + foreach ($record->facts($tags) as $fact) { |
|
112 | + $source_status = $source_status->combineWith($this->sourceStatusForFact($fact)); |
|
113 | + if ($source_status->isFullySourced()) { |
|
114 | + return $source_status; |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + return $source_status; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Return the status of source citations for an individual's birth events. |
|
123 | + * |
|
124 | + * @param Individual $individual |
|
125 | + * @return FactSourceStatus |
|
126 | + */ |
|
127 | + public function sourceStatusForBirth(Individual $individual): FactSourceStatus |
|
128 | + { |
|
129 | + return $this->sourceStatusForFactsWithTags($individual, Gedcom::BIRTH_EVENTS); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Return the status of source citations for an individual's death events. |
|
134 | + * |
|
135 | + * @param Individual $individual |
|
136 | + * @return FactSourceStatus |
|
137 | + */ |
|
138 | + public function sourceStatusForDeath(Individual $individual): FactSourceStatus |
|
139 | + { |
|
140 | + return $this->sourceStatusForFactsWithTags($individual, Gedcom::DEATH_EVENTS); |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Return the status of source citations for a family's marriage events. |
|
145 | + * |
|
146 | + * @param Family $family |
|
147 | + * @return FactSourceStatus |
|
148 | + */ |
|
149 | + public function sourceStatusForMarriage(Family $family): FactSourceStatus |
|
150 | + { |
|
151 | + $marr_events = [...Gedcom::MARRIAGE_EVENTS, 'MARC', 'MARL', 'MARS']; |
|
152 | + return $this->sourceStatusForFactsWithTags($family, $marr_events); |
|
153 | + } |
|
154 | 154 | } |