Ejemplo n.º 1
0
/**
 * Generate temporary openssl coonfiguration file
 *
 * @throws iMSCP_Exception_Database
 * @param array $data User data
 * @return bool|string Path to generate openssl temporary file, FALSE on failure
 */
function _client_generateOpenSSLConfFile($data)
{
    global $domainType, $domainId;
    $config = iMSCP_Registry::get('config');
    $altNames = <<<'EOF'
DNS.1 = {DOMAIN_NAME}
DNS.2 = www.{DOMAIN_NAME}
EOF;
    if ($domainType == 'dmn') {
        $altNames .= "\nDNS.3 = {ADMIN_SYS_NAME}.{BASE_SERVER_VHOST}\n";
    } elseif ($domainType == 'als') {
        $altNames .= "\nDNS.3 = {ADMIN_SYS_NAME}als{$domainId}.{BASE_SERVER_VHOST}\n";
    } elseif ($domainType == 'sub') {
        $altNames .= "\nDNS.3 = {ADMIN_SYS_NAME}sub{$domainId}.{BASE_SERVER_VHOST}\n";
    } else {
        $altNames .= "\nDNS.3 = {ADMIN_SYS_NAME}alssub{$domainId}.{BASE_SERVER_VHOST}\n";
    }
    $sslTpl = new iMSCP_pTemplate();
    $sslTpl->setRootDir(LIBRARY_PATH . '/Resources/ssl');
    $sslTpl->define('tpl', 'openssl.cnf.tpl');
    $sslTpl->assign(array('DOMAIN_NAME' => $data['domain_name'], 'ALT_NAMES' => $altNames, 'ADMIN_SYS_NAME' => $data['admin_sys_name'], 'BASE_SERVER_VHOST' => $config['BASE_SERVER_VHOST']));
    $sslTpl->parse('TPL', 'tpl');
    if (!($opensslConfFile = @tempnam(sys_get_temp_dir(), $_SESSION['user_id'] . '-openssl.cnf'))) {
        write_log('Could not create temporary openssl configuration file.', E_USER_ERROR);
        return false;
    }
    register_shutdown_function(function ($file) {
        @unlink($file);
    }, $opensslConfFile);
    if (!@file_put_contents($opensslConfFile, $sslTpl->getLastParseResult())) {
        write_log(sprintf('Could not write in %s openssl temporary configuration file.', $opensslConfFile), E_USER_ERROR);
        return false;
    }
    return $opensslConfFile;
}