forked from PhotoboothProject/photobooth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationService.php
More file actions
342 lines (284 loc) · 13.3 KB
/
ConfigurationService.php
File metadata and controls
342 lines (284 loc) · 13.3 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
namespace Photobooth\Service;
use Photobooth\Config\Loader\PhpArrayLoader;
use Photobooth\Configuration\PhotoboothConfiguration;
use Photobooth\Environment;
use Photobooth\Helper;
use Photobooth\Utility\ArrayUtility;
use Photobooth\Utility\PathUtility;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\Loader\DelegatingLoader;
class ConfigurationService
{
protected array $defaultConfiguration;
protected array $configuration;
protected array $processedConfiguration;
public function __construct()
{
$this->load();
}
public function load(): void
{
$fileLocator = new FileLocator(PathUtility::getAbsolutePath('config'));
$loaderResolver = new LoaderResolver([
new PhpArrayLoader($fileLocator),
]);
$loader = new DelegatingLoader($loaderResolver);
// default configuration
$this->defaultConfiguration = (new Processor())->processConfiguration(new PhotoboothConfiguration(), [[]]);
// configuration
$userConfiguration = [];
if (file_exists(PathUtility::getAbsolutePath('config/my.config.inc.php'))) {
$userConfiguration = $loader->load('my.config.inc.php', 'php_array');
$userConfiguration = $this->processMigration($userConfiguration);
}
$configuration = (new Processor())->processConfiguration(new PhotoboothConfiguration(), [$userConfiguration]);
$configuration = $this->addDefaults($configuration);
$this->configuration = $configuration;
}
public function update(array $data): void
{
$data = (new Processor())->processConfiguration(new PhotoboothConfiguration(), [$data]);
$content = "<?php\n\nreturn " . ArrayUtility::export(ArrayUtility::diffRecursive($data, $this->defaultConfiguration)) . ";\n";
$userConfigurationFile = PathUtility::getAbsolutePath('config/my.config.inc.php');
if (file_put_contents($userConfigurationFile, $content)) {
Helper::clearCache($userConfigurationFile);
return;
}
throw new \RuntimeException('Config can not be saved!');
}
protected function addDefaults(array $config): array
{
$default_font = 'resources/fonts/GreatVibes-Regular.ttf';
$default_frame = 'resources/img/frames/frame.png';
$random_frame = 'api/randomImg.php?dir=demoframes';
if (empty($config['picture']['frame'])) {
$config['picture']['frame'] = $random_frame;
}
if (empty($config['textonpicture']['font'])) {
$config['textonpicture']['font'] = $default_font;
}
if (empty($config['collage']['frame'])) {
$config['collage']['frame'] = $default_frame;
}
if (empty($config['collage']['placeholderpath'])) {
$config['collage']['placeholderpath'] = 'resources/img/background/01.jpg';
}
if (empty($config['textoncollage']['font'])) {
$config['textoncollage']['font'] = $default_font;
}
if (empty($config['print']['frame'])) {
$config['print']['frame'] = $default_frame;
}
if (empty($config['textonprint']['font'])) {
$config['textonprint']['font'] = $default_font;
}
if (empty($config['collage']['limit'])) {
$config['collage']['limit'] = 4;
}
$bg_path = 'resources/img/background.png';
$logo_url = 'resources/img/logo/logo-qrcode-text.png';
if (empty($config['logo']['path'])) {
$config['logo']['path'] = $logo_url;
}
if (empty($config['background']['defaults'])) {
$config['background']['defaults'] = $bg_path;
}
if (empty($config['background']['admin'])) {
$config['background']['admin'] = $bg_path;
}
if (empty($config['background']['chroma'])) {
$config['background']['chroma'] = $bg_path;
}
if (empty($config['remotebuzzer']['serverip'])) {
$config['remotebuzzer']['serverip'] = Environment::getIp();
}
if (empty($config['qr']['url'])) {
$config['qr']['url'] = 'view.php?image=';
}
return $config;
}
protected function processMigration(array $config): array
{
// Normalize legacy paths that may contain absolute URLs or subfolder prefixes (e.g. /photobooth/)
$baseUrl = PathUtility::getBaseUrl();
$hostBase = '';
if (isset($_SERVER['HTTP_HOST'])) {
$scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ? 'https' : 'http';
$hostBase = $scheme . '://' . $_SERVER['HTTP_HOST'] . $baseUrl;
}
$normalizePath = static function (?string $path) use ($baseUrl, $hostBase): ?string {
if ($path === null || $path === '') {
return $path;
}
$value = (string)$path;
// Strip surrounding url("...") / url('...') / url(...)
if (substr($value, 0, 4) === 'url(' && substr($value, -1) === ')') {
$value = trim(substr($value, 4, -1), '"\'');
}
// Strip project root based absolute paths
try {
$rootPath = PathUtility::getRootPath();
if (str_starts_with($value, $rootPath)) {
$value = substr($value, strlen($rootPath));
}
} catch (\InvalidArgumentException) {
// ignore if root path can't be resolved in this context
}
// Remove full host+base prefix, e.g. "https://host/photobooth/"
if ($hostBase !== '' && str_starts_with($value, $hostBase)) {
$value = substr($value, strlen($hostBase));
}
// Remove base URL prefix, e.g. "/photobooth/"
if ($baseUrl !== '' && str_starts_with($value, $baseUrl)) {
$value = substr($value, strlen($baseUrl));
}
// If path still contains a known project-relative marker, strip everything before it
foreach (['/private/', '/resources/'] as $marker) {
$pos = strpos($value, $marker);
if ($pos !== false) {
$value = substr($value, $pos);
break;
}
}
return $value;
};
// Migrate Commands
$commands = [
'take_picture',
'take_custom',
'take_video',
'print',
'exiftool',
'preview',
'nodebin',
'pre_photo',
'post_photo',
'reboot',
'shutdown',
];
foreach ($commands as $command) {
if (isset($config[$command]['cmd'])) {
$config['commands'][$command] = $config[$command]['cmd'];
unset($config[$command]['cmd']);
if (count($config[$command]) === 0) {
unset($config[$command]);
}
}
}
if (isset($config['preview']['killcmd']) && trim($config['preview']['killcmd']) !== '') {
$config['commands']['preview_kill'] = trim($config['preview']['killcmd']);
}
// Migrate Preview Mode
if (isset($config['preview']['mode']) && $config['preview']['mode'] === 'gphoto') {
$config['preview']['mode'] = 'device_cam';
}
// Migrate Preview URL, remove surrounding url("...")
if (isset($config['preview']['url']) && substr($config['preview']['url'], 0, 4) === 'url(' && substr($config['preview']['url'], -1) === ')') {
$config['preview']['url'] = trim(substr($config['preview']['url'], 4, -1), '"\'');
}
// Migrate screensaver switch interval from minutes to seconds
if (isset($config['screensaver'])) {
if (isset($config['screensaver']['switch_minutes']) && !isset($config['screensaver']['switch_seconds'])) {
$config['screensaver']['switch_seconds'] = (int)$config['screensaver']['switch_minutes'] * 60;
}
unset($config['screensaver']['switch_minutes']);
}
// Migrate button font color from old colors config
if (!empty($config['colors']['button_font']) && empty($config['fonts']['button_font_color'])) {
$config['fonts']['button_font_color'] = $config['colors']['button_font'];
}
// Migrate countdown color from colors to fonts section
if (!empty($config['colors']['countdown']) && empty($config['fonts']['countdown_text_color'])) {
$config['fonts']['countdown_text_color'] = $config['colors']['countdown'];
}
// Migrate start font color to new font slots if empty
if (!empty($config['colors']['start_font'])) {
if (empty($config['fonts']['start_screen_title_color'])) {
$config['fonts']['start_screen_title_color'] = $config['colors']['start_font'];
}
if (empty($config['fonts']['event_text_color'])) {
$config['fonts']['event_text_color'] = $config['colors']['start_font'];
}
}
// Migrate general font color to default font color
if (!empty($config['colors']['font']) && empty($config['fonts']['default_color'])) {
$config['fonts']['default_color'] = $config['colors']['font'];
}
// Migrate Background URLs
if (isset($config['background']) && is_array($config['background'])) {
$baseUrl = PathUtility::getBaseUrl();
foreach (['defaults', 'admin', 'chroma'] as $backgroundKey) {
if (!isset($config['background'][$backgroundKey]) || $config['background'][$backgroundKey] === '') {
continue;
}
$value = (string)$config['background'][$backgroundKey];
// Strip surrounding url("...") / url('...') / url(...)
if (substr($value, 0, 4) === 'url(' && substr($value, -1) === ')') {
$value = trim(substr($value, 4, -1), '"\'');
}
// Strip document root based absolute paths
if (isset($_SERVER['DOCUMENT_ROOT']) && str_starts_with($value, $_SERVER['DOCUMENT_ROOT'])) {
$value = substr($value, strlen($_SERVER['DOCUMENT_ROOT']));
}
// Strip leading base URL so only a relative path is stored
if ($baseUrl !== '' && str_starts_with($value, $baseUrl)) {
$value = substr($value, strlen($baseUrl));
}
// Normalize leading slash
$value = ltrim($value, '/');
$config['background'][$backgroundKey] = $value;
}
}
// Normalize various media and font paths to be project-relative
$config['logo']['path'] = $normalizePath($config['logo']['path'] ?? null);
$config['ui']['shutter_cheese_img'] = $normalizePath($config['ui']['shutter_cheese_img'] ?? null);
$config['picture']['frame'] = $normalizePath($config['picture']['frame'] ?? null);
$config['collage']['frame'] = $normalizePath($config['collage']['frame'] ?? null);
$config['collage']['placeholderpath'] = $normalizePath($config['collage']['placeholderpath'] ?? null);
$config['background']['defaults'] = $normalizePath($config['background']['defaults'] ?? null);
$config['background']['admin'] = $normalizePath($config['background']['admin'] ?? null);
$config['background']['chroma'] = $normalizePath($config['background']['chroma'] ?? null);
$config['textonpicture']['font'] = $normalizePath($config['textonpicture']['font'] ?? null);
$config['textoncollage']['font'] = $normalizePath($config['textoncollage']['font'] ?? null);
$config['textonprint']['font'] = $normalizePath($config['textonprint']['font'] ?? null);
$config['print']['frame'] = $normalizePath($config['print']['frame'] ?? null);
// Hash legacy plain-text login pins
$hashPinIfNeeded = static function (?string $pin): ?string {
if ($pin === null || $pin === '') {
return $pin;
}
$info = password_get_info($pin);
if (($info['algo'] ?? 0) !== 0) {
return $pin;
}
return password_hash($pin, PASSWORD_DEFAULT);
};
if (array_key_exists('login', $config)) {
if (array_key_exists('pin', $config['login'])) {
$config['login']['pin'] = $hashPinIfNeeded($config['login']['pin']);
}
if (array_key_exists('rental_pin', $config['login'])) {
$config['login']['rental_pin'] = $hashPinIfNeeded($config['login']['rental_pin']);
}
}
return $config;
}
public function getDefaultConfiguration(): array
{
return $this->defaultConfiguration;
}
public function getConfiguration(): array
{
return $this->configuration;
}
public static function getInstance(): self
{
if (!isset($GLOBALS[self::class])) {
$GLOBALS[self::class] = new self();
}
return $GLOBALS[self::class];
}
}