Files
firefly-iii/app/Support/FireflyConfig.php

173 lines
5.0 KiB
PHP
Raw Normal View History

<?php
2022-12-29 19:42:26 +01:00
/**
* FireflyConfig.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Configuration;
2024-12-11 07:23:46 +01:00
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\EncryptException;
2020-06-06 12:14:55 +02:00
use Illuminate\Database\QueryException;
2025-05-24 17:15:46 +02:00
use Illuminate\Support\Facades\Cache;
2024-12-11 07:23:46 +01:00
use Illuminate\Support\Facades\Log;
use Exception;
/**
2017-11-15 12:25:49 +01:00
* Class FireflyConfig.
*/
class FireflyConfig
{
2018-07-14 15:21:05 +02:00
public function delete(string $name): void
{
$fullName = 'ff3-config-'.$name;
2025-05-24 17:15:46 +02:00
if (Cache::has($fullName)) {
Cache::forget($fullName);
}
2022-12-30 20:38:54 +01:00
Configuration::where('name', $name)->forceDelete();
}
2023-06-21 12:34:58 +02:00
public function has(string $name): bool
{
2023-12-20 19:35:52 +01:00
return 1 === Configuration::where('name', $name)->count();
2023-06-21 12:34:58 +02:00
}
2025-01-04 09:34:17 +01:00
public function getEncrypted(string $name, mixed $default = null): ?Configuration
2024-12-11 07:23:46 +01:00
{
$result = $this->get($name, $default);
2025-05-27 17:06:15 +02:00
if (!$result instanceof Configuration) {
2024-12-11 07:23:46 +01:00
return null;
}
if ('' === $result->data) {
Log::warning(sprintf('Empty encrypted configuration value found: "%s"', $name));
2024-12-14 05:45:54 +01:00
2024-12-11 07:23:46 +01:00
return $result;
}
2024-12-14 05:45:54 +01:00
2024-12-11 07:23:46 +01:00
try {
$result->data = decrypt($result->data);
} catch (DecryptException $e) {
Log::error(sprintf('Could not decrypt configuration value "%s": %s', $name, $e->getMessage()));
2024-12-14 05:45:54 +01:00
2024-12-11 07:23:46 +01:00
return $result;
}
2024-12-14 05:45:54 +01:00
2024-12-11 07:23:46 +01:00
return $result;
}
2023-06-21 12:34:58 +02:00
/**
2023-12-20 19:35:52 +01:00
* @param null|bool|int|string $default
*
2021-03-21 09:15:40 +01:00
* @throws FireflyException
*/
2025-01-04 09:34:17 +01:00
public function get(string $name, mixed $default = null): ?Configuration
{
$fullName = 'ff3-config-'.$name;
2025-05-24 17:15:46 +02:00
if (Cache::has($fullName)) {
return Cache::get($fullName);
}
2020-06-06 12:14:55 +02:00
try {
2023-12-20 19:35:52 +01:00
/** @var null|Configuration $config */
2020-06-06 12:14:55 +02:00
$config = Configuration::where('name', $name)->first(['id', 'name', 'data']);
} catch (Exception|QueryException $e) {
2022-12-30 20:25:04 +01:00
throw new FireflyException(sprintf('Could not poll the database: %s', $e->getMessage()), 0, $e);
2020-06-06 12:14:55 +02:00
}
2021-04-06 17:00:16 +02:00
if (null !== $config) {
2025-05-24 17:15:46 +02:00
Cache::forever($fullName, $config);
return $config;
}
// no preference found and default is null:
2017-11-15 12:25:49 +01:00
if (null === $default) {
return null;
}
2018-03-07 21:01:46 +01:00
return $this->set($name, $default);
}
2024-12-11 07:23:46 +01:00
public function set(string $name, mixed $value): Configuration
{
try {
$config = Configuration::whereName($name)->whereNull('deleted_at')->first();
2022-12-30 20:25:04 +01:00
} catch (QueryException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error($e->getMessage());
2022-10-30 14:24:37 +01:00
$item = new Configuration();
$item->name = $name;
$item->data = $value;
return $item;
}
if (null === $config) {
2022-10-30 14:24:37 +01:00
$item = new Configuration();
$item->name = $name;
$item->data = $value;
$item->save();
2025-05-24 17:15:46 +02:00
Cache::forget('ff3-config-'.$name);
return $item;
}
$config->data = $value;
$config->save();
2025-05-24 17:15:46 +02:00
Cache::forget('ff3-config-'.$name);
return $config;
}
2023-06-21 12:34:58 +02:00
2025-01-04 09:34:17 +01:00
public function getFresh(string $name, mixed $default = null): ?Configuration
2023-06-21 12:34:58 +02:00
{
$config = Configuration::where('name', $name)->first(['id', 'name', 'data']);
2023-11-05 09:40:45 +01:00
if (null !== $config) {
2023-06-21 12:34:58 +02:00
return $config;
}
// no preference found and default is null:
if (null === $default) {
return null;
}
return $this->set($name, $default);
}
/**
2023-12-20 19:35:52 +01:00
* @param mixed $value
2023-06-21 12:34:58 +02:00
*/
public function put(string $name, $value): Configuration
{
return $this->set($name, $value);
}
2024-12-22 08:43:12 +01:00
public function setEncrypted(string $name, mixed $value): Configuration
{
try {
$encrypted = encrypt($value);
} catch (EncryptException $e) {
Log::error(sprintf('Could not encrypt configuration value "%s": %s', $name, $e->getMessage()));
throw new FireflyException(sprintf('Could not encrypt configuration value "%s". Cowardly refuse to continue.', $name));
}
return $this->set($name, $encrypted);
}
}