report-expiry.php 1001 Bytes
Newer Older
Erick Hitter's avatar
Erick Hitter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
<?php

require __DIR__ . '/vendor/autoload.php';

$data = [
	[
		'Filename',
		'CN',
		'Expires',
		'Days Left',
		'SAN',
	],
];

foreach ( glob( '*.crt', GLOB_NOSORT ) as $cert ) {
	$path = getcwd() . '/' . $cert;
	$x509 = openssl_x509_parse( file_get_contents( $path ) );

	if ( ! is_array( $x509 ) ) {
		printf( 'Failed to parse certificate from `%1$s`%2$s', $path, "\n\n" );
		continue;
	}

	$cert_data = [
		0 => $cert,
		1 => $x509['subject']['CN'],
		2 => date( 'Y-m-d H:i:s T', $x509['validTo_time_t'] ),
		3 => (int) round( ( $x509['validTo_time_t'] - time() ) / 86400 ),
		4 => '',
	];

	if ( isset( $x509['extensions']['subjectAltName'] ) ) {
		$sans = explode( ',', $x509['extensions']['subjectAltName'] );

		foreach ( $sans as $i => $san ) {
			$san        = explode( ':', $san );
			$sans[ $i ] = array_pop( $san );
		}

		$cert_data[4] = implode( ' ', $sans );
	}

	$data[] = $cert_data;
}

$table = new \cli\Table( array_shift( $data ), $data );
$table->sort( 2 );
$table->display();