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