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