Config.php 21.6 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 3/6/17
 * Time: 8:47 PM
 */

Marc Egger's avatar
Marc Egger committed
9
namespace IMATHUZH\Qfq\Core\Store;
10

11
use IMATHUZH\Qfq\Core\Exception\Thrower;
12
use IMATHUZH\Qfq\Core\Helper\HelperFile;
Marc Egger's avatar
Marc Egger committed
13
use IMATHUZH\Qfq\Core\Helper\Logger;
14
use IMATHUZH\Qfq\Core\Helper\OnArray;
15
use IMATHUZH\Qfq\Core\Helper\Path;
Marc Egger's avatar
Marc Egger committed
16
17
use IMATHUZH\Qfq\Core\Helper\Support;
use IMATHUZH\Qfq\Core\Helper\OnString;
18

19
20
21
22
/**
 * Class Config
 * @package qfq
 */
23
24
class Config {

25
26
    private static $config = null;

Marc Egger's avatar
Marc Egger committed
27
28
29
30
31
32
    const CONFIG_REQUIRED_TEMPLATE = [
        SYSTEM_DB_1_USER => null,
        SYSTEM_DB_1_PASSWORD => null,
        SYSTEM_DB_1_SERVER => "localhost",
        SYSTEM_DB_1_NAME=> null,
    ];
33

34
35
36
37
38
    /**
     * Get config value with given key. Throws exception if config has not been read.
     *
     * @param string $key
     * @return mixed
39
     * @throws \CodeException
40
     * @throws \UserFormException
41
     * @throws \UserReportException
42
43
44
     */
    public static function get(string $key)
    {
45
        self::readConfig();
46
47
48
49
50
51
        return self::$config[$key];
    }

    /**
     * Returns a copy of the config array. Throws exception if config has not been read.
     *
52
     * @param string $PhpUnitOverloadCwdToConfigFile
53
     * @return array
54
     * @throws \CodeException
55
     * @throws \UserFormException
56
     * @throws \UserReportException
57
     */
58
    public static function getConfigArray($PhpUnitOverloadCwdToConfigFile = ''): array
59
    {
60
        self::readConfig($PhpUnitOverloadCwdToConfigFile);
61
62
63
64
        return self::$config;
    }

    /**
Marc Egger's avatar
Marc Egger committed
65
66
     * Read qfq.json (merge with Typo3-qfq config if exists).
     * Note: Deprecated config file typo3conf/config.qfq.php is translated to JSON in PATH:cwdToProject(..)
67
68
69
70
71
72
     *
     * @param string $PhpUnitOverloadCwdToConfigFile
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
     */
73
    private static function readConfig($PhpUnitOverloadCwdToConfigFile = '') {
74
75
76
77
78
79
80
81
82

        if (self::$config !== null && $PhpUnitOverloadCwdToConfigFile === '') {
            return;
        }

        // read and parse config. Throw exception if not exists.
        $cwdToConfigFile = $PhpUnitOverloadCwdToConfigFile === '' ? Path::cwdToProject(CONFIG_QFQ_JSON) : $PhpUnitOverloadCwdToConfigFile;
        if (!file_exists($cwdToConfigFile)) {
            HelperFile::file_put_contents(Path::cwdToProject(CONFIG_QFQ_JSON_EXAMPLE), json_encode(self::CONFIG_REQUIRED_TEMPLATE, JSON_PRETTY_PRINT));
Marc Egger's avatar
Marc Egger committed
83
            Thrower::userFormException("Please create qfq config file '" . CONFIG_QFQ_JSON . "' in project directory. Example config file '" . CONFIG_QFQ_JSON_EXAMPLE . "' was created in project directory.", "Project directory: " . realpath(Path::cwdToProject()));
84
85
86
87
88
89
90
91
92
93
94
        }
        $config = HelperFile::json_decode(HelperFile::file_get_contents($cwdToConfigFile));

        // check required keys
        foreach (self::CONFIG_REQUIRED_TEMPLATE as $key => $value) {
            if (!array_key_exists($key, $config) || is_null($config[$key]) || $config[$key] === '') {
                Thrower::userFormException("Required key '$key' missing in config file " . CONFIG_QFQ_JSON,  "Config file: $cwdToConfigFile");
            }
        }

        if ($PhpUnitOverloadCwdToConfigFile === '') {
Marc Egger's avatar
Marc Egger committed
95
            $configT3qfq = self::readTypo3QfqConfig();
96
97
98
99
100
101
102
103
104
105
106
107
108

            // Settings in qfq.json overwrite T3 settings
            $config = array_merge($configT3qfq, $config);
        }

        $config = self::renameConfigElements($config);
        $config = self::setDefaults($config);
        self::checkDeprecated($config);
        self::checkForAttack($config);

        // Copy values to detect custom settings later
        $config[F_FE_DATA_PATTERN_ERROR_SYSTEM] = $config[F_FE_DATA_PATTERN_ERROR];

109
110
111
112
113
        $config = self::adjustConfig($config);
        $config = self::setAutoConfigValue($config);

        self::checkMandatoryParameter($config);

114
115
116
117
118
119
        self::$config = $config;

        // Set log paths
        Path::overrideLogPathsFromConfig();
    }

120
121
122
123
124
    /**
     * Iterates over all 30 custom vars, explode them to split between key and value, append to $config.
     *
     * @param array $config
     * @return array
Marc Egger's avatar
Marc Egger committed
125
     * @throws \UserReportException
126
     */
127
    private static function getCustomVariable(array $config) {
128
129
130
131
132
133

        for ($i = 1; $i <= 30; $i++) {
            if (isset($config['custom' . $i])) {
                $arr = explode('=', $config['custom' . $i], 2);
                if (!empty($arr[0]) && !empty($arr[1])) {

134
                    $arr[0] = trim($arr[0]);
135
                    $arr[1] = OnString::trimQuote(trim($arr[1]));
136
137

                    if (isset($config[$arr[0]])) {
Marc Egger's avatar
Marc Egger committed
138
                        throw new \UserReportException("Variable '$arr[0]' already defined", ERROR_INVALID_OR_MISSING_PARAMETER);
139
140
141
142
143
144
145
146
147
148
                    }

                    $config[$arr[0]] = $arr[1];
                }
            }
        }

        return $config;
    }

149
    /**
150
     * Overwrite the qfq config file with data from given array.
151
152
153
     *
     * @param array $config
     * @throws \UserFormException
154
     * @throws \CodeException
155
     */
156
    private static function writeConfig(array $config)
157
158
159
160
161
162
    {
        $cwdToProject = Path::cwdToProject();
        HelperFile::createPathRecursive($cwdToProject);
        HelperFile::file_put_contents(Path::join($cwdToProject, CONFIG_QFQ_JSON), json_encode($config, JSON_PRETTY_PRINT));
    }

163
    /**
164
165
166
167
168
169
170
171
     * Replace typo3conf/config.qfq.php with fileadmin/protected/qfqProject/qfq.json
     *
     * @throws \CodeException
     * @throws \UserFormException
     */
    public static function migrateConfigPhpToJson(): void
    {
        // read old config.qfq.php
Marc Egger's avatar
Marc Egger committed
172
        $cwdToOldConfigFile = Path::cwdToApp(Path::APP_TO_TYPO3_CONF, CONFIG_QFQ_PHP);
173
174
175
        HelperFile::enforce_writable($cwdToOldConfigFile);  // so we can delete it.
        $config = include($cwdToOldConfigFile);

176
177
178
179
180
181
182
183
        // In case the database credentials are given in the old style: rename the keys
        $config = OnArray::renameKeys([
            SYSTEM_DB_USER => SYSTEM_DB_1_USER,
            SYSTEM_DB_SERVER => SYSTEM_DB_1_SERVER,
            SYSTEM_DB_PASSWORD => SYSTEM_DB_1_PASSWORD,
            SYSTEM_DB_NAME => SYSTEM_DB_1_NAME
            ], $config);

184
185
186
187
188
189
190
        // write new qfq.config.json
        self::writeConfig($config);

        // remove old
        HelperFile::unlink($cwdToOldConfigFile);
    }

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    /**
     * Read Typo3-QFQ config first from global variable, then from typo3conf/Localconfig.php.
     * If both not exist, return empty array.
     *
     * @return array Empty if config not found.
     * @throws \UserFormException
     * @throws \UserReportException
     */
    private static function readTypo3QfqConfig(): array
    {
        $configT3qfq = array();
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][EXT_KEY])) {
            $configT3qfq = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][EXT_KEY]);
            $configT3qfq[SYSTEM_DB_NAME_T3] = self::getDbName($GLOBALS['TYPO3_CONF_VARS']['DB']);

Marc Egger's avatar
Marc Egger committed
206
207
        } elseif (is_readable(Path::cwdToApp(Path::APP_TO_TYPO3_CONF, CONFIG_T3))) {
            $cwdToTypo3ConfigFile = Path::cwdToApp(Path::APP_TO_TYPO3_CONF, CONFIG_T3);
208
209
210
211
212
213
214
215
216
217
218
219
            $configT3 = HelperFile::include($cwdToTypo3ConfigFile);
            $configT3qfq = unserialize($configT3['EXT']['extConf'][EXT_KEY]);
            if (!is_array($configT3qfq)) {
                Thrower::userFormException('Error read file', "Error while unserializing qfq config from: $cwdToTypo3ConfigFile");
            }
            $configT3qfq[SYSTEM_DB_NAME_T3] = self::getDbName($configT3['DB']);
            unset($configT3);
        }
        $configT3qfq = self::getCustomVariable($configT3qfq);
        return $configT3qfq;
    }

220
221
222
223
224
225
    /**
     * Returns T3 DB-Name, depending on T3 version
     *
     * @param array $db
     * @return mixed
     */
Carsten  Rose's avatar
Carsten Rose committed
226
    private static function getDbName(array $db) {
227
228
229
230
231

        // T3 7.x: $GLOBALS['TYPO3_CONF_VARS']['DB']['database'],  T3 8.x: $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['dbname']
        return isset($db['database']) ? $db['database'] : $db['Connections']['Default']['dbname'];
    }

232
233
    /**
     * Checks for deprecated options.
234
     *
235
     * @param array $config
Marc Egger's avatar
Marc Egger committed
236
     * @throws \UserFormException
237
238
239
240
241
242
243
244
245
     */
    private static function checkDeprecated(array $config) {

        foreach ([SYSTEM_VAR_ADD_BY_SQL] as $key) {

            if (isset($config[$key])) {
                $msg = '';
                switch ($key) {
                    case SYSTEM_VAR_ADD_BY_SQL:
246
                        $msg = 'Replaced by: ' . SYSTEM_FILL_STORE_SYSTEM_BY_SQL . '1|2|3';
247
                }
Marc Egger's avatar
Marc Egger committed
248
                throw new \UserFormException ("Deprecated option in " . CONFIG_QFQ_PHP . ": " . SYSTEM_VAR_ADD_BY_SQL . " - " . $msg);
249
250
251
252
            }
        }
    }

253
    /**
254
255
     * Check for attack
     *
256
     * @param array $config
Marc Egger's avatar
Marc Egger committed
257
258
259
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
260
     */
261
    public static function checkForAttack(array $config) {
262
        $attack = false;
263
        $key = '';
264
        $reason = 'Problem: ';
265
266
267
268

        // Iterate over all fake vars
        $arr = explode(',', $config[SYSTEM_SECURITY_VARS_HONEYPOT]);
        foreach ($arr as $key) {
269

270
271
272
273
            $key = trim($key);
            if ($key === '') {
                continue;
            }
274
275

            if (!empty($_POST[$key])) {
276
                $attack = true;
277
                $reason .= "Post/Get Honeypot variable '$key' detected: " . htmlentities($_POST[$key]) . PHP_EOL;
278
279
280
281
282
            }
        }

        // Limit length of all get vars: protect against SQL injection based on long ...%34%34%24%34...
        $maxLength = $config[SYSTEM_SECURITY_GET_MAX_LENGTH];
283
        if ($maxLength > 0 && $attack === false) {
284
            foreach ($_GET as $key => $value) {
Carsten  Rose's avatar
Carsten Rose committed
285
286
287
288
289

                if (!is_string($value)) {
                    continue;
                }

290
291
                // Check if the variable is something like 'my_name_100' - if the part after the last '_' is numerical, this means a valid, non standard length.
                $arr = explode(GET_EXTRA_LENGTH_TOKEN, $key);
292

293
294
                $cnt = count($arr);
                if ($cnt > 1 && is_numeric($arr[$cnt - 1])) {
295
296
297
298
299
                    $maxLength = $arr[$cnt - 1];
                } else {
                    $maxLength = $config[SYSTEM_SECURITY_GET_MAX_LENGTH]; // might change again.
                }

300
301
                $len = strlen($value);
                if ($len > $maxLength) {
302
                    $attack = true;
303
                    $reason .= "Value of GET variable '$key' too long. Allowed: $maxLength, Length: $len. Value: '" . htmlentities($_GET[$key]) . "'" . PHP_EOL;
304
                }
305
306
307
308
            }
        }

        // Nothing found?
309
        if ($attack === false) {
310
311
312
            return;
        }

313
        self::attackDetectedExitNow($config, $reason);
314
315
316
    }

    /**
317
     * @param array $config
318
     * @param string $reason
Marc Egger's avatar
Marc Egger committed
319
320
321
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
322
     */
323
    public static function attackDetectedExitNow(array $config = array(), $reason = '') {
324
325

        if (count($config) == 0) {
326
            $config = self::getConfigArray();
327
328
        }

329
        Logger::logMessage(Logger::linePre() . 'Security: attack detected' . PHP_EOL . $reason, Path::absoluteQfqLogFile());
330

331
332
333
        // In case of an attack: log out the current user.
        Session::destroy();

334
335
336
337
        // Sleep
        $penalty = (empty($config[SYSTEM_SECURITY_ATTACK_DELAY]) || !is_numeric($config[SYSTEM_SECURITY_ATTACK_DELAY])) ?
            SYSTEM_SECURITY_ATTACK_DELAY_DEFAULT : $config[SYSTEM_SECURITY_ATTACK_DELAY];

Carsten  Rose's avatar
Carsten Rose committed
338
339
340
        if (!defined('PHPUNIT_QFQ')) {
            sleep($penalty);
        }
341

342
        if ($config[SYSTEM_SECURITY_SHOW_MESSAGE] == 'true' || $config[SYSTEM_SECURITY_SHOW_MESSAGE] == 1) {
343

344
            echo "Attack detected - stop process <p>" . $reason . '</p>';
345
346
347
348
349
350
351
//            $answer[API_STATUS] = API_ANSWER_STATUS_ERROR;
//            $answer[API_MESSAGE] = 'Attack detected - stop process.';
//            if($getParamName!='') {
//                $answer[API_MESSAGE] .= " Attack parameter: $getParamName";
//            }
//            header("Content-Type: application/json");
//            echo json_encode($answer);
352
353
        }

354
        if (defined('PHPUNIT_QFQ')) {
Marc Egger's avatar
Marc Egger committed
355
            throw new \UserFormException('Attack detected', 1);
356
357
        }

358
359
360
361
362
        exit;
    }

    /**
     * @param array $config
Carsten  Rose's avatar
Carsten Rose committed
363
     *
364
365
     * @return array
     */
366
    public static function setDefaults(array $config) {
367
368
369

        $default = [

370
            SYSTEM_DB_INIT => 'set names utf8',
371
372
            SYSTEM_DB_INDEX_DATA => DB_INDEX_DEFAULT,
            SYSTEM_DB_INDEX_QFQ => DB_INDEX_DEFAULT,
373

374
            SYSTEM_RENDER => SYSTEM_RENDER_SINGLE,
375
376
            SYSTEM_DATE_FORMAT => 'yyyy-mm-dd',
            SYSTEM_SHOW_DEBUG_INFO => SYSTEM_SHOW_DEBUG_INFO_AUTO,
377
378
379
            SYSTEM_MAIL_LOG_PATHFILENAME => '',
            SYSTEM_QFQ_LOG_PATHFILENAME => '',
            SYSTEM_SQL_LOG_PATHFILENAME => '',
380
            SYSTEM_SQL_LOG_MODE => 'modify',
381
            SYSTEM_SQL_LOG_MODE_AUTOCRON => 'error',
382
383
384
385
            F_BS_COLUMNS => 'col-md-12 col-lg-10',
            F_BS_LABEL_COLUMNS => 'col-md-3 col-lg-3',
            F_BS_INPUT_COLUMNS => 'col-md-6 col-lg-6',
            F_BS_NOTE_COLUMNS => 'col-md-3 col-lg-3',
386

387
388
            F_CLASS => 'qfq-notify',

389
390
391
392
            F_CLASS_PILL => 'qfq-color-grey-1',
            F_CLASS_BODY => 'qfq-color-grey-2',

            F_SAVE_BUTTON_TEXT => '',
393
            F_SAVE_BUTTON_TOOLTIP => '',
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
            F_SAVE_BUTTON_CLASS => 'btn btn-default navbar-btn',
            F_SAVE_BUTTON_GLYPH_ICON => GLYPH_ICON_CHECK,

            F_CLOSE_BUTTON_TEXT => '',
            F_CLOSE_BUTTON_TOOLTIP => 'Close',
            F_CLOSE_BUTTON_CLASS => 'btn btn-default navbar-btn',
            F_CLOSE_BUTTON_GLYPH_ICON => GLYPH_ICON_CLOSE,

            F_DELETE_BUTTON_TEXT => '',
            F_DELETE_BUTTON_TOOLTIP => 'Delete',
            F_DELETE_BUTTON_CLASS => 'btn btn-default navbar-btn',
            F_DELETE_BUTTON_GLYPH_ICON => GLYPH_ICON_DELETE,

            F_NEW_BUTTON_TEXT => '',
            F_NEW_BUTTON_TOOLTIP => 'New',
            F_NEW_BUTTON_CLASS => 'btn btn-default navbar-btn',
            F_NEW_BUTTON_GLYPH_ICON => GLYPH_ICON_NEW,

            F_BUTTON_ON_CHANGE_CLASS => 'btn-info alert-info',
            SYSTEM_EDIT_FORM_PAGE => 'form',
414
            SYSTEM_SECURITY_VARS_HONEYPOT => SYSTEM_SECURITY_VARS_HONEYPOT_NAMES,
415
416
417
            SYSTEM_SECURITY_ATTACK_DELAY => SYSTEM_SECURITY_ATTACK_DELAY_DEFAULT,
            SYSTEM_SECURITY_SHOW_MESSAGE => '0',
            SYSTEM_SECURITY_GET_MAX_LENGTH => SYSTEM_SECURITY_GET_MAX_LENGTH_DEFAULT,
418
419
420

            SYSTEM_LABEL_ALIGN => SYSTEM_LABEL_ALIGN_LEFT,

421
            SYSTEM_ESCAPE_TYPE_DEFAULT => TOKEN_ESCAPE_MYSQL,
422
423
            SYSTEM_EXTRA_BUTTON_INFO_INLINE => '<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>',
            SYSTEM_EXTRA_BUTTON_INFO_BELOW => '<span class="glyphicon glyphicon-info-sign text-info" aria-hidden="true"></span>',
424
425
426
427
428
            SYSTEM_EXTRA_BUTTON_INFO_CLASS => '',

            SYSTEM_DB_UPDATE => SYSTEM_DB_UPDATE_AUTO,
            SYSTEM_RECORD_LOCK_TIMEOUT_SECONDS => SYSTEM_RECORD_LOCK_TIMEOUT_SECONDS_DEFAULT,

429
            SYSTEM_SESSION_TIMEOUT_SECONDS => Session::getPhpSessionTimeout(),
430

431
            SYSTEM_DOCUMENTATION_QFQ => SYSTEM_DOCUMENTATION_QFQ_URL,
432
433
            SYSTEM_ENTER_AS_SUBMIT => 1,

434
            SYSTEM_CMD_WKHTMLTOPDF => '/opt/wkhtmltox/bin/wkhtmltopdf',
435
436
            SYSTEM_CMD_INKSCAPE => 'inkscape',
            SYSTEM_CMD_CONVERT => 'convert',
437
438
439
            SYSTEM_CMD_QPDF => 'qpdf',
            SYSTEM_CMD_GS => 'gs',
            SYSTEM_CMD_PDFUNITE => 'pdfunite',
440
            SYSTEM_CMD_IMG2PDF => 'img2pdf',
441

442
443
            SYSTEM_THUMBNAIL_DIR_SECURE_REL_TO_APP => Path::APP_TO_SYSTEM_THUMBNAIL_DIR_SECURE_DEFAULT,
            SYSTEM_THUMBNAIL_DIR_PUBLIC_REL_TO_APP => Path::APP_TO_SYSTEM_THUMBNAIL_DIR_PUBLIC_DEFAULT,
444

445
            F_FE_DATA_REQUIRED_ERROR => F_FE_DATA_REQUIRED_ERROR_DEFAULT,
446
            F_FE_DATA_MATCH_ERROR => F_FE_DATA_MATCH_ERROR_DEFAULT,
447
            F_FE_DATA_ERROR => F_FE_DATA_ERROR_DEFAULT,
448
            F_FE_DATA_PATTERN_ERROR => F_FE_DATA_PATTERN_ERROR_DEFAULT,
449
450
451

            SYSTEM_FLAG_PRODUCTION => 'yes',
            SYSTEM_THROW_GENERAL_ERROR => 'auto',
Carsten  Rose's avatar
Carsten Rose committed
452
453

            SYSTEM_SECURITY_FAILED_AUTH_DELAY => '3',
454
455
456

            SYSTEM_FILE_MAX_FILE_SIZE => min(Support::returnBytes(ini_get('post_max_size')), Support::returnBytes(ini_get('upload_max_filesize'))),

457
458
        ];

459
460
461
462
463
464
        foreach ($default as $key => $value) {
            if (!isset($config[$key]) || $config[$key] == '') {
                $config[$key] = $value;
            }
        }

Marc Egger's avatar
Newdoc    
Marc Egger committed
465
466
467
468
469
        // don't accept deprecated documentation url
        if ($config[SYSTEM_DOCUMENTATION_QFQ] === 'https://docs.typo3.org/typo3cms/drafts/github/T3DocumentationStarter/Public-Info-053/Manual.html') {
            $config[SYSTEM_DOCUMENTATION_QFQ] = SYSTEM_DOCUMENTATION_QFQ_URL;
        }

470
        return $config;
471
472
    }

473

474
475
    /**
     * Rename Elements defined in config.qfq.ini to more appropriate in user interaction.
Carsten  Rose's avatar
Carsten Rose committed
476
477
     * E.g.: in config.qfq.ini everything is in upper case and word space is '_'. In Form.parameter it's lowercase and
     * camel hook.
478
479
     *
     * @param array $config
Carsten  Rose's avatar
Carsten Rose committed
480
     *
481
482
483
484
485
486
     * @return array
     */
    private static function renameConfigElements(array $config) {

        // oldname > newname
        $setting = [
487
            [SYSTEM_FORM_BS_COLUMNS, F_BS_COLUMNS],
488
489
490
            [SYSTEM_FORM_BS_LABEL_COLUMNS, F_BS_LABEL_COLUMNS],
            [SYSTEM_FORM_BS_INPUT_COLUMNS, F_BS_INPUT_COLUMNS],
            [SYSTEM_FORM_BS_NOTE_COLUMNS, F_BS_NOTE_COLUMNS],
491

492
493
494
495
            [SYSTEM_FORM_DATA_PATTERN_ERROR, F_FE_DATA_PATTERN_ERROR],
            [SYSTEM_FORM_DATA_REQUIRED_ERROR, F_FE_DATA_REQUIRED_ERROR],
            [SYSTEM_FORM_DATA_MATCH_ERROR, F_FE_DATA_MATCH_ERROR],
            [SYSTEM_FORM_DATA_ERROR, F_FE_DATA_ERROR],
496

497
498
499
            [SYSTEM_CSS_CLASS_QFQ_FORM, F_CLASS],
            [SYSTEM_CSS_CLASS_QFQ_FORM_PILL, F_CLASS_PILL],
            [SYSTEM_CSS_CLASS_QFQ_FORM_BODY, F_CLASS_BODY],
500
            [SYSTEM_SAVE_BUTTON_CLASS_ON_CHANGE, F_BUTTON_ON_CHANGE_CLASS],
501

502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
        ];

        foreach ($setting as $row) {
            $oldName = $row[0];
            $newName = $row[1];

            if (isset($config[$oldName])) {
                $config[$newName] = $config[$oldName];
                if ($oldName != $newName) {
                    unset($config[$oldName]);
                }
            }
        }

        return $config;
    }
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618

    /**
     * Depending on some configuration value, update corresponding values.
     *
     * @param array $config
     *
     * @return array
     */
    private static function adjustConfig(array $config) {

        $config[SYSTEM_SHOW_DEBUG_INFO] = self::adjustConfigDebugInfoAuto($config[SYSTEM_SHOW_DEBUG_INFO], self::beUserLoggedIn());

        // In case the database credentials are given in the old style: copy them to the new style
        if (!isset($config[SYSTEM_DB_1_USER]) && isset($config[SYSTEM_DB_USER])) {
            $config[SYSTEM_DB_1_USER] = $config[SYSTEM_DB_USER];
            $config[SYSTEM_DB_1_SERVER] = $config[SYSTEM_DB_SERVER];
            $config[SYSTEM_DB_1_PASSWORD] = $config[SYSTEM_DB_PASSWORD];
            $config[SYSTEM_DB_1_NAME] = $config[SYSTEM_DB_NAME];
        }

        if ($config[SYSTEM_THROW_GENERAL_ERROR] == 'auto') {
            $config[SYSTEM_THROW_GENERAL_ERROR] = $config[SYSTEM_FLAG_PRODUCTION] == 'yes' ? 'no' : 'yes';
        }

        return $config;
    }

    /**
     * @param string $value
     *
     * @param $flag
     * @return string
     */
    public static function adjustConfigDebugInfoAuto($value, $flag) {

        // Check if SHOW_DEBUG_INFO contains 'auto'. Replace with appropriate.
        if (Support::findInSet(SYSTEM_SHOW_DEBUG_INFO_AUTO, $value) && $flag) {
            $value = str_replace(SYSTEM_SHOW_DEBUG_INFO_AUTO, SYSTEM_SHOW_DEBUG_INFO_YES, $value);
        }

        return $value;
    }

    /**
     * Check if there is a Typo3 beUser is logged in. This is only possible if QFQ is called via T3, not via API
     *
     * @return bool  true: current Browser session is a logged in BE User.
     */
    private static function beUserLoggedIn() {

        return (!empty($GLOBALS["TSFE"]->beUserLogin) && $GLOBALS["TSFE"]->beUserLogin === true);
    }

    /**
     * Set automatic filled values
     *
     * @param array $config
     * @return array
     */
    private static function setAutoConfigValue(array $config) {

        $config[SYSTEM_DB_NAME_DATA] = $config['DB_' . $config[SYSTEM_DB_INDEX_DATA] . '_NAME'] ?? '';
        $config[SYSTEM_DB_NAME_QFQ] = $config['DB_' . $config[SYSTEM_DB_INDEX_QFQ] . '_NAME'] ?? '';

        return $config;
    }

    /**
     * Iterate over all Parameter which have to exist in the config. Throw an array if any is missing.
     *
     * @param array $config
     *
     * @throws \UserFormException
     */
    private static function checkMandatoryParameter(array $config) {

        // Check mandatory config vars.
        $names = array_merge([SYSTEM_SQL_LOG_MODE],
            self::dbCredentialName($config[SYSTEM_DB_INDEX_DATA]),
            self::dbCredentialName($config[SYSTEM_DB_INDEX_QFQ]));

        foreach ($names as $name) {
            if (!isset($config[$name])) {
                throw new \UserFormException ("Missing configuration in `" . CONFIG_QFQ_PHP . "`: $name", ERROR_MISSING_CONFIG_INI_VALUE);
            }
        }
    }

    /**
     * @param $index
     * @return array
     */
    private static function dbCredentialName($index) {
        $names = array();
        $names[] = 'DB_' . $index . '_USER';
        $names[] = 'DB_' . $index . '_SERVER';
        $names[] = 'DB_' . $index . '_PASSWORD';
        $names[] = 'DB_' . $index . '_NAME';

        return $names;
    }
619
}