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