1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-29 11:15:09 +00:00
pixelfed/app/Http/Controllers/Admin/AdminSettingsController.php

286 lines
8.4 KiB
PHP
Raw Normal View History

2019-02-07 18:59:38 +00:00
<?php
namespace App\Http\Controllers\Admin;
use Artisan, Cache, DB;
use Illuminate\Http\Request;
use Carbon\Carbon;
2021-05-19 08:15:37 +00:00
use App\{Comment, Like, Media, Page, Profile, Report, Status, User};
use App\Models\InstanceActor;
2019-02-07 18:59:38 +00:00
use App\Http\Controllers\Controller;
use App\Util\Lexer\PrettyNumber;
2021-05-08 04:19:58 +00:00
use App\Models\ConfigCache;
2023-05-07 11:08:45 +00:00
use App\Services\AccountService;
2021-05-08 04:19:58 +00:00
use App\Services\ConfigCacheService;
2021-05-19 08:01:33 +00:00
use App\Util\Site\Config;
2023-05-07 11:08:45 +00:00
use Illuminate\Support\Str;
2019-02-07 18:59:38 +00:00
trait AdminSettingsController
{
2019-06-21 02:01:16 +00:00
public function settings(Request $request)
{
2021-05-19 08:01:33 +00:00
$cloud_storage = ConfigCacheService::get('pixelfed.cloud_storage');
$cloud_disk = config('filesystems.cloud');
$cloud_ready = !empty(config('filesystems.disks.' . $cloud_disk . '.key')) && !empty(config('filesystems.disks.' . $cloud_disk . '.secret'));
2021-05-12 04:07:55 +00:00
$types = explode(',', ConfigCacheService::get('pixelfed.media_types'));
2021-05-14 05:45:36 +00:00
$rules = ConfigCacheService::get('app.rules') ? json_decode(ConfigCacheService::get('app.rules'), true) : null;
2022-05-21 06:32:55 +00:00
$jpeg = in_array('image/jpg', $types) || in_array('image/jpeg', $types);
2021-05-12 04:07:55 +00:00
$png = in_array('image/png', $types);
$gif = in_array('image/gif', $types);
$mp4 = in_array('video/mp4', $types);
2021-06-08 09:22:01 +00:00
$webp = in_array('image/webp', $types);
2021-05-08 04:19:58 +00:00
2023-05-07 11:08:45 +00:00
$availableAdmins = User::whereIsAdmin(true)->get();
$currentAdmin = config_cache('instance.admin.pid') ? AccountService::get(config_cache('instance.admin.pid'), true) : null;
2021-05-19 08:01:33 +00:00
// $system = [
// 'permissions' => is_writable(base_path('storage')) && is_writable(base_path('bootstrap')),
// 'max_upload_size' => ini_get('post_max_size'),
// 'image_driver' => config('image.driver'),
// 'image_driver_loaded' => extension_loaded(config('image.driver'))
// ];
2021-05-08 04:19:58 +00:00
return view('admin.settings.home', compact(
2021-05-12 04:07:55 +00:00
'jpeg',
'png',
'gif',
2021-05-14 05:45:36 +00:00
'mp4',
2021-06-08 09:22:01 +00:00
'webp',
2021-05-19 08:01:33 +00:00
'rules',
'cloud_storage',
'cloud_disk',
'cloud_ready',
2023-05-07 11:08:45 +00:00
'availableAdmins',
'currentAdmin'
2021-05-19 08:01:33 +00:00
// 'system'
2021-05-08 04:19:58 +00:00
));
}
public function settingsHomeStore(Request $request)
{
$this->validate($request, [
'name' => 'nullable|string',
'short_description' => 'nullable',
2021-05-12 04:07:55 +00:00
'long_description' => 'nullable',
'max_photo_size' => 'nullable|integer|min:1',
'max_album_length' => 'nullable|integer|min:1|max:100',
'image_quality' => 'nullable|integer|min:1|max:100',
'type_jpeg' => 'nullable',
'type_png' => 'nullable',
'type_gif' => 'nullable',
'type_mp4' => 'nullable',
2021-06-08 09:22:01 +00:00
'type_webp' => 'nullable',
2023-05-07 11:08:45 +00:00
'admin_account_id' => 'nullable',
2021-05-08 04:19:58 +00:00
]);
2023-05-07 11:08:45 +00:00
if($request->filled('admin_account_id')) {
ConfigCacheService::put('instance.admin.pid', $request->admin_account_id);
Cache::forget('api:v1:instance-data:contact');
Cache::forget('api:v1:instance-data-response-v1');
}
2021-05-14 05:45:36 +00:00
if($request->filled('rule_delete')) {
$index = (int) $request->input('rule_delete');
$rules = ConfigCacheService::get('app.rules');
$json = json_decode($rules, true);
if(!$rules || empty($json)) {
return;
}
unset($json[$index]);
$json = json_encode(array_values($json));
ConfigCacheService::put('app.rules', $json);
2023-05-07 11:08:45 +00:00
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response-v1');
2021-05-14 05:45:36 +00:00
return 200;
}
2021-05-12 04:07:55 +00:00
$media_types = explode(',', config_cache('pixelfed.media_types'));
$media_types_original = $media_types;
$mimes = [
'type_jpeg' => 'image/jpeg',
'type_png' => 'image/png',
'type_gif' => 'image/gif',
'type_mp4' => 'video/mp4',
2021-06-08 09:22:01 +00:00
'type_webp' => 'image/webp',
2021-05-12 04:07:55 +00:00
];
foreach ($mimes as $key => $value) {
if($request->input($key) == 'on') {
if(!in_array($value, $media_types)) {
array_push($media_types, $value);
}
} else {
$media_types = array_diff($media_types, [$value]);
}
}
if($media_types !== $media_types_original) {
ConfigCacheService::put('pixelfed.media_types', implode(',', array_unique($media_types)));
2021-05-08 04:19:58 +00:00
}
2021-05-12 04:07:55 +00:00
$keys = [
'name' => 'app.name',
'short_description' => 'app.short_description',
'long_description' => 'app.description',
'max_photo_size' => 'pixelfed.max_photo_size',
'max_album_length' => 'pixelfed.max_album_length',
'image_quality' => 'pixelfed.image_quality',
'account_limit' => 'pixelfed.max_account_size',
'custom_css' => 'uikit.custom.css',
2021-05-19 08:01:33 +00:00
'custom_js' => 'uikit.custom.js',
'about_title' => 'about.title'
2021-05-12 04:07:55 +00:00
];
foreach ($keys as $key => $value) {
$cc = ConfigCache::whereK($value)->first();
$val = $request->input($key);
if($cc && $cc->v != $val) {
ConfigCacheService::put($value, $val);
2022-04-08 05:52:55 +00:00
} else if(!empty($val)) {
2023-05-07 11:08:45 +00:00
ConfigCacheService::put($value, $val);
}
2021-05-08 04:19:58 +00:00
}
2021-05-12 04:07:55 +00:00
$bools = [
'activitypub' => 'federation.activitypub.enabled',
'open_registration' => 'pixelfed.open_registration',
'mobile_apis' => 'pixelfed.oauth_enabled',
'stories' => 'instance.stories.enabled',
'ig_import' => 'pixelfed.import.instagram.enabled',
'spam_detection' => 'pixelfed.bouncer.enabled',
'require_email_verification' => 'pixelfed.enforce_email_verification',
'enforce_account_limit' => 'pixelfed.enforce_account_limit',
'show_custom_css' => 'uikit.show_custom.css',
'show_custom_js' => 'uikit.show_custom.js',
2021-06-11 03:07:35 +00:00
'cloud_storage' => 'pixelfed.cloud_storage',
2023-04-08 02:22:10 +00:00
'account_autofollow' => 'account.autofollow',
2023-05-07 11:08:45 +00:00
'show_directory' => 'instance.landing.show_directory',
'show_explore_feed' => 'instance.landing.show_explore',
2021-05-12 04:07:55 +00:00
];
foreach ($bools as $key => $value) {
$active = $request->input($key) == 'on';
2021-05-19 08:01:33 +00:00
if($key == 'activitypub' && $active && !InstanceActor::exists()) {
Artisan::call('instance:actor');
}
if( $key == 'mobile_apis' &&
$active &&
!file_exists(storage_path('oauth-public.key')) &&
!file_exists(storage_path('oauth-private.key'))
) {
Artisan::call('passport:keys');
Artisan::call('route:cache');
}
2021-05-12 04:07:55 +00:00
if(config_cache($value) !== $active) {
ConfigCacheService::put($value, (bool) $active);
}
2021-05-08 04:19:58 +00:00
}
2021-05-14 05:45:36 +00:00
if($request->filled('new_rule')) {
$rules = ConfigCacheService::get('app.rules');
$val = $request->input('new_rule');
if(!$rules) {
ConfigCacheService::put('app.rules', json_encode([$val]));
} else {
$json = json_decode($rules, true);
$json[] = $val;
ConfigCacheService::put('app.rules', json_encode(array_values($json)));
}
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response-v1');
2021-05-14 05:45:36 +00:00
}
2021-06-11 03:07:35 +00:00
if($request->filled('account_autofollow_usernames')) {
$usernames = explode(',', $request->input('account_autofollow_usernames'));
$names = [];
foreach($usernames as $n) {
$p = Profile::whereUsername($n)->first();
if(!$p) {
continue;
}
array_push($names, $p->username);
}
ConfigCacheService::put('account.autofollow_usernames', implode(',', $names));
}
2021-05-19 08:01:33 +00:00
Cache::forget(Config::CACHE_KEY);
2021-05-12 04:07:55 +00:00
2021-05-19 08:01:33 +00:00
return redirect('/i/admin/settings')->with('status', 'Successfully updated settings!');
2019-06-21 02:01:16 +00:00
}
public function settingsBackups(Request $request)
{
$path = storage_path('app/'.config('app.name'));
$files = is_dir($path) ? new \DirectoryIterator($path) : [];
return view('admin.settings.backups', compact('files'));
}
public function settingsMaintenance(Request $request)
{
return view('admin.settings.maintenance');
}
public function settingsStorage(Request $request)
{
$storage = [];
return view('admin.settings.storage', compact('storage'));
}
public function settingsFeatures(Request $request)
{
return view('admin.settings.features');
}
2019-02-07 18:59:38 +00:00
public function settingsPages(Request $request)
{
2019-06-21 02:01:16 +00:00
$pages = Page::orderByDesc('updated_at')->paginate(10);
2019-02-07 18:59:38 +00:00
return view('admin.pages.home', compact('pages'));
}
public function settingsPageEdit(Request $request)
{
return view('admin.pages.edit');
}
2019-06-21 02:01:16 +00:00
public function settingsSystem(Request $request)
{
$sys = [
'pixelfed' => config('pixelfed.version'),
'php' => phpversion(),
'laravel' => app()->version(),
];
switch (config('database.default')) {
case 'pgsql':
$exp = DB::raw('select version();');
$expQuery = $exp->getValue(DB::connection()->getQueryGrammar());
2019-06-21 02:01:16 +00:00
$sys['database'] = [
'name' => 'Postgres',
'version' => explode(' ', DB::select($expQuery)[0]->version)[1]
2019-06-21 02:01:16 +00:00
];
break;
case 'mysql':
$exp = DB::raw('select version()');
$expQuery = $exp->getValue(DB::connection()->getQueryGrammar());
2019-06-21 02:01:16 +00:00
$sys['database'] = [
'name' => 'MySQL',
'version' => DB::select($expQuery)[0]->{'version()'}
2019-06-21 02:01:16 +00:00
];
break;
default:
$sys['database'] = [
'name' => 'Unknown',
'version' => '?'
];
break;
}
return view('admin.settings.system', compact('sys'));
}
2021-05-08 04:19:58 +00:00
}