Add TuxiNet branding and document templates

This commit is contained in:
Thomas
2025-10-28 13:56:54 +01:00
parent f956a735ca
commit 3755435890
48 changed files with 1762 additions and 1 deletions

View File

@@ -0,0 +1,69 @@
<?php
namespace App\Services;
use App\Models\User;
use App\Support\PasswordHasher;
class AuthService
{
private static ?self $instance = null;
private ?User $user = null;
private function __construct()
{
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (isset($_SESSION['user_id'])) {
$this->user = User::find((int) $_SESSION['user_id']);
}
}
public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function attempt(string $email, string $password): bool
{
$user = User::findByEmail($email);
if (!$user || !$user->is_active) {
return false;
}
if (!PasswordHasher::verify($password, $user->pass_hash)) {
return false;
}
$_SESSION['user_id'] = $user->id;
$this->user = $user;
return true;
}
public function logout(): void
{
$_SESSION = [];
if (ini_get('session.use_cookies')) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
}
session_destroy();
$this->user = null;
}
public function check(): bool
{
return $this->user !== null;
}
public function user(): ?User
{
return $this->user;
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace App\Services;
use PHPMailer\PHPMailer\Exception as MailException;
use PHPMailer\PHPMailer\PHPMailer;
class MailService
{
/**
* @param string|array<int|string, string|array{address:string,name?:string}> $to
* @param array<int|string, string|array{address:string,name?:string}> $cc
* @param array<int|string, string|array{address:string,name?:string}> $bcc
*/
public function send($to, string $subject, string $htmlBody, ?string $textBody = null, array $cc = [], array $bcc = []): void
{
$config = config('mail');
$mailer = new PHPMailer(true);
$mailer->CharSet = 'UTF-8';
$mailer->isHTML(true);
$driver = strtolower((string)($config['driver'] ?? 'smtp'));
if ($driver === 'sendmail') {
$mailer->isSendmail();
if (!empty($config['sendmail_path'])) {
$mailer->Sendmail = $config['sendmail_path'];
}
} elseif ($driver === 'mail') {
$mailer->isMail();
} else {
$mailer->isSMTP();
$mailer->Host = (string)($config['host'] ?? 'localhost');
$mailer->Port = (int)($config['port'] ?? 25);
$mailer->Timeout = (int)($config['timeout'] ?? 30);
$username = $config['username'] ?? null;
$password = $config['password'] ?? null;
if ($username) {
$mailer->SMTPAuth = true;
$mailer->Username = $username;
$mailer->Password = (string)$password;
} else {
$mailer->SMTPAuth = false;
}
$encryption = strtolower((string)($config['encryption'] ?? ''));
if ($encryption === 'ssl') {
$mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
} elseif ($encryption === 'tls') {
$mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
}
}
$fromAddress = $config['from']['address'] ?? null;
$fromName = $config['from']['name'] ?? '';
if ($fromAddress) {
$mailer->setFrom($fromAddress, $fromName ?: $fromAddress);
}
foreach ($this->normalizeRecipients($to) as $recipient) {
$mailer->addAddress($recipient['address'], $recipient['name']);
}
foreach ($this->normalizeRecipients($cc) as $recipient) {
$mailer->addCC($recipient['address'], $recipient['name']);
}
foreach ($this->normalizeRecipients($bcc) as $recipient) {
$mailer->addBCC($recipient['address'], $recipient['name']);
}
$mailer->Subject = $subject;
$mailer->Body = $htmlBody;
$mailer->AltBody = $textBody ?? strip_tags($htmlBody);
try {
$mailer->send();
} catch (MailException $exception) {
throw new \RuntimeException('Unable to send mail: ' . $exception->getMessage(), previous: $exception);
}
}
/**
* @param string|array<int|string, string|array{address:string,name?:string}> $addresses
* @return list<array{address:string,name:string}>
*/
private function normalizeRecipients($addresses): array
{
if ($addresses === null) {
return [];
}
if (!is_array($addresses)) {
return [['address' => (string)$addresses, 'name' => '']];
}
$normalized = [];
foreach ($addresses as $key => $value) {
if (is_array($value)) {
$address = $value['address'] ?? null;
if (!$address) {
continue;
}
$normalized[] = [
'address' => (string)$address,
'name' => (string)($value['name'] ?? ''),
];
} elseif (is_string($key) && is_string($value)) {
$normalized[] = [
'address' => $key,
'name' => $value,
];
} else {
$normalized[] = [
'address' => (string)$value,
'name' => '',
];
}
}
return $normalized;
}
}