@@ -43,394 +43,394 @@ |
||
| 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 | - 'integration_dropbox' => [ |
|
| 59 | - '/^client_id$/', |
|
| 60 | - '/^client_secret$/', |
|
| 61 | - ], |
|
| 62 | - 'integration_github' => [ |
|
| 63 | - '/^client_id$/', |
|
| 64 | - '/^client_secret$/', |
|
| 65 | - ], |
|
| 66 | - 'integration_gitlab' => [ |
|
| 67 | - '/^client_id$/', |
|
| 68 | - '/^client_secret$/', |
|
| 69 | - '/^oauth_instance_url$/', |
|
| 70 | - ], |
|
| 71 | - 'integration_google' => [ |
|
| 72 | - '/^client_id$/', |
|
| 73 | - '/^client_secret$/', |
|
| 74 | - ], |
|
| 75 | - 'integration_jira' => [ |
|
| 76 | - '/^client_id$/', |
|
| 77 | - '/^client_secret$/', |
|
| 78 | - '/^forced_instance_url$/', |
|
| 79 | - ], |
|
| 80 | - 'integration_onedrive' => [ |
|
| 81 | - '/^client_id$/', |
|
| 82 | - '/^client_secret$/', |
|
| 83 | - ], |
|
| 84 | - 'integration_openproject' => [ |
|
| 85 | - '/^client_id$/', |
|
| 86 | - '/^client_secret$/', |
|
| 87 | - '/^oauth_instance_url$/', |
|
| 88 | - ], |
|
| 89 | - 'integration_reddit' => [ |
|
| 90 | - '/^client_id$/', |
|
| 91 | - '/^client_secret$/', |
|
| 92 | - ], |
|
| 93 | - 'integration_suitecrm' => [ |
|
| 94 | - '/^client_id$/', |
|
| 95 | - '/^client_secret$/', |
|
| 96 | - '/^oauth_instance_url$/', |
|
| 97 | - ], |
|
| 98 | - 'integration_twitter' => [ |
|
| 99 | - '/^consumer_key$/', |
|
| 100 | - '/^consumer_secret$/', |
|
| 101 | - '/^followed_user$/', |
|
| 102 | - ], |
|
| 103 | - 'integration_zammad' => [ |
|
| 104 | - '/^client_id$/', |
|
| 105 | - '/^client_secret$/', |
|
| 106 | - '/^oauth_instance_url$/', |
|
| 107 | - ], |
|
| 108 | - 'notify_push' => [ |
|
| 109 | - '/^cookie$/', |
|
| 110 | - ], |
|
| 111 | - 'spreed' => [ |
|
| 112 | - '/^bridge_bot_password/', |
|
| 113 | - '/^signaling_servers$/', |
|
| 114 | - '/^signaling_ticket_secret$/', |
|
| 115 | - '/^sip_bridge_dialin_info$/', |
|
| 116 | - '/^sip_bridge_shared_secret$/', |
|
| 117 | - '/^stun_servers$/', |
|
| 118 | - '/^turn_servers$/', |
|
| 119 | - '/^turn_server_secret$/', |
|
| 120 | - ], |
|
| 121 | - 'support' => [ |
|
| 122 | - '/^last_response$/', |
|
| 123 | - '/^potential_subscription_key$/', |
|
| 124 | - '/^subscription_key$/', |
|
| 125 | - ], |
|
| 126 | - 'theming' => [ |
|
| 127 | - '/^imprintUrl$/', |
|
| 128 | - '/^privacyUrl$/', |
|
| 129 | - '/^slogan$/', |
|
| 130 | - '/^url$/', |
|
| 131 | - ], |
|
| 132 | - 'user_ldap' => [ |
|
| 133 | - '/^(s..)?ldap_agent_password$/', |
|
| 134 | - ], |
|
| 135 | - ]; |
|
| 136 | - |
|
| 137 | - /** @var Connection */ |
|
| 138 | - protected $conn; |
|
| 139 | - |
|
| 140 | - /** @var array[] */ |
|
| 141 | - private $cache = []; |
|
| 142 | - |
|
| 143 | - /** @var bool */ |
|
| 144 | - private $configLoaded = false; |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @param Connection $conn |
|
| 148 | - */ |
|
| 149 | - public function __construct(Connection $conn) { |
|
| 150 | - $this->conn = $conn; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @param string $app |
|
| 155 | - * @return array |
|
| 156 | - */ |
|
| 157 | - private function getAppValues($app) { |
|
| 158 | - $this->loadConfigValues(); |
|
| 159 | - |
|
| 160 | - if (isset($this->cache[$app])) { |
|
| 161 | - return $this->cache[$app]; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - return []; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Get all apps using the config |
|
| 169 | - * |
|
| 170 | - * @return array an array of app ids |
|
| 171 | - * |
|
| 172 | - * This function returns a list of all apps that have at least one |
|
| 173 | - * entry in the appconfig table. |
|
| 174 | - */ |
|
| 175 | - public function getApps() { |
|
| 176 | - $this->loadConfigValues(); |
|
| 177 | - |
|
| 178 | - return $this->getSortedKeys($this->cache); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Get the available keys for an app |
|
| 183 | - * |
|
| 184 | - * @param string $app the app we are looking for |
|
| 185 | - * @return array an array of key names |
|
| 186 | - * |
|
| 187 | - * This function gets all keys of an app. Please note that the values are |
|
| 188 | - * not returned. |
|
| 189 | - */ |
|
| 190 | - public function getKeys($app) { |
|
| 191 | - $this->loadConfigValues(); |
|
| 192 | - |
|
| 193 | - if (isset($this->cache[$app])) { |
|
| 194 | - return $this->getSortedKeys($this->cache[$app]); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - return []; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - public function getSortedKeys($data) { |
|
| 201 | - $keys = array_keys($data); |
|
| 202 | - sort($keys); |
|
| 203 | - return $keys; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Gets the config value |
|
| 208 | - * |
|
| 209 | - * @param string $app app |
|
| 210 | - * @param string $key key |
|
| 211 | - * @param string $default = null, default value if the key does not exist |
|
| 212 | - * @return string the value or $default |
|
| 213 | - * |
|
| 214 | - * This function gets a value from the appconfig table. If the key does |
|
| 215 | - * not exist the default value will be returned |
|
| 216 | - */ |
|
| 217 | - public function getValue($app, $key, $default = null) { |
|
| 218 | - $this->loadConfigValues(); |
|
| 219 | - |
|
| 220 | - if ($this->hasKey($app, $key)) { |
|
| 221 | - return $this->cache[$app][$key]; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - return $default; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * check if a key is set in the appconfig |
|
| 229 | - * |
|
| 230 | - * @param string $app |
|
| 231 | - * @param string $key |
|
| 232 | - * @return bool |
|
| 233 | - */ |
|
| 234 | - public function hasKey($app, $key) { |
|
| 235 | - $this->loadConfigValues(); |
|
| 236 | - |
|
| 237 | - return isset($this->cache[$app][$key]); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Sets a value. If the key did not exist before it will be created. |
|
| 242 | - * |
|
| 243 | - * @param string $app app |
|
| 244 | - * @param string $key key |
|
| 245 | - * @param string|float|int $value value |
|
| 246 | - * @return bool True if the value was inserted or updated, false if the value was the same |
|
| 247 | - */ |
|
| 248 | - public function setValue($app, $key, $value) { |
|
| 249 | - if (!$this->hasKey($app, $key)) { |
|
| 250 | - $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [ |
|
| 251 | - 'appid' => $app, |
|
| 252 | - 'configkey' => $key, |
|
| 253 | - 'configvalue' => $value, |
|
| 254 | - ], [ |
|
| 255 | - 'appid', |
|
| 256 | - 'configkey', |
|
| 257 | - ]); |
|
| 258 | - |
|
| 259 | - if ($inserted) { |
|
| 260 | - if (!isset($this->cache[$app])) { |
|
| 261 | - $this->cache[$app] = []; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - $this->cache[$app][$key] = $value; |
|
| 265 | - return true; |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - $sql = $this->conn->getQueryBuilder(); |
|
| 270 | - $sql->update('appconfig') |
|
| 271 | - ->set('configvalue', $sql->createNamedParameter($value)) |
|
| 272 | - ->where($sql->expr()->eq('appid', $sql->createNamedParameter($app))) |
|
| 273 | - ->andWhere($sql->expr()->eq('configkey', $sql->createNamedParameter($key))); |
|
| 274 | - |
|
| 275 | - /* |
|
| 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 | + 'integration_dropbox' => [ |
|
| 59 | + '/^client_id$/', |
|
| 60 | + '/^client_secret$/', |
|
| 61 | + ], |
|
| 62 | + 'integration_github' => [ |
|
| 63 | + '/^client_id$/', |
|
| 64 | + '/^client_secret$/', |
|
| 65 | + ], |
|
| 66 | + 'integration_gitlab' => [ |
|
| 67 | + '/^client_id$/', |
|
| 68 | + '/^client_secret$/', |
|
| 69 | + '/^oauth_instance_url$/', |
|
| 70 | + ], |
|
| 71 | + 'integration_google' => [ |
|
| 72 | + '/^client_id$/', |
|
| 73 | + '/^client_secret$/', |
|
| 74 | + ], |
|
| 75 | + 'integration_jira' => [ |
|
| 76 | + '/^client_id$/', |
|
| 77 | + '/^client_secret$/', |
|
| 78 | + '/^forced_instance_url$/', |
|
| 79 | + ], |
|
| 80 | + 'integration_onedrive' => [ |
|
| 81 | + '/^client_id$/', |
|
| 82 | + '/^client_secret$/', |
|
| 83 | + ], |
|
| 84 | + 'integration_openproject' => [ |
|
| 85 | + '/^client_id$/', |
|
| 86 | + '/^client_secret$/', |
|
| 87 | + '/^oauth_instance_url$/', |
|
| 88 | + ], |
|
| 89 | + 'integration_reddit' => [ |
|
| 90 | + '/^client_id$/', |
|
| 91 | + '/^client_secret$/', |
|
| 92 | + ], |
|
| 93 | + 'integration_suitecrm' => [ |
|
| 94 | + '/^client_id$/', |
|
| 95 | + '/^client_secret$/', |
|
| 96 | + '/^oauth_instance_url$/', |
|
| 97 | + ], |
|
| 98 | + 'integration_twitter' => [ |
|
| 99 | + '/^consumer_key$/', |
|
| 100 | + '/^consumer_secret$/', |
|
| 101 | + '/^followed_user$/', |
|
| 102 | + ], |
|
| 103 | + 'integration_zammad' => [ |
|
| 104 | + '/^client_id$/', |
|
| 105 | + '/^client_secret$/', |
|
| 106 | + '/^oauth_instance_url$/', |
|
| 107 | + ], |
|
| 108 | + 'notify_push' => [ |
|
| 109 | + '/^cookie$/', |
|
| 110 | + ], |
|
| 111 | + 'spreed' => [ |
|
| 112 | + '/^bridge_bot_password/', |
|
| 113 | + '/^signaling_servers$/', |
|
| 114 | + '/^signaling_ticket_secret$/', |
|
| 115 | + '/^sip_bridge_dialin_info$/', |
|
| 116 | + '/^sip_bridge_shared_secret$/', |
|
| 117 | + '/^stun_servers$/', |
|
| 118 | + '/^turn_servers$/', |
|
| 119 | + '/^turn_server_secret$/', |
|
| 120 | + ], |
|
| 121 | + 'support' => [ |
|
| 122 | + '/^last_response$/', |
|
| 123 | + '/^potential_subscription_key$/', |
|
| 124 | + '/^subscription_key$/', |
|
| 125 | + ], |
|
| 126 | + 'theming' => [ |
|
| 127 | + '/^imprintUrl$/', |
|
| 128 | + '/^privacyUrl$/', |
|
| 129 | + '/^slogan$/', |
|
| 130 | + '/^url$/', |
|
| 131 | + ], |
|
| 132 | + 'user_ldap' => [ |
|
| 133 | + '/^(s..)?ldap_agent_password$/', |
|
| 134 | + ], |
|
| 135 | + ]; |
|
| 136 | + |
|
| 137 | + /** @var Connection */ |
|
| 138 | + protected $conn; |
|
| 139 | + |
|
| 140 | + /** @var array[] */ |
|
| 141 | + private $cache = []; |
|
| 142 | + |
|
| 143 | + /** @var bool */ |
|
| 144 | + private $configLoaded = false; |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @param Connection $conn |
|
| 148 | + */ |
|
| 149 | + public function __construct(Connection $conn) { |
|
| 150 | + $this->conn = $conn; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @param string $app |
|
| 155 | + * @return array |
|
| 156 | + */ |
|
| 157 | + private function getAppValues($app) { |
|
| 158 | + $this->loadConfigValues(); |
|
| 159 | + |
|
| 160 | + if (isset($this->cache[$app])) { |
|
| 161 | + return $this->cache[$app]; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + return []; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Get all apps using the config |
|
| 169 | + * |
|
| 170 | + * @return array an array of app ids |
|
| 171 | + * |
|
| 172 | + * This function returns a list of all apps that have at least one |
|
| 173 | + * entry in the appconfig table. |
|
| 174 | + */ |
|
| 175 | + public function getApps() { |
|
| 176 | + $this->loadConfigValues(); |
|
| 177 | + |
|
| 178 | + return $this->getSortedKeys($this->cache); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Get the available keys for an app |
|
| 183 | + * |
|
| 184 | + * @param string $app the app we are looking for |
|
| 185 | + * @return array an array of key names |
|
| 186 | + * |
|
| 187 | + * This function gets all keys of an app. Please note that the values are |
|
| 188 | + * not returned. |
|
| 189 | + */ |
|
| 190 | + public function getKeys($app) { |
|
| 191 | + $this->loadConfigValues(); |
|
| 192 | + |
|
| 193 | + if (isset($this->cache[$app])) { |
|
| 194 | + return $this->getSortedKeys($this->cache[$app]); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + return []; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + public function getSortedKeys($data) { |
|
| 201 | + $keys = array_keys($data); |
|
| 202 | + sort($keys); |
|
| 203 | + return $keys; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Gets the config value |
|
| 208 | + * |
|
| 209 | + * @param string $app app |
|
| 210 | + * @param string $key key |
|
| 211 | + * @param string $default = null, default value if the key does not exist |
|
| 212 | + * @return string the value or $default |
|
| 213 | + * |
|
| 214 | + * This function gets a value from the appconfig table. If the key does |
|
| 215 | + * not exist the default value will be returned |
|
| 216 | + */ |
|
| 217 | + public function getValue($app, $key, $default = null) { |
|
| 218 | + $this->loadConfigValues(); |
|
| 219 | + |
|
| 220 | + if ($this->hasKey($app, $key)) { |
|
| 221 | + return $this->cache[$app][$key]; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + return $default; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * check if a key is set in the appconfig |
|
| 229 | + * |
|
| 230 | + * @param string $app |
|
| 231 | + * @param string $key |
|
| 232 | + * @return bool |
|
| 233 | + */ |
|
| 234 | + public function hasKey($app, $key) { |
|
| 235 | + $this->loadConfigValues(); |
|
| 236 | + |
|
| 237 | + return isset($this->cache[$app][$key]); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Sets a value. If the key did not exist before it will be created. |
|
| 242 | + * |
|
| 243 | + * @param string $app app |
|
| 244 | + * @param string $key key |
|
| 245 | + * @param string|float|int $value value |
|
| 246 | + * @return bool True if the value was inserted or updated, false if the value was the same |
|
| 247 | + */ |
|
| 248 | + public function setValue($app, $key, $value) { |
|
| 249 | + if (!$this->hasKey($app, $key)) { |
|
| 250 | + $inserted = (bool) $this->conn->insertIfNotExist('*PREFIX*appconfig', [ |
|
| 251 | + 'appid' => $app, |
|
| 252 | + 'configkey' => $key, |
|
| 253 | + 'configvalue' => $value, |
|
| 254 | + ], [ |
|
| 255 | + 'appid', |
|
| 256 | + 'configkey', |
|
| 257 | + ]); |
|
| 258 | + |
|
| 259 | + if ($inserted) { |
|
| 260 | + if (!isset($this->cache[$app])) { |
|
| 261 | + $this->cache[$app] = []; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + $this->cache[$app][$key] = $value; |
|
| 265 | + return true; |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + $sql = $this->conn->getQueryBuilder(); |
|
| 270 | + $sql->update('appconfig') |
|
| 271 | + ->set('configvalue', $sql->createNamedParameter($value)) |
|
| 272 | + ->where($sql->expr()->eq('appid', $sql->createNamedParameter($app))) |
|
| 273 | + ->andWhere($sql->expr()->eq('configkey', $sql->createNamedParameter($key))); |
|
| 274 | + |
|
| 275 | + /* |
|
| 276 | 276 | * Only limit to the existing value for non-Oracle DBs: |
| 277 | 277 | * http://docs.oracle.com/cd/E11882_01/server.112/e26088/conditions002.htm#i1033286 |
| 278 | 278 | * > Large objects (LOBs) are not supported in comparison conditions. |
| 279 | 279 | */ |
| 280 | - if (!($this->conn instanceof OracleConnection)) { |
|
| 280 | + if (!($this->conn instanceof OracleConnection)) { |
|
| 281 | 281 | |
| 282 | - /* |
|
| 282 | + /* |
|
| 283 | 283 | * Only update the value when it is not the same |
| 284 | 284 | * Note that NULL requires some special handling. Since comparing |
| 285 | 285 | * against null can have special results. |
| 286 | 286 | */ |
| 287 | 287 | |
| 288 | - if ($value === null) { |
|
| 289 | - $sql->andWhere( |
|
| 290 | - $sql->expr()->isNotNull('configvalue') |
|
| 291 | - ); |
|
| 292 | - } else { |
|
| 293 | - $sql->andWhere( |
|
| 294 | - $sql->expr()->orX( |
|
| 295 | - $sql->expr()->isNull('configvalue'), |
|
| 296 | - $sql->expr()->neq('configvalue', $sql->createNamedParameter($value)) |
|
| 297 | - ) |
|
| 298 | - ); |
|
| 299 | - } |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - $changedRow = (bool) $sql->execute(); |
|
| 303 | - |
|
| 304 | - $this->cache[$app][$key] = $value; |
|
| 305 | - |
|
| 306 | - return $changedRow; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Deletes a key |
|
| 311 | - * |
|
| 312 | - * @param string $app app |
|
| 313 | - * @param string $key key |
|
| 314 | - * @return boolean |
|
| 315 | - */ |
|
| 316 | - public function deleteKey($app, $key) { |
|
| 317 | - $this->loadConfigValues(); |
|
| 318 | - |
|
| 319 | - $sql = $this->conn->getQueryBuilder(); |
|
| 320 | - $sql->delete('appconfig') |
|
| 321 | - ->where($sql->expr()->eq('appid', $sql->createParameter('app'))) |
|
| 322 | - ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey'))) |
|
| 323 | - ->setParameter('app', $app) |
|
| 324 | - ->setParameter('configkey', $key); |
|
| 325 | - $sql->execute(); |
|
| 326 | - |
|
| 327 | - unset($this->cache[$app][$key]); |
|
| 328 | - return false; |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Remove app from appconfig |
|
| 333 | - * |
|
| 334 | - * @param string $app app |
|
| 335 | - * @return boolean |
|
| 336 | - * |
|
| 337 | - * Removes all keys in appconfig belonging to the app. |
|
| 338 | - */ |
|
| 339 | - public function deleteApp($app) { |
|
| 340 | - $this->loadConfigValues(); |
|
| 341 | - |
|
| 342 | - $sql = $this->conn->getQueryBuilder(); |
|
| 343 | - $sql->delete('appconfig') |
|
| 344 | - ->where($sql->expr()->eq('appid', $sql->createParameter('app'))) |
|
| 345 | - ->setParameter('app', $app); |
|
| 346 | - $sql->execute(); |
|
| 347 | - |
|
| 348 | - unset($this->cache[$app]); |
|
| 349 | - return false; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * get multiple values, either the app or key can be used as wildcard by setting it to false |
|
| 354 | - * |
|
| 355 | - * @param string|false $app |
|
| 356 | - * @param string|false $key |
|
| 357 | - * @return array|false |
|
| 358 | - */ |
|
| 359 | - public function getValues($app, $key) { |
|
| 360 | - if (($app !== false) === ($key !== false)) { |
|
| 361 | - return false; |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - if ($key === false) { |
|
| 365 | - return $this->getAppValues($app); |
|
| 366 | - } else { |
|
| 367 | - $appIds = $this->getApps(); |
|
| 368 | - $values = array_map(function ($appId) use ($key) { |
|
| 369 | - return isset($this->cache[$appId][$key]) ? $this->cache[$appId][$key] : null; |
|
| 370 | - }, $appIds); |
|
| 371 | - $result = array_combine($appIds, $values); |
|
| 372 | - |
|
| 373 | - return array_filter($result); |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * get all values of the app or and filters out sensitive data |
|
| 379 | - * |
|
| 380 | - * @param string $app |
|
| 381 | - * @return array |
|
| 382 | - */ |
|
| 383 | - public function getFilteredValues($app) { |
|
| 384 | - $values = $this->getValues($app, false); |
|
| 385 | - |
|
| 386 | - if (isset($this->sensitiveValues[$app])) { |
|
| 387 | - foreach ($this->sensitiveValues[$app] as $sensitiveKeyExp) { |
|
| 388 | - $sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values)); |
|
| 389 | - foreach ($sensitiveKeys as $sensitiveKey) { |
|
| 390 | - $values[$sensitiveKey] = IConfig::SENSITIVE_VALUE; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - return $values; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Load all the app config values |
|
| 400 | - */ |
|
| 401 | - protected function loadConfigValues() { |
|
| 402 | - if ($this->configLoaded) { |
|
| 403 | - return; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - $this->cache = []; |
|
| 407 | - |
|
| 408 | - $sql = $this->conn->getQueryBuilder(); |
|
| 409 | - $sql->select('*') |
|
| 410 | - ->from('appconfig'); |
|
| 411 | - $result = $sql->execute(); |
|
| 412 | - |
|
| 413 | - // we are going to store the result in memory anyway |
|
| 414 | - $rows = $result->fetchAll(); |
|
| 415 | - foreach ($rows as $row) { |
|
| 416 | - if (!isset($this->cache[$row['appid']])) { |
|
| 417 | - $this->cache[(string)$row['appid']] = []; |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - $this->cache[(string)$row['appid']][(string)$row['configkey']] = (string)$row['configvalue']; |
|
| 421 | - } |
|
| 422 | - $result->closeCursor(); |
|
| 423 | - |
|
| 424 | - $this->configLoaded = true; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * Clear all the cached app config values |
|
| 429 | - * |
|
| 430 | - * WARNING: do not use this - this is only for usage with the SCSSCacher to |
|
| 431 | - * clear the memory cache of the app config |
|
| 432 | - */ |
|
| 433 | - public function clearCachedConfig() { |
|
| 434 | - $this->configLoaded = false; |
|
| 435 | - } |
|
| 288 | + if ($value === null) { |
|
| 289 | + $sql->andWhere( |
|
| 290 | + $sql->expr()->isNotNull('configvalue') |
|
| 291 | + ); |
|
| 292 | + } else { |
|
| 293 | + $sql->andWhere( |
|
| 294 | + $sql->expr()->orX( |
|
| 295 | + $sql->expr()->isNull('configvalue'), |
|
| 296 | + $sql->expr()->neq('configvalue', $sql->createNamedParameter($value)) |
|
| 297 | + ) |
|
| 298 | + ); |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + $changedRow = (bool) $sql->execute(); |
|
| 303 | + |
|
| 304 | + $this->cache[$app][$key] = $value; |
|
| 305 | + |
|
| 306 | + return $changedRow; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Deletes a key |
|
| 311 | + * |
|
| 312 | + * @param string $app app |
|
| 313 | + * @param string $key key |
|
| 314 | + * @return boolean |
|
| 315 | + */ |
|
| 316 | + public function deleteKey($app, $key) { |
|
| 317 | + $this->loadConfigValues(); |
|
| 318 | + |
|
| 319 | + $sql = $this->conn->getQueryBuilder(); |
|
| 320 | + $sql->delete('appconfig') |
|
| 321 | + ->where($sql->expr()->eq('appid', $sql->createParameter('app'))) |
|
| 322 | + ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey'))) |
|
| 323 | + ->setParameter('app', $app) |
|
| 324 | + ->setParameter('configkey', $key); |
|
| 325 | + $sql->execute(); |
|
| 326 | + |
|
| 327 | + unset($this->cache[$app][$key]); |
|
| 328 | + return false; |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Remove app from appconfig |
|
| 333 | + * |
|
| 334 | + * @param string $app app |
|
| 335 | + * @return boolean |
|
| 336 | + * |
|
| 337 | + * Removes all keys in appconfig belonging to the app. |
|
| 338 | + */ |
|
| 339 | + public function deleteApp($app) { |
|
| 340 | + $this->loadConfigValues(); |
|
| 341 | + |
|
| 342 | + $sql = $this->conn->getQueryBuilder(); |
|
| 343 | + $sql->delete('appconfig') |
|
| 344 | + ->where($sql->expr()->eq('appid', $sql->createParameter('app'))) |
|
| 345 | + ->setParameter('app', $app); |
|
| 346 | + $sql->execute(); |
|
| 347 | + |
|
| 348 | + unset($this->cache[$app]); |
|
| 349 | + return false; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * get multiple values, either the app or key can be used as wildcard by setting it to false |
|
| 354 | + * |
|
| 355 | + * @param string|false $app |
|
| 356 | + * @param string|false $key |
|
| 357 | + * @return array|false |
|
| 358 | + */ |
|
| 359 | + public function getValues($app, $key) { |
|
| 360 | + if (($app !== false) === ($key !== false)) { |
|
| 361 | + return false; |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + if ($key === false) { |
|
| 365 | + return $this->getAppValues($app); |
|
| 366 | + } else { |
|
| 367 | + $appIds = $this->getApps(); |
|
| 368 | + $values = array_map(function ($appId) use ($key) { |
|
| 369 | + return isset($this->cache[$appId][$key]) ? $this->cache[$appId][$key] : null; |
|
| 370 | + }, $appIds); |
|
| 371 | + $result = array_combine($appIds, $values); |
|
| 372 | + |
|
| 373 | + return array_filter($result); |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * get all values of the app or and filters out sensitive data |
|
| 379 | + * |
|
| 380 | + * @param string $app |
|
| 381 | + * @return array |
|
| 382 | + */ |
|
| 383 | + public function getFilteredValues($app) { |
|
| 384 | + $values = $this->getValues($app, false); |
|
| 385 | + |
|
| 386 | + if (isset($this->sensitiveValues[$app])) { |
|
| 387 | + foreach ($this->sensitiveValues[$app] as $sensitiveKeyExp) { |
|
| 388 | + $sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values)); |
|
| 389 | + foreach ($sensitiveKeys as $sensitiveKey) { |
|
| 390 | + $values[$sensitiveKey] = IConfig::SENSITIVE_VALUE; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + return $values; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Load all the app config values |
|
| 400 | + */ |
|
| 401 | + protected function loadConfigValues() { |
|
| 402 | + if ($this->configLoaded) { |
|
| 403 | + return; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + $this->cache = []; |
|
| 407 | + |
|
| 408 | + $sql = $this->conn->getQueryBuilder(); |
|
| 409 | + $sql->select('*') |
|
| 410 | + ->from('appconfig'); |
|
| 411 | + $result = $sql->execute(); |
|
| 412 | + |
|
| 413 | + // we are going to store the result in memory anyway |
|
| 414 | + $rows = $result->fetchAll(); |
|
| 415 | + foreach ($rows as $row) { |
|
| 416 | + if (!isset($this->cache[$row['appid']])) { |
|
| 417 | + $this->cache[(string)$row['appid']] = []; |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + $this->cache[(string)$row['appid']][(string)$row['configkey']] = (string)$row['configvalue']; |
|
| 421 | + } |
|
| 422 | + $result->closeCursor(); |
|
| 423 | + |
|
| 424 | + $this->configLoaded = true; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * Clear all the cached app config values |
|
| 429 | + * |
|
| 430 | + * WARNING: do not use this - this is only for usage with the SCSSCacher to |
|
| 431 | + * clear the memory cache of the app config |
|
| 432 | + */ |
|
| 433 | + public function clearCachedConfig() { |
|
| 434 | + $this->configLoaded = false; |
|
| 435 | + } |
|
| 436 | 436 | } |