]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SettingController.php
7f7724468214101cbc443cdee63800d8cbc886e7
[bookstack] / app / Http / Controllers / SettingController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use BookStack\Auth\User;
4 use BookStack\Notifications\TestEmail;
5 use BookStack\Uploads\ImageRepo;
6 use BookStack\Uploads\ImageService;
7 use Illuminate\Http\Request;
8
9 class SettingController extends Controller
10 {
11     protected $imageRepo;
12
13     /**
14      * SettingController constructor.
15      */
16     public function __construct(ImageRepo $imageRepo)
17     {
18         $this->imageRepo = $imageRepo;
19         parent::__construct();
20     }
21
22     /**
23      * Display a listing of the settings.
24      */
25     public function index()
26     {
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.index', [
34             'version' => $version,
35             'guestUser' => User::getDefault()
36         ]);
37     }
38
39     /**
40      * Update the specified settings in storage.
41      */
42     public function update(Request $request)
43     {
44         $this->preventAccessInDemoMode();
45         $this->checkPermission('settings-manage');
46         $this->validate($request, [
47             'app_logo' => $this->imageRepo->getImageValidationRules(),
48         ]);
49
50         // Cycles through posted settings and update them
51         foreach ($request->all() as $name => $value) {
52             if (strpos($name, 'setting-') !== 0) {
53                 continue;
54             }
55             $key = str_replace('setting-', '', trim($name));
56             setting()->put($key, $value);
57         }
58
59         // Update logo image if set
60         if ($request->has('app_logo')) {
61             $logoFile = $request->file('app_logo');
62             $this->imageRepo->destroyByType('system');
63             $image = $this->imageRepo->saveNew($logoFile, 'system', 0, null, 86);
64             setting()->put('app-logo', $image->url);
65         }
66
67         // Clear logo image if requested
68         if ($request->get('app_logo_reset', null)) {
69             $this->imageRepo->destroyByType('system');
70             setting()->remove('app-logo');
71         }
72
73         $this->showSuccessNotification(trans('settings.settings_save_success'));
74         return redirect('/settings#' . $request->get('section', ''));
75     }
76
77     /**
78      * Show the page for application maintenance.
79      */
80     public function showMaintenance()
81     {
82         $this->checkPermission('settings-manage');
83         $this->setPageTitle(trans('settings.maint'));
84
85         // Get application version
86         $version = trim(file_get_contents(base_path('version')));
87
88         return view('settings.maintenance', ['version' => $version]);
89     }
90
91     /**
92      * Action to clean-up images in the system.
93      */
94     public function cleanupImages(Request $request, ImageService $imageService)
95     {
96         $this->checkPermission('settings-manage');
97
98         $checkRevisions = !($request->get('ignore_revisions', 'false') === 'true');
99         $dryRun = !($request->has('confirm'));
100
101         $imagesToDelete = $imageService->deleteUnusedImages($checkRevisions, $dryRun);
102         $deleteCount = count($imagesToDelete);
103         if ($deleteCount === 0) {
104             $this->showWarningNotification(trans('settings.maint_image_cleanup_nothing_found'));
105             return redirect('/settings/maintenance')->withInput();
106         }
107
108         if ($dryRun) {
109             session()->flash('cleanup-images-warning', trans('settings.maint_image_cleanup_warning', ['count' => $deleteCount]));
110         } else {
111             $this->showSuccessNotification(trans('settings.maint_image_cleanup_success', ['count' => $deleteCount]));
112         }
113
114         return redirect('/settings/maintenance#image-cleanup')->withInput();
115     }
116
117     /**
118      * Action to send a test e-mail to the current user.
119      */
120     public function sendTestEmail()
121     {
122         $this->checkPermission('settings-manage');
123
124         user()->notify(new TestEmail());
125         $this->showSuccessNotification(trans('settings.maint_send_test_email_success', ['address' => user()->email]));
126
127         return redirect('/settings/maintenance#image-cleanup')->withInput();
128     }
129 }