GetSSLDaysController.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace application\controllers;
  3. class GetSSLDaysController {
  4. public static function getSSLDays($data): void {
  5. $domain = $data['domain'] ?? '';
  6. if (empty($domain)) {
  7. http_response_code(400);
  8. echo json_encode(['error' => 'Missing required parameter: domain']);
  9. return;
  10. }
  11. exec("sudo certbot certificates --cert-name $domain | grep 'Expiry' | sed 's/..:..:..+..:..../\n/g
  12. ' | sed 's/days)//g' | sed 's/[[:space:]]//g' | cut -d: -f2", $phpOutput, $phpReturnCode);
  13. /*
  14. $certFile = "/etc/letsencrypt/live/$domain/fullchain.pem";
  15. if (!file_exists($certFile)) {
  16. error_log("certfile: " . $certFile);
  17. http_response_code(404);
  18. echo json_encode(['error' => 'SSL certificate not found']);
  19. return;
  20. }
  21. $certData = openssl_x509_parse(file_get_contents($certFile));
  22. if (!$certData || !isset($certData['validTo_time_t'])) {
  23. http_response_code(500);
  24. echo json_encode(['error' => 'Failed to parse SSL certificate']);
  25. return;
  26. }
  27. $expiryTimestamp = $certData['validTo_time_t'];
  28. $expiryDate = date('Y-m-d', $expiryTimestamp);
  29. $daysRemaining = ceil(($expiryTimestamp - time()) / 86400);
  30. */
  31. error_log("Debug certbot respond =>" . implode("\n", $phpOutput));
  32. echo json_encode([
  33. 'ssl_expiry' => $phpOutput,
  34. 'ssl_remaining' => $phpOutput
  35. ]);
  36. }
  37. }