wip(ISP config connections and command)

This commit is contained in:
2025-12-29 18:23:32 +01:00
parent ad47b58ad3
commit 54f056ca5f
12 changed files with 455 additions and 115 deletions

View File

@@ -0,0 +1,90 @@
<?php
namespace App\Services\ISPConfig;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
class ISPConfigMailService extends ISPConfigService
{
public function __construct()
{
parent::__construct('mail_server');
}
/**
* Récupère tous les domaines mail
*/
public function getAllMailDomains(): array
{
return Cache::remember(
"ispconfig.mail.domains.all",
config('services.ispconfig.cache_ttl'),
fn() => $this->call('mail_domain_get', ['primary_id' => -1])
);
}
/**
* Récupère tous les utilisateurs mail
*/
public function getAllMailUsers(): array
{
return Cache::remember(
"ispconfig.mail.users.all",
config('services.ispconfig.cache_ttl'),
fn() => $this->call('mail_user_get', ['primary_id' => -1])
);
}
/**
* Récupère les domaines mail d'un client ISPConfig
*/
public function getMailDomainsForClient(int $ispConfigClientId): Collection
{
$allDomains = $this->getAllMailDomains();
return collect($allDomains)->filter(function ($domain) use ($ispConfigClientId) {
return isset($domain['sys_groupid']) && $domain['sys_groupid'] == $ispConfigClientId;
});
}
/**
* Récupère les boîtes mail pour un domaine
*/
public function getMailUsersForDomain(string $domain): Collection
{
$allUsers = $this->getAllMailUsers();
return collect($allUsers)->filter(function ($user) use ($domain) {
return str_ends_with($user['email'], '@' . $domain);
});
}
/**
* Récupère les détails d'une boîte mail
*/
public function getMailUserDetails(string $email): ?array
{
$allUsers = $this->getAllMailUsers();
$user = collect($allUsers)->firstWhere('email', $email);
if (!$user) {
return null;
}
return [
'email' => $user['email'],
'name' => $user['name'] ?? '',
'quota' => (int) ($user['quota'] ?? 0),
'usage' => (int) ($user['maildir_usage'] ?? 0),
'usage_mb' => round(($user['maildir_usage'] ?? 0) / 1024 / 1024, 2),
'active' => $user['postfix'] === 'y',
'imap_enabled' => $user['disableimap'] === 'n',
'pop3_enabled' => $user['disablepop3'] === 'n',
'smtp_enabled' => $user['disablesmtp'] === 'n',
'autoresponder' => $user['autoresponder'] === 'y',
'spam_filter' => $user['move_junk'] === 'y',
];
}
}

View File

@@ -0,0 +1,139 @@
<?php
namespace App\Services\ISPConfig;
use Exception;
use Illuminate\Support\Facades\Cache;
use SoapClient;
use SoapFault;
use Illuminate\Support\Facades\Log;
class ISPConfigService
{
protected ?SoapClient $client = null;
protected ?string $sessionId = null;
protected array $config;
protected string $serverType;
/**
* @throws Exception
*/
public function __construct(string $serverType = 'mail_server')
{
$this->serverType = $serverType;
$this->config = config("services.ispconfig.servers.{$serverType}");
if (!$this->config) {
throw new Exception("ISPConfig server configuration not found for: {$serverType}");
}
}
protected function connect(): void
{
if ($this->client !== null && $this->sessionId !== null) {
return;
}
try {
$this->client = new SoapClient(null, [
'location' => $this->config['soap_location'],
'uri' => $this->config['soap_uri'],
'trace' => 1,
'exceptions' => 1,
'stream_context' => stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
]
])
]);
$this->sessionId = $this->client->login(
$this->config['username'],
$this->config['password']
);
Log::info("ISPConfig connected", [
'server' => $this->serverType,
'session_id' => $this->sessionId
]);
} catch (SoapFault $e) {
Log::error("ISPConfig connection failed", [
'server' => $this->serverType,
'error' => $e->getMessage()
]);
throw new Exception("Failed to connect to ISPConfig: " . $e->getMessage());
}
}
public function disconnect(): void
{
if ($this->client && $this->sessionId) {
try {
$this->client->logout($this->sessionId);
Log::info("ISPConfig disconnected", ['server' => $this->serverType]);
} catch (SoapFault $e) {
Log::warning("ISPConfig logout failed", [
'server' => $this->serverType,
'error' => $e->getMessage()
]);
}
$this->sessionId = null;
$this->client = null;
}
}
/**
* @throws Exception
*/
protected function call(string $method, array $params = []): mixed
{
$this->connect();
try {
array_unshift($params, $this->sessionId);
$result = $this->client->__soapCall($method, $params);
Log::debug("ISPConfig API call", [
'method' => $method,
'server' => $this->serverType,
'success' => true
]);
return $result;
} catch (SoapFault $e) {
Log::error("ISPConfig API call failed", [
'method' => $method,
'server' => $this->serverType,
'error' => $e->getMessage()
]);
throw new Exception("ISPConfig API call failed: " . $e->getMessage());
}
}
public function __destruct()
{
$this->disconnect();
}
/**
* Récupère tous les clients
*/
public function getAllClients(): array
{
return Cache::remember(
"ispconfig.mail.clients.all",
config('services.ispconfig.cache_ttl'),
fn() => $this->call('client_get_all')
);
}
public function getClientData(string $clientId): array
{
return $this->call('client_get', ['client_id' => $clientId]);
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Services\ISPConfig;
class ISPConfigWebService extends ISPConfigService
{
public function __construct()
{
parent::__construct('web_server');
}
/**
* @throws \Exception
*/
public function getAllWebsites(): array
{
return $this->call('sites_web_domain_get', [['primary_id' => -1]]);
}
}

View File

@@ -1,89 +0,0 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Log;
use SoapClient;
use SoapFault;
use Exception;
class ISPConfigService
{
protected ?SoapClient $client = null;
protected ?string $sessionId = null;
public function __construct()
{
}
/**
* ISPConfig Login
*/
public function connect(string $type): void
{
// Type = 'hosting' or 'mailbox'
$username = $username ?? config('services.ispconfig'.$type.'.username');
$password = $password ?? config('services.ispconfig'.$type.'.password');
try {
$this->client = new SoapClient(null, [
'location' => config('services.ispconfig' . $type . '.base_url'),
'trace' => true,
'exceptions' => true,
'stream_context' => stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
],
]),
]);
$this->sessionId = $this->client->login($username, $password);
} catch (SoapFault $e) {
throw new Exception("An error occurred : " . $e->getMessage());
}
}
/**
* Get all clients
*/
public function getAllClients(string $username = null, string $password = null): array
{
if (!$this->sessionId) {
$this->connect($username, $password);
}
try {
$clientIds = $this->client->client_get_all($this->sessionId);
$clients = [];
foreach ($clientIds as $id) {
$details = $this->client->client_get($this->sessionId, (int)$id);
if (!empty($details)) {
$clients[] = (array) $details;
}
}
return $clients;
} catch (SoapFault $e) {
throw new Exception("An error occurred : " . $e->getMessage());
} finally {
$this->disconnect();
}
}
/**
* Logout
*/
public function disconnect(): void
{
if ($this->client && $this->sessionId) {
try {
$this->client->logout($this->sessionId);
} catch (SoapFault $e) {
Log::info('ISP Config logout succeeded');
}
}
}
}

View File

@@ -6,21 +6,15 @@ use App\Events\MemberRegistered;
use App\Models\Member;
use App\Models\MemberGroup;
use App\Models\Package;
use Carbon\Carbon;
class MemberService
{
public function __construct()
{
// No repositories used in this project
}
public function registerNewMember(array $data): Member
{
// Check if the member already exists
$member = Member::where('email', $data['email'])->first();
if (! $member) {
if (!$member) {
// Create a new member
$member = new Member();
$member->status = 'pending';