Store.php 8.32 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/1/16
 * Time: 6:51 PM
 */

9
namespace qfq\store;
10
11

use qfq\exceptions\CodeException;
12
use qfq;
13

14
15
require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php');
require_once(__DIR__ . '/../../qfq/helper/Sanatize.php');
16
require_once(__DIR__ . '/../../qfq/Constants.php');
17
require_once(__DIR__ . '/../../qfq/store/Sip.php');
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32


/*
 * Stores:
 * - SIP
 * - webVar
 * - record
 * - form
 * - formElement
 */

class Store {

    private static $instance = null;

33
34
    private static $sip = null;

35
36
37
    private static $raw = array();
    private static $cache = array();
    private static $defaultSanatizeClass = array();
38
39

    /**
40
     * @param string $bodytext
41
     */
42
43
44
    private function __construct($bodytext = '') {

        self::$defaultSanatizeClass = [
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
            T3_BODYTEXT_DEBUG_LOAD => SANATIZE_ALLOW_DIGIT,
            T3_BODYTEXT_DEBUG_SAVE => SANATIZE_ALLOW_DIGIT,
            T3_BODYTEXT_FORM => SANATIZE_ALLOW_ALNUMX,

            CLIENT_SIP => SANATIZE_ALLOW_ALNUMX,
            CLIENT_GET_SIP => SANATIZE_ALLOW_DIGIT,
            CLIENT_POST_SIP => SANATIZE_ALLOW_DIGIT,
            CLIENT_RECORD_ID => SANATIZE_ALLOW_DIGIT,
            CLIENT_KEY_SEM_ID => SANATIZE_ALLOW_DIGIT,
            CLIENT_KEY_SEM_ID_USER => SANATIZE_ALLOW_DIGIT,
            CLIENT_PAGE_ID => SANATIZE_ALLOW_DIGIT,
            CLIENT_PAGE_TYPE => SANATIZE_ALLOW_DIGIT,
            CLIENT_PAGE_LANGUAGE => SANATIZE_ALLOW_DIGIT,
            CLIENT_FORM => SANATIZE_ALLOW_ALNUMX,

            SYSTEM_DBUSER => SANATIZE_ALLOW_ALNUMX,
            SYSTEM_DBSERVER => SANATIZE_ALLOW_ALNUMX,
            SYSTEM_DBPW => SANATIZE_ALLOW_ALL,
            SYSTEM_DB => SANATIZE_ALLOW_ALNUMX,
            SYSTEM_TESTDB => SANATIZE_ALLOW_ALNUMX,
            SYSTEM_SESSIONNAME => SANATIZE_ALLOW_ALNUMX,
            SYSTEM_DBH => SANATIZE_ALLOW_ALL,
67
68
69
70

//            SYSTEM_FORM_DEF => SANATIZE_ALL,
//            SYSTEM_FORM_ELEMENT_DEF => SANATIZE_ALL,
//            SYSTEM_FORM_ELEMENT_FIELD => SANATIZE_ALL,
71
72
73
74
75
76
77
78
79
            SYSTEM_SQL_RAW => SANATIZE_ALLOW_ALL,
            SYSTEM_SQL_FINAL => SANATIZE_ALLOW_ALL,
            SYSTEM_SQL_COUNT => SANATIZE_ALLOW_DIGIT,
            SYSTEM_SQL_PARAM_ARRAY => SANATIZE_ALLOW_ALL,

            SIP_SIP => SANATIZE_ALLOW_ALNUMX,
            SIP_RECORD_ID => SANATIZE_ALLOW_DIGIT,
            SIP_FORM => SANATIZE_ALLOW_ALNUMX,
            SIP_URLPARAM => SANATIZE_ALLOW_ALL
80

81
82
83
84
85
        ];

        self::fillSystemStore();
        self::fillStoreT3Bodytext($bodytext);
        self::fillStoreClient();
86
87
        self::fillStoreSip();

88
    }
89

90
91
92
93
94
    /**
     * @throws CodeException
     * @throws qfq\exceptions\UserException
     */
    private function fillSystemStore() {
95
96
        try {
            $config = parse_ini_file(QFQ_INI, false);
97
        } catch (\Exception $e) {
98
99
            throw new qfq\exceptions\UserException ("Error read file " . QFQ_INI . ": " . $e->getMessage(), ERROR_READ_FILE);
        }
100
        self::setVarArray($config, STORE_SYSTEM);
101
102
    }

103
104
105
106
107
    /**
     * @param array $dataArray
     * @param $store
     * @throws CodeException
     */
108
109
    public function setVarArray(array $dataArray, $store) {

110
111
112
        if (isset(self::$raw[$store]) && count(self::$raw[$store]) > 0) {
            throw new CodeException("Raw values already been copied to store '$store'. Do this only one time.", ERROR_VALUE_ALREADY_CODPIED);
        }
113

114
115
        self::$raw[$store] = $dataArray;
    }
116

117
118
119
120
121
    /**
     * @param $bodytext
     * @throws CodeException
     */
    private function fillStoreT3Bodytext($bodytext) {
122
        $parser = new \qfq\helper\KeyValueStringParser("=", "\n");
123

124
        self::setVarArray($parser->parse($bodytext), STORE_T3_BODYTEXT);
125
    }
126

127
128
129
130
131
132
133
134
    /**
     * @throws CodeException
     */
    private function fillStoreClient() {
        // copy GET and POST Parameter. POST Parameter have a higher priority.
        $arr = $_GET;
        foreach ($_POST as $key => $value) {
            $arr[$key] = $value;
135
        }
136
        self::setVarArray($arr, STORE_CLIENT);
137
138
139
140

        # SIP: if POST exist, this means 'form save'. To distinguish load and save, store both.
        if (isset($_GET[CLIENT_SIP]))
            self::setVar(CLIENT_GET_SIP, $_GET[CLIENT_SIP], STORE_CLIENT);
141

142
143
144
145
        if (isset($_POST[CLIENT_SIP]))
            self::setVar(CLIENT_POST_SIP, $_GET[CLIENT_SIP], STORE_CLIENT);
    }

146
147
148
149
150

    /*
     *
     */

151
152
153
154
155
156
157
158
159
160
161
162
    /**
     * @param $key
     * @param $value
     * @param $store
     * @throws CodeException
     */
    public static function setVar($key, $value, $store) {

        self::$raw[$store][$key] = $value;
        // Invalidate cache to force new sanatize
        self::$cache[$store][$key] = null;

163
164
    }

165
    private function fillStoreSip() {
Carsten  Rose's avatar
Carsten Rose committed
166

167
168
        $sessionName = self::getVar(SYSTEM_SESSIONNAME, STORE_SYSTEM);
        self::$sip = new \qfq\store\Sip($sessionName);
169

170
171
172
173
174
175
176
        $s = self::getVar(CLIENT_SIP, STORE_CLIENT);
        if ($s !== false) {
            // if session is given, copy values to store
            $param = self::$sip->getVarsFromSip($s);

            $parser = new \qfq\helper\KeyValueStringParser("=", "&");
            self::setVarArray($parser->parse($param), STORE_SIP);
177
178
179
        }
    }

180
181
182
183
184
    /**
     * Cycles throught all stores in $useStore.
     * First match will return the found value.
     * During cycling:, fill cache and sanatize a raw value.
     *
185
     * @param string $key
186
     * @param string $useStores f.e.: 'FSRD'
187
188
189
     * @param string $sanatizeClass
     * @return string a) if found: value, b) false
     */
190
    public static function getVar($key, $useStores = USE_STORE_DEFAULT, $sanatizeClass = '') {
191
192
193
194
195
196
197
198
199
200
201

        // no store specifed?
        if ($useStores === "") {
            return false;
        }

        while ($useStores !== false) {

            $store = substr($useStores, 0, 1); // next store
            $useStores = substr($useStores, 1); // shift left remaining stores

Carsten  Rose's avatar
Carsten Rose committed
202
203
            $cacheVal = isset(self::$cache[$store][$key]) ? self::$cache[$store][$key] : null;
            $rawVal = isset(self::$raw[$store][$key]) ? self::$raw[$store][$key] : null;
204
205

            // Already cached?
Carsten  Rose's avatar
Carsten Rose committed
206
207
            if (isset($cacheVal) && $cacheVal !== false)
                return $cacheVal;
208
209

            // No value in store: try next store
Carsten  Rose's avatar
Carsten Rose committed
210
            if ($cacheVal === false) {
211
                continue;
212
213
            }

214
            // No value in raw? set cache to 'nothing found' and try next store
Carsten  Rose's avatar
Carsten Rose committed
215
            if (!isset($rawVal)) {
216
217
218
219
220
221
222
223
224
225
                self::$cache[$store][$key] = false;
                continue;
            }

            // get final sanatizeClass
            if ($sanatizeClass === '') {
                $sanatizeClass = isset(self::$defaultSanatizeClass[$key]) ? self::$defaultSanatizeClass[$key] : SANATIZE_DEFAULT;
            }

            // sanatize
Carsten  Rose's avatar
Carsten Rose committed
226
            self::$cache[$store][$key] = \qfq\Sanatize::sanatize($rawVal, $sanatizeClass);
227
            return self::$cache[$store][$key];
228
229
230

        }
        return false;
231
    }
232

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
    /**
     * @param string $bodytext
     * @param bool|false $phpunit
     * @return null|\qfq\store\Store
     */
    public static function getInstance($bodytext = '', $phpunit = false) {

        if ($phpunit) {
            if (self::$instance === null) {
//                unset(self::$instance);
                self::$instance = null;
            }
        }

        // Design Pattern: Singleton
        if (self::$instance === null) {
            self::$instance = new self($bodytext);
        }

        return self::$instance;
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
    /**
     * @param $formName
     * @throws CodeException
     */
    public static function createFakeSipAfterFormLoad($formName) {
        $recordId = self::getVar(CLIENT_RECORD_ID, STORE_T3_BODYTEXT . STORE_CLIENT);
        if ($recordId === false) {
            $recordId = 0;
        }

        $tmpParam = [SIP_RECORD_ID => $recordId, SIP_FORM => $formName];

        // Store in SIP Store (cause it's empty until now).
        self::setVarArray($tmpParam, STORE_SIP);

        // Construct fake urlparam
        $tmpUrlparam = \qfq\helper\OnArray::toString($tmpParam);

        // Create a fake SIP which has never been passed by URL - further processing might expect this to exist.
        $sip = self::getSip()->urlparamToSip($tmpUrlparam, RETURN_SIP);
        self::setVar(CLIENT_SIP, $sip, STORE_CLIENT);
    }

    /**
     * @return null
     */
    public static function getSip() {
        return self::$sip;
    }
284
285
286
287
288
}