1 <?php namespace BookStack\Settings;
3 use Illuminate\Contracts\Cache\Repository as Cache;
8 * The settings are a simple key-value database store.
10 * @package BookStack\Services
17 protected $localCache = [];
19 protected $cachePrefix = 'setting-';
22 * SettingService constructor.
23 * @param Setting $setting
26 public function __construct(Setting $setting, Cache $cache)
28 $this->setting = $setting;
29 $this->cache = $cache;
33 * Gets a setting from the database,
34 * If not found, Returns default, Which is false by default.
36 * @param string|bool $default
39 public function get($key, $default = false)
41 if ($default === false) {
42 $default = config('setting-defaults.' . $key, false);
45 if (isset($this->localCache[$key])) {
46 return $this->localCache[$key];
49 $value = $this->getValueFromStore($key, $default);
50 $formatted = $this->formatValue($value, $default);
51 $this->localCache[$key] = $formatted;
56 * Get a user-specific setting from the database or cache.
57 * @param \BookStack\Auth\User $user
59 * @param bool $default
62 public function getUser($user, $key, $default = false)
64 if ($user->isDefault()) {
65 return session()->get($key, $default);
67 return $this->get($this->userKey($user->id, $key), $default);
71 * Gets a setting value from the cache or database.
72 * Looks at the system defaults if not cached or in database.
77 protected function getValueFromStore($key, $default)
79 // Check for an overriding value
80 $overrideValue = $this->getOverrideValue($key);
81 if ($overrideValue !== null) {
82 return $overrideValue;
86 $cacheKey = $this->cachePrefix . $key;
87 $cacheVal = $this->cache->get($cacheKey, null);
88 if ($cacheVal !== null) {
93 $settingObject = $this->getSettingObjectByKey($key);
94 if ($settingObject !== null) {
95 $value = $settingObject->value;
96 $this->cache->forever($cacheKey, $value);
104 * Clear an item from the cache completely.
107 protected function clearFromCache($key)
109 $cacheKey = $this->cachePrefix . $key;
110 $this->cache->forget($cacheKey);
111 if (isset($this->localCache[$key])) {
112 unset($this->localCache[$key]);
117 * Format a settings value
122 protected function formatValue($value, $default)
124 // Change string booleans to actual booleans
125 if ($value === 'true') {
128 if ($value === 'false') {
132 // Set to default if empty
140 * Checks if a setting exists.
144 public function has($key)
146 $setting = $this->getSettingObjectByKey($key);
147 return $setting !== null;
151 * Check if a user setting is in the database.
155 public function hasUser($key)
157 return $this->has($this->userKey($key));
161 * Add a setting to the database.
166 public function put($key, $value)
168 $setting = $this->setting->firstOrNew([
169 'setting_key' => $key
171 $setting->value = $value;
173 $this->clearFromCache($key);
178 * Put a user-specific setting into the database.
179 * @param \BookStack\Auth\User $user
184 public function putUser($user, $key, $value)
186 if ($user->isDefault()) {
187 return session()->put($key, $value);
189 return $this->put($this->userKey($user->id, $key), $value);
193 * Convert a setting key into a user-specific key.
197 protected function userKey($userId, $key = '')
199 return 'user:' . $userId . ':' . $key;
203 * Removes a setting from the database.
207 public function remove($key)
209 $setting = $this->getSettingObjectByKey($key);
213 $this->clearFromCache($key);
218 * Delete settings for a given user id.
222 public function deleteUserSettings($userId)
224 return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
228 * Gets a setting model from the database for the given key.
232 protected function getSettingObjectByKey($key)
234 return $this->setting->where('setting_key', '=', $key)->first();
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.
245 protected function getOverrideValue($key)
247 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') {