← All changes
|
backend/src/Services/Notification/NotificationService.php
+307
-67
0.8.1
→
1.5
View file →
| @@ -1,32 +1,36 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Services\Notification; |
| 4 | 4 | |
| 5 | -// phpcs:disable PSR1.Files.SideEffects | |
| 6 | -if (!defined('ABSPATH')) { | |
| 7 | - exit; // Exit if accessed directly | |
| 8 | -} | |
| 9 | - | |
| 10 | 5 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 11 | 6 | use IvyForms\Common\Exceptions\NotFoundException; |
| 12 | 7 | use IvyForms\Common\Exceptions\QueryExecutionException; |
| 8 | +use IvyForms\Common\Sanitizer\HtmlSanitizer; | |
| 13 | 9 | use IvyForms\Common\Sanitizer\Sanitizer; |
| 14 | 10 | use IvyForms\Entity\Notification\Notification; |
| 15 | 11 | use IvyForms\Factory\Notification\NotificationFactory; |
| 16 | 12 | use IvyForms\Repository\Notification\NotificationRepositoryInterface; |
| 17 | 13 | use IvyForms\Services\Placeholder\PlaceholderService; |
| 14 | +use IvyForms\Services\Media\ImageService; | |
| 18 | 15 | use IvyForms\Services\Translations\BackendStrings; |
| 19 | 16 | use IvyForms\Services\Mailer\MailerService; |
| 20 | 17 | |
| 18 | +/** | |
| 19 | + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) | |
| 20 | + */ | |
| 21 | 21 | class NotificationService |
| 22 | 22 | { |
| 23 | 23 | private NotificationRepositoryInterface $notificationRepository; |
| 24 | + private ImageService $imageService; | |
| 24 | 25 | |
| 25 | 26 | // Constructor injection for the NotificationRepository via PHP-DI |
| 26 | - public function __construct(NotificationRepositoryInterface $notificationRepository) | |
| 27 | - { | |
| 27 | + public function __construct( | |
| 28 | + NotificationRepositoryInterface $notificationRepository, | |
| 29 | + ImageService $imageService | |
| 30 | + ) { | |
| 28 | 31 | $this->notificationRepository = $notificationRepository; |
| 32 | + $this->imageService = $imageService; | |
| 29 | 33 | } |
| 30 | 34 | |
| 31 | 35 | /** |
| 32 | 36 | * Get all notifications from the repository. |
| @@ -46,8 +50,15 @@ | ||
| 46 | 50 | * @return int Notification ID |
| 47 | 51 | */ |
| 48 | 52 | public function createNotification(Notification $notificationData): int |
| 49 | 53 | { |
| 54 | + // Process images in message content before saving | |
| 55 | + $message = $notificationData->getMessage(); | |
| 56 | + if (!empty($message)) { | |
| 57 | + $processedMessage = $this->imageService->processImagesInContent($message, 'notification'); | |
| 58 | + $notificationData->setMessage($processedMessage); | |
| 59 | + } | |
| 60 | + | |
| 50 | 61 | // Create notification via repository and return new notification ID |
| 51 | 62 | return $this->notificationRepository->add($notificationData); |
| 52 | 63 | } |
| 53 | 64 | |
| @@ -82,8 +93,15 @@ | ||
| 82 | 93 | * @return bool |
| 83 | 94 | */ |
| 84 | 95 | public function updateNotification(int $notificationId, Notification $notificationData): bool |
| 85 | 96 | { |
| 97 | + // Process images in message content before saving | |
| 98 | + $message = $notificationData->getMessage(); | |
| 99 | + if (!empty($message)) { | |
| 100 | + $processedMessage = $this->imageService->processImagesInContent($message, 'notification'); | |
| 101 | + $notificationData->setMessage($processedMessage); | |
| 102 | + } | |
| 103 | + | |
| 86 | 104 | // Validate notification data before updating |
| 87 | 105 | |
| 88 | 106 | return $this->notificationRepository->update($notificationId, $notificationData); |
| 89 | 107 | } |
| @@ -117,22 +135,12 @@ | ||
| 117 | 135 | * |
| 118 | 136 | * @param int $formId |
| 119 | 137 | * |
| 120 | 138 | * @return array<Notification> |
| 121 | - * | |
| 122 | - * @throws NotFoundException If no notifications are found | |
| 123 | 139 | */ |
| 124 | 140 | public function getNotificationsByFormId(int $formId): array |
| 125 | 141 | { |
| 126 | - $notification = $this->notificationRepository->getAllById($formId); | |
| 127 | - | |
| 128 | - if (!$notification) { | |
| 129 | - throw new NotFoundException( | |
| 130 | - BackendStrings::getExceptionStrings()['notification_not_found'] | |
| 131 | - ); | |
| 132 | - } | |
| 133 | - | |
| 134 | - return $notification; | |
| 142 | + return $this->notificationRepository->getAllById($formId); | |
| 135 | 143 | } |
| 136 | 144 | |
| 137 | 145 | /** |
| 138 | 146 | * Get active notifications by form ID. |
| @@ -227,9 +235,13 @@ | ||
| 227 | 235 | 'receiver' => $adminEmail, |
| 228 | 236 | 'enabled' => 1, |
| 229 | 237 | 'subject' => $formName, |
| 230 | 238 | 'message' => '{{all_data}}', |
| 231 | - 'smartLogic' => 0, | |
| 239 | + 'smartLogic' => [ | |
| 240 | + 'enabled' => false, | |
| 241 | + 'match' => 'any', | |
| 242 | + 'rules' => [], | |
| 243 | + ], | |
| 232 | 244 | ]; |
| 233 | 245 | $notification = NotificationFactory::create($defaultNotification); |
| 234 | 246 | $notificationId = $this->createNotification($notification); |
| 235 | 247 | if (!$notificationId) { |
| @@ -244,8 +256,9 @@ | ||
| 244 | 256 | * Process notifications for the form. |
| 245 | 257 | * |
| 246 | 258 | * @param int $formId |
| 247 | 259 | * @param array<mixed> $submissionData |
| 260 | + * @param array<mixed> $formFields | |
| 248 | 261 | * @param array<string, mixed> $fieldData |
| 249 | 262 | * @param array<string, mixed> $generalData |
| 250 | 263 | * @param MailerService $mailerService |
| 251 | 264 | * @param array<string, string> $fieldLabels |
| @@ -253,8 +266,9 @@ | ||
| 253 | 266 | */ |
| 254 | 267 | public function processNotifications( |
| 255 | 268 | int $formId, |
| 256 | 269 | array $submissionData, |
| 270 | + array $formFields, | |
| 257 | 271 | array $fieldData, |
| 258 | 272 | array $generalData, |
| 259 | 273 | MailerService $mailerService, |
| 260 | 274 | array $fieldLabels = [] |
| @@ -260,76 +274,254 @@ | ||
| 260 | 274 | array $fieldLabels = [] |
| 261 | 275 | ): bool { |
| 262 | 276 | $activeNotifications = $this->getActiveNotificationsByFormId($formId); |
| 263 | 277 | $exceptionStrings = BackendStrings::getExceptionStrings(); |
| 278 | + | |
| 264 | 279 | foreach ($activeNotifications as $notification) { |
| 265 | - if (empty($notification->getReceiver())) { | |
| 280 | + if (empty($notification->getNotificationContact()->getReceiver())) { | |
| 266 | 281 | continue; |
| 267 | 282 | } |
| 268 | 283 | |
| 269 | - // Resolve sender and receiver | |
| 270 | - $sender = $this->resolveAddress( | |
| 271 | - $notification->getSender(), | |
| 272 | - $fieldData, | |
| 273 | - $generalData, | |
| 274 | - $fieldLabels | |
| 284 | + $shouldSend = apply_filters( | |
| 285 | + 'ivyforms/notification/should_send', | |
| 286 | + true, | |
| 287 | + $notification->getSmartLogic(), | |
| 288 | + $submissionData, | |
| 289 | + $formFields, | |
| 290 | + $notification, | |
| 291 | + $formId | |
| 275 | 292 | ); |
| 276 | 293 | |
| 277 | - $receiver = $this->resolveAddress( | |
| 278 | - $notification->getReceiver(), | |
| 294 | + if (!$shouldSend) { | |
| 295 | + continue; | |
| 296 | + } | |
| 297 | + | |
| 298 | + if ( | |
| 299 | + !$this->dispatchNotificationEmail( | |
| 300 | + $notification, | |
| 301 | + $formId, | |
| 302 | + $submissionData, | |
| 303 | + $formFields, | |
| 304 | + $fieldData, | |
| 305 | + $generalData, | |
| 306 | + $mailerService, | |
| 307 | + $fieldLabels, | |
| 308 | + $exceptionStrings | |
| 309 | + ) | |
| 310 | + ) { | |
| 311 | + return false; | |
| 312 | + } | |
| 313 | + } | |
| 314 | + | |
| 315 | + return true; | |
| 316 | + } | |
| 317 | + | |
| 318 | + /** | |
| 319 | + * Resolve placeholders, apply attachments, and send one notification email. | |
| 320 | + * | |
| 321 | + * @param Notification $notification | |
| 322 | + * @param int $formId | |
| 323 | + * @param array<mixed> $submissionData | |
| 324 | + * @param array<mixed> $formFields | |
| 325 | + * @param array<string, mixed> $fieldData | |
| 326 | + * @param array<string, mixed> $generalData | |
| 327 | + * @param MailerService $mailerService | |
| 328 | + * @param array<string, string> $fieldLabels | |
| 329 | + * @param array<string, string> $exceptionStrings | |
| 330 | + * @return bool False when sender/receiver are invalid; true otherwise (send failure is logged) | |
| 331 | + */ | |
| 332 | + private function dispatchNotificationEmail( | |
| 333 | + Notification $notification, | |
| 334 | + int $formId, | |
| 335 | + array $submissionData, | |
| 336 | + array $formFields, | |
| 337 | + array $fieldData, | |
| 338 | + array $generalData, | |
| 339 | + MailerService $mailerService, | |
| 340 | + array $fieldLabels, | |
| 341 | + array $exceptionStrings | |
| 342 | + ): bool { | |
| 343 | + if ( | |
| 344 | + !$this->hydrateNotificationForSend( | |
| 345 | + $notification, | |
| 279 | 346 | $fieldData, |
| 280 | 347 | $generalData, |
| 281 | - $fieldLabels | |
| 348 | + $fieldLabels, | |
| 349 | + $exceptionStrings | |
| 350 | + ) | |
| 351 | + ) { | |
| 352 | + return false; | |
| 353 | + } | |
| 354 | + | |
| 355 | + $this->sendHydratedNotificationEmail( | |
| 356 | + $notification, | |
| 357 | + $formId, | |
| 358 | + $submissionData, | |
| 359 | + $formFields, | |
| 360 | + $fieldData, | |
| 361 | + $mailerService, | |
| 362 | + $exceptionStrings | |
| 363 | + ); | |
| 364 | + | |
| 365 | + return true; | |
| 366 | + } | |
| 367 | + | |
| 368 | + /** | |
| 369 | + * Resolve and apply sender/receiver/replyTo/cc/bcc/subject/message onto the notification. | |
| 370 | + * | |
| 371 | + * @param Notification $notification | |
| 372 | + * @param array<string, mixed> $fieldData | |
| 373 | + * @param array<string, mixed> $generalData | |
| 374 | + * @param array<string, string> $fieldLabels | |
| 375 | + * @param array<string, string> $exceptionStrings | |
| 376 | + */ | |
| 377 | + private function hydrateNotificationForSend( | |
| 378 | + Notification $notification, | |
| 379 | + array $fieldData, | |
| 380 | + array $generalData, | |
| 381 | + array $fieldLabels, | |
| 382 | + array $exceptionStrings | |
| 383 | + ): bool { | |
| 384 | + $contact = $notification->getNotificationContact(); | |
| 385 | + $sender = $this->resolveAddress($contact->getSender(), $fieldData, $generalData, $fieldLabels); | |
| 386 | + $receiver = $this->resolveAddress($contact->getReceiver(), $fieldData, $generalData, $fieldLabels); | |
| 387 | + | |
| 388 | + if ($sender === '' || $receiver === '') { | |
| 389 | + error_log( | |
| 390 | + $exceptionStrings['error_log_invalid_email'] . $sender . ' ' . $receiver | |
| 282 | 391 | ); |
| 392 | + return false; | |
| 393 | + } | |
| 283 | 394 | |
| 284 | - // Build subject and sanitized message | |
| 285 | - $subject = PlaceholderService::replacePlaceholders( | |
| 395 | + $contact->setSender($sender); | |
| 396 | + $contact->setReceiver($receiver); | |
| 397 | + $contact->setReplyTo( | |
| 398 | + $this->resolveReplyTo($contact->getReplyTo(), $sender, $fieldData, $generalData, $fieldLabels) | |
| 399 | + ); | |
| 400 | + $contact->setCc( | |
| 401 | + $this->resolveAddressList($contact->getCc(), $fieldData, $generalData, $fieldLabels) | |
| 402 | + ); | |
| 403 | + $contact->setBcc( | |
| 404 | + $this->resolveAddressList($contact->getBcc(), $fieldData, $generalData, $fieldLabels) | |
| 405 | + ); | |
| 406 | + $notification->setSubject( | |
| 407 | + PlaceholderService::replacePlaceholders( | |
| 286 | 408 | $notification->getSubject(), |
| 287 | 409 | $fieldData, |
| 288 | 410 | $generalData, |
| 289 | - $fieldLabels | |
| 290 | - ); | |
| 291 | - | |
| 292 | - $safeMessage = $this->buildSafeMessage( | |
| 411 | + $fieldLabels, | |
| 412 | + $notification | |
| 413 | + ) | |
| 414 | + ); | |
| 415 | + $notification->setMessage( | |
| 416 | + $this->buildSafeMessage( | |
| 293 | 417 | $notification->getMessage(), |
| 294 | 418 | $fieldData, |
| 295 | 419 | $generalData, |
| 296 | - $fieldLabels | |
| 297 | - ); | |
| 420 | + $fieldLabels, | |
| 421 | + $notification | |
| 422 | + ) | |
| 423 | + ); | |
| 298 | 424 | |
| 299 | - // Resolve replyTo and validate; fallback to sender if empty | |
| 300 | - $replyTo = $this->resolveAddress( | |
| 301 | - $notification->getReplyTo(), | |
| 302 | - $fieldData, | |
| 303 | - $generalData, | |
| 304 | - $fieldLabels | |
| 305 | - ); | |
| 306 | - if (empty($replyTo) && !empty($sender)) { | |
| 307 | - $replyTo = $sender; | |
| 308 | - } | |
| 425 | + return true; | |
| 426 | + } | |
| 309 | 427 | |
| 310 | - if (empty($sender) || empty($receiver)) { | |
| 311 | - error_log( | |
| 312 | - $exceptionStrings['error_log_invalid_email'] . $sender . ' ' . $receiver | |
| 313 | - ); | |
| 314 | - return false; | |
| 315 | - } | |
| 428 | + /** | |
| 429 | + * @param Notification $notification | |
| 430 | + * @param int $formId | |
| 431 | + * @param array<mixed> $submissionData | |
| 432 | + * @param array<mixed> $formFields | |
| 433 | + * @param array<string, mixed> $fieldData | |
| 434 | + * @param MailerService $mailerService | |
| 435 | + * @param array<string, string> $exceptionStrings | |
| 436 | + */ | |
| 437 | + private function sendHydratedNotificationEmail( | |
| 438 | + Notification $notification, | |
| 439 | + int $formId, | |
| 440 | + array $submissionData, | |
| 441 | + array $formFields, | |
| 442 | + array $fieldData, | |
| 443 | + MailerService $mailerService, | |
| 444 | + array $exceptionStrings | |
| 445 | + ): void { | |
| 446 | + $sent = $mailerService->sendEmail( | |
| 447 | + $notification, | |
| 448 | + $submissionData, | |
| 449 | + $this->resolveEmailAttachments($notification, $submissionData, $formFields, $fieldData) | |
| 450 | + ); | |
| 316 | 451 | |
| 317 | - // Apply resolved values back to the notification | |
| 318 | - $notification->setSender($sender); | |
| 319 | - $notification->setReceiver($receiver); | |
| 320 | - $notification->setReplyTo($replyTo); | |
| 321 | - $notification->setSubject($subject); | |
| 322 | - $notification->setMessage($safeMessage); | |
| 452 | + if ($sent) { | |
| 453 | + return; | |
| 454 | + } | |
| 323 | 455 | |
| 324 | - // Send notification | |
| 325 | - $sent = $mailerService->sendEmail($notification, $submissionData); | |
| 326 | - if (!$sent) { | |
| 327 | - error_log($exceptionStrings['error_log_failed_send'] . ' ' . $formId); | |
| 456 | + error_log($exceptionStrings['error_log_failed_send'] . ' ' . $formId); | |
| 457 | + } | |
| 458 | + | |
| 459 | + /** | |
| 460 | + * @param string $template | |
| 461 | + * @param string $sender | |
| 462 | + * @param array<string, mixed> $fieldData | |
| 463 | + * @param array<string, mixed> $generalData | |
| 464 | + * @param array<string, string> $fieldLabels | |
| 465 | + */ | |
| 466 | + private function resolveReplyTo( | |
| 467 | + string $template, | |
| 468 | + string $sender, | |
| 469 | + array $fieldData, | |
| 470 | + array $generalData, | |
| 471 | + array $fieldLabels | |
| 472 | + ): string { | |
| 473 | + $replyTo = $this->resolveAddress($template, $fieldData, $generalData, $fieldLabels); | |
| 474 | + | |
| 475 | + return $replyTo !== '' ? $replyTo : $sender; | |
| 476 | + } | |
| 477 | + | |
| 478 | + /** | |
| 479 | + * @param Notification $notification | |
| 480 | + * @param array<mixed> $submissionData | |
| 481 | + * @param array<mixed> $formFields | |
| 482 | + * @param array<string, mixed> $fieldData | |
| 483 | + * @return string[] | |
| 484 | + */ | |
| 485 | + private function resolveEmailAttachments( | |
| 486 | + Notification $notification, | |
| 487 | + array $submissionData, | |
| 488 | + array $formFields, | |
| 489 | + array $fieldData | |
| 490 | + ): array { | |
| 491 | + /** | |
| 492 | + * Local file paths to attach to the notification email (Pro file attachments). | |
| 493 | + * | |
| 494 | + * @since 1.2.0 | |
| 495 | + * | |
| 496 | + * @param mixed $attachments | |
| 497 | + * @param \IvyForms\Entity\Notification\Notification $notification | |
| 498 | + * @param array<string, mixed> $submissionData | |
| 499 | + * @param array<int, mixed> $formFields | |
| 500 | + * @param array<string, mixed> $fieldData | |
| 501 | + */ | |
| 502 | + $attachments = apply_filters( | |
| 503 | + 'ivyforms/notification/email_attachments', | |
| 504 | + [], | |
| 505 | + $notification, | |
| 506 | + $submissionData, | |
| 507 | + $formFields, | |
| 508 | + $fieldData | |
| 509 | + ); | |
| 510 | + | |
| 511 | + if (!is_array($attachments)) { | |
| 512 | + return []; | |
| 513 | + } | |
| 514 | + | |
| 515 | + $paths = []; | |
| 516 | + foreach ($attachments as $path) { | |
| 517 | + if (!is_string($path) || $path === '') { | |
| 518 | + continue; | |
| 328 | 519 | } |
| 520 | + $paths[] = $path; | |
| 329 | 521 | } |
| 330 | 522 | |
| 331 | - return true; | |
| 523 | + return $paths; | |
| 332 | 524 | } |
| 333 | 525 | |
| 334 | 526 | /** |
| 335 | 527 | * Resolve an email address template that may contain placeholders |
| @@ -343,8 +535,12 @@ | ||
| 343 | 535 | * @return string |
| 344 | 536 | */ |
| 345 | 537 | private function resolveAddress(string $template, array $fieldData, array $generalData, array $fieldLabels): string |
| 346 | 538 | { |
| 539 | + if (Sanitizer::containsEmailHeaderInjection($template)) { | |
| 540 | + return ''; | |
| 541 | + } | |
| 542 | + | |
| 347 | 543 | $rawReplaced = PlaceholderService::replacePlaceholders( |
| 348 | 544 | $template, |
| 349 | 545 | $fieldData, |
| 350 | 546 | $generalData, |
| @@ -350,8 +546,12 @@ | ||
| 350 | 546 | $generalData, |
| 351 | 547 | $fieldLabels |
| 352 | 548 | ); |
| 353 | 549 | |
| 550 | + if (Sanitizer::containsEmailHeaderInjection($rawReplaced)) { | |
| 551 | + return ''; | |
| 552 | + } | |
| 553 | + | |
| 354 | 554 | $emailCandidate = $rawReplaced; |
| 355 | 555 | if (preg_match('/<([^>]+)>/', $rawReplaced, $match)) { |
| 356 | 556 | $emailCandidate = trim($match[1]); |
| 357 | 557 | } |
| @@ -359,8 +559,45 @@ | ||
| 359 | 559 | return Sanitizer::isValidEmail($emailCandidate) ? $rawReplaced : ''; |
| 360 | 560 | } |
| 361 | 561 | |
| 362 | 562 | /** |
| 563 | + * Resolve a comma-separated list of email address templates. | |
| 564 | + * | |
| 565 | + * @param string $template | |
| 566 | + * @param array<string,mixed> $fieldData | |
| 567 | + * @param array<string,mixed> $generalData | |
| 568 | + * @param array<string,string> $fieldLabels | |
| 569 | + * @return string | |
| 570 | + */ | |
| 571 | + private function resolveAddressList( | |
| 572 | + string $template, | |
| 573 | + array $fieldData, | |
| 574 | + array $generalData, | |
| 575 | + array $fieldLabels | |
| 576 | + ): string { | |
| 577 | + $template = trim($template); | |
| 578 | + if ($template === '') { | |
| 579 | + return ''; | |
| 580 | + } | |
| 581 | + | |
| 582 | + $parts = array_map('trim', explode(',', $template)); | |
| 583 | + $resolved = []; | |
| 584 | + | |
| 585 | + foreach ($parts as $part) { | |
| 586 | + if ($part === '' || Sanitizer::containsEmailHeaderInjection($part)) { | |
| 587 | + continue; | |
| 588 | + } | |
| 589 | + | |
| 590 | + $address = $this->resolveAddress($part, $fieldData, $generalData, $fieldLabels); | |
| 591 | + if ($address !== '') { | |
| 592 | + $resolved[] = $address; | |
| 593 | + } | |
| 594 | + } | |
| 595 | + | |
| 596 | + return implode(', ', $resolved); | |
| 597 | + } | |
| 598 | + | |
| 599 | + /** | |
| 363 | 600 | * Build a sanitized HTML message from a template with placeholders. |
| 364 | 601 | * |
| 365 | 602 | * @param string $template |
| 366 | 603 | * @param array<string,mixed> $fieldData |
| @@ -365,8 +602,9 @@ | ||
| 365 | 602 | * @param string $template |
| 366 | 603 | * @param array<string,mixed> $fieldData |
| 367 | 604 | * @param array<string,mixed> $generalData |
| 368 | 605 | * @param array<string,string> $fieldLabels |
| 606 | + * @param Notification $notification | |
| 369 | 607 | * @return string |
| 370 | 608 | */ |
| 371 | 609 | private function buildSafeMessage( |
| 372 | 610 | string $template, |
| @@ -371,16 +609,18 @@ | ||
| 371 | 609 | private function buildSafeMessage( |
| 372 | 610 | string $template, |
| 373 | 611 | array $fieldData, |
| 374 | 612 | array $generalData, |
| 375 | - array $fieldLabels | |
| 613 | + array $fieldLabels, | |
| 614 | + Notification $notification | |
| 376 | 615 | ): string { |
| 377 | 616 | $message = PlaceholderService::replacePlaceholders( |
| 378 | 617 | $template, |
| 379 | 618 | $fieldData, |
| 380 | 619 | $generalData, |
| 381 | - $fieldLabels | |
| 620 | + $fieldLabels, | |
| 621 | + $notification | |
| 382 | 622 | ); |
| 383 | 623 | |
| 384 | - return Sanitizer::sanitizeHtmlContent($message); | |
| 624 | + return HtmlSanitizer::sanitizeHtmlContent($message); | |
| 385 | 625 | } |
| 386 | 626 | } |