]> BookStack Code Mirror - bookstack/blob - app/Settings/SettingService.php
Added locale and text direction to html templates
[bookstack] / app / Settings / SettingService.php
1 <?php namespace BookStack\Settings;
2
3 use Illuminate\Contracts\Cache\Repository as Cache;
4
5 /**
6  * Class SettingService
7  * The settings are a simple key-value database store.
8  * For non-authenticated users, user settings are stored via the session instead.
9  */
10 class SettingService
11 {
12
13     protected $setting;
14     protected $cache;
15     protected $localCache = [];
16
17     protected $cachePrefix = 'setting-';
18
19     /**
20      * SettingService constructor.
21      * @param Setting $setting
22      * @param Cache   $cache
23      */
24     public function __construct(Setting $setting, Cache $cache)
25     {
26         $this->setting = $setting;
27         $this->cache = $cache;
28     }
29
30     /**
31      * Gets a setting from the database,
32      * If not found, Returns default, Which is false by default.
33      * @param             $key
34      * @param string|bool $default
35      * @return bool|string
36      */
37     public function get($key, $default = false)
38     {
39         if ($default === false) {
40             $default = config('setting-defaults.' . $key, false);
41         }
42
43         if (isset($this->localCache[$key])) {
44             return $this->localCache[$key];
45         }
46
47         $value = $this->getValueFromStore($key, $default);
48         $formatted = $this->formatValue($value, $default);
49         $this->localCache[$key] = $formatted;
50         return $formatted;
51     }
52
53     /**
54      * Get a value from the session instead of the main store option.
55      * @param $key
56      * @param bool $default
57      * @return mixed
58      */
59     protected function getFromSession($key, $default = false)
60     {
61         $value = session()->get($key, $default);
62         $formatted = $this->formatValue($value, $default);
63         return $formatted;
64     }
65
66     /**
67      * Get a user-specific setting from the database or cache.
68      * @param \BookStack\Auth\User $user
69      * @param $key
70      * @param bool $default
71      * @return bool|string
72      */
73     public function getUser($user, $key, $default = false)
74     {
75         if ($user->isDefault()) {
76             return $this->getFromSession($key, $default);
77         }
78         return $this->get($this->userKey($user->id, $key), $default);
79     }
80
81     /**
82      * Get a value for the current logged-in user.
83      * @param $key
84      * @param bool $default
85      * @return bool|string
86      */
87     public function getForCurrentUser($key, $default = false)
88     {
89         return $this->getUser(user(), $key, $default);
90     }
91
92     /**
93      * Gets a setting value from the cache or database.
94      * Looks at the system defaults if not cached or in database.
95      * @param $key
96      * @param $default
97      * @return mixed
98      */
99     protected function getValueFromStore($key, $default)
100     {
101         // Check for an overriding value
102         $overrideValue = $this->getOverrideValue($key);
103         if ($overrideValue !== null) {
104             return $overrideValue;
105         }
106
107         // Check the cache
108         $cacheKey = $this->cachePrefix . $key;
109         $cacheVal = $this->cache->get($cacheKey, null);
110         if ($cacheVal !== null) {
111             return $cacheVal;
112         }
113
114         // Check the database
115         $settingObject = $this->getSettingObjectByKey($key);
116         if ($settingObject !== null) {
117             $value = $settingObject->value;
118             $this->cache->forever($cacheKey, $value);
119             return $value;
120         }
121
122         return $default;
123     }
124
125     /**
126      * Clear an item from the cache completely.
127      * @param $key
128      */
129     protected function clearFromCache($key)
130     {
131         $cacheKey = $this->cachePrefix . $key;
132         $this->cache->forget($cacheKey);
133         if (isset($this->localCache[$key])) {
134             unset($this->localCache[$key]);
135         }
136     }
137
138     /**
139      * Format a settings value
140      * @param $value
141      * @param $default
142      * @return mixed
143      */
144     protected function formatValue($value, $default)
145     {
146         // Change string booleans to actual booleans
147         if ($value === 'true') {
148             $value = true;
149         }
150         if ($value === 'false') {
151             $value = false;
152         }
153
154         // Set to default if empty
155         if ($value === '') {
156             $value = $default;
157         }
158         return $value;
159     }
160
161     /**
162      * Checks if a setting exists.
163      * @param $key
164      * @return bool
165      */
166     public function has($key)
167     {
168         $setting = $this->getSettingObjectByKey($key);
169         return $setting !== null;
170     }
171
172     /**
173      * Check if a user setting is in the database.
174      * @param $key
175      * @return bool
176      */
177     public function hasUser($key)
178     {
179         return $this->has($this->userKey($key));
180     }
181
182     /**
183      * Add a setting to the database.
184      * @param $key
185      * @param $value
186      * @return bool
187      */
188     public function put($key, $value)
189     {
190         $setting = $this->setting->firstOrNew([
191             'setting_key' => $key
192         ]);
193         $setting->value = $value;
194         $setting->save();
195         $this->clearFromCache($key);
196         return true;
197     }
198
199     /**
200      * Put a user-specific setting into the database.
201      * @param \BookStack\Auth\User $user
202      * @param $key
203      * @param $value
204      * @return bool
205      */
206     public function putUser($user, $key, $value)
207     {
208         if ($user->isDefault()) {
209             return session()->put($key, $value);
210         }
211         return $this->put($this->userKey($user->id, $key), $value);
212     }
213
214     /**
215      * Convert a setting key into a user-specific key.
216      * @param $key
217      * @return string
218      */
219     protected function userKey($userId, $key = '')
220     {
221         return 'user:' . $userId . ':' . $key;
222     }
223
224     /**
225      * Removes a setting from the database.
226      * @param $key
227      * @return bool
228      */
229     public function remove($key)
230     {
231         $setting = $this->getSettingObjectByKey($key);
232         if ($setting) {
233             $setting->delete();
234         }
235         $this->clearFromCache($key);
236         return true;
237     }
238
239     /**
240      * Delete settings for a given user id.
241      * @param $userId
242      * @return mixed
243      */
244     public function deleteUserSettings($userId)
245     {
246         return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
247     }
248
249     /**
250      * Gets a setting model from the database for the given key.
251      * @param $key
252      * @return mixed
253      */
254     protected function getSettingObjectByKey($key)
255     {
256         return $this->setting->where('setting_key', '=', $key)->first();
257     }
258
259
260     /**
261      * Returns an override value for a setting based on certain app conditions.
262      * Used where certain configuration options overrule others.
263      * Returns null if no override value is available.
264      * @param $key
265      * @return bool|null
266      */
267     protected function getOverrideValue($key)
268     {
269         if ($key === 'registration-enabled' && config('auth.method') === 'ldap') {
270             return false;
271         }
272         return null;
273     }
274 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.