2020-04-17 08:50:16 +00:00
|
|
|
<?php
|
|
|
|
// https://stackoverflow.com/questions/3464113/is-it-possible-to-read-ssl-information-in-php-from-any-website
|
|
|
|
|
2020-04-17 08:55:46 +00:00
|
|
|
if(!class_exists("SSL")) {
|
|
|
|
class SSL {
|
|
|
|
public $domain, $validFrom, $validTo, $issuer, $validity, $validitytot, $crtValRemaining;
|
|
|
|
|
|
|
|
private static function instantiate($url, $info) {
|
|
|
|
$obj = new static;
|
|
|
|
$obj->domain = $url;
|
|
|
|
$obj->validFrom = $info['validFrom'];
|
|
|
|
$obj->validTo = $info['validTo'];
|
|
|
|
$obj->issuer = $info['issuer'];
|
|
|
|
$obj->validity = $info['validity'];
|
|
|
|
$obj->validitytot = $info['validitytot'];
|
|
|
|
$obj->crtValRemaining = $info['crtValRemaining'];
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
2020-04-17 08:50:16 +00:00
|
|
|
|
2020-04-17 09:21:43 +00:00
|
|
|
public static function getSSLinfo($url, $port=443) {
|
2020-04-17 08:55:46 +00:00
|
|
|
$ssl_info = [];
|
2020-04-17 09:21:43 +00:00
|
|
|
$certinfo = static::getCertificateDetails($url, $port);
|
2020-04-17 08:55:46 +00:00
|
|
|
$validFrom_time_t_m = static::dateFormatMonth($certinfo['validFrom_time_t']);
|
|
|
|
$validTo_time_t_m = static::dateFormatMonth($certinfo['validTo_time_t']);
|
2020-04-17 08:50:16 +00:00
|
|
|
|
2020-04-17 08:55:46 +00:00
|
|
|
$validFrom_time_t = static::dateFormat($certinfo['validFrom_time_t']);
|
|
|
|
$validTo_time_t = static::dateFormat($certinfo['validTo_time_t']);
|
|
|
|
$current_t = static::dateFormat(time());
|
2020-04-17 08:50:16 +00:00
|
|
|
|
2020-04-17 08:55:46 +00:00
|
|
|
$ssl_info['validFrom'] = $validFrom_time_t_m;
|
|
|
|
$ssl_info['validTo'] = $validTo_time_t_m;
|
|
|
|
$ssl_info['issuer'] = $certinfo['issuer']['O'];
|
2020-04-17 08:50:16 +00:00
|
|
|
|
2020-04-17 08:55:46 +00:00
|
|
|
$ssl_info['validity'] = static::diffDate($current_t, $validTo_time_t)." days";
|
|
|
|
$ssl_info['validitytot'] = (static::diffDate($validFrom_time_t, $validTo_time_t)-1).' days';
|
2020-04-17 08:50:16 +00:00
|
|
|
|
2020-04-17 08:55:46 +00:00
|
|
|
$ssl_info['crtValRemaining'] = $certinfo['validTo_time_t'];
|
2020-04-17 08:50:16 +00:00
|
|
|
|
2020-04-17 08:55:46 +00:00
|
|
|
return static::instantiate($url, $ssl_info); // return an object
|
|
|
|
}
|
2020-04-17 08:50:16 +00:00
|
|
|
|
2020-04-17 09:21:43 +00:00
|
|
|
private static function getCertificateDetails($url, $port=443) {
|
2020-04-17 08:55:46 +00:00
|
|
|
$urlStr = strtolower(trim($url));
|
|
|
|
|
|
|
|
$parsed = parse_url($urlStr);// add http://
|
|
|
|
if (empty($parsed['scheme'])) {
|
|
|
|
$urlStr = 'http://' . ltrim($urlStr, '/');
|
|
|
|
}
|
|
|
|
$orignal_parse = parse_url($urlStr, PHP_URL_HOST);
|
|
|
|
$get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE)));
|
2020-04-17 09:21:43 +00:00
|
|
|
$read = stream_socket_client("ssl://".$orignal_parse.":".$port, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get);
|
2020-04-17 08:55:46 +00:00
|
|
|
$cert = stream_context_get_params($read);
|
|
|
|
$certinfo = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
|
|
|
|
return $certinfo;
|
2020-04-17 08:50:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 08:55:46 +00:00
|
|
|
private static function dateFormat($stamp) {
|
|
|
|
return strftime("%Y-%m-%d", $stamp);
|
|
|
|
}
|
2020-04-17 08:50:16 +00:00
|
|
|
|
2020-04-17 08:55:46 +00:00
|
|
|
private static function dateFormatMonth($stamp) {
|
|
|
|
return strftime("%Y-%b-%d", $stamp);
|
|
|
|
}
|
2020-04-17 08:50:16 +00:00
|
|
|
|
2020-04-17 08:55:46 +00:00
|
|
|
private static function diffDate($from, $to) {
|
|
|
|
$date1=date_create($from);
|
|
|
|
$date2=date_create($to);
|
|
|
|
$diff=date_diff($date1,$date2);
|
|
|
|
return ltrim($diff->format("%R%a"), "+");
|
|
|
|
}
|
2020-04-17 08:50:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-17 09:08:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
## Example
|
|
|
|
```
|
|
|
|
$certInfo = SSL::getSSLinfo('stackoverflow.com');
|
|
|
|
echo $certInfo->validFrom .'<br>';
|
|
|
|
echo $certInfo->validTo .'<br>';
|
|
|
|
echo $certInfo->issuer .'<br>';
|
|
|
|
echo $certInfo->validity .'<br>';
|
|
|
|
echo $certInfo->validitytot .'<br>';
|
|
|
|
echo $certInfo->crtValRemaining .'<br>';
|
|
|
|
```
|
|
|
|
*/
|