]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SettingController.php
PHPStan and StyleCI fixes
[bookstack] / app / Http / Controllers / SettingController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\User;
7 use BookStack\Uploads\ImageRepo;
8 use Illuminate\Http\Request;
9
10 class SettingController extends Controller
11 {
12     protected ImageRepo $imageRepo;
13
14     protected array $settingCategories = ['features', 'customization', 'registration'];
15
16     public function __construct(ImageRepo $imageRepo)
17     {
18         $this->imageRepo = $imageRepo;
19     }
20
21     /**
22      * Display a listing of the settings.
23      */
24     public function index(string $category)
25     {
26         $this->ensureCategoryExists($category);
27         $this->checkPermission('settings-manage');
28         $this->setPageTitle(trans('settings.settings'));
29
30         // Get application version
31         $version = trim(file_get_contents(base_path('version')));
32
33         return view('settings.' . $category, [
34             'category'  => $category,
35             'version'   => $version,
36             'guestUser' => User::getDefault(),
37         ]);
38     }
39
40     /**
41      * Update the specified settings in storage.
42      */
43     public function update(Request $request, string $category)
44     {
45         $this->ensureCategoryExists($category);
46         $this->preventAccessInDemoMode();
47         $this->checkPermission('settings-manage');
48         $this->validate($request, [
49             'app_logo' => array_merge(['nullable'], $this->getImageValidationRules()),
50         ]);
51
52         // Cycles through posted settings and update them
53         foreach ($request->all() as $name => $value) {
54             $key = str_replace('setting-', '', trim($name));
55             if (strpos($name, 'setting-') !== 0) {
56                 continue;
57             }
58             setting()->put($key, $value);
59         }
60
61         // Update logo image if set
62         if ($category === 'customization' && $request->hasFile('app_logo')) {
63             $logoFile = $request->file('app_logo');
64             $this->imageRepo->destroyByType('system');
65             $image = $this->imageRepo->saveNew($logoFile, 'system', 0, null, 86);
66             setting()->put('app-logo', $image->url);
67         }
68
69         // Clear logo image if requested
70         if ($category === 'customization' && $request->get('app_logo_reset', null)) {
71             $this->imageRepo->destroyByType('system');
72             setting()->remove('app-logo');
73         }
74
75         $this->logActivity(ActivityType::SETTINGS_UPDATE, $category);
76         $this->showSuccessNotification(trans('settings.settings_save_success'));
77
78         return redirect("/settings/${category}");
79     }
80
81     protected function ensureCategoryExists(string $category): void
82     {
83         if (!in_array($category, $this->settingCategories)) {
84             abort(404);
85         }
86     }
87 }