feat-wip(ISPConfig & Dolibarr connections)

This commit is contained in:
2025-11-14 17:27:39 +01:00
parent 94a5794bab
commit de5e81fe22
10 changed files with 370 additions and 6 deletions

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Services;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
class DolibarrService
{
protected string $baseUrl;
protected string $username;
protected string $password;
protected string $apiKey;
public function __construct()
{
$this->baseUrl = config('services.dolibarr.base_url');
$this->username = config('services.dolibarr.username');
$this->password = config('services.dolibarr.password');
$this->apiKey = config('services.dolibarr.api_key');
}
/**
* Create an authenticated HTTP client for Dolibarr
*/
protected function client(): PendingRequest
{
return Http::withBasicAuth($this->username, $this->password)
->withHeaders([
'Accept' => 'application/json',
'DOLAPIKEY' => $this->apiKey,
]);
}
/**
* Get all members
* @throws ConnectionException
*/
public function getAllMembers(int $limit = 400, string $sortField = 't.rowid', string $sortOrder = 'ASC'): array
{
$response = $this->client()->get($this->baseUrl . '/members', [
'sortfield' => $sortField,
'sortorder' => $sortOrder,
'limit' => $limit,
]);
return $response->json();
}
/**
* Get member subscriptions
* @throws ConnectionException
*/
public function getMemberSubscriptions(int|string $id): array
{
$response = $this->client()->get($this->baseUrl . '/members/'. $id . '/subscriptions');
return $response->json();
}
}

View File

@@ -0,0 +1,89 @@
<?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');
}
}
}
}