2020-02-25 16:09:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
2021-08-13 07:17:24 +00:00
|
|
|
use App\Database\Repositories\UserRepository;
|
2021-08-16 11:55:31 +00:00
|
|
|
use App\Web\Theme;
|
2020-02-25 20:54:54 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
2021-08-16 11:55:31 +00:00
|
|
|
use Slim\Exception\HttpBadRequestException;
|
|
|
|
use Slim\Exception\HttpInternalServerErrorException;
|
2020-02-25 20:54:54 +00:00
|
|
|
|
2020-02-25 16:09:36 +00:00
|
|
|
class SettingController extends Controller
|
|
|
|
{
|
2020-02-25 20:54:54 +00:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @param Response $response
|
|
|
|
*
|
|
|
|
* @return Response
|
2021-08-16 11:55:31 +00:00
|
|
|
* @throws HttpInternalServerErrorException
|
2020-02-25 20:54:54 +00:00
|
|
|
*/
|
|
|
|
public function saveSettings(Request $request, Response $response): Response
|
|
|
|
{
|
2020-02-26 17:26:19 +00:00
|
|
|
if (!preg_match('/[0-9]+[K|M|G|T]/i', param($request, 'default_user_quota', '1G'))) {
|
|
|
|
$this->session->alert(lang('invalid_quota', 'danger'));
|
|
|
|
return redirect($response, route('system'));
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:25:45 +00:00
|
|
|
if (param($request, 'recaptcha_enabled', 'off') === 'on' && (empty(param($request, 'recaptcha_site_key')) || empty(param($request, 'recaptcha_secret_key')))) {
|
|
|
|
$this->session->alert(lang('recaptcha_keys_required', 'danger'));
|
|
|
|
return redirect($response, route('system'));
|
|
|
|
}
|
|
|
|
|
2020-04-03 13:59:49 +00:00
|
|
|
// registrations
|
2020-02-26 11:22:25 +00:00
|
|
|
$this->updateSetting('register_enabled', param($request, 'register_enabled', 'off'));
|
2020-04-07 16:43:34 +00:00
|
|
|
$this->updateSetting('auto_tagging', param($request, 'auto_tagging', 'off'));
|
2020-03-01 16:03:07 +00:00
|
|
|
|
2020-04-03 13:59:49 +00:00
|
|
|
// quota
|
|
|
|
$this->updateSetting('quota_enabled', param($request, 'quota_enabled', 'off'));
|
|
|
|
$this->updateSetting('default_user_quota', stringToBytes(param($request, 'default_user_quota', '1G')));
|
2021-08-13 07:17:24 +00:00
|
|
|
$user = make(UserRepository::class)->get($request, $this->session->get('user_id'));
|
2020-03-01 16:03:07 +00:00
|
|
|
$this->setSessionQuotaInfo($user->current_disk_quota, $user->max_disk_quota);
|
|
|
|
|
2020-02-26 11:22:25 +00:00
|
|
|
$this->updateSetting('custom_head', param($request, 'custom_head'));
|
2020-03-04 14:25:45 +00:00
|
|
|
$this->updateSetting('recaptcha_enabled', param($request, 'recaptcha_enabled', 'off'));
|
|
|
|
$this->updateSetting('recaptcha_site_key', param($request, 'recaptcha_site_key'));
|
|
|
|
$this->updateSetting('recaptcha_secret_key', param($request, 'recaptcha_secret_key'));
|
2021-08-10 11:50:58 +00:00
|
|
|
$this->updateSetting('image_embeds', param($request, 'image_embeds'));
|
2020-03-04 14:25:45 +00:00
|
|
|
|
2020-04-03 13:59:49 +00:00
|
|
|
$this->applyTheme($request);
|
|
|
|
$this->applyLang($request);
|
|
|
|
|
|
|
|
$this->logger->info("User $user->username updated the system settings.");
|
2020-02-25 20:54:54 +00:00
|
|
|
$this->session->alert(lang('settings_saved'));
|
|
|
|
|
|
|
|
return redirect($response, route('system'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
|
|
|
public function applyLang(Request $request)
|
|
|
|
{
|
|
|
|
if (param($request, 'lang') !== 'auto') {
|
2020-03-19 08:39:32 +00:00
|
|
|
$this->updateSetting('lang', param($request, 'lang'));
|
2020-02-25 20:54:54 +00:00
|
|
|
} else {
|
|
|
|
$this->database->query('DELETE FROM `settings` WHERE `key` = \'lang\'');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
2021-08-16 11:55:31 +00:00
|
|
|
* @throws HttpInternalServerErrorException
|
2020-02-25 20:54:54 +00:00
|
|
|
*/
|
|
|
|
public function applyTheme(Request $request)
|
|
|
|
{
|
2021-08-16 11:55:31 +00:00
|
|
|
$css = param($request, 'css');
|
|
|
|
if ($css === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_writable(BASE_DIR.'static/bootstrap/css/bootstrap.min.css')) {
|
|
|
|
$this->session->alert(lang('cannot_write_file'), 'danger');
|
|
|
|
throw new HttpInternalServerErrorException($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
make(Theme::class)->applyTheme($css);
|
|
|
|
|
|
|
|
// if is default, remove setting
|
|
|
|
if ($css !== Theme::default()) {
|
|
|
|
$this->updateSetting('css', $css);
|
|
|
|
} else {
|
|
|
|
$this->database->query('DELETE FROM `settings` WHERE `key` = \'css\'');
|
2020-02-25 20:54:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-25 16:09:36 +00:00
|
|
|
|
2020-02-25 20:54:54 +00:00
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
* @param null $value
|
|
|
|
*/
|
2020-02-26 11:22:25 +00:00
|
|
|
private function updateSetting($key, $value = null)
|
2020-02-25 20:54:54 +00:00
|
|
|
{
|
|
|
|
if (!$this->database->query('SELECT `value` FROM `settings` WHERE `key` = '.$this->database->getPdo()->quote($key))->fetch()) {
|
2021-08-16 11:56:07 +00:00
|
|
|
$this->database->query(
|
|
|
|
'INSERT INTO `settings`(`key`, `value`) VALUES ('.$this->database->getPdo()->quote($key).', ?)',
|
|
|
|
$value
|
|
|
|
);
|
2020-02-25 20:54:54 +00:00
|
|
|
} else {
|
2021-08-16 11:56:07 +00:00
|
|
|
$this->database->query(
|
|
|
|
'UPDATE `settings` SET `value`=? WHERE `key` = '.$this->database->getPdo()->quote($key),
|
|
|
|
$value
|
|
|
|
);
|
2020-02-25 20:54:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 11:22:40 +00:00
|
|
|
}
|