v2board/app/Http/Controllers/V1/Admin/ConfigController.php

114 lines
3.4 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
2023-06-12 02:32:49 +08:00
namespace App\Http\Controllers\V1\Admin;
2019-10-29 15:33:36 +08:00
2023-06-12 02:32:49 +08:00
use App\Http\Controllers\Controller;
2019-10-29 15:33:36 +08:00
use App\Http\Requests\Admin\ConfigSave;
2021-11-30 16:23:46 +08:00
use App\Jobs\SendEmailJob;
use App\Services\ConfigService;
2020-05-17 15:23:39 +08:00
use App\Services\TelegramService;
2020-02-09 18:01:06 +08:00
use App\Utils\Dict;
2023-06-12 02:32:49 +08:00
use Illuminate\Http\Request;
2022-05-09 00:26:38 +08:00
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
2021-10-06 00:20:28 +08:00
use Illuminate\Support\Facades\Mail;
2019-10-29 15:33:36 +08:00
class ConfigController extends Controller
{
2020-03-18 17:33:58 +08:00
public function getEmailTemplate()
{
2020-03-18 18:12:19 +08:00
$path = resource_path('views/mail/');
$files = array_map(function ($item) use ($path) {
return str_replace($path, '', $item);
}, glob($path . '*'));
2020-03-18 17:33:58 +08:00
return response([
2020-03-18 18:12:19 +08:00
'data' => $files
2020-03-18 17:33:58 +08:00
]);
}
2021-07-29 01:41:31 +08:00
public function getThemeTemplate()
{
$path = public_path('theme/');
$files = array_map(function ($item) use ($path) {
return str_replace($path, '', $item);
}, glob($path . '*'));
return response([
'data' => $files
]);
}
2021-10-08 20:00:34 +08:00
public function testSendMail(Request $request)
2021-10-06 00:20:28 +08:00
{
2021-11-30 16:23:46 +08:00
$obj = new SendEmailJob([
2022-07-19 03:11:36 +08:00
'email' => $request->user['email'],
2021-11-30 16:23:46 +08:00
'subject' => 'This is v2board test email',
'template_name' => 'notify',
'template_value' => [
'name' => config('v2board.app_name', 'V2Board'),
'content' => 'This is v2board test email',
'url' => config('v2board.app_url')
]
]);
2021-10-06 00:20:28 +08:00
return response([
2021-11-30 16:23:46 +08:00
'data' => true,
'log' => $obj->handle()
2021-10-06 00:20:28 +08:00
]);
}
2020-05-17 16:00:28 +08:00
public function setTelegramWebhook(Request $request)
2020-05-17 15:23:39 +08:00
{
2022-05-09 23:46:33 +08:00
$hookUrl = url('/api/v1/guest/telegram/webhook?access_token=' . md5(config('v2board.telegram_bot_token', $request->input('telegram_bot_token'))));
2020-05-17 16:00:28 +08:00
$telegramService = new TelegramService($request->input('telegram_bot_token'));
2020-05-19 16:32:35 +08:00
$telegramService->getMe();
2022-05-09 23:26:59 +08:00
$telegramService->setWebhook($hookUrl);
2020-05-17 15:23:39 +08:00
return response([
'data' => true
]);
}
2022-03-09 22:20:34 +08:00
public function fetch(Request $request)
2020-01-11 13:36:52 +08:00
{
2022-03-09 22:20:34 +08:00
$key = $request->input('key');
$data = (new ConfigService)->getDefaultConfig();
2022-03-09 22:20:34 +08:00
if ($key && isset($data[$key])) {
return response([
'data' => [
$key => $data[$key]
]
]);
};
2020-02-09 18:53:45 +08:00
// TODO: default should be in Dict
2019-10-29 15:33:36 +08:00
return response([
2022-03-09 22:20:34 +08:00
'data' => $data
2019-10-29 15:33:36 +08:00
]);
}
2020-01-11 13:36:52 +08:00
public function save(ConfigSave $request)
{
2021-07-31 02:36:49 +08:00
$data = $request->validated();
2022-05-09 00:26:38 +08:00
$config = config('v2board');
2022-05-10 01:07:12 +08:00
foreach (ConfigSave::RULES as $k => $v) {
2022-05-29 21:21:24 +08:00
if (!in_array($k, array_keys(ConfigSave::RULES))) {
2022-05-09 00:26:38 +08:00
unset($config[$k]);
continue;
}
2022-06-01 12:37:31 +08:00
if (array_key_exists($k, $data)) {
2022-12-15 11:39:00 +08:00
$config[$k] = $data[$k];
2019-10-29 15:33:36 +08:00
}
2020-01-31 17:18:00 +08:00
}
2022-05-09 00:26:38 +08:00
$data = var_export($config, 1);
if (!File::put(base_path() . '/config/v2board.php', "<?php\n return $data ;")) {
2020-01-31 17:18:00 +08:00
abort(500, '修改失败');
2019-10-29 15:33:36 +08:00
}
2020-07-25 00:28:04 +08:00
if (function_exists('opcache_reset')) {
2020-11-18 11:12:38 +08:00
if (opcache_reset() === false) {
2020-09-23 22:40:20 +08:00
abort(500, '缓存清除失败请卸载或检查opcache配置状态');
2020-09-23 14:37:24 +08:00
}
2020-03-30 16:11:08 +08:00
}
2022-05-09 00:26:38 +08:00
Artisan::call('config:cache');
2019-10-29 15:33:36 +08:00
return response([
'data' => true
]);
}
}