@@ -35,178 +35,178 @@ |
||
| 35 | 35 | */ |
| 36 | 36 | class SystemConfig { |
| 37 | 37 | |
| 38 | - /** @var array */ |
|
| 39 | - protected $sensitiveValues = [ |
|
| 40 | - 'instanceid' => true, |
|
| 41 | - 'datadirectory' => true, |
|
| 42 | - 'dbname' => true, |
|
| 43 | - 'dbhost' => true, |
|
| 44 | - 'dbpassword' => true, |
|
| 45 | - 'dbuser' => true, |
|
| 46 | - 'activity_dbname' => true, |
|
| 47 | - 'activity_dbhost' => true, |
|
| 48 | - 'activity_dbpassword' => true, |
|
| 49 | - 'activity_dbuser' => true, |
|
| 50 | - 'mail_from_address' => true, |
|
| 51 | - 'mail_domain' => true, |
|
| 52 | - 'mail_smtphost' => true, |
|
| 53 | - 'mail_smtpname' => true, |
|
| 54 | - 'mail_smtppassword' => true, |
|
| 55 | - 'passwordsalt' => true, |
|
| 56 | - 'secret' => true, |
|
| 57 | - 'updater.secret' => true, |
|
| 58 | - 'trusted_proxies' => true, |
|
| 59 | - 'proxyuserpwd' => true, |
|
| 60 | - 'sentry.dsn' => true, |
|
| 61 | - 'sentry.public-dsn' => true, |
|
| 62 | - 'zammad.download.secret' => true, |
|
| 63 | - 'zammad.portal.secret' => true, |
|
| 64 | - 'zammad.secret' => true, |
|
| 65 | - 'github.client_id' => true, |
|
| 66 | - 'github.client_secret' => true, |
|
| 67 | - 'log.condition' => [ |
|
| 68 | - 'shared_secret' => true, |
|
| 69 | - ], |
|
| 70 | - 'license-key' => true, |
|
| 71 | - 'redis' => [ |
|
| 72 | - 'host' => true, |
|
| 73 | - 'password' => true, |
|
| 74 | - ], |
|
| 75 | - 'objectstore' => [ |
|
| 76 | - 'arguments' => [ |
|
| 77 | - // Legacy Swift (https://github.com/nextcloud/server/pull/17696#discussion_r341302207) |
|
| 78 | - 'options' => [ |
|
| 79 | - 'credentials' => [ |
|
| 80 | - 'key' => true, |
|
| 81 | - 'secret' => true, |
|
| 82 | - ] |
|
| 83 | - ], |
|
| 84 | - // S3 |
|
| 85 | - 'key' => true, |
|
| 86 | - 'secret' => true, |
|
| 87 | - // Swift v2 |
|
| 88 | - 'username' => true, |
|
| 89 | - 'password' => true, |
|
| 90 | - // Swift v3 |
|
| 91 | - 'user' => [ |
|
| 92 | - 'name' => true, |
|
| 93 | - 'password' => true, |
|
| 94 | - ], |
|
| 95 | - ], |
|
| 96 | - ], |
|
| 97 | - 'objectstore_multibucket' => [ |
|
| 98 | - 'arguments' => [ |
|
| 99 | - 'options' => [ |
|
| 100 | - 'credentials' => [ |
|
| 101 | - 'key' => true, |
|
| 102 | - 'secret' => true, |
|
| 103 | - ] |
|
| 104 | - ], |
|
| 105 | - // S3 |
|
| 106 | - 'key' => true, |
|
| 107 | - 'secret' => true, |
|
| 108 | - // Swift v2 |
|
| 109 | - 'username' => true, |
|
| 110 | - 'password' => true, |
|
| 111 | - // Swift v3 |
|
| 112 | - 'user' => [ |
|
| 113 | - 'name' => true, |
|
| 114 | - 'password' => true, |
|
| 115 | - ], |
|
| 116 | - ], |
|
| 117 | - ], |
|
| 118 | - ]; |
|
| 119 | - |
|
| 120 | - /** @var Config */ |
|
| 121 | - private $config; |
|
| 122 | - |
|
| 123 | - public function __construct(Config $config) { |
|
| 124 | - $this->config = $config; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Lists all available config keys |
|
| 129 | - * @return array an array of key names |
|
| 130 | - */ |
|
| 131 | - public function getKeys() { |
|
| 132 | - return $this->config->getKeys(); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Sets a new system wide value |
|
| 137 | - * |
|
| 138 | - * @param string $key the key of the value, under which will be saved |
|
| 139 | - * @param mixed $value the value that should be stored |
|
| 140 | - */ |
|
| 141 | - public function setValue($key, $value) { |
|
| 142 | - $this->config->setValue($key, $value); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Sets and deletes values and writes the config.php |
|
| 147 | - * |
|
| 148 | - * @param array $configs Associative array with `key => value` pairs |
|
| 149 | - * If value is null, the config key will be deleted |
|
| 150 | - */ |
|
| 151 | - public function setValues(array $configs) { |
|
| 152 | - $this->config->setValues($configs); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Looks up a system wide defined value |
|
| 157 | - * |
|
| 158 | - * @param string $key the key of the value, under which it was saved |
|
| 159 | - * @param mixed $default the default value to be returned if the value isn't set |
|
| 160 | - * @return mixed the value or $default |
|
| 161 | - */ |
|
| 162 | - public function getValue($key, $default = '') { |
|
| 163 | - return $this->config->getValue($key, $default); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Looks up a system wide defined value and filters out sensitive data |
|
| 168 | - * |
|
| 169 | - * @param string $key the key of the value, under which it was saved |
|
| 170 | - * @param mixed $default the default value to be returned if the value isn't set |
|
| 171 | - * @return mixed the value or $default |
|
| 172 | - */ |
|
| 173 | - public function getFilteredValue($key, $default = '') { |
|
| 174 | - $value = $this->getValue($key, $default); |
|
| 175 | - |
|
| 176 | - if (isset($this->sensitiveValues[$key])) { |
|
| 177 | - $value = $this->removeSensitiveValue($this->sensitiveValues[$key], $value); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - return $value; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Delete a system wide defined value |
|
| 185 | - * |
|
| 186 | - * @param string $key the key of the value, under which it was saved |
|
| 187 | - */ |
|
| 188 | - public function deleteValue($key) { |
|
| 189 | - $this->config->deleteKey($key); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * @param bool|array $keysToRemove |
|
| 194 | - * @param mixed $value |
|
| 195 | - * @return mixed |
|
| 196 | - */ |
|
| 197 | - protected function removeSensitiveValue($keysToRemove, $value) { |
|
| 198 | - if ($keysToRemove === true) { |
|
| 199 | - return IConfig::SENSITIVE_VALUE; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - if (is_array($value)) { |
|
| 203 | - foreach ($keysToRemove as $keyToRemove => $valueToRemove) { |
|
| 204 | - if (isset($value[$keyToRemove])) { |
|
| 205 | - $value[$keyToRemove] = $this->removeSensitiveValue($valueToRemove, $value[$keyToRemove]); |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - return $value; |
|
| 211 | - } |
|
| 38 | + /** @var array */ |
|
| 39 | + protected $sensitiveValues = [ |
|
| 40 | + 'instanceid' => true, |
|
| 41 | + 'datadirectory' => true, |
|
| 42 | + 'dbname' => true, |
|
| 43 | + 'dbhost' => true, |
|
| 44 | + 'dbpassword' => true, |
|
| 45 | + 'dbuser' => true, |
|
| 46 | + 'activity_dbname' => true, |
|
| 47 | + 'activity_dbhost' => true, |
|
| 48 | + 'activity_dbpassword' => true, |
|
| 49 | + 'activity_dbuser' => true, |
|
| 50 | + 'mail_from_address' => true, |
|
| 51 | + 'mail_domain' => true, |
|
| 52 | + 'mail_smtphost' => true, |
|
| 53 | + 'mail_smtpname' => true, |
|
| 54 | + 'mail_smtppassword' => true, |
|
| 55 | + 'passwordsalt' => true, |
|
| 56 | + 'secret' => true, |
|
| 57 | + 'updater.secret' => true, |
|
| 58 | + 'trusted_proxies' => true, |
|
| 59 | + 'proxyuserpwd' => true, |
|
| 60 | + 'sentry.dsn' => true, |
|
| 61 | + 'sentry.public-dsn' => true, |
|
| 62 | + 'zammad.download.secret' => true, |
|
| 63 | + 'zammad.portal.secret' => true, |
|
| 64 | + 'zammad.secret' => true, |
|
| 65 | + 'github.client_id' => true, |
|
| 66 | + 'github.client_secret' => true, |
|
| 67 | + 'log.condition' => [ |
|
| 68 | + 'shared_secret' => true, |
|
| 69 | + ], |
|
| 70 | + 'license-key' => true, |
|
| 71 | + 'redis' => [ |
|
| 72 | + 'host' => true, |
|
| 73 | + 'password' => true, |
|
| 74 | + ], |
|
| 75 | + 'objectstore' => [ |
|
| 76 | + 'arguments' => [ |
|
| 77 | + // Legacy Swift (https://github.com/nextcloud/server/pull/17696#discussion_r341302207) |
|
| 78 | + 'options' => [ |
|
| 79 | + 'credentials' => [ |
|
| 80 | + 'key' => true, |
|
| 81 | + 'secret' => true, |
|
| 82 | + ] |
|
| 83 | + ], |
|
| 84 | + // S3 |
|
| 85 | + 'key' => true, |
|
| 86 | + 'secret' => true, |
|
| 87 | + // Swift v2 |
|
| 88 | + 'username' => true, |
|
| 89 | + 'password' => true, |
|
| 90 | + // Swift v3 |
|
| 91 | + 'user' => [ |
|
| 92 | + 'name' => true, |
|
| 93 | + 'password' => true, |
|
| 94 | + ], |
|
| 95 | + ], |
|
| 96 | + ], |
|
| 97 | + 'objectstore_multibucket' => [ |
|
| 98 | + 'arguments' => [ |
|
| 99 | + 'options' => [ |
|
| 100 | + 'credentials' => [ |
|
| 101 | + 'key' => true, |
|
| 102 | + 'secret' => true, |
|
| 103 | + ] |
|
| 104 | + ], |
|
| 105 | + // S3 |
|
| 106 | + 'key' => true, |
|
| 107 | + 'secret' => true, |
|
| 108 | + // Swift v2 |
|
| 109 | + 'username' => true, |
|
| 110 | + 'password' => true, |
|
| 111 | + // Swift v3 |
|
| 112 | + 'user' => [ |
|
| 113 | + 'name' => true, |
|
| 114 | + 'password' => true, |
|
| 115 | + ], |
|
| 116 | + ], |
|
| 117 | + ], |
|
| 118 | + ]; |
|
| 119 | + |
|
| 120 | + /** @var Config */ |
|
| 121 | + private $config; |
|
| 122 | + |
|
| 123 | + public function __construct(Config $config) { |
|
| 124 | + $this->config = $config; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Lists all available config keys |
|
| 129 | + * @return array an array of key names |
|
| 130 | + */ |
|
| 131 | + public function getKeys() { |
|
| 132 | + return $this->config->getKeys(); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Sets a new system wide value |
|
| 137 | + * |
|
| 138 | + * @param string $key the key of the value, under which will be saved |
|
| 139 | + * @param mixed $value the value that should be stored |
|
| 140 | + */ |
|
| 141 | + public function setValue($key, $value) { |
|
| 142 | + $this->config->setValue($key, $value); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Sets and deletes values and writes the config.php |
|
| 147 | + * |
|
| 148 | + * @param array $configs Associative array with `key => value` pairs |
|
| 149 | + * If value is null, the config key will be deleted |
|
| 150 | + */ |
|
| 151 | + public function setValues(array $configs) { |
|
| 152 | + $this->config->setValues($configs); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Looks up a system wide defined value |
|
| 157 | + * |
|
| 158 | + * @param string $key the key of the value, under which it was saved |
|
| 159 | + * @param mixed $default the default value to be returned if the value isn't set |
|
| 160 | + * @return mixed the value or $default |
|
| 161 | + */ |
|
| 162 | + public function getValue($key, $default = '') { |
|
| 163 | + return $this->config->getValue($key, $default); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Looks up a system wide defined value and filters out sensitive data |
|
| 168 | + * |
|
| 169 | + * @param string $key the key of the value, under which it was saved |
|
| 170 | + * @param mixed $default the default value to be returned if the value isn't set |
|
| 171 | + * @return mixed the value or $default |
|
| 172 | + */ |
|
| 173 | + public function getFilteredValue($key, $default = '') { |
|
| 174 | + $value = $this->getValue($key, $default); |
|
| 175 | + |
|
| 176 | + if (isset($this->sensitiveValues[$key])) { |
|
| 177 | + $value = $this->removeSensitiveValue($this->sensitiveValues[$key], $value); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + return $value; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Delete a system wide defined value |
|
| 185 | + * |
|
| 186 | + * @param string $key the key of the value, under which it was saved |
|
| 187 | + */ |
|
| 188 | + public function deleteValue($key) { |
|
| 189 | + $this->config->deleteKey($key); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * @param bool|array $keysToRemove |
|
| 194 | + * @param mixed $value |
|
| 195 | + * @return mixed |
|
| 196 | + */ |
|
| 197 | + protected function removeSensitiveValue($keysToRemove, $value) { |
|
| 198 | + if ($keysToRemove === true) { |
|
| 199 | + return IConfig::SENSITIVE_VALUE; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + if (is_array($value)) { |
|
| 203 | + foreach ($keysToRemove as $keyToRemove => $valueToRemove) { |
|
| 204 | + if (isset($value[$keyToRemove])) { |
|
| 205 | + $value[$keyToRemove] = $this->removeSensitiveValue($valueToRemove, $value[$keyToRemove]); |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + return $value; |
|
| 211 | + } |
|
| 212 | 212 | } |
@@ -43,344 +43,344 @@ |
||
| 43 | 43 | */ |
| 44 | 44 | class AppConfig implements IAppConfig { |
| 45 | 45 | |
| 46 | - /** @var array[] */ |
|
| 47 | - protected $sensitiveValues = [ |
|
| 48 | - 'circles' => [ |
|
| 49 | - '/^local_gskey$/', |
|
| 50 | - ], |
|
| 51 | - 'external' => [ |
|
| 52 | - '/^sites$/', |
|
| 53 | - ], |
|
| 54 | - 'integration_discourse' => [ |
|
| 55 | - '/^private_key$/', |
|
| 56 | - '/^public_key$/', |
|
| 57 | - ], |
|
| 58 | - 'notify_push' => [ |
|
| 59 | - '/^cookie$/', |
|
| 60 | - ], |
|
| 61 | - 'spreed' => [ |
|
| 62 | - '/^bridge_bot_password/', |
|
| 63 | - '/^signaling_servers$/', |
|
| 64 | - '/^signaling_ticket_secret$/', |
|
| 65 | - '/^sip_bridge_dialin_info$/', |
|
| 66 | - '/^sip_bridge_shared_secret$/', |
|
| 67 | - '/^stun_servers$/', |
|
| 68 | - '/^turn_servers$/', |
|
| 69 | - '/^turn_server_secret$/', |
|
| 70 | - ], |
|
| 71 | - 'support' => [ |
|
| 72 | - '/^last_response$/', |
|
| 73 | - '/^potential_subscription_key$/', |
|
| 74 | - '/^subscription_key$/', |
|
| 75 | - ], |
|
| 76 | - 'theming' => [ |
|
| 77 | - '/^imprintUrl$/', |
|
| 78 | - '/^privacyUrl$/', |
|
| 79 | - '/^slogan$/', |
|
| 80 | - '/^url$/', |
|
| 81 | - ], |
|
| 82 | - 'user_ldap' => [ |
|
| 83 | - '/^(s..)?ldap_agent_password$/', |
|
| 84 | - ], |
|
| 85 | - ]; |
|
| 86 | - |
|
| 87 | - /** @var Connection */ |
|
| 88 | - protected $conn; |
|
| 89 | - |
|
| 90 | - /** @var array[] */ |
|
| 91 | - private $cache = []; |
|
| 92 | - |
|
| 93 | - /** @var bool */ |
|
| 94 | - private $configLoaded = false; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @param Connection $conn |
|
| 98 | - */ |
|
| 99 | - public function __construct(Connection $conn) { |
|
| 100 | - $this->conn = $conn; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param string $app |
|
| 105 | - * @return array |
|
| 106 | - */ |
|
| 107 | - private function getAppValues($app) { |
|
| 108 | - $this->loadConfigValues(); |
|
| 109 | - |
|
| 110 | - if (isset($this->cache[$app])) { |
|
| 111 | - return $this->cache[$app]; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return []; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Get all apps using the config |
|
| 119 | - * |
|
| 120 | - * @return array an array of app ids |
|
| 121 | - * |
|
| 122 | - * This function returns a list of all apps that have at least one |
|
| 123 | - * entry in the appconfig table. |
|
| 124 | - */ |
|
| 125 | - public function getApps() { |
|
| 126 | - $this->loadConfigValues(); |
|
| 127 | - |
|
| 128 | - return $this->getSortedKeys($this->cache); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Get the available keys for an app |
|
| 133 | - * |
|
| 134 | - * @param string $app the app we are looking for |
|
| 135 | - * @return array an array of key names |
|
| 136 | - * |
|
| 137 | - * This function gets all keys of an app. Please note that the values are |
|
| 138 | - * not returned. |
|
| 139 | - */ |
|
| 140 | - public function getKeys($app) { |
|
| 141 | - $this->loadConfigValues(); |
|
| 142 | - |
|
| 143 | - if (isset($this->cache[$app])) { |
|
| 144 | - return $this->getSortedKeys($this->cache[$app]); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - return []; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - public function getSortedKeys($data) { |
|
| 151 | - $keys = array_keys($data); |
|
| 152 | - sort($keys); |
|
| 153 | - return $keys; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Gets the config value |
|
| 158 | - * |
|
| 159 | - * @param string $app app |
|
| 160 | - * @param string $key key |
|
| 161 | - * @param string $default = null, default value if the key does not exist |
|
| 162 | - * @return string the value or $default |
|
| 163 | - * |
|
| 164 | - * This function gets a value from the appconfig table. If the key does |
|
| 165 | - * not exist the default value will be returned |
|
| 166 | - */ |
|
| 167 | - public function getValue($app, $key, $default = null) { |
|
| 168 | - $this->loadConfigValues(); |
|
| 169 | - |
|
| 170 | - if ($this->hasKey($app, $key)) { |
|
| 171 | - return $this->cache[$app][$key]; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - return $default; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * check if a key is set in the appconfig |
|
| 179 | - * |
|
| 180 | - * @param string $app |
|
| 181 | - * @param string $key |
|
| 182 | - * @return bool |
|
| 183 | - */ |
|
| 184 | - public function hasKey($app, $key) { |
|
| 185 | - $this->loadConfigValues(); |
|
| 186 | - |
|
| 187 | - return isset($this->cache[$app][$key]); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Sets a value. If the key did not exist before it will be created. |
|
| 192 | - * |
|
| 193 | - * @param string $app app |
|
| 194 | - * @param string $key key |
|
| 195 | - * @param string|float|int $value value |
|
| 196 | - * @return bool True if the value was inserted or updated, false if the value was the same |
|
| 197 | - */ |
|
| 198 | - public function setValue($app, $key, $value) { |
|
| 199 | - if (!$this->hasKey($app, $key)) { |
|
| 200 | - $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [ |
|
| 201 | - 'appid' => $app, |
|
| 202 | - 'configkey' => $key, |
|
| 203 | - 'configvalue' => $value, |
|
| 204 | - ], [ |
|
| 205 | - 'appid', |
|
| 206 | - 'configkey', |
|
| 207 | - ]); |
|
| 208 | - |
|
| 209 | - if ($inserted) { |
|
| 210 | - if (!isset($this->cache[$app])) { |
|
| 211 | - $this->cache[$app] = []; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $this->cache[$app][$key] = $value; |
|
| 215 | - return true; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $sql = $this->conn->getQueryBuilder(); |
|
| 220 | - $sql->update('appconfig') |
|
| 221 | - ->set('configvalue', $sql->createNamedParameter($value)) |
|
| 222 | - ->where($sql->expr()->eq('appid', $sql->createNamedParameter($app))) |
|
| 223 | - ->andWhere($sql->expr()->eq('configkey', $sql->createNamedParameter($key))); |
|
| 224 | - |
|
| 225 | - /* |
|
| 46 | + /** @var array[] */ |
|
| 47 | + protected $sensitiveValues = [ |
|
| 48 | + 'circles' => [ |
|
| 49 | + '/^local_gskey$/', |
|
| 50 | + ], |
|
| 51 | + 'external' => [ |
|
| 52 | + '/^sites$/', |
|
| 53 | + ], |
|
| 54 | + 'integration_discourse' => [ |
|
| 55 | + '/^private_key$/', |
|
| 56 | + '/^public_key$/', |
|
| 57 | + ], |
|
| 58 | + 'notify_push' => [ |
|
| 59 | + '/^cookie$/', |
|
| 60 | + ], |
|
| 61 | + 'spreed' => [ |
|
| 62 | + '/^bridge_bot_password/', |
|
| 63 | + '/^signaling_servers$/', |
|
| 64 | + '/^signaling_ticket_secret$/', |
|
| 65 | + '/^sip_bridge_dialin_info$/', |
|
| 66 | + '/^sip_bridge_shared_secret$/', |
|
| 67 | + '/^stun_servers$/', |
|
| 68 | + '/^turn_servers$/', |
|
| 69 | + '/^turn_server_secret$/', |
|
| 70 | + ], |
|
| 71 | + 'support' => [ |
|
| 72 | + '/^last_response$/', |
|
| 73 | + '/^potential_subscription_key$/', |
|
| 74 | + '/^subscription_key$/', |
|
| 75 | + ], |
|
| 76 | + 'theming' => [ |
|
| 77 | + '/^imprintUrl$/', |
|
| 78 | + '/^privacyUrl$/', |
|
| 79 | + '/^slogan$/', |
|
| 80 | + '/^url$/', |
|
| 81 | + ], |
|
| 82 | + 'user_ldap' => [ |
|
| 83 | + '/^(s..)?ldap_agent_password$/', |
|
| 84 | + ], |
|
| 85 | + ]; |
|
| 86 | + |
|
| 87 | + /** @var Connection */ |
|
| 88 | + protected $conn; |
|
| 89 | + |
|
| 90 | + /** @var array[] */ |
|
| 91 | + private $cache = []; |
|
| 92 | + |
|
| 93 | + /** @var bool */ |
|
| 94 | + private $configLoaded = false; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @param Connection $conn |
|
| 98 | + */ |
|
| 99 | + public function __construct(Connection $conn) { |
|
| 100 | + $this->conn = $conn; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param string $app |
|
| 105 | + * @return array |
|
| 106 | + */ |
|
| 107 | + private function getAppValues($app) { |
|
| 108 | + $this->loadConfigValues(); |
|
| 109 | + |
|
| 110 | + if (isset($this->cache[$app])) { |
|
| 111 | + return $this->cache[$app]; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return []; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Get all apps using the config |
|
| 119 | + * |
|
| 120 | + * @return array an array of app ids |
|
| 121 | + * |
|
| 122 | + * This function returns a list of all apps that have at least one |
|
| 123 | + * entry in the appconfig table. |
|
| 124 | + */ |
|
| 125 | + public function getApps() { |
|
| 126 | + $this->loadConfigValues(); |
|
| 127 | + |
|
| 128 | + return $this->getSortedKeys($this->cache); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Get the available keys for an app |
|
| 133 | + * |
|
| 134 | + * @param string $app the app we are looking for |
|
| 135 | + * @return array an array of key names |
|
| 136 | + * |
|
| 137 | + * This function gets all keys of an app. Please note that the values are |
|
| 138 | + * not returned. |
|
| 139 | + */ |
|
| 140 | + public function getKeys($app) { |
|
| 141 | + $this->loadConfigValues(); |
|
| 142 | + |
|
| 143 | + if (isset($this->cache[$app])) { |
|
| 144 | + return $this->getSortedKeys($this->cache[$app]); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + return []; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + public function getSortedKeys($data) { |
|
| 151 | + $keys = array_keys($data); |
|
| 152 | + sort($keys); |
|
| 153 | + return $keys; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Gets the config value |
|
| 158 | + * |
|
| 159 | + * @param string $app app |
|
| 160 | + * @param string $key key |
|
| 161 | + * @param string $default = null, default value if the key does not exist |
|
| 162 | + * @return string the value or $default |
|
| 163 | + * |
|
| 164 | + * This function gets a value from the appconfig table. If the key does |
|
| 165 | + * not exist the default value will be returned |
|
| 166 | + */ |
|
| 167 | + public function getValue($app, $key, $default = null) { |
|
| 168 | + $this->loadConfigValues(); |
|
| 169 | + |
|
| 170 | + if ($this->hasKey($app, $key)) { |
|
| 171 | + return $this->cache[$app][$key]; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + return $default; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * check if a key is set in the appconfig |
|
| 179 | + * |
|
| 180 | + * @param string $app |
|
| 181 | + * @param string $key |
|
| 182 | + * @return bool |
|
| 183 | + */ |
|
| 184 | + public function hasKey($app, $key) { |
|
| 185 | + $this->loadConfigValues(); |
|
| 186 | + |
|
| 187 | + return isset($this->cache[$app][$key]); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Sets a value. If the key did not exist before it will be created. |
|
| 192 | + * |
|
| 193 | + * @param string $app app |
|
| 194 | + * @param string $key key |
|
| 195 | + * @param string|float|int $value value |
|
| 196 | + * @return bool True if the value was inserted or updated, false if the value was the same |
|
| 197 | + */ |
|
| 198 | + public function setValue($app, $key, $value) { |
|
| 199 | + if (!$this->hasKey($app, $key)) { |
|
| 200 | + $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [ |
|
| 201 | + 'appid' => $app, |
|
| 202 | + 'configkey' => $key, |
|
| 203 | + 'configvalue' => $value, |
|
| 204 | + ], [ |
|
| 205 | + 'appid', |
|
| 206 | + 'configkey', |
|
| 207 | + ]); |
|
| 208 | + |
|
| 209 | + if ($inserted) { |
|
| 210 | + if (!isset($this->cache[$app])) { |
|
| 211 | + $this->cache[$app] = []; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $this->cache[$app][$key] = $value; |
|
| 215 | + return true; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $sql = $this->conn->getQueryBuilder(); |
|
| 220 | + $sql->update('appconfig') |
|
| 221 | + ->set('configvalue', $sql->createNamedParameter($value)) |
|
| 222 | + ->where($sql->expr()->eq('appid', $sql->createNamedParameter($app))) |
|
| 223 | + ->andWhere($sql->expr()->eq('configkey', $sql->createNamedParameter($key))); |
|
| 224 | + |
|
| 225 | + /* |
|
| 226 | 226 | * Only limit to the existing value for non-Oracle DBs: |
| 227 | 227 | * http://docs.oracle.com/cd/E11882_01/server.112/e26088/conditions002.htm#i1033286 |
| 228 | 228 | * > Large objects (LOBs) are not supported in comparison conditions. |
| 229 | 229 | */ |
| 230 | - if (!($this->conn instanceof OracleConnection)) { |
|
| 230 | + if (!($this->conn instanceof OracleConnection)) { |
|
| 231 | 231 | |
| 232 | - /* |
|
| 232 | + /* |
|
| 233 | 233 | * Only update the value when it is not the same |
| 234 | 234 | * Note that NULL requires some special handling. Since comparing |
| 235 | 235 | * against null can have special results. |
| 236 | 236 | */ |
| 237 | 237 | |
| 238 | - if ($value === null) { |
|
| 239 | - $sql->andWhere( |
|
| 240 | - $sql->expr()->isNotNull('configvalue') |
|
| 241 | - ); |
|
| 242 | - } else { |
|
| 243 | - $sql->andWhere( |
|
| 244 | - $sql->expr()->orX( |
|
| 245 | - $sql->expr()->isNull('configvalue'), |
|
| 246 | - $sql->expr()->neq('configvalue', $sql->createNamedParameter($value)) |
|
| 247 | - ) |
|
| 248 | - ); |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $changedRow = (bool) $sql->execute(); |
|
| 253 | - |
|
| 254 | - $this->cache[$app][$key] = $value; |
|
| 255 | - |
|
| 256 | - return $changedRow; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Deletes a key |
|
| 261 | - * |
|
| 262 | - * @param string $app app |
|
| 263 | - * @param string $key key |
|
| 264 | - * @return boolean |
|
| 265 | - */ |
|
| 266 | - public function deleteKey($app, $key) { |
|
| 267 | - $this->loadConfigValues(); |
|
| 268 | - |
|
| 269 | - $sql = $this->conn->getQueryBuilder(); |
|
| 270 | - $sql->delete('appconfig') |
|
| 271 | - ->where($sql->expr()->eq('appid', $sql->createParameter('app'))) |
|
| 272 | - ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey'))) |
|
| 273 | - ->setParameter('app', $app) |
|
| 274 | - ->setParameter('configkey', $key); |
|
| 275 | - $sql->execute(); |
|
| 276 | - |
|
| 277 | - unset($this->cache[$app][$key]); |
|
| 278 | - return false; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Remove app from appconfig |
|
| 283 | - * |
|
| 284 | - * @param string $app app |
|
| 285 | - * @return boolean |
|
| 286 | - * |
|
| 287 | - * Removes all keys in appconfig belonging to the app. |
|
| 288 | - */ |
|
| 289 | - public function deleteApp($app) { |
|
| 290 | - $this->loadConfigValues(); |
|
| 291 | - |
|
| 292 | - $sql = $this->conn->getQueryBuilder(); |
|
| 293 | - $sql->delete('appconfig') |
|
| 294 | - ->where($sql->expr()->eq('appid', $sql->createParameter('app'))) |
|
| 295 | - ->setParameter('app', $app); |
|
| 296 | - $sql->execute(); |
|
| 297 | - |
|
| 298 | - unset($this->cache[$app]); |
|
| 299 | - return false; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * get multiple values, either the app or key can be used as wildcard by setting it to false |
|
| 304 | - * |
|
| 305 | - * @param string|false $app |
|
| 306 | - * @param string|false $key |
|
| 307 | - * @return array|false |
|
| 308 | - */ |
|
| 309 | - public function getValues($app, $key) { |
|
| 310 | - if (($app !== false) === ($key !== false)) { |
|
| 311 | - return false; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - if ($key === false) { |
|
| 315 | - return $this->getAppValues($app); |
|
| 316 | - } else { |
|
| 317 | - $appIds = $this->getApps(); |
|
| 318 | - $values = array_map(function ($appId) use ($key) { |
|
| 319 | - return isset($this->cache[$appId][$key]) ? $this->cache[$appId][$key] : null; |
|
| 320 | - }, $appIds); |
|
| 321 | - $result = array_combine($appIds, $values); |
|
| 322 | - |
|
| 323 | - return array_filter($result); |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * get all values of the app or and filters out sensitive data |
|
| 329 | - * |
|
| 330 | - * @param string $app |
|
| 331 | - * @return array |
|
| 332 | - */ |
|
| 333 | - public function getFilteredValues($app) { |
|
| 334 | - $values = $this->getValues($app, false); |
|
| 335 | - |
|
| 336 | - if (isset($this->sensitiveValues[$app])) { |
|
| 337 | - foreach ($this->sensitiveValues[$app] as $sensitiveKeyExp) { |
|
| 338 | - $sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values)); |
|
| 339 | - foreach ($sensitiveKeys as $sensitiveKey) { |
|
| 340 | - $values[$sensitiveKey] = IConfig::SENSITIVE_VALUE; |
|
| 341 | - } |
|
| 342 | - } |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - return $values; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Load all the app config values |
|
| 350 | - */ |
|
| 351 | - protected function loadConfigValues() { |
|
| 352 | - if ($this->configLoaded) { |
|
| 353 | - return; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - $this->cache = []; |
|
| 357 | - |
|
| 358 | - $sql = $this->conn->getQueryBuilder(); |
|
| 359 | - $sql->select('*') |
|
| 360 | - ->from('appconfig'); |
|
| 361 | - $result = $sql->execute(); |
|
| 362 | - |
|
| 363 | - // we are going to store the result in memory anyway |
|
| 364 | - $rows = $result->fetchAll(); |
|
| 365 | - foreach ($rows as $row) { |
|
| 366 | - if (!isset($this->cache[$row['appid']])) { |
|
| 367 | - $this->cache[(string)$row['appid']] = []; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - $this->cache[(string)$row['appid']][(string)$row['configkey']] = (string)$row['configvalue']; |
|
| 371 | - } |
|
| 372 | - $result->closeCursor(); |
|
| 373 | - |
|
| 374 | - $this->configLoaded = true; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * Clear all the cached app config values |
|
| 379 | - * |
|
| 380 | - * WARNING: do not use this - this is only for usage with the SCSSCacher to |
|
| 381 | - * clear the memory cache of the app config |
|
| 382 | - */ |
|
| 383 | - public function clearCachedConfig() { |
|
| 384 | - $this->configLoaded = false; |
|
| 385 | - } |
|
| 238 | + if ($value === null) { |
|
| 239 | + $sql->andWhere( |
|
| 240 | + $sql->expr()->isNotNull('configvalue') |
|
| 241 | + ); |
|
| 242 | + } else { |
|
| 243 | + $sql->andWhere( |
|
| 244 | + $sql->expr()->orX( |
|
| 245 | + $sql->expr()->isNull('configvalue'), |
|
| 246 | + $sql->expr()->neq('configvalue', $sql->createNamedParameter($value)) |
|
| 247 | + ) |
|
| 248 | + ); |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $changedRow = (bool) $sql->execute(); |
|
| 253 | + |
|
| 254 | + $this->cache[$app][$key] = $value; |
|
| 255 | + |
|
| 256 | + return $changedRow; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Deletes a key |
|
| 261 | + * |
|
| 262 | + * @param string $app app |
|
| 263 | + * @param string $key key |
|
| 264 | + * @return boolean |
|
| 265 | + */ |
|
| 266 | + public function deleteKey($app, $key) { |
|
| 267 | + $this->loadConfigValues(); |
|
| 268 | + |
|
| 269 | + $sql = $this->conn->getQueryBuilder(); |
|
| 270 | + $sql->delete('appconfig') |
|
| 271 | + ->where($sql->expr()->eq('appid', $sql->createParameter('app'))) |
|
| 272 | + ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey'))) |
|
| 273 | + ->setParameter('app', $app) |
|
| 274 | + ->setParameter('configkey', $key); |
|
| 275 | + $sql->execute(); |
|
| 276 | + |
|
| 277 | + unset($this->cache[$app][$key]); |
|
| 278 | + return false; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Remove app from appconfig |
|
| 283 | + * |
|
| 284 | + * @param string $app app |
|
| 285 | + * @return boolean |
|
| 286 | + * |
|
| 287 | + * Removes all keys in appconfig belonging to the app. |
|
| 288 | + */ |
|
| 289 | + public function deleteApp($app) { |
|
| 290 | + $this->loadConfigValues(); |
|
| 291 | + |
|
| 292 | + $sql = $this->conn->getQueryBuilder(); |
|
| 293 | + $sql->delete('appconfig') |
|
| 294 | + ->where($sql->expr()->eq('appid', $sql->createParameter('app'))) |
|
| 295 | + ->setParameter('app', $app); |
|
| 296 | + $sql->execute(); |
|
| 297 | + |
|
| 298 | + unset($this->cache[$app]); |
|
| 299 | + return false; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * get multiple values, either the app or key can be used as wildcard by setting it to false |
|
| 304 | + * |
|
| 305 | + * @param string|false $app |
|
| 306 | + * @param string|false $key |
|
| 307 | + * @return array|false |
|
| 308 | + */ |
|
| 309 | + public function getValues($app, $key) { |
|
| 310 | + if (($app !== false) === ($key !== false)) { |
|
| 311 | + return false; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + if ($key === false) { |
|
| 315 | + return $this->getAppValues($app); |
|
| 316 | + } else { |
|
| 317 | + $appIds = $this->getApps(); |
|
| 318 | + $values = array_map(function ($appId) use ($key) { |
|
| 319 | + return isset($this->cache[$appId][$key]) ? $this->cache[$appId][$key] : null; |
|
| 320 | + }, $appIds); |
|
| 321 | + $result = array_combine($appIds, $values); |
|
| 322 | + |
|
| 323 | + return array_filter($result); |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * get all values of the app or and filters out sensitive data |
|
| 329 | + * |
|
| 330 | + * @param string $app |
|
| 331 | + * @return array |
|
| 332 | + */ |
|
| 333 | + public function getFilteredValues($app) { |
|
| 334 | + $values = $this->getValues($app, false); |
|
| 335 | + |
|
| 336 | + if (isset($this->sensitiveValues[$app])) { |
|
| 337 | + foreach ($this->sensitiveValues[$app] as $sensitiveKeyExp) { |
|
| 338 | + $sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values)); |
|
| 339 | + foreach ($sensitiveKeys as $sensitiveKey) { |
|
| 340 | + $values[$sensitiveKey] = IConfig::SENSITIVE_VALUE; |
|
| 341 | + } |
|
| 342 | + } |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + return $values; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Load all the app config values |
|
| 350 | + */ |
|
| 351 | + protected function loadConfigValues() { |
|
| 352 | + if ($this->configLoaded) { |
|
| 353 | + return; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + $this->cache = []; |
|
| 357 | + |
|
| 358 | + $sql = $this->conn->getQueryBuilder(); |
|
| 359 | + $sql->select('*') |
|
| 360 | + ->from('appconfig'); |
|
| 361 | + $result = $sql->execute(); |
|
| 362 | + |
|
| 363 | + // we are going to store the result in memory anyway |
|
| 364 | + $rows = $result->fetchAll(); |
|
| 365 | + foreach ($rows as $row) { |
|
| 366 | + if (!isset($this->cache[$row['appid']])) { |
|
| 367 | + $this->cache[(string)$row['appid']] = []; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + $this->cache[(string)$row['appid']][(string)$row['configkey']] = (string)$row['configvalue']; |
|
| 371 | + } |
|
| 372 | + $result->closeCursor(); |
|
| 373 | + |
|
| 374 | + $this->configLoaded = true; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Clear all the cached app config values |
|
| 379 | + * |
|
| 380 | + * WARNING: do not use this - this is only for usage with the SCSSCacher to |
|
| 381 | + * clear the memory cache of the app config |
|
| 382 | + */ |
|
| 383 | + public function clearCachedConfig() { |
|
| 384 | + $this->configLoaded = false; |
|
| 385 | + } |
|
| 386 | 386 | } |