]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/MaintenanceController.php
Apply fixes from StyleCI
[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
49             return redirect('/settings/maintenance')->withInput();
50         }
51
52         if ($dryRun) {
53             session()->flash('cleanup-images-warning', trans('settings.maint_image_cleanup_warning', ['count' => $deleteCount]));
54         } else {
55             $this->showSuccessNotification(trans('settings.maint_image_cleanup_success', ['count' => $deleteCount]));
56         }
57
58         return redirect('/settings/maintenance#image-cleanup')->withInput();
59     }
60
61     /**
62      * Action to send a test e-mail to the current user.
63      */
64     public function sendTestEmail()
65     {
66         $this->checkPermission('settings-manage');
67         $this->logActivity(ActivityType::MAINTENANCE_ACTION_RUN, 'send-test-email');
68
69         try {
70             user()->notify(new TestEmail());
71             $this->showSuccessNotification(trans('settings.maint_send_test_email_success', ['address' => user()->email]));
72         } catch (\Exception $exception) {
73             $errorMessage = trans('errors.maintenance_test_email_failure') . "\n" . $exception->getMessage();
74             $this->showErrorNotification($errorMessage);
75         }
76
77         return redirect('/settings/maintenance#image-cleanup')->withInput();
78     }
79 }