<?php
//---------------------------------
//  送给hostloc论坛的朋友,感觉大家
//对我母亲的祝福,今天母亲要出院了.
//2012.02.11
//----------------------------------

function freeradius_ConfigOptions() {

	# Should return an array of the module options for each product - maximum of 24

    $configarray = array(
	 "Package Name" => array( "Type" => "text", "Size" => "20", ),
	 "Suspend User Group" => array( "Type" => "text", "Size" => "20","Description" => "This group is stop user login to freeradius." ),
	);

	return $configarray;

}

function freeradius_CreateAccount($params) {

	$username = $params["username"];
	$password = $params["password"];
    $package = $params["configoption1"];
    $serverip = $params["serverip"];
    $serverusername = $params["serverusername"];
    $serverpassword = $params["serverpassword"];
    $serveraccesshash = $params["serveraccesshash"];
	# Code to perform action goes here...

	$user_passwd='INSERT INTO '.$serveraccesshash.'.radcheck (username,attribute,op,VALUE) VALUES (\''.$username.'\',\'Cleartext-Password\',\':=\',\''.$password.'\')';
	$user_group='INSERT INTO '.$serveraccesshash.'.radusergroup (username,groupname) VALUES (\''.$username.'\',\''.$package.'\')';
	$conn =connectMysql($serverip,$serverusername,$serverpassword);
	$result1=sqlQuery($user_passwd,$conn);
	$result2=sqlQuery($user_group,$conn);
	sqlClose($conn);

	if($result1&&$result2){
		$result = "success";
	} else {
		$result = "Error Message Goes Here...";
	}

	return $result;

}

function freeradius_TerminateAccount($params) {
	$username = $params["username"];
	$package = $params["configoption1"];

	$serverip = $params["serverip"];
    $serverusername = $params["serverusername"];
    $serverpassword = $params["serverpassword"];
    $serveraccesshash = $params["serveraccesshash"];

	$del_user='DELETE FROM '.$serveraccesshash.'.radcheck WHERE username = \''.$username.'\'';
	$del_group='DELETE FROM '.$serveraccesshash.'.radusergroup WHERE username=\''.$username.'\' AND groupname =\''.$package.'\'';
	$conn =connectMysql($serverip,$serverusername,$serverpassword);
	$result1=sqlQuery($del_user,$conn);
	$result2=sqlQuery($del_group,$conn);
	sqlClose($conn);

	# Code to perform action goes here...

	if($result1&&$result2){
		$result = "success";
	} else {
		$result = "Error Message Goes Here...";
	}
	return $result;
	//return $del_user;

}

function freeradius_SuspendAccount($params) {
	$username = $params["username"];
	$suspend_package = $params["configoption2"];

	$serverip = $params["serverip"];
    $serverusername = $params["serverusername"];
    $serverpassword = $params["serverpassword"];
    $serveraccesshash = $params["serveraccesshash"];

	$suspend_user='UPDATE '.$serveraccesshash.'.radusergroup SET  groupname = \''.$suspend_package.'\' WHERE  username = \''.$username.'\'';
	$conn =connectMysql($serverip,$serverusername,$serverpassword);
	$successful=sqlQuery($suspend_user,$conn);
	sqlClose($conn);

	# Code to perform action goes here...

    if ($successful) {
		$result = "success";
	} else {
		$result = "Error Message Goes Here...";
	}
	return $result;
	//return $suspend_user;

}

function freeradius_UnsuspendAccount($params) {
	$username = $params["username"];
	$unsuspend_package = $params["configoption1"];

	$serverip = $params["serverip"];
    $serverusername = $params["serverusername"];
    $serverpassword = $params["serverpassword"];
    $serveraccesshash = $params["serveraccesshash"];

	$unsuspend_user='UPDATE '.$serveraccesshash.'.radusergroup SET  groupname = \''.$unsuspend_package.'\' WHERE  username = \''.$username.'\'';
	$conn =connectMysql($serverip,$serverusername,$serverpassword);
	$successful=sqlQuery($unsuspend_user,$conn);
	sqlClose($conn);
	# Code to perform action goes here...

    if ($successful) {
		$result = "success";
	} else {
		$result = "Error Message Goes Here...";
	}
	return $result;
	//return $unsuspend_user;

}

function freeradius_ChangePassword($params) {
	$username = $params["username"];
	$password = $params["password"];

	$serverip = $params["serverip"];
    $serverusername = $params["serverusername"];
    $serverpassword = $params["serverpassword"];
    $serveraccesshash = $params["serveraccesshash"];

	$changepasswd_user='UPDATE  '.$serveraccesshash.'.radcheck SET  value = \''.$password.'\' WHERE username = \''.$username.'\'';
	$conn =connectMysql($serverip,$serverusername,$serverpassword);
	$successful=sqlQuery($changepasswd_user,$conn);
	sqlClose($conn);
	# Code to perform action goes here...

    if ($successful) {
		$result = "success";
	} else {
		$result = "Error Message Goes Here...";
	}
	return $result;

}

function freeradius_ChangePackage($params) {
	$username = $params["username"];
	$unsuspend_package = $params["configoption1"];

	$serverip = $params["serverip"];
    $serverusername = $params["serverusername"];
    $serverpassword = $params["serverpassword"];
    $serveraccesshash = $params["serveraccesshash"];

	$changepackage_user='UPDATE '.$serveraccesshash.'.radusergroup SET  groupname = \''.$unsuspend_package.'\' WHERE  username = \''.$username.'\'';
	$conn =connectMysql($serverip,$serverusername,$serverpassword);
	$successful=sqlQuery($changepackage_user,$conn);
	sqlClose($conn);
	# Code to perform action goes here...

    if ($successful) {
		$result = "success";
	} else {
		$result = "Error Message Goes Here...";
	}
	return $result;

}

/*This is mysql connect*/
function connectMysql($ip,$user,$passwd){
	$mysql_connection = mysql_connect($ip,$user,$passwd);
	if ($mysql_connection === FALSE)
		throw new Exception("cannot connect to DB");
	return $mysql_connection;
}

function sqlQuery($query,$mysql_connection)
{
	$resource = mysql_query($query, $mysql_connection);

	if($resource == FALSE)
		return FALSE;
	else
		return TRUE;

}

function sqlClose($mysql_connection){
	return mysql_close($mysql_connection);
}

?>