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