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