Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Properties.php « lib - github.com/nextcloud/user_sql.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bd97d2c24dfbafe6a242b306790977b400c1cdfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
 * Nextcloud - user_sql
 *
 * @copyright 2020 Marcin Łojewski <dev@mlojewski.me>
 * @author    Marcin Łojewski <dev@mlojewski.me>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

namespace OCA\UserSQL;

use OCA\UserSQL\Constant\App;
use OCA\UserSQL\Constant\DB;
use OCA\UserSQL\Constant\Opt;
use OCP\IConfig;
use OCP\ILogger;

/**
 * Store and retrieve application properties.
 *
 * @author Marcin Łojewski <dev@mlojewski.me>
 */
class Properties implements \ArrayAccess
{
    /**
     * @var string The cache key name.
     */
    const CACHE_KEY = "Properties_data";

    /**
     * @var string The application name.
     */
    private $appName;
    /**
     * @var IConfig The config instance.
     */
    private $config;
    /**
     * @var ILogger The logger instance.
     */
    private $logger;
    /**
     * @var Cache The cache instance.
     */
    private $cache;
    /**
     * @var array The properties array.
     */
    private $data;
    /**
     * @var boolean Store confidential data in file.
     */
    private $safeStore;

    /**
     * The default constructor.
     *
     * @param string  $AppName The application name.
     * @param IConfig $config  The config instance.
     * @param ILogger $logger  The logger instance.
     * @param Cache   $cache   The cache instance.
     */
    public function __construct(
        $AppName, IConfig $config, ILogger $logger, Cache $cache
    ) {
        $this->appName = $AppName;
        $this->config = $config;
        $this->logger = $logger;
        $this->cache = $cache;

        $this->loadProperties();
    }

    /**
     * Load the application properties.
     *
     * First the values are fetched from the cache memory.
     * If these are not available, the database values are fetched.
     */
    private function loadProperties()
    {
        $this->data = $this->cache->get(self::CACHE_KEY);

        if (!is_null($this->data)) {
            return;
        }

        $params = $this->getParameterArray();
        $this->data = [];
        $this->safeStore
            = $this->config->getAppValue($this->appName, Opt::SAFE_STORE, App::FALSE_VALUE) === App::TRUE_VALUE;

        foreach ($params as $param) {
            if ($this->isSystemValue($param)) {
                $value = $this->config->getSystemValue("user_sql." . $param, null);
            } else {
                $value = $this->config->getAppValue($this->appName, $param, null);
            }

            if ($this->isBooleanParam($param)) {
                if ($value === App::FALSE_VALUE) {
                    $value = false;
                } elseif ($value === App::TRUE_VALUE) {
                    $value = true;
                }
            }

            $this->data[$param] = $value;
        }

        $this->store();

        $this->logger->debug(
            "The application properties has been loaded.",
            ["app" => $this->appName]
        );
    }

    /**
     * Return an array with all supported parameters.
     *
     * @return array Array containing strings of the parameters.
     */
    private function getParameterArray()
    {
        $params = [];

        foreach ([DB::class, Opt::class] as $class) {
            try {
                $reflection = new \ReflectionClass($class);
                $params = array_merge(
                    $params, array_values($reflection->getConstants())
                );
            } catch (\ReflectionException $exception) {
                $this->logger->logException(
                    $exception, ["app" => $this->appName]
                );
            }
        }

        return $params;
    }

    /**
     * @param $param string Parameter name.
     *
     * @return bool TRUE if this is a system wide parameter FALSE otherwise.
     */
    private function isSystemValue($param)
    {
        return $this->safeStore && in_array($param, array(DB::HOSTNAME, DB::PASSWORD, DB::USERNAME, DB::DATABASE));
    }

    /**
     * Is given parameter a boolean parameter.
     *
     * @param $param string Parameter name.
     *
     * @return bool Is a boolean parameter.
     */
    private function isBooleanParam($param)
    {
        return in_array(
            $param, [
                Opt::APPEND_SALT, Opt::CASE_INSENSITIVE_USERNAME, Opt::EMAIL_LOGIN,
                Opt::NAME_CHANGE, Opt::PASSWORD_CHANGE, Opt::PREPEND_SALT,
                Opt::PROVIDE_AVATAR, Opt::REVERSE_ACTIVE, Opt::SAFE_STORE,
                Opt::USE_CACHE
            ]
        );
    }

    /**
     * Store properties in the cache memory.
     */
    private function store()
    {
        $this->cache->set(self::CACHE_KEY, $this->data);
    }

    /**
     * Get properties array.
     *
     * @return array The properties array.
     */
    public function getArray()
    {
        return $this->data;
    }

    /**
     * @inheritdoc
     */
    public function offsetExists($offset)
    {
        return isset($this->data[$offset]);
    }

    /**
     * @inheritdoc
     */
    public function offsetGet($offset)
    {
        if (isset($this->data[$offset])) {
            return $this->data[$offset];
        } else {
            return null;
        }
    }

    /**
     * @inheritdoc
     */
    public function offsetSet($offset, $value)
    {
        if ($offset == Opt::SAFE_STORE) {
            $this->safeStore = ($value === App::TRUE_VALUE);
        }

        if ($this->isSystemValue($offset)) {
            $this->config->setSystemValue("user_sql." . $offset, $value);
        } else {
            $this->config->setAppValue($this->appName, $offset, $value);
        }

        if ($this->isBooleanParam($offset)) {
            if ($value === App::FALSE_VALUE) {
                $value = false;
            } elseif ($value === App::TRUE_VALUE) {
                $value = true;
            }
        }

        $this->data[$offset] = $value;

        if ($offset === Opt::USE_CACHE && $value === false) {
            $this->cache->clear();
        } else {
            $this->store();
        }
    }

    /**
     * @inheritdoc
     */
    public function offsetUnset($offset)
    {
        if ($offset == Opt::SAFE_STORE) {
            $this->safeStore = App::FALSE_VALUE;
        }

        if ($this->isSystemValue($offset)) {
            $this->config->deleteSystemValue("user_sql." . $offset);
        } else {
            $this->config->deleteAppValue($this->appName, $offset);
        }
        unset($this->data[$offset]);
    }
}