]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/SettingController.php
Default OpenID display name set to standard value
[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' => 'nullable|' . $this->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->hasFile('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         $redirectLocation = '/settings#' . $request->get('section', '');
75         return redirect(rtrim($redirectLocation, '#'));
76     }
77
78     /**
79      * Show the page for application maintenance.
80      */
81     public function showMaintenance()
82     {
83         $this->checkPermission('settings-manage');
84         $this->setPageTitle(trans('settings.maint'));
85
86         // Get application version
87         $version = trim(file_get_contents(base_path('version')));
88
89         return view('settings.maintenance', ['version' => $version]);
90     }
91
92     /**
93      * Action to clean-up images in the system.
94      */
95     public function cleanupImages(Request $request, ImageService $imageService)
96     {
97         $this->checkPermission('settings-manage');
98
99         $checkRevisions = !($request->get('ignore_revisions', 'false') === 'true');
100         $dryRun = !($request->has('confirm'));
101
102         $imagesToDelete = $imageService->deleteUnusedImages($checkRevisions, $dryRun);
103         $deleteCount = count($imagesToDelete);
104         if ($deleteCount === 0) {
105             $this->showWarningNotification(trans('settings.maint_image_cleanup_nothing_found'));
106             return redirect('/settings/maintenance')->withInput();
107         }
108
109         if ($dryRun) {
110             session()->flash('cleanup-images-warning', trans('settings.maint_image_cleanup_warning', ['count' => $deleteCount]));
111         } else {
112             $this->showSuccessNotification(trans('settings.maint_image_cleanup_success', ['count' => $deleteCount]));
113         }
114
115         return redirect('/settings/maintenance#image-cleanup')->withInput();
116     }
117
118     /**
119      * Action to send a test e-mail to the current user.
120      */
121     public function sendTestEmail()
122     {
123         $this->checkPermission('settings-manage');
124
125         try {
126             user()->notify(new TestEmail());
127             $this->showSuccessNotification(trans('settings.maint_send_test_email_success', ['address' => user()->email]));
128         } catch (\Exception $exception) {
129             $errorMessage = trans('errors.maintenance_test_email_failure') . "\n" . $exception->getMessage();
130             $this->showErrorNotification($errorMessage);
131         }
132
133
134         return redirect('/settings/maintenance#image-cleanup')->withInput();
135     }
136 }