| 
									
										
										
										
											2024-12-11 07:23:46 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 07:23:46 +01:00
										 |  |  | /* | 
					
						
							|  |  |  |  * ReturnsAvailableChannels.php | 
					
						
							|  |  |  |  * Copyright (c) 2024 james@firefly-iii.org. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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/. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Notifications; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Support\Notifications\UrlValidator; | 
					
						
							| 
									
										
										
										
											2024-12-14 06:57:57 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2024-12-12 06:33:48 +01:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							|  |  |  | use NotificationChannels\Pushover\PushoverChannel; | 
					
						
							|  |  |  | use Wijourdil\NtfyNotificationChannel\Channels\NtfyChannel; | 
					
						
							| 
									
										
										
										
											2024-12-11 07:23:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ReturnsAvailableChannels | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-12-14 06:57:57 +01:00
										 |  |  |     public static function returnChannels(string $type, ?User $user = null): array | 
					
						
							| 
									
										
										
										
											2024-12-12 06:33:48 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-11 07:23:46 +01:00
										 |  |  |         $channels = ['mail']; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 06:33:48 +01:00
										 |  |  |         if ('owner' === $type) { | 
					
						
							|  |  |  |             return self::returnOwnerChannels(); | 
					
						
							| 
									
										
										
										
											2024-12-11 07:23:46 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-14 07:22:46 +01:00
										 |  |  |         if ('user' === $type && null !== $user) { | 
					
						
							| 
									
										
										
										
											2024-12-14 06:57:57 +01:00
										 |  |  |             return self::returnUserChannels($user); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-11 07:23:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 06:33:48 +01:00
										 |  |  |         return $channels; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private static function returnOwnerChannels(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-14 21:55:42 +01:00
										 |  |  |         $channels          = ['mail']; | 
					
						
							|  |  |  |         $slackUrl          = app('fireflyconfig')->getEncrypted('slack_webhook_url', '')->data; | 
					
						
							| 
									
										
										
										
											2024-12-12 06:33:48 +01:00
										 |  |  |         if (UrlValidator::isValidWebhookURL($slackUrl)) { | 
					
						
							|  |  |  |             $channels[] = 'slack'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // validate presence of of Ntfy settings.
 | 
					
						
							|  |  |  |         if ('' !== (string) app('fireflyconfig')->getEncrypted('ntfy_topic', '')->data) { | 
					
						
							|  |  |  |             Log::debug('Enabled ntfy.'); | 
					
						
							|  |  |  |             $channels[] = NtfyChannel::class; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ('' === (string) app('fireflyconfig')->getEncrypted('ntfy_topic', '')->data) { | 
					
						
							|  |  |  |             Log::warning('No topic name for Ntfy, channel is disabled.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // pushover
 | 
					
						
							|  |  |  |         $pushoverAppToken  = (string) app('fireflyconfig')->getEncrypted('pushover_app_token', '')->data; | 
					
						
							|  |  |  |         $pushoverUserToken = (string) app('fireflyconfig')->getEncrypted('pushover_user_token', '')->data; | 
					
						
							|  |  |  |         if ('' === $pushoverAppToken || '' === $pushoverUserToken) { | 
					
						
							|  |  |  |             Log::warning('[b] No Pushover token, channel is disabled.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ('' !== $pushoverAppToken && '' !== $pushoverUserToken) { | 
					
						
							|  |  |  |             Log::debug('Enabled pushover.'); | 
					
						
							|  |  |  |             $channels[] = PushoverChannel::class; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-11 07:23:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 06:33:48 +01:00
										 |  |  |         Log::debug(sprintf('Final channel set in ReturnsAvailableChannels: %s ', implode(', ', $channels))); | 
					
						
							| 
									
										
										
										
											2024-12-14 05:45:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-14 06:57:57 +01:00
										 |  |  |         return $channels; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-14 07:22:46 +01:00
										 |  |  |     private static function returnUserChannels(User $user): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-31 08:05:25 +01:00
										 |  |  |         Log::debug(sprintf('Checking channels for user #%d', $user->id)); | 
					
						
							| 
									
										
										
										
											2024-12-14 21:55:42 +01:00
										 |  |  |         $channels          = ['mail']; | 
					
						
							| 
									
										
										
										
											2024-12-31 08:05:25 +01:00
										 |  |  |         $slackUrl          = (string) app('preferences')->getEncryptedForUser($user, 'slack_webhook_url', '')->data; | 
					
						
							| 
									
										
										
										
											2024-12-14 06:57:57 +01:00
										 |  |  |         if (UrlValidator::isValidWebhookURL($slackUrl)) { | 
					
						
							|  |  |  |             $channels[] = 'slack'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // validate presence of of Ntfy settings.
 | 
					
						
							| 
									
										
										
										
											2024-12-31 08:09:51 +01:00
										 |  |  |         $ntfyTopic         = (string) app('preferences')->getEncryptedForUser($user, 'ntfy_topic', '')->data; | 
					
						
							| 
									
										
										
										
											2024-12-31 08:05:25 +01:00
										 |  |  |         if ('' !== $ntfyTopic) { | 
					
						
							|  |  |  |             Log::debug(sprintf('Enabled ntfy, "%s"', $ntfyTopic)); | 
					
						
							| 
									
										
										
										
											2024-12-14 06:57:57 +01:00
										 |  |  |             $channels[] = NtfyChannel::class; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-12-14 07:22:46 +01:00
										 |  |  |         if ('' === (string) app('preferences')->getEncryptedForUser($user, 'ntfy_topic', '')->data) { | 
					
						
							| 
									
										
										
										
											2024-12-14 06:57:57 +01:00
										 |  |  |             Log::warning('No topic name for Ntfy, channel is disabled.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // pushover
 | 
					
						
							|  |  |  |         $pushoverAppToken  = (string) app('preferences')->getEncryptedForUser($user, 'pushover_app_token', '')->data; | 
					
						
							|  |  |  |         $pushoverUserToken = (string) app('preferences')->getEncryptedForUser($user, 'pushover_user_token', '')->data; | 
					
						
							|  |  |  |         if ('' === $pushoverAppToken || '' === $pushoverUserToken) { | 
					
						
							|  |  |  |             Log::warning('[b] No Pushover token, channel is disabled.'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ('' !== $pushoverAppToken && '' !== $pushoverUserToken) { | 
					
						
							|  |  |  |             Log::debug('Enabled pushover.'); | 
					
						
							|  |  |  |             $channels[] = PushoverChannel::class; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Log::debug(sprintf('Final channel set in ReturnsAvailableChannels (user): %s ', implode(', ', $channels))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 06:33:48 +01:00
										 |  |  |         // only the owner can get notifications over
 | 
					
						
							| 
									
										
										
										
											2024-12-11 07:23:46 +01:00
										 |  |  |         return $channels; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |