]> BookStack Code Mirror - bookstack/blob - app/Services/SettingService.php
set uploaded files public visibliity (relevant for S3 storage)
[bookstack] / app / Services / SettingService.php
1 <?php namespace BookStack\Services;
2
3 use BookStack\Setting;
4 use Illuminate\Contracts\Cache\Repository as Cache;
5
6 /**
7  * Class SettingService
8  *
9  * The settings are a simple key-value database store.
10  *
11  * @package BookStack\Services
12  */
13 class SettingService
14 {
15
16     protected $setting;
17     protected $cache;
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         $value = $this->getValueFromStore($key, $default);
42         return $this->formatValue($value, $default);
43     }
44
45     /**
46      * Gets a setting value from the cache or database.
47      * Looks at the system defaults if not cached or in database.
48      * @param $key
49      * @param $default
50      * @return mixed
51      */
52     protected function getValueFromStore($key, $default)
53     {
54         // Check for an overriding value
55         $overrideValue = $this->getOverrideValue($key);
56         if ($overrideValue !== null) return $overrideValue;
57
58         // Check the cache
59         $cacheKey = $this->cachePrefix . $key;
60         if ($this->cache->has($cacheKey)) {
61             return $this->cache->get($cacheKey);
62         }
63
64         // Check the database
65         $settingObject = $this->getSettingObjectByKey($key);
66         if ($settingObject !== null) {
67             $value = $settingObject->value;
68             $this->cache->forever($cacheKey, $value);
69             return $value;
70         }
71
72         // Check the defaults set in the app config.
73         $configPrefix = 'setting-defaults.' . $key;
74         if (config()->has($configPrefix)) {
75             $value = config($configPrefix);
76             $this->cache->forever($cacheKey, $value);
77             return $value;
78         }
79
80         return $default;
81     }
82
83     /**
84      * Clear an item from the cache completely.
85      * @param $key
86      */
87     protected function clearFromCache($key)
88     {
89         $cacheKey = $this->cachePrefix . $key;
90         $this->cache->forget($cacheKey);
91     }
92
93     /**
94      * Format a settings value
95      * @param $value
96      * @param $default
97      * @return mixed
98      */
99     protected function formatValue($value, $default)
100     {
101         // Change string booleans to actual booleans
102         if ($value === 'true') $value = true;
103         if ($value === 'false') $value = false;
104
105         // Set to default if empty
106         if ($value === '') $value = $default;
107         return $value;
108     }
109
110     /**
111      * Checks if a setting exists.
112      * @param $key
113      * @return bool
114      */
115     public function has($key)
116     {
117         $setting = $this->getSettingObjectByKey($key);
118         return $setting !== null;
119     }
120
121     /**
122      * Add a setting to the database.
123      * @param $key
124      * @param $value
125      * @return bool
126      */
127     public function put($key, $value)
128     {
129         $setting = $this->setting->firstOrNew([
130             'setting_key' => $key
131         ]);
132         $setting->value = $value;
133         $setting->save();
134         $this->clearFromCache($key);
135         return true;
136     }
137
138     /**
139      * Removes a setting from the database.
140      * @param $key
141      * @return bool
142      */
143     public function remove($key)
144     {
145         $setting = $this->getSettingObjectByKey($key);
146         if ($setting) {
147             $setting->delete();
148         }
149         $this->clearFromCache($key);
150         return true;
151     }
152
153     /**
154      * Gets a setting model from the database for the given key.
155      * @param $key
156      * @return mixed
157      */
158     protected function getSettingObjectByKey($key)
159     {
160         return $this->setting->where('setting_key', '=', $key)->first();
161     }
162
163
164     /**
165      * Returns an override value for a setting based on certain app conditions.
166      * Used where certain configuration options overrule others.
167      * Returns null if no override value is available.
168      * @param $key
169      * @return bool|null
170      */
171     protected function getOverrideValue($key)
172     {
173         if ($key === 'registration-enabled' && config('auth.method') === 'ldap') return false;
174         return null;
175     }
176
177 }