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