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