Exemple #1
0
<?php

use DirectAdmin\LetsEncrypt\Lib\Account;
use DirectAdmin\LetsEncrypt\Lib\Challenges;
use DirectAdmin\LetsEncrypt\Lib\Config;
use DirectAdmin\LetsEncrypt\Lib\Domain;
use DirectAdmin\LetsEncrypt\Lib\Logger;
define('CRON', true);
require_once dirname(__DIR__) . '/includes/bootstrap.php';
$log = new Logger();
$config = new Config();
$usersPath = '/usr/local/directadmin/data/users/';
// Get all users
$users = scandir($usersPath);
// Loop through all users
foreach ($users as $user) {
    // Check if it's not some junk thingy
    if (in_array($user, ['.', '..']) || empty($user)) {
        continue;
    }
    // Create account object
    $account = new Account($user, null, $config->config('server'));
    // Is there a config file present?
    if (!$account->existsInStorage('config.json')) {
        $log->log('Skipped user ' . $account->getUsername());
        continue;
    }
    $log->log('Processing user ' . $account->getUsername());
    if (!$account->loadKeys()) {
        $log->log('No keys present at user ' . $account->getUsername());
        continue;
 /**
  * Apply certificates to DirectAdmin
  *
  * @return bool
  * @throws \Exception
  */
 public function applyCertificates()
 {
     if (defined('CRON')) {
         $domainPath = '/usr/local/directadmin/data/users/' . $this->account->getUsername() . '/domains/' . $this->getDomain();
         file_put_contents($domainPath . '.key', $this->domainKeys->getPrivate());
         chown($domainPath . '.key', 'diradmin');
         chgrp($domainPath . '.key', 'diradmin');
         chmod($domainPath . '.key', 0600);
         file_put_contents($domainPath . '.cert', $this->getCertificate());
         chown($domainPath . '.cert', 'diradmin');
         chgrp($domainPath . '.cert', 'diradmin');
         chmod($domainPath . '.cert', 0600);
         file_put_contents($domainPath . '.cacert', implode("\n", $this->getCertificateAuthorityCertificates()));
         chown($domainPath . '.cacert', 'diradmin');
         chgrp($domainPath . '.cacert', 'diradmin');
         chmod($domainPath . '.cacert', 0600);
         $config = new Config($domainPath . '.conf');
         $config->config('SSLCertificateKeyFile', $domainPath . '.key');
         $config->config('SSLCertificateFile', $domainPath . '.cert');
         $config->config('SSLCACertificateFile', $domainPath . '.cacert');
         $config->config('ssl', 'ON');
     } else {
         $sock = $this->getSocket();
         $sock->set_method('POST');
         $sock->query('/CMD_API_SSL', ['domain' => $this->getDomain(), 'action' => 'save', 'type' => 'paste', 'certificate' => $this->domainKeys->getPrivate() . PHP_EOL . $this->getCertificate(), 'submit' => 'Save']);
         $result = $sock->fetch_parsed_body();
         if ($result['error'] != 0) {
             throw new \Exception('Error while executing first API request: ' . $result['details']);
         }
         $sock->set_method('POST');
         $sock->query('/CMD_API_SSL', ['domain' => $this->getDomain(), 'action' => 'save', 'type' => 'cacert', 'active' => 'yes', 'cacert' => implode("\n", $this->getCertificateAuthorityCertificates()), 'submit' => 'Save']);
         $result = $sock->fetch_parsed_body();
         if ($result['error'] != 0) {
             throw new \Exception('Error while executing second API request: ' . $result['details']);
         }
     }
     return true;
 }