199 lines
6.0 KiB
PHP
199 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use App\Enums\GoogleService;
|
|
use App\Services\GoogleOAuthService;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
class EmailSetting extends Model
|
|
{
|
|
protected $table = 'email_settings';
|
|
|
|
protected $fillable = [
|
|
'imap_host', 'imap_port', 'imap_encryption', 'imap_username', 'imap_password',
|
|
'smtp_host', 'smtp_port', 'smtp_encryption', 'smtp_username', 'smtp_password',
|
|
'email_address', 'email_name', 'reply_to', 'sync_interval_minutes',
|
|
'last_sync_at', 'is_active', 'signature', 'signature_enabled', 'auth_method'
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_active' => 'boolean',
|
|
'signature_enabled' => 'boolean',
|
|
'last_sync_at' => 'datetime',
|
|
];
|
|
|
|
public function folders(): HasMany
|
|
{
|
|
return $this->hasMany(EmailFolder::class)->orderBy('sort_order');
|
|
}
|
|
|
|
public function getImapConfig(): array
|
|
{
|
|
return [
|
|
'host' => $this->imap_host,
|
|
'port' => $this->imap_port,
|
|
'encryption' => $this->imap_encryption,
|
|
'username' => $this->imap_username,
|
|
'password' => $this->imap_password,
|
|
];
|
|
}
|
|
|
|
public function getDecryptedPassword(): string
|
|
{
|
|
if ($this->isOauth()) {
|
|
return $this->getOAuthAccessToken() ?? '';
|
|
}
|
|
if (empty($this->imap_password)) {
|
|
return '';
|
|
}
|
|
try {
|
|
return Crypt::decryptString($this->imap_password);
|
|
} catch (\Exception $e) {
|
|
return $this->imap_password;
|
|
}
|
|
}
|
|
|
|
public function getDecryptedSmtpPassword(): string
|
|
{
|
|
if ($this->isOauth()) {
|
|
return $this->getOAuthAccessToken() ?? '';
|
|
}
|
|
if (empty($this->smtp_password)) {
|
|
return $this->getDecryptedPassword();
|
|
}
|
|
try {
|
|
return Crypt::decryptString($this->smtp_password);
|
|
} catch (\Exception $e) {
|
|
return $this->smtp_password;
|
|
}
|
|
}
|
|
|
|
|
|
public function isOauth(): bool
|
|
{
|
|
return $this->auth_method === 'oauth';
|
|
}
|
|
|
|
public function getOAuthAccessToken(): ?string
|
|
{
|
|
if (!$this->isOauth()) {
|
|
return null;
|
|
}
|
|
try {
|
|
$oauth = app(GoogleOAuthService::class);
|
|
return $oauth->getAccessToken(GoogleService::Gmail);
|
|
} catch (\Exception $e) {
|
|
\Illuminate\Support\Facades\Log::error('OAuth token refresh failed: ' . $e->getMessage());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function getSmtpConfig(): array
|
|
{
|
|
return [
|
|
'host' => $this->smtp_host ?? $this->imap_host,
|
|
'port' => $this->smtp_port ?? 587,
|
|
'encryption' => $this->smtp_encryption ?? 'tls',
|
|
'username' => $this->smtp_username ?? $this->imap_username,
|
|
'password' => $this->smtp_password ?? $this->imap_password,
|
|
];
|
|
}
|
|
|
|
public function getImapClient(): ?\DirectoryTree\ImapEngine\Mailbox
|
|
{
|
|
if (!$this->imap_host || !$this->imap_username) {
|
|
return null;
|
|
}
|
|
|
|
if ($this->isOauth()) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
$encryption = match ($this->imap_encryption) {
|
|
'ssl' => 'ssl',
|
|
'tls' => 'tls',
|
|
default => 'none',
|
|
};
|
|
|
|
return new \DirectoryTree\ImapEngine\Mailbox([
|
|
'host' => $this->imap_host,
|
|
'port' => $this->imap_port ?? 993,
|
|
'encryption' => $encryption,
|
|
'validate_cert' => false,
|
|
'username' => $this->imap_username,
|
|
'password' => $this->getDecryptedPassword(),
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function testConnection(): array
|
|
{
|
|
try {
|
|
if ($this->isOauth()) {
|
|
return $this->testOAuthImapConnection();
|
|
}
|
|
|
|
$mailbox = $this->getImapClient();
|
|
|
|
if (!$mailbox) {
|
|
return ['success' => false, 'message' => 'Configurazione IMAP non valida'];
|
|
}
|
|
|
|
$mailbox->connect();
|
|
$mailbox->disconnect();
|
|
|
|
return [
|
|
'success' => true,
|
|
'message' => 'Connessione IMAP riuscita! Server: ' . $this->imap_host
|
|
];
|
|
} catch (\Exception $e) {
|
|
$message = $e->getMessage();
|
|
if (strpos($message, 'Undefined property') !== false) {
|
|
return ['success' => true, 'message' => 'Connessione IMAP riuscita! (Server: ' . $this->imap_host . ')'];
|
|
}
|
|
return ['success' => false, 'message' => $message];
|
|
}
|
|
}
|
|
|
|
private function testOAuthImapConnection(): array
|
|
{
|
|
try {
|
|
$token = $this->getOAuthAccessToken();
|
|
if (!$token) {
|
|
return ['success' => false, 'message' => 'Impossibile ottenere il token OAuth. Verifica la connessione Google.'];
|
|
}
|
|
|
|
$client = new \Webklex\PHPIMAP\Client([
|
|
'host' => $this->imap_host,
|
|
'port' => $this->imap_port ?? 993,
|
|
'encryption' => $this->imap_encryption ?? 'ssl',
|
|
'validate_cert' => false,
|
|
'username' => $this->imap_username,
|
|
'password' => $token,
|
|
'authentication' => 'oauth',
|
|
]);
|
|
$client->connect();
|
|
$client->disconnect();
|
|
|
|
return ['success' => true, 'message' => 'Connessione IMAP OAuth riuscita! Server: ' . $this->imap_host];
|
|
} catch (\Exception $e) {
|
|
return ['success' => false, 'message' => 'Errore connessione IMAP OAuth: ' . $e->getMessage()];
|
|
}
|
|
}
|
|
|
|
public static function getActive()
|
|
{
|
|
return self::where('is_active', true)->first();
|
|
}
|
|
|
|
public function getSignature(): ?string
|
|
{
|
|
return $this->signature;
|
|
}
|
|
} |