]> BookStack Code Mirror - bookstack/blob - app/Settings/SettingService.php
Added code editor changes mobile design handling
[bookstack] / app / Settings / SettingService.php
1 <?php
2
3 namespace BookStack\Settings;
4
5 use BookStack\Auth\User;
6 use Illuminate\Contracts\Cache\Repository as Cache;
7
8 /**
9  * Class SettingService
10  * The settings are a simple key-value database store.
11  * For non-authenticated users, user settings are stored via the session instead.
12  */
13 class SettingService
14 {
15     protected $setting;
16     protected $cache;
17     protected $localCache = [];
18
19     protected $cachePrefix = 'setting-';
20
21     /**
22      * SettingService constructor.
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      */
34     public function get(string $key, $default = null)
35     {
36         if (is_null($default)) {
37             $default = config('setting-defaults.' . $key, false);
38         }
39
40         if (isset($this->localCache[$key])) {
41             return $this->localCache[$key];
42         }
43
44         $value = $this->getValueFromStore($key) ?? $default;
45         $formatted = $this->formatValue($value, $default);
46         $this->localCache[$key] = $formatted;
47
48         return $formatted;
49     }
50
51     /**
52      * Get a value from the session instead of the main store option.
53      */
54     protected function getFromSession(string $key, $default = false)
55     {
56         $value = session()->get($key, $default);
57
58         return $this->formatValue($value, $default);
59     }
60
61     /**
62      * Get a user-specific setting from the database or cache.
63      */
64     public function getUser(User $user, string $key, $default = null)
65     {
66         if (is_null($default)) {
67             $default = config('setting-defaults.user.' . $key, false);
68         }
69
70         if ($user->isDefault()) {
71             return $this->getFromSession($key, $default);
72         }
73
74         return $this->get($this->userKey($user->id, $key), $default);
75     }
76
77     /**
78      * Get a value for the current logged-in user.
79      */
80     public function getForCurrentUser(string $key, $default = null)
81     {
82         return $this->getUser(user(), $key, $default);
83     }
84
85     /**
86      * Gets a setting value from the cache or database.
87      * Looks at the system defaults if not cached or in database.
88      * Returns null if nothing is found.
89      */
90     protected function getValueFromStore(string $key)
91     {
92         // Check the cache
93         $cacheKey = $this->cachePrefix . $key;
94         $cacheVal = $this->cache->get($cacheKey, null);
95         if ($cacheVal !== null) {
96             return $cacheVal;
97         }
98
99         // Check the database
100         $settingObject = $this->getSettingObjectByKey($key);
101         if ($settingObject !== null) {
102             $value = $settingObject->value;
103
104             if ($settingObject->type === 'array') {
105                 $value = json_decode($value, true) ?? [];
106             }
107
108             $this->cache->forever($cacheKey, $value);
109
110             return $value;
111         }
112
113         return null;
114     }
115
116     /**
117      * Clear an item from the cache completely.
118      */
119     protected function clearFromCache(string $key)
120     {
121         $cacheKey = $this->cachePrefix . $key;
122         $this->cache->forget($cacheKey);
123         if (isset($this->localCache[$key])) {
124             unset($this->localCache[$key]);
125         }
126     }
127
128     /**
129      * Format a settings value.
130      */
131     protected function formatValue($value, $default)
132     {
133         // Change string booleans to actual booleans
134         if ($value === 'true') {
135             $value = true;
136         } elseif ($value === 'false') {
137             $value = false;
138         }
139
140         // Set to default if empty
141         if ($value === '') {
142             $value = $default;
143         }
144
145         return $value;
146     }
147
148     /**
149      * Checks if a setting exists.
150      */
151     public function has(string $key): bool
152     {
153         $setting = $this->getSettingObjectByKey($key);
154
155         return $setting !== null;
156     }
157
158     /**
159      * Add a setting to the database.
160      * Values can be an array or a string.
161      */
162     public function put(string $key, $value): bool
163     {
164         $setting = $this->setting->newQuery()->firstOrNew([
165             'setting_key' => $key,
166         ]);
167         $setting->type = 'string';
168
169         if (is_array($value)) {
170             $setting->type = 'array';
171             $value = $this->formatArrayValue($value);
172         }
173
174         $setting->value = $value;
175         $setting->save();
176         $this->clearFromCache($key);
177
178         return true;
179     }
180
181     /**
182      * Format an array to be stored as a setting.
183      * Array setting types are expected to be a flat array of child key=>value array items.
184      * This filters out any child items that are empty.
185      */
186     protected function formatArrayValue(array $value): string
187     {
188         $values = collect($value)->values()->filter(function (array $item) {
189             return count(array_filter($item)) > 0;
190         });
191
192         return json_encode($values);
193     }
194
195     /**
196      * Put a user-specific setting into the database.
197      */
198     public function putUser(User $user, string $key, string $value): bool
199     {
200         if ($user->isDefault()) {
201             session()->put($key, $value);
202
203             return true;
204         }
205
206         return $this->put($this->userKey($user->id, $key), $value);
207     }
208
209     /**
210      * Convert a setting key into a user-specific key.
211      */
212     protected function userKey(string $userId, string $key = ''): string
213     {
214         return 'user:' . $userId . ':' . $key;
215     }
216
217     /**
218      * Removes a setting from the database.
219      */
220     public function remove(string $key): void
221     {
222         $setting = $this->getSettingObjectByKey($key);
223         if ($setting) {
224             $setting->delete();
225         }
226         $this->clearFromCache($key);
227     }
228
229     /**
230      * Delete settings for a given user id.
231      */
232     public function deleteUserSettings(string $userId)
233     {
234         return $this->setting->newQuery()
235             ->where('setting_key', 'like', $this->userKey($userId) . '%')
236             ->delete();
237     }
238
239     /**
240      * Gets a setting model from the database for the given key.
241      */
242     protected function getSettingObjectByKey(string $key): ?Setting
243     {
244         return $this->setting->newQuery()
245             ->where('setting_key', '=', $key)->first();
246     }
247 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.