| 
									
										
										
										
											2017-09-27 15:45:55 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-27 15:45:55 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * AdminEventHandler.php | 
					
						
							| 
									
										
										
										
											2020-01-28 08:45:38 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2017-09-27 15:45:55 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2017-09-27 15:45:55 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Handlers\Events; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-01 12:21:42 +02:00
										 |  |  | use FireflyIII\Events\Admin\InvitationCreated; | 
					
						
							| 
									
										
										
										
											2017-09-27 15:45:55 +02:00
										 |  |  | use FireflyIII\Events\AdminRequestedTestMessage; | 
					
						
							| 
									
										
										
										
											2022-09-24 11:41:07 +02:00
										 |  |  | use FireflyIII\Events\NewVersionAvailable; | 
					
						
							| 
									
										
										
										
											2022-09-18 15:48:28 +02:00
										 |  |  | use FireflyIII\Notifications\Admin\TestNotification; | 
					
						
							| 
									
										
										
										
											2022-10-01 12:21:42 +02:00
										 |  |  | use FireflyIII\Notifications\Admin\UserInvitation; | 
					
						
							| 
									
										
										
										
											2022-09-24 11:41:07 +02:00
										 |  |  | use FireflyIII\Notifications\Admin\VersionCheckResult; | 
					
						
							| 
									
										
										
										
											2018-03-30 22:40:20 +02:00
										 |  |  | use FireflyIII\Repositories\User\UserRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2022-09-18 15:48:28 +02:00
										 |  |  | use Illuminate\Support\Facades\Notification; | 
					
						
							| 
									
										
										
										
											2017-09-27 15:45:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class AdminEventHandler. | 
					
						
							| 
									
										
										
										
											2017-09-27 15:45:55 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | class AdminEventHandler | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-01 12:21:42 +02:00
										 |  |  |     public function sendInvitationNotification(InvitationCreated $event): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $sendMail   = app('fireflyconfig')->get('notification_invite_created', true)->data; | 
					
						
							| 
									
										
										
										
											2022-10-01 12:21:42 +02:00
										 |  |  |         if (false === $sendMail) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var UserRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  |         $all        = $repository->all(); | 
					
						
							|  |  |  |         foreach ($all as $user) { | 
					
						
							|  |  |  |             if ($repository->hasRole($user, 'owner')) { | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                 try { | 
					
						
							|  |  |  |                     Notification::send($user, new UserInvitation($event->invitee)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |                 } catch (\Exception $e) { // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                     $message = $e->getMessage(); | 
					
						
							|  |  |  |                     if (str_contains($message, 'Bcc')) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:13 +01:00
										 |  |  |                         app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                         return; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     if (str_contains($message, 'RFC 2822')) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:13 +01:00
										 |  |  |                         app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                         return; | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |                     app('log')->error($e->getMessage()); | 
					
						
							|  |  |  |                     app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-10-01 12:21:42 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 11:41:07 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Send new version message to admin. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function sendNewVersion(NewVersionAvailable $event): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $sendMail   = app('fireflyconfig')->get('notification_new_version', true)->data; | 
					
						
							| 
									
										
										
										
											2022-09-24 11:41:07 +02:00
										 |  |  |         if (false === $sendMail) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var UserRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  |         $all        = $repository->all(); | 
					
						
							|  |  |  |         foreach ($all as $user) { | 
					
						
							|  |  |  |             if ($repository->hasRole($user, 'owner')) { | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                 try { | 
					
						
							|  |  |  |                     Notification::send($user, new VersionCheckResult($event->message)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |                 } catch (\Exception $e) {// @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                     $message = $e->getMessage(); | 
					
						
							|  |  |  |                     if (str_contains($message, 'Bcc')) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:13 +01:00
										 |  |  |                         app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                         return; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     if (str_contains($message, 'RFC 2822')) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:13 +01:00
										 |  |  |                         app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                         return; | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |                     app('log')->error($e->getMessage()); | 
					
						
							|  |  |  |                     app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-09-24 11:41:07 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Sends a test message to an administrator. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function sendTestMessage(AdminRequestedTestMessage $event): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var UserRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$repository->hasRole($event->user, 'owner')) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |         try { | 
					
						
							|  |  |  |             Notification::send($event->user, new TestNotification($event->user->email)); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         } catch (\Exception $e) { // @phpstan-ignore-line
 | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |             $message = $e->getMessage(); | 
					
						
							|  |  |  |             if (str_contains($message, 'Bcc')) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:13 +01:00
										 |  |  |                 app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (str_contains($message, 'RFC 2822')) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:13 +01:00
										 |  |  |                 app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error($e->getMessage()); | 
					
						
							|  |  |  |             app('log')->error($e->getTraceAsString()); | 
					
						
							| 
									
										
										
										
											2023-06-11 16:12:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-29 19:41:57 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-08 09:05:10 +01:00
										 |  |  | } |