GetSSLDaysController.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace application\controllers;
  3. class GetSSLDaysController {
  4. public static function getSSLDays($data): void {
  5. $domain = $data['domain'] ?? '';
  6. error_log("data: " . $domain);
  7. if (empty($domain)) {
  8. http_response_code(400);
  9. echo json_encode(['error' => 'Missing required parameter: domain']);
  10. return;
  11. }
  12. $certFile = "/etc/letsencrypt/live/$domain/fullchain.pem";
  13. error_log("certfile: " . $certFile);
  14. if (!file_exists($certFile)) {
  15. http_response_code(404);
  16. echo json_encode(['error' => 'SSL certificate not found']);
  17. return;
  18. }
  19. $certData = openssl_x509_parse(file_get_contents($certFile));
  20. if (!$certData || !isset($certData['validTo_time_t'])) {
  21. http_response_code(500);
  22. echo json_encode(['error' => 'Failed to parse SSL certificate']);
  23. return;
  24. }
  25. $expiryTimestamp = $certData['validTo_time_t'];
  26. $expiryDate = date('Y-m-d', $expiryTimestamp);
  27. $daysRemaining = ceil(($expiryTimestamp - time()) / 86400);
  28. echo json_encode([
  29. 'ssl_expiry' => $expiryDate,
  30. 'ssl_remaining' => $daysRemaining
  31. ]);
  32. }
  33. }