CaiWuXiTong/app/Abstracts/Notification.php
WuYongJie 9595267ee7
Some checks failed
Tests / PHP 8.1 (push) Has been cancelled
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
chore: 导入 Akaunting 初始源码与团队协作规则
2026-07-20 18:20:25 +08:00

180 lines
4.5 KiB
PHP

<?php
namespace App\Abstracts;
use App\Models\Setting\EmailTemplate;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification as BaseNotification;
use Illuminate\Support\Str;
abstract class Notification extends BaseNotification implements ShouldQueue
{
use Queueable;
/**
* Custom mail subject, body, etc.
*
* @var array
*/
public $custom_mail;
/**
* Create a notification instance.
*/
public function __construct()
{
$this->onQueue('notifications');
}
/**
* Get the notification's channels.
*
* @param mixed $notifiable
* @return array|string
*/
public function via($notifiable)
{
return ['mail', 'database'];
}
/**
* Initialise the mail representation of the notification.
*
*/
public function initMailMessage(): MailMessage
{
app('url')->defaults(['company_id' => company_id()]);
$message = (new MailMessage)
->from(config('mail.from.address'), config('mail.from.name'))
->subject($this->getSubject())
->view('components.email.body', ['body' => $this->getBody()]);
if (!empty($this->custom_mail['cc'])) {
$message->cc($this->custom_mail['cc']);
}
if (!empty($this->custom_mail['bcc'])) {
$message->bcc($this->custom_mail['bcc']);
}
return $message;
}
/**
* Initialise the array representation of the notification.
*
*/
public function initArrayMessage(): void
{
app('url')->defaults(['company_id' => company_id()]);
}
public function getSubject(): string
{
return !empty($this->custom_mail['subject'])
? $this->custom_mail['subject']
: $this->replaceTags($this->template->subject);
}
public function getBody()
{
$body = !empty($this->custom_mail['body'])
? EmailTemplate::sanitizeBody($this->custom_mail['body'])
: $this->replaceTags($this->template->body);
return $body . $this->getFooter();
}
public function replaceTags(string $content): string
{
$pattern = $this->getTagsPattern();
$replacement = $this->applyQuote($this->getTagsReplacement());
return $this->revertQuote(preg_replace($pattern, $replacement, $content));
}
public function getFooter()
{
$url = null;
$get_started = '<span style="color: #676ba2;">' . trans('footer.get_started') . '</span>';
return view('components.email.footer', compact('url', 'get_started'));
}
public function getTagsPattern(): array
{
$pattern = [];
foreach($this->getTags() as $tag) {
$pattern[] = "/" . $tag . "/";
}
return $pattern;
}
public function getTags(): array
{
return [];
}
public function getTagsReplacement(): array
{
return [];
}
public function getTagsBinding(): array
{
$bindings = [];
$tags = $this->getTags();
$replacements = $this->getTagsReplacement();
$wrappers = ['{', '}'];
foreach ($tags as $index => $tag) {
$key = Str::replace($wrappers, '', $tag);
$value = $replacements[$index] ?? '';
// HTML-encode string replacement values to prevent stored XSS (CWE-79).
// Notification description templates contain intentional HTML markup
// (<strong>, <a>) which must be preserved. This encodes only the
// user-supplied values (e.g. customer_name, vendor_name, email)
// while leaving the template's own markup untouched. Non-string
// values (Money, Carbon dates, numeric amounts) pass through
// unchanged since they do not carry attacker-controlled HTML.
$bindings[$key] = is_string($value) ? e($value) : $value;
}
return $bindings;
}
public function applyQuote(array $vars): array
{
$new_vars = [];
foreach ($vars as $var) {
// Ensure $var is a string, default to an empty string if it is null
$new_vars[] = preg_quote($var ?? '');
}
return $new_vars;
}
public function revertQuote(string $content): string
{
return str_replace('\\', '', $content);
}
/**
* @deprecated 3.0
*/
public function initMessage()
{
return $this->initMailMessage();
}
}