<?php
	if(count($argv) < 2)
	{
    	print "Usage: configure (install | upgrade <version> | configure | remove | enable | disable)\n";
    	exit(1);
	}

	$command = $argv[1];
	switch ($command) {
		case "install":
			install();
			break;
		case "enable":
			enable();
			break;
		case "disable":
			disable();
			break;
		case "remove":
			remove();
			break;
		case "configure":
			configure();
			break;
	}
	exit(0);

function configure()
	{
	echo "0";
	}

function install()
	{
		$username = getenv('SETTINGS_AccountName');
		$userpasswd = getenv('SETTINGS_AccountPwd');
		$server_ip = getenv('SETTINGS_server_ip');
		$adminuser = getenv('SETTINGS_GroupAccountName2');
		$product = getenv('SETTINGS_ProductName');
		$adminpwd = getenv('SETTINGS_GroupAccountPwd2');
		$server_wsdl = getenv('SETTINGS_server_wsdl');

		    $client = new SoapClient("$server_wsdl", array('cache_wsdl' => 0, 'connection_timeout' => 500));
		    $result = $client->CreateUser(array(
			"request" => array(
			    "username" => "$username",
			    "password" => "$userpasswd",									
			    "product" => "$product",											
			    "credentials" => array(
				"server" => "$server_ip",
				"user" => "$adminuser",
				"password" => "$adminpwd"
			    )
			)
		    ));
		    if ($result->result->errorCode == 0)
		    {
			print "<h1>Succeeded.</h1>";
			print "Username: ".$result->username."</br>";
			print "Password: ".$result->password."</br>";

//SEND MAIL
		$username = getenv('SETTINGS_AccountName');
		$adminemail = getenv('SETTINGS_zfromemail');
		$usermail = getenv('SETTINGS_AccountEmail');
		$adminemail2 = getenv('SETTINGS_zfromemail2');
		$mailbody = getenv('SETTINGS_zzmailbody2');
		$product = getenv('SETTINGS_ProductName');
		
		$USER = $result->username;
		$PWD = $result->password;

		$string = "$mailbody";
                $patterns[0] = "/USER/";
                $patterns[1] = "/PWD/";
                $patterns[3] = "/PRODUCTNAME/";
                $replacements[0] = $USER;
                $replacements[1] = $PWD;
                $replacements[3] = $product;
		$replace = preg_replace($patterns, $replacements, $string);

		$to = $usermail;
		$Cc = $adminemail2;
		$subject = getenv('SETTINGS_zzmailasubject');
		$message = $replace;

		$headers  = "Content-type: text/html; \r\n";
		$headers .= "From: <$adminemail>\r\n";
		$headers .= "Cc: <$Cc>\r\n";

		mail("$to", $subject, $message, $headers);

		    }
		    else
		    {
			print "Unable to create a account.";
			print $result->result->errorText."\n";
			exit(1);
		    }
	}

function remove()
	{
		$username = getenv('SETTINGS_AccountName');
		$server_ip = getenv('SETTINGS_server_ip');
		$adminuser = getenv('SETTINGS_GroupAccountName2');
		$product = getenv('SETTINGS_ProductName');
		$adminpwd = getenv('SETTINGS_GroupAccountPwd2');
		$server_wsdl = getenv('SETTINGS_server_wsdl');

		    $client = new SoapClient("$server_wsdl", array('cache_wsdl' => 0, 'connection_timeout' => 500));
		    $result = $client->DeleteUser(array(
			"request" => array(
			    "userName" => "$username",									
			    "credentials" => array(
				"server" => "$server_ip",
				"user" => "$adminuser",
				"password" => "$adminpwd"
			    )
			)
		    ));
		    if ($result->DeleteUserResult->result->errorCode == 0)
		    {
			print "<h1>Succeeded.</h1>";


//SEND MAIL
		$username = getenv('SETTINGS_AccountName');
		$adminemail = getenv('SETTINGS_zfromemail');
		$usermail = getenv('SETTINGS_AccountEmail');
		$adminemail2 = getenv('SETTINGS_zfromemail2');

		$to = $usermail;
		$Cc = $adminemail2;
		$subject = getenv('SETTINGS_zzmailasubject');
		$message = " 
		<html>
		    <head> 
			<title></title> 
		    </head>
		    <body> 
			<p>Account info</p>
			<p>Account ".$username." has been removed.</p>
			<p></p>
		    </body>
		</html>"; 
		$headers  = "Content-type: text/html; \r\n"; 
		$headers .= "From: <$adminemail>\r\n"; 
		$headers .= "Cc: <$Cc>\r\n";

		mail("$to", $subject, $message, $headers);

		    }
		    else
		    {
			print "Unable to remove a account.";
			print $result->result->errorText."\n";
			exit(1);
		    }
	}

// UNSUSPEND

function enable()
	{
		$nextWeek = time() + (14 * 24 * 60 * 60);
		$username = getenv('SETTINGS_AccountName');
		$server_ip = getenv('SETTINGS_server_ip');
		$adminuser = getenv('SETTINGS_GroupAccountName2');
		$product = getenv('SETTINGS_ProductName');
		$adminpwd = getenv('SETTINGS_GroupAccountPwd2');
		$server_wsdl = getenv('SETTINGS_server_wsdl');

    $client = new SoapClient("$server_wsdl", array('cache_wsdl' => 0, 'connection_timeout' => 500));
    $result = $client->ModifyUser(array(
        "request" => array(
            "username" => "$username",
            "field" => array(
                "Field" => array (
                    "shortName" => "ED",
                    "content" => "$nextWeek"
                )
            ),					
            "credentials" => array(
                "server" => "$server_ip",
                "user" => "$adminuser",
                "password" => "$adminpwd"
            )
        )
    ));
    if ($result->result->errorCode == 0)
    {
        print "<h1>Succeeded.</h1>";
		$username = getenv('SETTINGS_AccountName');
		$adminemail = getenv('SETTINGS_zfromemail');
		$usermail = getenv('SETTINGS_AccountEmail');
		$adminemail2 = getenv('SETTINGS_zfromemail2');

		$to = $usermail;
		$Cc = $adminemail2;
		$subject = getenv('SETTINGS_zzmailasubject');
		$message = " 
		<html> 
		    <body> 
			<p>Account info</p>
			<p>Account ".$username." has been unsuspened.</p>
		    </body>
		</html>"; 
		$headers  = "Content-type: text/html; \r\n"; 
		$headers .= "From: <$adminemail>\r\n"; 
		$headers .= "Cc: <$Cc>\r\n"; 

		mail("$to", $subject, $message, $headers);
    }
    else
    {
        print "Unable to enable a account.";
	print $result->result->errorText."\n";
	exit(1);
    }
}


// SUSPEND

function disable()
	{
		$oldWeek = time() - (1 * 24 * 60 * 60);
		$username = getenv('SETTINGS_AccountName');
		$server_ip = getenv('SETTINGS_server_ip');
		$adminuser = getenv('SETTINGS_GroupAccountName2');
		$product = getenv('SETTINGS_ProductName');
		$adminpwd = getenv('SETTINGS_GroupAccountPwd2');
		$server_wsdl = getenv('SETTINGS_server_wsdl');

    $client = new SoapClient("$server_wsdl", array('cache_wsdl' => 0, 'connection_timeout' => 500));
    $result = $client->ModifyUser(array(
        "request" => array(
            "username" => "$username",
            "field" => array(
                "Field" => array (
                    "shortName" => "ED", // Expiration date
                    "content" => "$oldWeek" // seconds from 1970 year (time_t)
                )
            ),
            "credentials" => array(
                "server" => "$server_ip",
                "user" => "$adminuser",
                "password" => "$adminpwd"
            )
        )
    ));
    if ($result->result->errorCode == 0)
    {
        print "<h1>Succeeded.</h1>";

		$username = getenv('SETTINGS_AccountName');
		$adminemail = getenv('SETTINGS_zfromemail');
		$usermail = getenv('SETTINGS_AccountEmail');
		$adminemail2 = getenv('SETTINGS_zfromemail2');

		$to = $usermail;
		$Cc = $adminemail2;
		$subject = getenv('SETTINGS_zzmailasubject');
		$message = " 
		<html> 
		    <body> 
			<p>Account info</p>
			<p>Account ".$username." has been suspened.</p>
		    </body>
		</html>"; 
		$headers  = "Content-type: text/html; \r\n"; 
		$headers .= "From: <$adminemail>\r\n"; 
		$headers .= "Cc: <$Cc>\r\n"; 

		mail("$to", $subject, $message, $headers);
    }
    else
    {
        print "Unable to disable a account.";
	print $result->result->errorText."\n";
	exit(1);
    }
}
		
?>
