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