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