#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then 
	LANG001="Force_webauth_users_offline "	#ǿweb֤û
	LANG002="Success"	#ɹ
fi


load_webauth_user()
{
	end=$(( ${CGI_page} * ${CGI_limit} ))
	start=$(( ${end} - ${CGI_limit} ))

	[ "${CGI_sort}" = "" ] && CGI_sort="-k14"

	types="type=webrad type=webacct type=cmccportal"
	${FLOWEYE} pppoesvr listclnt ${types} poolid=${CGI_ippool} | grep -i "${CGI_keyword}"  | sort ${CGI_sort} | awk \
	-v start=${start} -v end=${end} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		row++;
		if( row > end  || row < start ) next;

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"id\":\"%s\",", NR;
        printf "\"sid\":\"%s\",", $(col++);
        printf "\"name\":\"%s\",", $(col++);
		printf "\"poolid\":\"%s\",", $(col++);
		printf "\"poolname\":\"%s\",", $(col++);
		printf "\"svrname\":\"%s\",", $(col++);
		printf "\"pctime\":\"%s\",", $(col++);
		printf "\"clntmac\":\"%s\",", $(col++);
		printf "\"svrmac\":\"%s\",", $(col++);
		printf "\"ipstr\":\"%s\",", $(col++);
		printf "\"ipnum\":\"%s\",", $(col++);
		printf "\"mtu\":\"%s\",", $(col++);
		printf "\"vlan\":\"%s\",", $(col++);
		printf "\"state\":\"%s\",", $(col++);
		printf "\"birth\":\"%s\",", $(col++);
        printf "\"expired\":\"%s\",", $(col++);
        printf "\"auth\":\"%s\",", $(col++);
        printf "\"ratein\":\"%s\",", $(col++);
        printf "\"rateout\":\"%s\",", $(col++);
		printf "\"clntpxy\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":\"%s\"}", row;
	}'
}


kick_webauth_user()
{
	for ip in ${CGI_ips}
	do
		${FLOWEYE} ipobj remove ip=${ip} uid=webrad
		${FLOWEYE} ipobj remove ip=${ip} uid=webacct
		${FLOWEYE} ipobj remove ip=${ip} uid=cmccportal
	done

	WEB_LOGGER "${LANG001:=ǿweb֤û}" "ip=${CGI_ip} uid=${CGI_tname} account=${CGI_account}"
	retjson 0 "${LANG002:=ɹ}"
}


case "${CGI_action}" in
    "load_webauth_user")
        retjson 0 "OK" "`load_webauth_user`"
        ;;

    "kick_webauth_user")
        action_check
        kick_webauth_user
        ;;

	*)
		retjson 1 "UNKNOW_ACTION!"
		;;
esac
