]> BookStack Code Mirror - bookstack/blob - app/Settings/MaintenanceController.php
Cleaned up namespacing in routes
[bookstack] / app / Settings / MaintenanceController.php
1 <?php
2
3 namespace BookStack\Settings;
4
5 use BookStack\Activity\ActivityType;
6 use BookStack\Entities\Tools\TrashCan;
7 use BookStack\Http\Controller;
8 use BookStack\Notifications\TestEmail;
9 use BookStack\References\ReferenceStore;
10 use BookStack\Uploads\ImageService;
11 use Illuminate\Http\Request;
12
13 class MaintenanceController extends Controller
14 {
15     /**
16      * Show the page for application maintenance.
17      */
18     public function index()
19     {
20         $this->checkPermission('settings-manage');
21         $this->setPageTitle(trans('settings.maint'));
22
23         // Get application version
24         $version = trim(file_get_contents(base_path('version')));
25
26         // Recycle bin details
27         $recycleStats = (new TrashCan())->getTrashedCounts();
28
29         return view('settings.maintenance', [
30             'version'      => $version,
31             'recycleStats' => $recycleStats,
32         ]);
33     }
34
35     /**
36      * Action to clean-up images in the system.
37      */
38     public function cleanupImages(Request $request, ImageService $imageService)
39     {
40         $this->checkPermission('settings-manage');
41         $this->logActivity(ActivityType::MAINTENANCE_ACTION_RUN, 'cleanup-images');
42
43         $checkRevisions = !($request->get('ignore_revisions', 'false') === 'true');
44         $dryRun = !($request->has('confirm'));
45
46         $imagesToDelete = $imageService->deleteUnusedImages($checkRevisions, $dryRun);
47         $deleteCount = count($imagesToDelete);
48         if ($deleteCount === 0) {
49             $this->showWarningNotification(trans('settings.maint_image_cleanup_nothing_found'));
50
51             return redirect('/settings/maintenance')->withInput();
52         }
53
54         if ($dryRun) {
55             session()->flash('cleanup-images-warning', trans('settings.maint_image_cleanup_warning', ['count' => $deleteCount]));
56         } else {
57             $this->showSuccessNotification(trans('settings.maint_image_cleanup_success', ['count' => $deleteCount]));
58         }
59
60         return redirect('/settings/maintenance#image-cleanup')->withInput();
61     }
62
63     /**
64      * Action to send a test e-mail to the current user.
65      */
66     public function sendTestEmail()
67     {
68         $this->checkPermission('settings-manage');
69         $this->logActivity(ActivityType::MAINTENANCE_ACTION_RUN, 'send-test-email');
70
71         try {
72             user()->notifyNow(new TestEmail());
73             $this->showSuccessNotification(trans('settings.maint_send_test_email_success', ['address' => user()->email]));
74         } catch (\Exception $exception) {
75             $errorMessage = trans('errors.maintenance_test_email_failure') . "\n" . $exception->getMessage();
76             $this->showErrorNotification($errorMessage);
77         }
78
79         return redirect('/settings/maintenance#image-cleanup');
80     }
81
82     /**
83      * Action to regenerate the reference index in the system.
84      */
85     public function regenerateReferences(ReferenceStore $referenceStore)
86     {
87         $this->checkPermission('settings-manage');
88         $this->logActivity(ActivityType::MAINTENANCE_ACTION_RUN, 'regenerate-references');
89
90         try {
91             $referenceStore->updateForAllPages();
92             $this->showSuccessNotification(trans('settings.maint_regen_references_success'));
93         } catch (\Exception $exception) {
94             $this->showErrorNotification($exception->getMessage());
95         }
96
97         return redirect('/settings/maintenance#regenerate-references');
98     }
99 }