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