77 lines
2.3 KiB
PHP
77 lines
2.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Http\Controllers\Api;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Http\Requests\Api\SslHistoryRequest;
|
||
|
|
use App\Models\Site;
|
||
|
|
use Illuminate\Http\JsonResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
|
||
|
|
class SslController extends Controller
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Get SSL certificate data for a site.
|
||
|
|
*/
|
||
|
|
public function show(Request $request, Site $site): JsonResponse
|
||
|
|
{
|
||
|
|
// Ensure the site belongs to the authenticated user
|
||
|
|
if ($site->user_id !== $request->user()->id) {
|
||
|
|
return response()->json([
|
||
|
|
'message' => 'Unauthorized',
|
||
|
|
], 403);
|
||
|
|
}
|
||
|
|
|
||
|
|
$latestCertificate = $site->sslCertificates()
|
||
|
|
->latest('checked_at')
|
||
|
|
->first();
|
||
|
|
|
||
|
|
if (!$latestCertificate) {
|
||
|
|
return response()->json([
|
||
|
|
'message' => 'No SSL certificate data found',
|
||
|
|
], 404);
|
||
|
|
}
|
||
|
|
|
||
|
|
$daysUntilExpiry = null;
|
||
|
|
$isExpiring = false;
|
||
|
|
|
||
|
|
if ($latestCertificate->expires_at) {
|
||
|
|
$daysUntilExpiry = now()->diffInDays($latestCertificate->expires_at, false);
|
||
|
|
$isExpiring = $daysUntilExpiry <= 30 && $daysUntilExpiry > 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'data' => [
|
||
|
|
'issuer' => $latestCertificate->issuer,
|
||
|
|
'expires_at' => $latestCertificate->expires_at,
|
||
|
|
'checked_at' => $latestCertificate->checked_at,
|
||
|
|
'days_until_expiry' => $daysUntilExpiry ? (int) $daysUntilExpiry : null,
|
||
|
|
'is_expiring_soon' => $isExpiring,
|
||
|
|
'is_expired' => $daysUntilExpiry !== null && $daysUntilExpiry < 0,
|
||
|
|
],
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get SSL certificate history for a site.
|
||
|
|
*/
|
||
|
|
public function history(SslHistoryRequest $request, Site $site): JsonResponse
|
||
|
|
{
|
||
|
|
// Ensure the site belongs to the authenticated user
|
||
|
|
if ($site->user_id !== $request->user()->id) {
|
||
|
|
return response()->json([
|
||
|
|
'message' => 'Unauthorized',
|
||
|
|
], 403);
|
||
|
|
}
|
||
|
|
|
||
|
|
$perPage = $request->input('per_page', 20);
|
||
|
|
$perPage = min($perPage, 100); // Max 100 per page
|
||
|
|
|
||
|
|
$certificates = $site->sslCertificates()
|
||
|
|
->orderBy('checked_at', 'desc')
|
||
|
|
->paginate($perPage);
|
||
|
|
|
||
|
|
return response()->json($certificates);
|
||
|
|
}
|
||
|
|
}
|