-
Notifications
You must be signed in to change notification settings - Fork 25
/
LayoutService.php
210 lines (200 loc) · 7.26 KB
/
LayoutService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
declare(strict_types=1);
namespace In2code\Luxletter\Domain\Service;
use In2code\Luxletter\Domain\Repository\LanguageRepository;
use In2code\Luxletter\Exception\MisconfigurationException;
use In2code\Luxletter\Exception\RecordInDatabaseNotFoundException;
use In2code\Luxletter\Exception\UnvalidFilenameException;
use In2code\Luxletter\Utility\ConfigurationUtility;
use In2code\Luxletter\Utility\ObjectUtility;
use In2code\Luxletter\Utility\StringUtility;
use Throwable;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException;
class LayoutService
{
/**
* Get layouts to fill a select field in new form
*
* Example return values:
* [
* 'File1.html' => 'label',
* 'File2.html' => 'label2',
* ]
*
* @return array
* @throws InvalidConfigurationTypeException
*/
public function getLayouts(): array
{
$containers = $this->getLayoutConfiguration();
$layouts = [];
foreach ($containers as $container) {
if (!empty($container['label']) && !empty($container['fileName'])) {
$label = $container['label'];
if (StringUtility::startsWith($label, 'LLL:')) {
$label = ObjectUtility::getLanguageService()->sL($label);
}
$layouts[$container['fileName']] = $label;
}
}
return $layouts;
}
/**
* @param string $layout
* @param int $language
* @param string $origin
* @return string Relative path and filename like "EXT:sitepackage/../MailContainer.html"
* @throws InvalidConfigurationTypeException
* @throws MisconfigurationException
* @throws RecordInDatabaseNotFoundException
* @throws UnvalidFilenameException
*/
public function getPathAndFilenameFromLayout(string $layout, int $language, string $origin): string
{
$this->checkForValidFilename($layout);
$pathAndFilename = '';
if ($language > 0) {
$pathAndFilename = $this->getPathAndFilenameFromLayoutForSpecificLanguage($layout, $language, $origin);
}
if ($pathAndFilename === '') {
$pathAndFilename = $this->getPathAndFilenameFromLayoutForDefaultLanguage($layout);
}
return $pathAndFilename;
}
/**
* @param string $layout
* @return string
* @throws InvalidConfigurationTypeException
* @throws MisconfigurationException
*/
protected function getPathAndFilenameFromLayoutForDefaultLanguage(string $layout): string
{
$filename = $this->getLayoutPath() . $layout . '.html';
if (is_file(GeneralUtility::getFileAbsFileName($filename)) === false) {
throw new MisconfigurationException(
'Could not read template file with given path and filename',
1635495052
);
}
return $filename;
}
/**
* @param string $layout
* @param int $language
* @param string $origin
* @return string
* @throws InvalidConfigurationTypeException
* @throws MisconfigurationException
* @throws RecordInDatabaseNotFoundException
*/
protected function getPathAndFilenameFromLayoutForSpecificLanguage(
string $layout,
int $language,
string $origin
): string {
$languageRepository = GeneralUtility::makeInstance(LanguageRepository::class);
try {
$isocode = $languageRepository->getIsocodeFromIdentifier($language, $origin);
} catch (Throwable $exception) {
throw new RecordInDatabaseNotFoundException(
'No isocode found found in table sys_language for language with uid ' . $language,
1646250413
);
}
$filename = $this->getLayoutPath() . $layout . '_' . $isocode . '.html';
if (is_file(GeneralUtility::getFileAbsFileName($filename))) {
return $filename;
}
return '';
}
/**
* Check if given filename
* - Has no slashes (to prevent ../../anything.html)
* - Ends not with a ".html"
* - And is allowed by TypoScript configuration
*
* @param string $filename
* @return void
* @throws UnvalidFilenameException
* @throws InvalidConfigurationTypeException
*/
protected function checkForValidFilename(string $filename): void
{
if (stristr($filename, '/') !== false) {
throw new UnvalidFilenameException(
'Given filename (' . htmlspecialchars($filename) . ') contains invalid characters',
1635497109
);
}
if (StringUtility::endsWith($filename, '.html') === true) {
throw new UnvalidFilenameException(
'Given filename (' . htmlspecialchars($filename) . ') must NOT end with .html',
1646381056
);
}
$containers = $this->getLayoutConfiguration();
foreach ($containers as $container) {
if ($filename === $container['fileName']) {
return;
}
}
throw new UnvalidFilenameException(
'Given filename (' . htmlspecialchars($filename) . ') is invalid',
1635492796
);
}
/**
* Get layout configuration from TypoScript setup
* plugin {
* tx_luxletter {
* settings {
* containerHtml {
* path = EXT:luxletter/Resources/Private/Templates/Mail/
* options {
* 1 {
* # "NewsletterContainer" means:
* # "NewsletterContainer.html" in default language or
* # "NewsletterContainer_de.html" in german language and so on...
* fileName = NewsletterContainer
* label = LLL:EXT:path/locallang_db.xlf:newsletter.layouts.1
* }
* 2 {
* fileName = NewsletterContainer2
* label = C2
* }
* }
* }
* }
* }
* }
*
* @return array
* @throws InvalidConfigurationTypeException
*/
protected function getLayoutConfiguration(): array
{
$configuration = ConfigurationUtility::getExtensionSettings();
if (!empty($configuration['settings']['containerHtml']['options'])) {
return $configuration['settings']['containerHtml']['options'];
}
return [];
}
/**
* @return string
* @throws InvalidConfigurationTypeException
* @throws MisconfigurationException
*/
protected function getLayoutPath(): string
{
$configuration = ConfigurationUtility::getExtensionSettings();
if (!empty($configuration['settings']['containerHtml']['path'])) {
$path = $configuration['settings']['containerHtml']['path'];
if (StringUtility::endsWith($path, '/') === false) {
$path .= '/';
}
return $path;
}
throw new MisconfigurationException('No template path given in settings.containerHtml.path', 1635494884);
}
}