#!/usr/bin/perl

# ispCP ω (OMEGA) a Virtual Hosting Control Panel
# Copyright (C) 2001-2006 by moleSoftware GmbH - http://www.molesoftware.com
# Copyright (C) 2006-2010 by isp Control Panel - http://ispcp.net
#
# Version: $Id: ispcp-rqst-mngr 3471 2010-10-16 11:52:34Z scitech $
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is "VHCS - Virtual Hosting Control System".
#
# The Initial Developer of the Original Code is moleSoftware GmbH.
# Portions created by Initial Developer are Copyright (C) 2001-2006
# by moleSoftware GmbH. All Rights Reserved.
# Portions created by the ispCP Team are Copyright (C) 2006-2010 by
# isp Control Panel. All Rights Reserved.
#
# The ispCP ω Home Page is:
#
#    http://isp-control.net
#

use FindBin;
use lib "$FindBin::Bin/";
require 'ispcp_common_code.pl';

use strict;
use warnings;

sub mngr_start_up {

	push_el(\@main::el, 'mngr_start_up()', 'Starting...');

	my $rs;

	$rs = lock_system();
	return $rs if ($rs != 0);

	# Let's clear Execution Logs, if any.
	if (-e $main::ispcp_rqst_mngr_el) {
		$rs = del_file($main::ispcp_rqst_mngr_el);
		return $rs if ($rs != 0);
	}

	# Setting umask
	# Some files contain sensitive data and must not be world readable

	my $umask = umask();
	umask(027);

	push_el(\@main::el, 'mngr_start_up()', 'Ending...');

	 0;
}

sub mngr_shut_down {

	push_el(\@main::el, 'mngr_shut_down()', 'Starting...');
	push_el(\@main::el, 'mngr_shut_down()', 'Ending...');

	0;
}

sub mngr_engine_process{

	push_el(\@main::el, 'mngr_engine_process()', 'Starting...');

	my ($type, $sql, $count) = @_;

	if (defined $main::engine_debug) {
		push_el(
			\@main::el, 'mngr_engine_process()',
			"processing type $type."
		);
	}

	my($rs, $rdata) = doHashSQL($sql, 'id');
	return $rs if ($rs != 0);

	$$count += my @keys = keys(%$rdata);

	foreach (@keys) {
		my (
			$id,
			$name,
			$status
		) = (
			@$rdata{$_}->{'id'},
			@$rdata{$_}->{'name'},
			@$rdata{$_}->{'status'}
		);

		if ($status eq 'restore') {
			$$count--;
		}

		my $mngr	= eval('$main::ispcp_'.$type.'_mngr');
		my $el		= eval('$main::ispcp_'.$type.'_mngr_el');
		my $stdout	= eval('$main::ispcp_'.$type.'_mngr_stdout');
		my $stderr	= eval('$main::ispcp_'.$type.'_mngr_stderr');

		if (defined $main::engine_debug) {
			push_el(
				\@main::el, 'mngr_engine_process()',
				"processing $id, $name, $status."
			);
		}

		my $rs = sys_command("$mngr $id 1>$stdout 2>$stderr");

		if ($rs != 0) {
			if (-e $el) {

				my $rs;

				# removing 'sys_command()' error message;
				pop_el(\@main::el);

				($rs, my $rdata) = get_el_error($el);
				return $rs if ($rs != 0);

				push_el(\@main::el, $mngr, $rdata);

			}

		}

		if (!defined $main::engine_debug) {
			del_file($stdout);
			del_file($stderr);
		}

		return $rs if ($rs != 0);
	}

	push_el(\@main::el, 'mngr_engine_process()', 'Ending...');

	0;
}

sub mngr_engine {

	## First we add / alter main domain but not delete until all related data are processed
	## Then we add / alter subdomain for main domain but not delete until all related data are processed
	## Next we add / alter alias for main domain but not delete until all related data are processed
	## Next we add / alter subdomain for alias but not delete until all related data are processed
	## Next we process mailboxex
	## Next we process .htaccess related data
	## Finally we can delete in order alias subdomains
	## Subdomains
	## Alias
	## Main domain

	push_el(\@main::el, 'mngr_engine()', 'Starting...');

	my ($rs, $rdata, $sql);

	my $dmn_count = 0;

	$sql = "
		SELECT
			`domain_id` AS 'id',
			`domain_name` AS 'name',
			`domain_status` AS 'status'
		FROM
			`domain`
		WHERE
			`domain_status`
		IN (
			'toadd',
			'change',
			'dnschange',
			'restore',
			'toenable',
			'todisable'
		)
		ORDER BY
			`domain_id`
		;
	";

	$rs = mngr_engine_process(
		'dmn', $sql, \$dmn_count
	);
	return $rs if ($rs != 0);

	$sql = "
		SELECT
			`subdomain_id` AS 'id',
			`subdomain_name` AS 'name',
			`subdomain_status` AS 'status'
		FROM
			`subdomain`
		WHERE
			`subdomain_status`
		IN (
			'toadd',
			'change'
		)
		ORDER BY
			`subdomain_id`
		;
	";

	my $sub_count = 0;

	$rs = mngr_engine_process(
		'sub', $sql, \$sub_count
	);
	return $rs if ($rs != 0);

	$sql = "
		SELECT
			`alias_id` AS 'id',
			`alias_name` AS 'name',
			`alias_status` AS 'status'
		FROM
			`domain_aliasses`
		WHERE
			`alias_status`
		IN (
			'toadd',
			'change',
			'dnschange'
		)
		ORDER BY
			`alias_id`
		;
	";

	my $als_count = 0;

	$rs = mngr_engine_process(
		'als', $sql, \$als_count
	);
	return $rs if ($rs != 0);

	$sql = "
		SELECT
			`subdomain_alias_id` AS 'id',
			`subdomain_alias_name` AS 'name',
			`subdomain_alias_status` AS 'status'
		FROM
			`subdomain_alias`
		WHERE
			`subdomain_alias_status`
		IN (
			'toadd',
			'change'
		)
		ORDER BY
			`subdomain_alias_id`
		;
	";

	my $alssub_count = 0;

	$rs = mngr_engine_process(
		'alssub', $sql, \$alssub_count
	);
	return $rs if ($rs != 0);

	$sql = "
		SELECT
			`mail_id` AS 'id',
			`mail_acc` AS 'name',
			`status` as 'status'
		FROM
			`mail_users`
		WHERE
			`status`
		IN (
			'toadd',
			'change',
			'toenable',
			'delete',
			'todisable'
		)
		ORDER BY
			`mail_id`
		;
	";

	my $mail_count = 0;

	$rs = mngr_engine_process(
		'mbox', $sql, \$mail_count
	);
	return $rs if ($rs != 0);

	$sql = "
		SELECT
			`id`,
			`uname` as 'name',
			`status`
		FROM
			`htaccess_users`
		WHERE
			`status`
		IN (
			'toadd',
			'change',
			'delete'
		)
		ORDER BY
			`id`
		;
	";
	my $htcount = 0;

	$rs = mngr_engine_process(
		'htusers', $sql, \$htcount
	);
	return $rs if ($rs != 0);

	$sql = "
		SELECT
			`id` AS 'name',
			`dmn_id` AS 'id',
			`status`
		FROM
			`htaccess_groups`
		WHERE
			`status`
		IN (
			'toadd',
			'change',
			'delete'
		)
		GROUP BY
			`dmn_id`
		ORDER BY
			`dmn_id`
		;
	";

	$rs = mngr_engine_process(
		'htgroups', $sql, \$htcount
	);
	return $rs if ($rs != 0);


	$sql = "
		SELECT
			`id`,
			`dmn_id` AS 'name',
			`status`
		FROM
			`htaccess`
		WHERE
			`status`
		IN (
			'toadd',
			'change',
			'delete'
		)
		ORDER BY
			`id`
		;
	";

	$rs = mngr_engine_process(
		'htaccess', $sql, \$htcount
	);
	return $rs if ($rs != 0);

	$sql = "
		SELECT
			`subdomain_alias_id` AS 'id',
			`subdomain_alias_name` AS 'name',
			`subdomain_alias_status` AS 'status'
		FROM
			`subdomain_alias`
		WHERE
			`subdomain_alias_status` = 'delete'
		ORDER BY
			`subdomain_alias_id`
		;
	";

	$rs = mngr_engine_process(
		'alssub', $sql, \$alssub_count
	);
	return $rs if ($rs != 0);

	$sql = "
		SELECT
			`subdomain_id` AS 'id',
			`subdomain_name` AS 'name',
			`subdomain_status` AS 'status'
		FROM
			`subdomain`
		WHERE
			`subdomain_status` = 'delete'
		ORDER BY
			`subdomain_id`
		;
	";

	$rs = mngr_engine_process(
		'sub', $sql, \$sub_count
	);
	return $rs if ($rs != 0);

	$sql = "
		SELECT
			`alias_id` AS 'id',
			`alias_name` AS 'name',
			`alias_status` AS 'status'
		FROM
			`domain_aliasses`
		WHERE
			`alias_status` = 'delete'
		ORDER BY
			`alias_id`
		;
	";

	$rs = mngr_engine_process(
		'als', $sql, \$als_count
	);
	return $rs if ($rs != 0);

	$sql = "
		SELECT
			`domain_id` AS 'id',
			`domain_name` AS 'name',
			`domain_status` AS 'status'
		FROM
			`domain`
		WHERE
			`domain_status` = 'delete'
		ORDER BY
			`domain_id`
		;
	";

	$rs = mngr_engine_process(
		'dmn', $sql, \$dmn_count
	);
	return $rs if ($rs != 0);

	#
	# Network card management BEGIN.
	#
	$sql = "
		SELECT
			`value`, 'name'
		FROM
			`config`
		WHERE
			`name` = 'DATABASE_REVISION'
		;
	";
	($rs, $rdata) = doHashSQL($sql, 'name');
	return $rs if ($rs != 0);

	if (keys(%$rdata) > 0 && @$rdata{'name'}->{'value'} >= 33) {
		$sql = "
			SELECT
				`ip_id`
			FROM
				`server_ips`
			WHERE
				`ip_status` != 'ok'
			;
		";
		($rs, $rdata) = doHashSQL($sql,'ip_id');
		return $rs if ($rs != 0);

		if (keys(%$rdata) > 0) {
			$rs = sys_command(
				"$main::ispcp_net_interfaces_mngr stop " .
				"1>$main::ispcp_net_interfaces_mngr_stdout 2>&1"
			);
			if (!defined $main::engine_debug) {
				del_file($main::ispcp_net_interfaces_mngr_stdout);
			}
			return $rs if($rs != 0);
			$rs = sys_command(
				"$main::ispcp_net_interfaces_mngr start " .
				"1>>$main::ispcp_net_interfaces_mngr_stdout 2>&1"
			);
			if (!defined $main::engine_debug) {
				del_file($main::ispcp_net_interfaces_mngr_stdout);
			}
			return $rs if($rs != 0);
		}
	}
	#
	# Network card management END.
	#
	push_el(
		\@main::el,
		'mngr_engine()',
		"domain count: $dmn_count || ".
		"subdomain count: $sub_count || ".
		"aliasses count: $als_count || ".
		"email count: $mail_count || ".
		"subdomain aliasses count: $alssub_count"
	);
	if (($dmn_count > 0 || $sub_count > 0 || $als_count > 0 || $mail_count > 0 ||
		$alssub_count > 0) && (!defined($ARGV[0]) || $ARGV[0] eq '')) {
		$rs = sys_command(
			"$main::ispcp_serv_mngr $dmn_count $sub_count $als_count " .
			"$mail_count $alssub_count 1>$main::ispcp_serv_mngr_stdout " .
			"2>$main::ispcp_serv_mngr_stderr"
		);
		if ($rs != 0) {
			if (-e $main::ispcp_serv_mngr_el) {
				my $rs1;
				# removing 'sys_command()' error message;
				pop_el(\@main::el);
				($rs1, $rdata) = get_el_error($main::ispcp_serv_mngr_el);
				return $rs1 if ($rs1 != 0);
				push_el(\@main::el, $main::ispcp_serv_mngr, $rdata);
			}
			if (!defined($main::engine_debug)) {
				del_file($main::ispcp_serv_mngr_stdout);
				del_file($main::ispcp_serv_mngr_stderr);
			}
			return $rs;
		}
		if (!defined($main::engine_debug)) {
			del_file($main::ispcp_serv_mngr_stdout);
			del_file($main::ispcp_serv_mngr_stderr);
		}
	} else {
		push_el(\@main::el, 'mngr_engine()', 'Not restarting servers...');
	}

	push_el(\@main::el, 'mngr_engine()', 'Ending...');

	0;
}

my $rs;

$rs = mngr_start_up();

if ($rs != 0) {

	dump_el(\@main::el, $main::ispcp_rqst_mngr_el);

	mngr_shut_down();

	exit 1;
}

$rs = mngr_engine();

if ($rs != 0) {

	dump_el(\@main::el, $main::ispcp_rqst_mngr_el);

	mngr_shut_down();

	exit 1;
}

$rs = mngr_shut_down();

if ($rs != 0) {

	dump_el(\@main::el, $main::ispcp_rqst_mngr_el);

	exit 1;
}

exit 0;
