Store.php 5.46 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
60
61
62
63
64
65
66
    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,
            SYSTEM_SESSIONNAME => SANATIZE_ALNUMX
        ];

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

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

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

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

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

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

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

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

    /**
118
     *
119
120
     * @return Store
     */
121
    public static function getInstance($bodytext = '') {
122

123
124
        // Design Pattern: Singleton
        if (self::$instance == null) {
125
            self::$instance = new Store($bodytext);
126
127
128
129
130
        }
        return self::$instance;
    }

    /**
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
     * @param $key
     * @param $value
     * @param $store
     * @throws CodeException
     */
    public static function setVar($key, $value, $store) {

        if (isset(self::$raw[$store][$key])) {
            throw new CodeException("Raw value already been copied to store '$store'. Do this only one time.", ERROR_VALUE_ALREADY_CODPIED);
        }

        self::$raw[$store][$key] = $value;
    }

    /**
     * Cycles throught all stores in $useStore.
     * First match will return the found value.
     * During cycling:, fill cache and sanatize a raw value.
     *
150
     * @param string $key
151
     * @param string $useStores f.e.: 'FSRD'
152
153
154
     * @param string $sanatizeClass
     * @return string a) if found: value, b) false
     */
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    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

            $cache = isset(self::$cache[$store][$key]) ? self::$cache[$store][$key] : null;
            $raw = isset(self::$raw[$store][$key]) ? self::$raw[$store][$key] : null;

            // Already cached?
            if (isset($cache) && $cache !== false)
                return $cache;

            // No value in store: try next store
            if ($cache === false) {
                continue;
177
178
            }

179
180
181
182
183
184
185
186
187
188
189
190
191
192
            // No value in raw? cache 'nothing found' and try next store
            if (!isset($raw)) {
                self::$cache[$store][$key] = false;
                continue;
            }

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

            // sanatize
            self::$cache[$store][$key] = \qfq\Sanatize::sanatize($raw, $sanatizeClass);
            return self::$cache[$store][$key];
193
194
195

        }
        return false;
196

197
198
199
200
201
202
    }
}