]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SettingController.php
f5e48ca4cc5413ae6c6afcbf5fbb091391e6ce6a
[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      * Handle requests to the settings index path.
23      */
24     public function index()
25     {
26         return redirect('/settings/features');
27     }
28
29     /**
30      * Display the settings for the given category.
31      */
32     public function category(string $category)
33     {
34         $this->ensureCategoryExists($category);
35         $this->checkPermission('settings-manage');
36         $this->setPageTitle(trans('settings.settings'));
37
38         // Get application version
39         $version = trim(file_get_contents(base_path('version')));
40
41         return view('settings.' . $category, [
42             'category'  => $category,
43             'version'   => $version,
44             'guestUser' => User::getDefault(),
45         ]);
46     }
47
48     /**
49      * Update the specified settings in storage.
50      */
51     public function update(Request $request, string $category)
52     {
53         $this->ensureCategoryExists($category);
54         $this->preventAccessInDemoMode();
55         $this->checkPermission('settings-manage');
56         $this->validate($request, [
57             'app_logo' => array_merge(['nullable'], $this->getImageValidationRules()),
58         ]);
59
60         // Cycles through posted settings and update them
61         foreach ($request->all() as $name => $value) {
62             $key = str_replace('setting-', '', trim($name));
63             if (strpos($name, 'setting-') !== 0) {
64                 continue;
65             }
66             setting()->put($key, $value);
67         }
68
69         // Update logo image if set
70         if ($category === 'customization' && $request->hasFile('app_logo')) {
71             $logoFile = $request->file('app_logo');
72             $this->imageRepo->destroyByType('system');
73             $image = $this->imageRepo->saveNew($logoFile, 'system', 0, null, 86);
74             setting()->put('app-logo', $image->url);
75         }
76
77         // Clear logo image if requested
78         if ($category === 'customization' && $request->get('app_logo_reset', null)) {
79             $this->imageRepo->destroyByType('system');
80             setting()->remove('app-logo');
81         }
82
83         $this->logActivity(ActivityType::SETTINGS_UPDATE, $category);
84         $this->showSuccessNotification(trans('settings.settings_save_success'));
85
86         return redirect("/settings/{$category}");
87     }
88
89     protected function ensureCategoryExists(string $category): void
90     {
91         if (!in_array($category, $this->settingCategories)) {
92             abort(404);
93         }
94     }
95 }