#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then 
	LANG001="Error"		#ʧ
	LANG002="Success"	#ɹ
	LANG003="SET_PPPOESVR_STAT"	#PPPOE
	LANG004="SET_PPPOE_NOTICE"	#PPPOEҳ
	LANG005="Force_pppoe_users_offline"	#ǿPPPOEû
	LANG006="BIND_PPPOE_USER_MAC"		#PPPOEûMACַ
fi


load_pppoesvr_stat()
{
	printf "{"
	printf "\"_\":\"_\""

	${FLOWEYE} pppoesvr stat | awk -F"=" \
	'{
		printf ",\"%s\":\"%s\"", $1, $2;
	}'

	printf "}"
}


get_welcomemsg()
{
	printf "Content-Type: text/html; charset=gb2312\r\n"
	printf "\r\n"

	${FLOWEYE} pppoesvr config showwelcomemsg=1
}


get_initmemsg()
{
	printf "Content-Type: text/html; charset=gb2312\r\n"
	printf "\r\n"

	${FLOWEYE} pppoesvr config showintimemsg=1
}


get_expiremsg()
{
	printf "Content-Type: text/html; charset=gb2312\r\n"
	printf "\r\n"

	${FLOWEYE} pppoesvr config showexpiredmsg=1
}


set_pppoesvr_stat()
{
	[ "${CGI_enable}" = "on" ] && enable=1 || enable=0

	cmdargs="${cmdargs} enable=${enable} keepalive=${CGI_keepalive} radexptime=${CGI_radexptime}"
	cmdargs="${cmdargs} ignorecache=${CGI_ignorecache} auto_macbind=${CGI_auto_macbind} limit_clntpxy=${CGI_limit_clntpxy}"
	errmsg=`${FLOWEYE} pppoesvr config ${cmdargs}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		WEB_LOGGER "${LANG003:=PPPOE}" "${cmdargs}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


set_pppoe_notify_msg()
{
	[ "${CGI_welcome_enable}" = "on" ] && welcome_enable=1 || welcome_enable=0
	[ "${CGI_intime_enable}"  = "on" ] && intime_enable=1  || intime_enable=0

	printf "${CGI_welcomemsg}" > ${PGETC}/pppoe_welcome.html
	printf "${CGI_expiredmsg}" > ${PGETC}/pppoe_expired.html
	printf "${CGI_intimemsg}"  > ${PGETC}/pppoe_intime.html

	cmdstrs="${cmdstrs} welcome_enable=${welcome_enable}"
	cmdstrs="${cmdstrs} intime_enable=${intime_enable}"
	cmdstrs="${cmdstrs} intime_days=${CGI_intime_days}"
	cmdstrs="${cmdstrs} intime_hours=${CGI_intime_hours}"
	cmdstrs="${cmdstrs} loadmsg=1"

	errmsg=`${FLOWEYE} pppoesvr config ${cmdstrs}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		WEB_LOGGER "${LANG004:=PPPOEҳ}" "${cmdargs}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


load_pppoelocal_user()
{
	args=""
	end=$(( ${CGI_page} * ${CGI_limit} ))
	start=$(( ${end} - ${CGI_limit} ))

	[ "${CGI_sort}" = "" ] && CGI_sort="-k14"
	[ "${CGI_poolid}" != "" ] && args="${args} poolid=${CGI_poolid}"
	[ "${CGI_server}" != "" ] && args="${args} server=${CGI_server}"
	[ "${CGI_vlan}" != "" ] && args="${args} vlan=${CGI_vlan}"

	${FLOWEYE} pppoesvr listclnt ${args} | grep -i "${CGI_keyword}" | sort ${CGI_sort} | awk \
	-v auth=${CGI_auth} -v start=${start} -v end=${end} -v type=${CGI_type} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		if($20 == "PXY") next;
		
		if(type == "dialing"){
			if($14 == "CONNECTED")
				next;
		}else {
			if( $14 != "CONNECTED" )
				next;
		}

		if(auth != "" && $17 != auth) next;

		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 "\"ip6str\":\"%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;
	}'
}


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

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

	${FLOWEYE} clntpxy list | 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;
		gsub(/\\/, "", $3);

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"id\":\"%s\",", NR;
		printf "\"state\":\"%s\",", $(col++);
		printf "\"uid\":\"%s\",", $(col++);
		printf "\"name\":\"%s\",", $(col++);
		printf "\"outif\":\"%s\",", $(col++);
		printf "\"mtu\":\"%s\",", $(col++);
		printf "\"outvlan\":\"%s\",", $(col++);
		printf "\"vlan0\":\"%s\",", $(col++);
		printf "\"vlan1\":\"%s\",", $(col++);
		printf "\"clntmac\":\"%s\",", $(col++);
		printf "\"ipaddr\":\"%s\",", $(col++);
		printf "\"gwaddr\":\"%s\",", $(col++);
		printf "\"inbps\":\"%s\",", $(col++);
		printf "\"inbpsfmt\":\"%s\",", $(col++);
		printf "\"outbps\":\"%s\",", $(col++);
		printf "\"outbpsfmt\":\"%s\",", $(col++);
		printf "\"onlintime\":\"%s\",", $(col++);
		printf "\"svrname\":\"%s\",", $(col++);
		printf "\"poolid\":\"%s\",", $(col++);
		printf "\"poolname\":\"%s\",", $(col++);
		printf "\"ratein\":\"%s\",", $(col++);
		printf "\"rateout\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":\"%s\"}", row;
	}'
}


load_search_env()
{
	. ../common/ajax_object_lib

	printf "{"
	printf "\"server\":`lib_proxy_list posvrif`"
	printf ",\"group\":`lib_ippool_list`"
	printf "}"
}


kick_pppoelocal_user()
{
	errmsg=`${FLOWEYE} pppoesvr rmvclnt ${CGI_maclist}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        WEB_LOGGER "${LANG005:=ǿPPPOEû} ${CGI_maclist}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


bind_pppoelocal_user()
{
	errmsg=`${FLOWEYE} pppoeacct config ${CGI_maclist}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        WEB_LOGGER "${LANG006:=PPPOEûMACַ} ${CGI_maclist}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


dump_userinfo()
{
	${FLOWEYE} pppoesvr get mac=${CGI_mac} | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s\"%s\"", dot, $0;
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


dump_pxyuserinfo()
{
	${FLOWEYE} clntpxy get name=${CGI_name} | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s\"%s\"", dot, $0;
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


load_pppoe_exitlog()
{
	en=0
	[ "${PALANG}" = "en" ] && en=1

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

	${FLOWEYE} syslog list bras en=${en} | grep -i "${CGI_keyword}" | awk \
	-v start=${start} -v end=${end} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		row++;
		if(row > end || row < start) next;

		printf "%s{", dot;
		printf "\"time\":\"%s\",", $1;
		printf "\"name\":\"%s\",", $2;
		printf "\"event\":\"%s\"", $3;
		printf "}"

		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":\"%s\"}", row;
	}'
}


export_exitlog()
{
	en=0
	[ "${PALANG}" = "en" ] && en=1
	file_name="־.conf"
    file_path="${WEB_DOWNLOAD}/${file_name}"

    mkdir -p ${WEB_DOWNLOAD}

    ${FLOWEYE} syslog list bras en=${en} | grep "${CGI_keyword}" > ${file_path}

    WEB_LOGGER "־"
    retjson 0 "OK" "{\"file_name\":\"${file_name}\"}"
}


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

	"get_welcomemsg")
		get_welcomemsg
		;;

	"get_initmemsg")
		get_initmemsg
		;;

	"get_expiremsg")
		get_expiremsg
		;;

	"set_pppoesvr_stat")
		action_check
		set_pppoesvr_stat
		;;

	"set_pppoe_notify_msg")
		action_check
		set_pppoe_notify_msg
		;;

	"load_pppoelocal_user")
		retjson 0 "OK" "`load_pppoelocal_user`"
		;;

	"load_search_env")
		retjson 0 "OK" "`load_search_env`"
		;;

	"load_pppoepxy_user")
		retjson 0 "OK" "`load_pppoepxy_user`"
		;;

	"kick_pppoelocal_user")
		action_check
		kick_pppoelocal_user
		;;

	"bind_pppoelocal_user")
		action_check
		bind_pppoelocal_user
		;;

	"dump_userinfo")
		retjson 0 "OK" "`dump_userinfo`"
		;;

	"dump_pxyuserinfo")
		retjson 0 "OK" "`dump_pxyuserinfo`"
		;;

	"load_pppoe_dialing")
		retjson 0 "OK" "`load_pppoe_dialing`"
		;;

	"load_pppoe_exitlog")
		retjson 0 "OK" "`load_pppoe_exitlog`"
		;;

	"export_exitlog")
		action_check
		export_exitlog
		;;

	*)
		retjson 1 "UNKNOW_ACTION!"
		;;
esac
