GetSSLDaysController.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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/days)//g' | sed 's/[[:space:]]//g' | cut -d: -f2,f3", $phpOutput, $phpReturnCode);
  12. $sslData = implode("\n",$phpOutput);
  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. [ssl_expiry] => Expiry Date: 2025-08-05 08:21:58+00:00 (VALID: 86 days)
  22. $certData = openssl_x509_parse(file_get_contents($certFile));
  23. if (!$certData || !isset($certData['validTo_time_t'])) {
  24. http_response_code(500);
  25. echo json_encode(['error' => 'Failed to parse SSL certificate']);
  26. return;
  27. }
  28. $expiryTimestamp = $certData['validTo_time_t'];
  29. $expiryDate = date('Y-m-d', $expiryTimestamp);
  30. $daysRemaining = ceil(($expiryTimestamp - time()) / 86400);
  31. */
  32. echo json_encode([
  33. 'ssl_expiry' => $sslData,
  34. 'ssl_remaining' => $phpOutput
  35. ]);
  36. }
  37. }