Store.php 5.9 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
16
require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php');
require_once(__DIR__ . '/../../qfq/helper/Sanatize.php');
17
require_once(__DIR__ . '/../../qfq/Constants.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
35
    private static $raw = array();
    private static $cache = array();
    private static $defaultSanatizeClass = array();
36
37

    /**
38
     * Design Pattern: Singleton - class 'store' should only be instanced once.
39
     */
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    private function __construct($bodytext = '') {

        self::$defaultSanatizeClass = [
            T3_BODYTEXT_DEBUG_LOAD => SANATIZE_DIGIT,
            T3_BODYTEXT_DEBUG_SAVE => SANATIZE_DIGIT,

            CLIENT_SIP => SANATIZE_DIGIT,
            CLIENT_RECORD_ID => SANATIZE_DIGIT,
            CLIENT_KEY_SEM_ID => SANATIZE_DIGIT,
            CLIENT_KEY_SEM_ID_USER => SANATIZE_DIGIT,
            CLIENT_PAGE_ID => SANATIZE_DIGIT,
            CLIENT_PAGE_TYPE => SANATIZE_DIGIT,
            CLIENT_PAGE_LANGUAGE => SANATIZE_DIGIT,
            CLIENT_FORM => SANATIZE_ALNUMX,

            SYSTEM_DBUSER => SANATIZE_ALNUMX,
            SYSTEM_DBSERVER => SANATIZE_ALNUMX,
            SYSTEM_DBPW => SANATIZE_ALL,
            SYSTEM_DB => SANATIZE_ALNUMX,
            SYSTEM_TESTDB => SANATIZE_ALNUMX,
60
61
            SYSTEM_SESSIONNAME => SANATIZE_ALNUMX,
            SYSTEM_DBH => SANATIZE_ALL
62
63
64
65
66
67
        ];

        self::fillSystemStore();
        self::fillStoreT3Bodytext($bodytext);
        self::fillStoreClient();
    }
68

69
70
71
72
73
    /**
     * @throws CodeException
     * @throws qfq\exceptions\UserException
     */
    private function fillSystemStore() {
74
75
        try {
            $config = parse_ini_file(QFQ_INI, false);
76
        } catch (\Exception $e) {
77
78
            throw new qfq\exceptions\UserException ("Error read file " . QFQ_INI . ": " . $e->getMessage(), ERROR_READ_FILE);
        }
79
        self::setVarArray($config, STORE_SYSTEM);
80
81
    }

82
83
84
85
86
    /**
     * @param array $dataArray
     * @param $store
     * @throws CodeException
     */
87
88
    public function setVarArray(array $dataArray, $store) {

89
90
91
        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);
        }
92

93
94
        self::$raw[$store] = $dataArray;
    }
95

96
97
98
99
100
101
    /**
     * @param $bodytext
     * @throws CodeException
     */
    private function fillStoreT3Bodytext($bodytext) {
        $keyValue = new \qfq\helper\KeyValueStringParser("=", "\n");
102

103
104
        self::setVarArray($keyValue->parse($bodytext), STORE_T3_BODYTEXT);
    }
105

106
107
108
109
110
111
112
113
    /**
     * @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;
114
        }
115
        self::setVarArray($arr, STORE_CLIENT);
116
117
118
    }

    /**
119
     *
120
121
     * @return Store
     */
Carsten  Rose's avatar
Carsten Rose committed
122
123
124
125
126
127
128
129
    public static function getInstance($bodytext = '', $phpunit = false) {

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

131
        // Design Pattern: Singleton
Carsten  Rose's avatar
Carsten Rose committed
132
        if (self::$instance === null) {
133
            self::$instance = new Store($bodytext);
134
135
136
137
138
        }
        return self::$instance;
    }

    /**
139
140
141
142
143
144
145
146
     * @param $key
     * @param $value
     * @param $store
     * @throws CodeException
     */
    public static function setVar($key, $value, $store) {

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

150
151
152
153
154
155
156
    }

    /**
     * Cycles throught all stores in $useStore.
     * First match will return the found value.
     * During cycling:, fill cache and sanatize a raw value.
     *
157
     * @param string $key
158
     * @param string $useStores f.e.: 'FSRD'
159
160
161
     * @param string $sanatizeClass
     * @return string a) if found: value, b) false
     */
162
163
164
165
166
167
168
169
170
171
172
173
    public function getVar($key, $useStores = USE_STORE_DEFAULT, $sanatizeClass = '') {

        // 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
174
175
            $cacheVal = isset(self::$cache[$store][$key]) ? self::$cache[$store][$key] : null;
            $rawVal = isset(self::$raw[$store][$key]) ? self::$raw[$store][$key] : null;
176
177

            // Already cached?
Carsten  Rose's avatar
Carsten Rose committed
178
179
            if (isset($cacheVal) && $cacheVal !== false)
                return $cacheVal;
180
181

            // No value in store: try next store
Carsten  Rose's avatar
Carsten Rose committed
182
            if ($cacheVal === false) {
183
                continue;
184
185
            }

186
            // No value in raw? set cache to 'nothing found' and try next store
Carsten  Rose's avatar
Carsten Rose committed
187
            if (!isset($rawVal)) {
188
189
190
191
192
193
194
195
196
197
                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
198
199
200
201
202
203
204
205
//            $value =  \qfq\Sanatize::sanatize($rawVal, $sanatizeClass);
//            self::$cache[$store][$key] = $value;
//
//            if(!isset(self::$cache[$store])) {
//                self::$cache[$store]=array();
//                self::$cache[$store][$key] = $value;
//            }
            self::$cache[$store][$key] = \qfq\Sanatize::sanatize($rawVal, $sanatizeClass);
206
            return self::$cache[$store][$key];
207
208
209

        }
        return false;
210

211
212
213
214
215
216
    }
}