<?php

define('SM_PATH', '../../');
include_once(SM_PATH . 'include/validate.php');
include_once('functions.php');

// Make sure plugin is activated!
//
global $plugins;
if (!in_array('directadmin', $plugins))
   exit;

function directadmin_go_header()
{
	global $color;
	displayPageHeader($color, 'None');

	echo html_tag( 'table',
	        html_tag( 'tr',
	            html_tag( 'td','<div style="text-align: center;"><b>' . _("Change Password") .'</b></div>', 'center', $color[0] )
	        ) ,
	    'center', '', 'width="95%" cellpadding="1" cellspacing="2" border="0"' );

	echo "<center>";
}

function header_then_error($txt)
{
	directadmin_go_header();
	directadmin_error($txt);
}

if (isset($_POST['action']) && $_POST['action'] == "change")
{
        //oldpassword
        //password1
        //password2

        if (!is_pass($_POST['oldpassword']))    { return header_then_error("Old password is not syntactically correct."); }
        if ($_POST['oldpassword'] != sqauth_read_password()) { return header_then_error("Old password is not correct."); }
        if (!is_pass($_POST['password1']))     { return header_then_error("New password is not syntactically correct."); }
        if (!is_pass($_POST['password2']))     { return header_then_error("New password2 is not syntactically correct."); }
        if ($_POST['password1'] != $_POST['password2']) { return header_then_error("New passwords do not match."); }


	global $username;
	list($da_user, $da_domain) = explode('@', $username);
	if ($da_user == "" || $da_domain == "") return 0;


	$sock = newSock();
	set_ssl_setting_message($sock);
	$sock->query('/CMD_API_CHANGE_EMAIL_PASSWORD',
	array(
                'email' => $username,
                'oldpassword' => $_POST['oldpassword'],
                'password1'   => $_POST['password1'],
                'password2'   => $_POST['password2'],
                'api'         => '1',
         ));

        $result = $sock->fetch_parsed_body();

        if ( $result['error'] != "0" )
        {
		return header_then_error("Unable to change password:<br>".$result['text']);
        }

	sqauth_save_password($_POST['password1']);
	session_write_close();

	directadmin_go_header();

        echo "Password changed.";

        exit(0);
}
else
{
	directadmin_go_header();
?>
		<table align=center>
                <form action="?" method="POST">
                <input type=hidden name="action" value="change">
                <tr><td colspan=2 align=center><?=_("Enter the required information below");?></td></tr>
                <tr><td align=right><?=_("Old Password");?>:</td><td><input type=password name=oldpassword size=32></td></tr>
                <tr><td align=right><?=_("New Password");?>:</td><td><input type=password name=password1 size=32></td></tr>
                <tr><td align=right><?=_("Re-Type Password");?>:</td><td><input type=password name=password2 size=32></td></tr>
                <tr><td colspan=2 align=center><input type=submit value="<?=_("Change Password");?>"></td></tr>
                </form><table>
<?

}

echo "</center>";




?>
