<?php

# Domain valiadated wildcard cert

$plugin_name='dv_wc';

class dv_wc
{
	public static $order=4;

	public static $filename_key="{domain}-key.pem";
	public static $filename_cert="{domain}-cert.pem";

	public static function generate($ssldir, $ssl_all, $ini)
	{
		//sanity check
		if (!($openssl_bin=find_shell_command('openssl')))
		{
			logme('SSL error: openssl binary not found');
			return false;
		}

		foreach($ssl_all as $ssl)
		{
			$dn=$ssl['domain'];

			$key_path=$ssldir."/$dn-key.pem";
			$csr_path=$ssldir."/$dn.csr";
			$cert_path=$ssldir."/$dn-cert.pem";

			if (!(file_exists($key_path)))
			{
				if (file_exists($cert_path.'.bck'))
				{
					unlink($cert_path.'.bck');
				}
				if (file_exists($cert_path))
				{
					rename($cert_path, $cert_path.'.bck');
				}

				exec_as_err($openssl_bin." req -nodes -newkey rsa:2048 -nodes -keyout ".$key_path." -out ".$csr_path." -subj '/C=HU/ST=Budapest/L=Budapest/O=".$ini['global']['name']."/OU=internet/CN=*.".$dn."'", $ini['ssl']['user'], false);

				if (file_exists($csr_path))
				{
					//send CSR to admin in e-mail
					mailsend($ini['global']['name'], $ini['global']['email'], $ini['global']['email'], "Wildcard CSR: *.$dn", "*.$dn\n\n".file_get_contents($csr_path));
					sleep(1);
				}
			}
		}
	}
}
