Initial commit with contrats and domaines modules
This commit is contained in:
148
app/Http/Controllers/DomaineController.php
Normal file
148
app/Http/Controllers/DomaineController.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Domaine;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Iodev\Whois\Factory as WhoisFactory;
|
||||
use Iodev\Whois\Exceptions\ConnectionException;
|
||||
use Iodev\Whois\Exceptions\ServerMismatchException;
|
||||
use Iodev\Whois\Exceptions\WhoisException;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class DomaineController extends Controller
|
||||
{
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$this->authorize('viewAny', Domaine::class);
|
||||
|
||||
$query = Domaine::query();
|
||||
|
||||
$query->when($request->search, function ($q, $search) {
|
||||
$q->where('nom', 'like', "%{$search}%")
|
||||
->orWhere('prestataire', 'like', "%{$search}%")
|
||||
->orWhere('hebergeur', 'like', "%{$search}%");
|
||||
});
|
||||
|
||||
$domaines = $query->orderBy('date_echeance', 'asc')->paginate(20)->withQueryString();
|
||||
|
||||
$domaines->getCollection()->transform(function ($domaine) {
|
||||
$domaine->append(['est_proche_echeance', 'est_en_retard']);
|
||||
return $domaine;
|
||||
});
|
||||
|
||||
return Inertia::render('Domaines/Index', [
|
||||
'domaines' => $domaines,
|
||||
'filters' => $request->only(['search']),
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(): Response
|
||||
{
|
||||
$this->authorize('create', Domaine::class);
|
||||
|
||||
return Inertia::render('Domaines/Create');
|
||||
}
|
||||
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$this->authorize('create', Domaine::class);
|
||||
|
||||
$validated = $request->validate([
|
||||
'nom' => 'required|string|max:255|unique:domaines,nom',
|
||||
'date_echeance' => 'nullable|date',
|
||||
'prestataire' => 'nullable|string',
|
||||
'hebergeur' => 'nullable|string|max:255',
|
||||
]);
|
||||
|
||||
$domaine = Domaine::create($validated);
|
||||
|
||||
// Si la date d'échéance n'est pas fournie, on tente de la récupérer via WHOIS
|
||||
if (empty($validated['date_echeance'])) {
|
||||
$this->syncWhoisDate($domaine);
|
||||
}
|
||||
|
||||
return redirect()->route('domaines.index')
|
||||
->with('success', 'Domaine créé avec succès.');
|
||||
}
|
||||
|
||||
public function edit(Domaine $domaine): Response
|
||||
{
|
||||
$this->authorize('update', $domaine);
|
||||
|
||||
return Inertia::render('Domaines/Edit', [
|
||||
'domaine' => $domaine,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, Domaine $domaine): RedirectResponse
|
||||
{
|
||||
$this->authorize('update', $domaine);
|
||||
|
||||
$validated = $request->validate([
|
||||
'nom' => 'required|string|max:255|unique:domaines,nom,' . $domaine->id,
|
||||
'date_echeance' => 'nullable|date',
|
||||
'prestataire' => 'nullable|string',
|
||||
'hebergeur' => 'nullable|string|max:255',
|
||||
]);
|
||||
|
||||
$domaine->update($validated);
|
||||
|
||||
return redirect()->route('domaines.index')
|
||||
->with('success', 'Domaine mis à jour.');
|
||||
}
|
||||
|
||||
public function destroy(Domaine $domaine): RedirectResponse
|
||||
{
|
||||
$this->authorize('delete', $domaine);
|
||||
|
||||
$domaine->delete();
|
||||
|
||||
return redirect()->route('domaines.index')
|
||||
->with('success', 'Domaine supprimé.');
|
||||
}
|
||||
|
||||
public function syncWhois(Domaine $domaine): RedirectResponse
|
||||
{
|
||||
$this->authorize('update', $domaine);
|
||||
|
||||
if ($this->syncWhoisDate($domaine)) {
|
||||
return back()->with('success', 'Date d\'échéance synchronisée avec le Whois depuis le port 43.');
|
||||
}
|
||||
|
||||
return back()->with('error', 'Impossible de récupérer la date d\'échéance pour ce domaine (Whois indisponible ou format inconnu).');
|
||||
}
|
||||
|
||||
private function syncWhoisDate(Domaine $domaine): bool
|
||||
{
|
||||
try {
|
||||
$whois = WhoisFactory::get()->createWhois();
|
||||
$info = $whois->loadDomainInfo($domaine->nom);
|
||||
|
||||
if ($info && $info->expirationDate) {
|
||||
// expirationDate is a unix timestamp integer in the package
|
||||
$domaine->update([
|
||||
'date_echeance' => Carbon::createFromTimestamp($info->expirationDate)->format('Y-m-d')
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
} catch (ConnectionException $e) {
|
||||
// Port 43 bloqué ou timeout
|
||||
return false;
|
||||
} catch (ServerMismatchException $e) {
|
||||
// Serveur Whois inconnu
|
||||
return false;
|
||||
} catch (WhoisException $e) {
|
||||
// Autre erreur Whois
|
||||
return false;
|
||||
} catch (\Exception $e) {
|
||||
// Erreur inattendue
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user