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;
|
2022-09-24 11:41:07 +02:00
|
|
|
use FireflyIII\Events\NewVersionAvailable;
|
2024-12-12 07:09:17 +01:00
|
|
|
use FireflyIII\Events\Security\UnknownUserAttemptedLogin;
|
2024-12-14 06:30:27 +01:00
|
|
|
use FireflyIII\Events\Test\OwnerTestNotificationChannel;
|
2024-12-12 07:09:17 +01:00
|
|
|
use FireflyIII\Notifications\Admin\UnknownUserLoginAttempt;
|
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;
|
2024-12-12 06:33:48 +01:00
|
|
|
use FireflyIII\Notifications\Notifiables\OwnerNotifiable;
|
2024-12-14 06:30:27 +01:00
|
|
|
use FireflyIII\Notifications\Test\OwnerTestNotificationEmail;
|
|
|
|
use FireflyIII\Notifications\Test\OwnerTestNotificationNtfy;
|
|
|
|
use FireflyIII\Notifications\Test\OwnerTestNotificationPushover;
|
|
|
|
use FireflyIII\Notifications\Test\OwnerTestNotificationSlack;
|
2024-12-08 16:28:22 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
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
|
|
|
|
{
|
2024-12-22 08:43:12 +01:00
|
|
|
public function sendInvitationNotification(InvitationCreated $event): void
|
2024-12-14 05:45:54 +01:00
|
|
|
{
|
2024-12-22 08:43:12 +01:00
|
|
|
$sendMail = app('fireflyconfig')->get('notification_invite_created', true)->data;
|
|
|
|
if (false === $sendMail) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-12 07:09:17 +01:00
|
|
|
try {
|
2025-01-04 08:42:06 +01:00
|
|
|
Notification::send(new OwnerNotifiable(), new UserInvitation($event->invitee));
|
2025-01-04 19:43:58 +01:00
|
|
|
} catch (\Exception $e) {
|
2024-12-12 07:09:17 +01:00
|
|
|
$message = $e->getMessage();
|
|
|
|
if (str_contains($message, 'Bcc')) {
|
|
|
|
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str_contains($message, 'RFC 2822')) {
|
|
|
|
app('log')->warning('[RFC] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
app('log')->error($e->getMessage());
|
|
|
|
app('log')->error($e->getTraceAsString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-22 08:43:12 +01:00
|
|
|
public function sendLoginAttemptNotification(UnknownUserAttemptedLogin $event): void
|
2022-10-01 12:21:42 +02:00
|
|
|
{
|
2024-12-12 06:33:48 +01:00
|
|
|
try {
|
|
|
|
$owner = new OwnerNotifiable();
|
2024-12-22 08:43:12 +01:00
|
|
|
Notification::send($owner, new UnknownUserLoginAttempt($event->address));
|
2025-01-04 19:43:58 +01:00
|
|
|
} catch (\Exception $e) {
|
2024-12-12 06:33:48 +01:00
|
|
|
$message = $e->getMessage();
|
|
|
|
if (str_contains($message, 'Bcc')) {
|
|
|
|
app('log')->warning('[Bcc] Could not send notification. Please validate your email settings, use the .env.example file as a guide.');
|
2022-10-01 12:21:42 +02:00
|
|
|
|
2024-12-12 06:33:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str_contains($message, 'RFC 2822')) {
|
|
|
|
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
|
|
|
|
2024-12-12 06:33:48 +01:00
|
|
|
return;
|
2022-10-01 12:21:42 +02:00
|
|
|
}
|
2024-12-12 06:33:48 +01:00
|
|
|
app('log')->error($e->getMessage());
|
|
|
|
app('log')->error($e->getTraceAsString());
|
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-12-08 16:28:22 +01:00
|
|
|
$sendMail = app('fireflyconfig')->get('notification_new_version', true)->data;
|
2022-09-24 11:41:07 +02:00
|
|
|
if (false === $sendMail) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-12 06:33:48 +01:00
|
|
|
try {
|
|
|
|
$owner = new OwnerNotifiable();
|
|
|
|
Notification::send($owner, new VersionCheckResult($event->message));
|
2025-01-04 19:43:58 +01:00
|
|
|
} catch (\Exception $e) {
|
2024-12-12 06:33:48 +01:00
|
|
|
$message = $e->getMessage();
|
|
|
|
if (str_contains($message, 'Bcc')) {
|
|
|
|
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
|
|
|
|
2024-12-12 06:33:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (str_contains($message, 'RFC 2822')) {
|
|
|
|
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
|
|
|
|
2024-12-12 06:33:48 +01:00
|
|
|
return;
|
2022-09-24 11:41:07 +02:00
|
|
|
}
|
2024-12-12 06:33:48 +01:00
|
|
|
app('log')->error($e->getMessage());
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2022-09-24 11:41:07 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-29 19:41:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a test message to an administrator.
|
|
|
|
*/
|
2024-12-14 06:30:27 +01:00
|
|
|
public function sendTestNotification(OwnerTestNotificationChannel $event): void
|
2022-12-29 19:41:57 +01:00
|
|
|
{
|
2024-12-11 07:23:46 +01:00
|
|
|
Log::debug(sprintf('Now in sendTestNotification("%s")', $event->channel));
|
2022-12-29 19:41:57 +01:00
|
|
|
|
2024-12-12 06:33:48 +01:00
|
|
|
switch ($event->channel) {
|
2024-12-08 16:28:22 +01:00
|
|
|
case 'email':
|
2024-12-14 06:30:27 +01:00
|
|
|
$class = OwnerTestNotificationEmail::class;
|
2024-12-14 05:45:54 +01:00
|
|
|
|
2024-12-08 16:28:22 +01:00
|
|
|
break;
|
2024-12-14 05:45:54 +01:00
|
|
|
|
2024-12-08 16:28:22 +01:00
|
|
|
case 'slack':
|
2024-12-14 06:30:27 +01:00
|
|
|
$class = OwnerTestNotificationSlack::class;
|
2024-12-14 05:45:54 +01:00
|
|
|
|
2024-12-08 16:28:22 +01:00
|
|
|
break;
|
2024-12-14 05:45:54 +01:00
|
|
|
|
2024-12-09 06:27:37 +01:00
|
|
|
case 'ntfy':
|
2024-12-14 06:30:27 +01:00
|
|
|
$class = OwnerTestNotificationNtfy::class;
|
2024-12-14 05:45:54 +01:00
|
|
|
|
2024-12-08 16:28:22 +01:00
|
|
|
break;
|
2024-12-14 05:45:54 +01:00
|
|
|
|
2024-12-09 07:34:01 +01:00
|
|
|
case 'pushover':
|
2024-12-14 06:30:27 +01:00
|
|
|
$class = OwnerTestNotificationPushover::class;
|
2024-12-14 05:45:54 +01:00
|
|
|
|
2024-12-09 07:34:01 +01:00
|
|
|
break;
|
2024-12-14 05:45:54 +01:00
|
|
|
|
2024-12-08 16:28:22 +01:00
|
|
|
default:
|
|
|
|
app('log')->error(sprintf('Unknown channel "%s" in sendTestNotification method.', $event->channel));
|
2024-12-14 05:45:54 +01:00
|
|
|
|
2024-12-08 16:28:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log::debug(sprintf('Will send %s as a notification.', $class));
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2023-06-11 16:12:13 +02:00
|
|
|
try {
|
2025-01-04 08:42:06 +01:00
|
|
|
Notification::send($event->owner, new $class());
|
2025-01-04 19:43:58 +01:00
|
|
|
} catch (\Exception $e) {
|
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
|
|
|
}
|
2024-12-08 16:28:22 +01:00
|
|
|
Log::debug(sprintf('If you see no errors above this line, test notification was sent over channel "%s"', $event->channel));
|
2022-12-29 19:41:57 +01:00
|
|
|
}
|
2017-11-08 09:05:10 +01:00
|
|
|
}
|