1<?php
2
3
4
5namespace dokuwiki\plugin\letsencrypt\classes;
6
7
8require_once __DIR__ . '/../Lescript.php';
9
10
11class Lescript extends \Analogic\ACME\Lescript {
12
13    public function __construct($certificatesDir, $webRootDir, $logger) {
14        if(!$certificatesDir) throw new Exception('no cert dir');
15        if(!$webRootDir) throw new Exception('no root dir');
16        $client = new Client($this->ca);
17
18        parent::__construct($certificatesDir, $webRootDir, $logger, $client);
19    }
20
21    /**
22     * All our certificates are for the same setup
23     *
24     * @param string $domain ignored
25     * @return string
26     */
27    protected function getDomainPath($domain) {
28        return parent::getDomainPath('wiki');
29    }
30
31    /**
32     * Returns info about the given's domain certificate
33     *
34     * @param string $domain
35     * @return array|null
36     */
37    public function getCertInfo($domain) {
38        $certfile = $this->certificatesDir.'/'.$domain.'/cert.pem';
39        if (!file_exists($certfile)) {
40            return null;
41        }
42
43        $data = openssl_x509_parse(file_get_contents($certfile));
44
45        // parse some data and add it for convenience
46        $domains = explode(',', $data['extensions']['subjectAltName']);
47        $domains = array_map(function($domain){
48            return preg_replace('/^DNS:/', '', trim($domain));
49        }, $domains);
50
51        $validto = $data['validTo_time_t'];
52        $expiredays = floor(($validto - time()) / (60*60*24));
53        if($expiredays < 0) $expiredays = 0;
54        $renew =  ($expiredays < 31);
55
56        $data['domains'] = $domains;
57        $data['expires_in_days'] = $expiredays;
58        $data['should_renew'] = $renew;
59        return $data;
60    }
61
62}
63