]> BookStack Code Mirror - bookstack/blob - app/Settings/SettingService.php
Merge branch 'master' into 2019-design
[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      * Gets a setting value from the cache or database.
72      * Looks at the system defaults if not cached or in database.
73      * @param $key
74      * @param $default
75      * @return mixed
76      */
77     protected function getValueFromStore($key, $default)
78     {
79         // Check for an overriding value
80         $overrideValue = $this->getOverrideValue($key);
81         if ($overrideValue !== null) {
82             return $overrideValue;
83         }
84
85         // Check the cache
86         $cacheKey = $this->cachePrefix . $key;
87         $cacheVal = $this->cache->get($cacheKey, null);
88         if ($cacheVal !== null) {
89             return $cacheVal;
90         }
91
92         // Check the database
93         $settingObject = $this->getSettingObjectByKey($key);
94         if ($settingObject !== null) {
95             $value = $settingObject->value;
96             $this->cache->forever($cacheKey, $value);
97             return $value;
98         }
99
100         return $default;
101     }
102
103     /**
104      * Clear an item from the cache completely.
105      * @param $key
106      */
107     protected function clearFromCache($key)
108     {
109         $cacheKey = $this->cachePrefix . $key;
110         $this->cache->forget($cacheKey);
111         if (isset($this->localCache[$key])) {
112             unset($this->localCache[$key]);
113         }
114     }
115
116     /**
117      * Format a settings value
118      * @param $value
119      * @param $default
120      * @return mixed
121      */
122     protected function formatValue($value, $default)
123     {
124         // Change string booleans to actual booleans
125         if ($value === 'true') {
126             $value = true;
127         }
128         if ($value === 'false') {
129             $value = false;
130         }
131
132         // Set to default if empty
133         if ($value === '') {
134             $value = $default;
135         }
136         return $value;
137     }
138
139     /**
140      * Checks if a setting exists.
141      * @param $key
142      * @return bool
143      */
144     public function has($key)
145     {
146         $setting = $this->getSettingObjectByKey($key);
147         return $setting !== null;
148     }
149
150     /**
151      * Check if a user setting is in the database.
152      * @param $key
153      * @return bool
154      */
155     public function hasUser($key)
156     {
157         return $this->has($this->userKey($key));
158     }
159
160     /**
161      * Add a setting to the database.
162      * @param $key
163      * @param $value
164      * @return bool
165      */
166     public function put($key, $value)
167     {
168         $setting = $this->setting->firstOrNew([
169             'setting_key' => $key
170         ]);
171         $setting->value = $value;
172         $setting->save();
173         $this->clearFromCache($key);
174         return true;
175     }
176
177     /**
178      * Put a user-specific setting into the database.
179      * @param \BookStack\Auth\User $user
180      * @param $key
181      * @param $value
182      * @return bool
183      */
184     public function putUser($user, $key, $value)
185     {
186         if ($user->isDefault()) {
187             return session()->put($key, $value);
188         }
189         return $this->put($this->userKey($user->id, $key), $value);
190     }
191
192     /**
193      * Convert a setting key into a user-specific key.
194      * @param $key
195      * @return string
196      */
197     protected function userKey($userId, $key = '')
198     {
199         return 'user:' . $userId . ':' . $key;
200     }
201
202     /**
203      * Removes a setting from the database.
204      * @param $key
205      * @return bool
206      */
207     public function remove($key)
208     {
209         $setting = $this->getSettingObjectByKey($key);
210         if ($setting) {
211             $setting->delete();
212         }
213         $this->clearFromCache($key);
214         return true;
215     }
216
217     /**
218      * Delete settings for a given user id.
219      * @param $userId
220      * @return mixed
221      */
222     public function deleteUserSettings($userId)
223     {
224         return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
225     }
226
227     /**
228      * Gets a setting model from the database for the given key.
229      * @param $key
230      * @return mixed
231      */
232     protected function getSettingObjectByKey($key)
233     {
234         return $this->setting->where('setting_key', '=', $key)->first();
235     }
236
237
238     /**
239      * Returns an override value for a setting based on certain app conditions.
240      * Used where certain configuration options overrule others.
241      * Returns null if no override value is available.
242      * @param $key
243      * @return bool|null
244      */
245     protected function getOverrideValue($key)
246     {
247         if ($key === 'registration-enabled' && config('auth.method') === 'ldap') {
248             return false;
249         }
250         return null;
251     }
252 }