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