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

namespace qfq;

use qfq;

13
14
require_once(__DIR__ . '/../Constants.php');
require_once(__DIR__ . '/../helper/Support.php');
15

16
17
18
19
/**
 * Class Config
 * @package qfq
 */
20
21
class Config {

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    /**
     * @param $configIni
     * @param $configPhp
     */
    private function migrateConfigIniToPhp($configIni, $configPhp) {

        $config = parse_ini_file($configIni, false);

        $pre = isset($config[SYSTEM_DB_NAME]) ? 'DB' : 'DB_1';

        $content = '<?php' . PHP_EOL . 'return [' . PHP_EOL;
        $content .= "  '" . $pre . "_NAME' => '" . $config[$pre . '_NAME'] . "'," . PHP_EOL;
        $content .= "  '" . $pre . "_PASSWORD' => '" . $config[$pre . '_PASSWORD'] . "'," . PHP_EOL;
        $content .= "  '" . $pre . "_SERVER' => '" . $config[$pre . '_SERVER'] . "'," . PHP_EOL;
        $content .= "  '" . $pre . "_USER' => '" . $config[$pre . '_USER'] . "'," . PHP_EOL;
        $content .= "];" . PHP_EOL;

        // Write new config file
        file_put_contents($configPhp, $content);

        // Make old file unreadable
        chmod($configIni, 000);
    }

46
47
48
    /**
     * Read config.qfq.ini.
     *
49
     * @param string $configIni
50
51
     * @return array
     * @throws UserFormException
52
     */
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    public function readConfig($configIni = '') {
        $configT3qfq = array();

        // Production Path to CONFIG_INI
        $pathTypo3Conf = __DIR__ . '/../../../../..';
        if (!file_exists($pathTypo3Conf . '/' . CONFIG_T3)) {
            // PHPUnit Path to CONFIG_INI
            $pathTypo3Conf = __DIR__ . '/../../..';
        }

        // In case there is a $configIni given: do not try to include T3 LocalConfiguration.php
        if (empty($configIni)) {
            # Read 'LocalConfiguration.php'
            if (isset($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][EXT_KEY])) {
                $configT3qfq = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][EXT_KEY]);
            } else {
                $all = include_once($pathTypo3Conf . '/' . CONFIG_T3);
                $configT3qfq = unserialize($all['EXT']['extConf'][EXT_KEY]);
                unset($all);
72
            }
73
74
75
76
77
78
79
            $configIni = $pathTypo3Conf . '/' . CONFIG_QFQ_INI;
            $configPhp = $pathTypo3Conf . '/' . CONFIG_QFQ_PHP;
        }

        // Migrate legacy config file.
        if (is_readable($configIni) && !is_readable($configPhp)) {
            $this->migrateConfigIniToPhp($configIni, $configPhp);
80
81
82
        }

        try {
83
84
            $config = include_once($configPhp);

85
86
87
88
            // in case $configIni doesn't exist: just skip
            if ($config === false) {
                $config = array();
            }
89
        } catch (\Exception $e) {
90
            throw new qfq\UserFormException ("Error read file " . $pathTypo3Conf . ": " . $e->getMessage(), ERROR_IO_READ_FILE);
91
        }
92
93
        // Settings in  config.qfq.ini overwrite T3 settings
        $config = array_merge($configT3qfq, $config);
94
95

        $config = self::renameConfigElements($config);
96
        $config = self::setDefaults($config);
97
        self::checkDeprecated($config);
98

99
100
101
102
103
        self::checkForAttack($config);

        return $config;
    }

104
105
    /**
     * Checks for deprecated options.
106
107
     * @param array $config
     * @throws UserFormException
108
109
110
111
112
113
114
115
116
     */
    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:
117
                        $msg = 'Replaced by: ' . SYSTEM_FILL_STORE_SYSTEM_BY_SQL . '1|2|3';
118
                }
119
                throw new qfq\UserFormException ("Deprecated option in " . CONFIG_QFQ_INI . ": " . SYSTEM_VAR_ADD_BY_SQL . " - " . $msg);
120
121
122
123
124
            }
        }
    }


125
126
    /**
     * @param array $config
127
     * @throws UserFormException
128
129
     */
    private static function checkForAttack(array $config) {
130
        $attack = false;
131
        $key = '';
132
133
134
135

        // Iterate over all fake vars
        $arr = explode(',', $config[SYSTEM_SECURITY_VARS_HONEYPOT]);
        foreach ($arr as $key) {
136
137
138
139
            $key = trim($key);
            if ($key === '') {
                continue;
            }
140
            if (!empty($_POST[$key]) || !empty($_GET[$key])) {
141
142
143
144
145
146
147
                $attack = true;
            }
        }

        // Limit length of all get vars: protect against SQL injection based on long ...%34%34%24%34...
        $maxLength = $config[SYSTEM_SECURITY_GET_MAX_LENGTH];
        if ($maxLength > 0) {
148
149
150
            foreach ($_GET as $key => $value) {
                // 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);
151

152
153
                $cnt = count($arr);
                if ($cnt > 1 && is_numeric($arr[$cnt - 1])) {
154
155
156
157
158
159
                    $maxLength = $arr[$cnt - 1];
                } else {
                    $maxLength = $config[SYSTEM_SECURITY_GET_MAX_LENGTH]; // might change again.
                }

                if (strlen($value) > $maxLength) {
160
                    $attack = true;
161
                    break;
162
                }
163
164
165
166
            }
        }

        // Nothing found?
167
        if ($attack === false) {
168
169
170
            return;
        }

171
        self::attackDetectedExitNow($config, $key);
172
173
174
    }

    /**
175
176
     * @param array $config
     * @param string $getParamName
177
178
     * @throws UserFormException
     */
179
    public static function attackDetectedExitNow(array $config = array(), $getParamName = '') {
180
181
182

        if (count($config) == 0) {
            $config = self::readConfig();
183
184
        }

185
186
187
        // In case of an attack: log out the current user.
        Session::destroy();

188
189
190
191
192
193
        // 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];

        sleep($penalty);

194
        if ($config[SYSTEM_SECURITY_SHOW_MESSAGE] == 'true' || $config[SYSTEM_SECURITY_SHOW_MESSAGE] == 1) {
195

196
            echo "Attack detected - stop process";
197
198
199
200
201
202
203
//            $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);
204
205
206
207
208
209
210
        }

        exit;
    }

    /**
     * @param array $config
Carsten  Rose's avatar
Carsten Rose committed
211
     *
212
213
214
     * @return array
     */
    private static function setDefaults(array $config) {
215
216
217
218
        // Defaults: do not worry: parse_ini_file() will replace 'none' and 'off' by ''.

        $default = [

219
            SYSTEM_DB_INIT => 'set names utf8',
220
221
            SYSTEM_DB_INDEX_DATA => DB_INDEX_DEFAULT,
            SYSTEM_DB_INDEX_QFQ => DB_INDEX_DEFAULT,
222
223
224
225
226
227
228
229
230
231
232

            SYSTEM_DATE_FORMAT => 'yyyy-mm-dd',
            SYSTEM_SHOW_DEBUG_INFO => SYSTEM_SHOW_DEBUG_INFO_AUTO,
            SYSTEM_SQL_LOG => SYSTEM_SQL_LOG_FILE,
            SYSTEM_SQL_LOG_MODE => 'modify',
            SYSTEM_MAIL_LOG => SYSTEM_MAIL_LOG_FILE,
            F_BS_COLUMNS => '12',
            F_BS_LABEL_COLUMNS => '3',
            F_BS_INPUT_COLUMNS => '6',
            F_BS_NOTE_COLUMNS => '3',

233
            SYSTEM_CMD_WKHTMLTOPDF => '/opt/wkhtmltox/bin/wkhtmltopdf',
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

            F_CLASS_PILL => 'qfq-color-grey-1',
            F_CLASS_BODY => 'qfq-color-grey-2',

            F_SAVE_BUTTON_TEXT => '',
            F_SAVE_BUTTON_TOOLTIP => 'Save',
            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',
Carsten  Rose's avatar
Carsten Rose committed
260
            SYSTEM_SECURITY_VARS_HONEYPOT => 'email,username,password',
261
262
263
264
            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,
            SYSTEM_ESCAPE_TYPE_DEFAULT => TOKEN_ESCAPE_MYSQL,
265
266
            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>',
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
            SYSTEM_EXTRA_BUTTON_INFO_CLASS => '',

            SYSTEM_DB_UPDATE => SYSTEM_DB_UPDATE_AUTO,
            SYSTEM_RECORD_LOCK_TIMEOUT_SECONDS => SYSTEM_RECORD_LOCK_TIMEOUT_SECONDS_DEFAULT,

            DOCUMENTATION_QFQ => DOCUMENTATION_QFQ_URL,
            SYSTEM_ENTER_AS_SUBMIT => 1,

            SYSTEM_CMD_INKSCAPE => 'inkscape',
            SYSTEM_CMD_CONVERT => 'convert',
            SYSTEM_THUMBNAIL_DIR_SECURE => SYSTEM_THUMBNAIL_DIR_SECURE_DEFAULT,
            SYSTEM_THUMBNAIL_DIR_PUBLIC => SYSTEM_THUMBNAIL_DIR_PUBLIC_DEFAULT,

        ];

282
283
284
285
        // To let run legacy code
        $config[SYSTEM_DB_INDEX_DATA_DEPRECATED] = $config[SYSTEM_DB_INDEX_DATA];
        $config[SYSTEM_DB_INDEX_QFQ_DEPRECATED] = $config[SYSTEM_DB_INDEX_QFQ];

286
        return array_merge($default, $config);
287
288
289
290
    }

    /**
     * Rename Elements defined in config.qfq.ini to more appropriate in user interaction.
Carsten  Rose's avatar
Carsten Rose committed
291
292
     * E.g.: in config.qfq.ini everything is in upper case and word space is '_'. In Form.parameter it's lowercase and
     * camel hook.
293
294
     *
     * @param array $config
Carsten  Rose's avatar
Carsten Rose committed
295
     *
296
297
298
299
300
301
     * @return array
     */
    private static function renameConfigElements(array $config) {

        // oldname > newname
        $setting = [
302
            [SYSTEM_FORM_BS_COLUMNS, F_BS_COLUMNS],
303
304
305
            [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],
306

307
308
309
310
            [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],
311

312
313
314
            [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],
315
            [SYSTEM_SAVE_BUTTON_CLASS_ON_CHANGE, F_BUTTON_ON_CHANGE_CLASS],
316

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
        ];

        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;
    }
}