Current File : /home/inlingua/public_html/decay_sym/root/var/softaculous/apps/dovecot/3/install.php
<?php

/*
function __dovecot_requirements(){
	
}

function __dovecot_preinstall(){

}
*/

function __dovecot_postinstall(){
	vexec('rm -rf /usr/libexec/dovecot');
	symlink('/usr/local/apps/dovecot/libexec/dovecot', '/usr/libexec/dovecot');
	vexec('systemctl restart dovecot');
}

function __dovecot_install(){
	
	global $globals, $error, $infos;
	
	// $ref pass by reference
	yum('dovecot*', 'remove', 0, 0, $ref, 1);
	
	yum_exclude('dovecot*');

	vexec('useradd -d /usr/libexec/dovecot -s /sbin/nologin -c "Dovecot IMAP server" dovecot');	
	vexec('useradd -d /usr/libexec/dovecot -s /sbin/nologin -c "Dovecot\'s unauthorized user" dovenull');	
	
	vexec('mkdir -p /etc/dovecot/conf.d');
	
	vexec('mkdir -p /var/run/dovecot');
	vexec('chown dovecot:dovecot /var/run/dovecot');
	
	
	//Copy service file
	scopy(DOVECOT_WAPP.'/3/dovecot.conf', '/etc/dovecot/dovecot.conf');
	if(file_exists('/usr/lib/systemd/system')){
		scopy(DOVECOT_WAPP.'/3/dovecot.service', '/usr/lib/systemd/system/dovecot.service');
	}else{
		scopy(DOVECOT_WAPP.'/3/dovecot.service', '/etc/systemd/system/dovecot.service');
	}
	
	scopy(DOVECOT_WAPP.'/3/dovecot-sql.conf.ext', '/etc/dovecot/dovecot-sql.conf.ext');
	scopy(DOVECOT_WAPP.'/3/dovecot-tmp-sql.conf.ext', '/etc/dovecot/dovecot-tmp-sql.conf.ext');
	
	dovecot_cert_changed($globals['certs_path'], 0);
	
	// Because on Ubuntu restarting service fails sometimes
	vexec('systemctl stop dovecot');
	kill_by_port(143);
	
	// It requires to fetch version in dovecot_rebuild_conf()
	vexec('ln -s '.DOVECOT_APP.'sbin/dovecot /usr/sbin/dovecot');
	
	// Dovecot conf rebuild
	dovecot_rebuild_conf();
	
	// Rebuild certs
	dovecot_rebuild_sni_file();
	
	// Reload daemon for dovecot service file
	vexec('systemctl daemon-reload');
	
	vexec('systemctl restart dovecot > '.$globals['var_tmp'].'/service_action 2>&1 &');
	
}

function __dovecot_conf($pemfile, $domain){
	global $infos, $webuzo;
	
	$dovecotconf = file_get_contents('/etc/dovecot/dovecot.conf');
		
	$dpattern[] = '/^.*\bssl_cert\b.*$/m';
	$dpattern[] = '/^.*\bssl_key\b.*$/m';
		
	foreach($dpattern as $k => $v){
		preg_match_all($v, $dovecotconf, $matches);
		$d[] = $matches[0];
	}

	foreach($d as $k => $v){
		foreach($v as $key => $value){
			$val = explode('=', $value);
			$dovecotconf = str_replace($val[1], " <".$pemfile, $dovecotconf);
		}
	}
	
	writefile('/etc/dovecot/dovecot.conf', $dovecotconf, 1);
	
	// Update global variable
	$addemailssl['LE_EMAIL_SSL'] = $domain;
	saveglobals($addemailssl);
	
	//Copying domain-ca-bundle.crt contents to domain.pem file so that PHPMailer never fails!!!
	if(file_exists('/etc/ssl/cert/'.$domain.'-cabundle.crt')){
		unlink($pemfile);
		
		vexec('(cat '.xss('/etc/ssl/private/'.$domain.'.key').';printf "\n";cat '.xss('/etc/ssl/cert/'.$domain.'.crt').') > '.xss('/etc/ssl/cert/'.$domain.'.pem'));
		
		$file = file_get_contents($pemfile);
	
		$file1 = file_get_contents('/etc/ssl/cert/'.$domain.'-cabundle.crt');
	
		$file .= "\n".$file1."\n";
	
		writefile($pemfile, $file, 1);
	}
}