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