#!/bin/sh

. ../common/ajax_common

CONFDIR=${PGETC}/App/sac
APPCONF="${CONFDIR}/sac.conf"


load_ssid_list()
{
	AutoDelSSID="no"
	[ -f ${APPCONF} ] && . ${APPCONF}

	dot=""
	result=`${FLOWEYE} sacssid list | grep -i "${CGI_keyword}"`

	if [ "${result}" = "" ]; then
		printf "[]"
		return
	fi

	printf "["

	while read id ssidvlan ssidname vlan pwd mode apcnt stacnt hide maxonline isolate intf_tx intf_rx mcast_tx mcast_rx sta_tx sta_rx kick reject
	do
		if [ "${first}" = "" -a "${CGI_keyword}" = "" ]; then
			first="first"
			continue
		fi

		# ԶɾδAPSSID
		if [ "${apcnt}" = "0" -a "${AutoDelSSID}" = "yes" ]; then
			${FLOWEYE} sacssid remove id=${id}
			continue
		fi

		apidstr=""
		for i in `${FLOWEYE} sac list listssid=1 ssid=${ssidvlan} | cut -d" " -f2`
		do
			apidstr="${apidstr},${i}"
		done

		ssidname=`utf8togb2312 ${ssidname}`
		ssidvlan=`utf8togb2312 ${ssidvlan}`

		printf "${dot}{"
		printf "\"ssidname\":\"%s\"," "`echo ${ssidname} | sed 's/#/ /g'`"
		printf "\"ssidvlan\":\"%s\"," "`echo ${ssidvlan} | sed 's/#/ /g'`"
		printf "\"vlan\":\"%s\"," ${vlan}
		printf "\"pwd\":\"%s\"," ${pwd}
		printf "\"id\":%d," ${id}
		printf "\"mode\":%d," ${mode}
		printf "\"apcnt\":%d," ${apcnt}
		printf "\"apidstr\":\"%s\"," ${apidstr}
		printf "\"stacnt\":%d," ${stacnt}
		printf "\"hide\":%d," ${hide}
		printf "\"maxonline\":%d," ${maxonline}
		printf "\"isolate\":%d," ${isolate}
		printf "\"intf_tx\":%d," ${intf_tx}
		printf "\"intf_rx\":%d," ${intf_rx}
		printf "\"mcast_tx\":%d," ${mcast_tx}
		printf "\"mcast_rx\":%d," ${mcast_rx}
		printf "\"sta_tx\":%d," ${sta_tx}
		printf "\"sta_rx\":%d," ${sta_rx}
		printf "\"kick\":%d," ${kick}
		printf "\"reject\":%d" ${reject}

		printf "}"

		[ "${dot}" = "" ] && dot=","
	done << EOF
${result}
EOF

	printf "]"
}


show_ssid_bindap()
{
	if [ "${CGI_ssidvlan}" = "" ]; then
		printf "[]"
		return
	fi

	CGI_ssidvlan=`echo "${CGI_ssidvlan}" | sed 's/ /#/g'`
	CGI_ssidvlan=`gb2312toutf8 ${CGI_ssidvlan}`

	printf "["

	while read apname apid radioid wlanid
	do
		apname=`utf8togb2312 ${apname}`

		printf "${dot}"
		printf "{"
		printf "\"apname\":\"${apname}\","
		printf "\"apid\":\"${apid}\","
		printf "\"radioid\":\"${radioid}\","
		printf "\"wlanid\":\"${wlanid}\""
		printf "}"
		[ "${dot}" = "" ] && dot=","
	done << EOF
`${FLOWEYE} sac list listssid=1 ssid=${CGI_ssidvlan} `
EOF
	printf "]"
}


add_ssid()
{
	[ "${CGI_vlan}"  = "" ] && retjson 1 "NO_VLAN"
	[ "${CGI_mode}"  = "" ] && retjson 1 "NO_SSID_MODE"
    [ "${CGI_ssidname}"  = "" ] && retjson 1 "NO_SSID_NAME"
    [ "${CGI_radio0}" = "" -a "${CGI_radio1}" = "" ] && retjson 1 "NO_RADIO"

	if [ "${CGI_mode}" != "0" ]; then
		[ "${CGI_pwd}" = "" ] && retjson 1 "NO_PASSWORD"
		cmdargs="mode=${CGI_mode} pwd=${CGI_pwd}"
	else
		cmdargs="mode=0"
	fi

	[ "${CGI_hide}" = "on" ] && ssidhide=0 || ssidhide=1
	[ "${CGI_isolate}" = "on" ] && isolate=1 || isolate=0

	CGI_ssidname=`echo "${CGI_ssidname}" | sed 's/ /#/g'`
	CGI_ssidname=`gb2312toutf8 ${CGI_ssidname}`
    cmdargs="vlan=${CGI_vlan} hide=${ssidhide} maxsta=${CGI_maxonline} isolate=${isolate} ${cmdargs}"

	errmsg=`${FLOWEYE} sacssid add name=${CGI_ssidname} ${cmdargs}`

	if [ $? -ne 0 ]; then 
		retjson 1 "ʧܣ${errmsg}"
		return
	fi

	[ "${CGI_apidstr}" = "0" ] && retjson 0 "${LANG_004:=ɹ}"

	if [ "${CGI_radio0}" != "" -a "${CGI_radio1}" != "" ]; then
		rdidx=2
	elif [ "${CGI_radio0}" != "" ]; then
		rdidx=0
	else
		rdidx=1
	fi

	cmdstr="op=addwlan&ridx=${rdidx}&ssid=${CGI_ssidname}&amode=${CGI_mode}&vlan=${CGI_vlan}&hide=${ssidhide}&isolate=${isolate}&maxsta=${CGI_maxonline}"
	[ "${CGI_wlanid}" != "0" ] && cmdstr="${cmdstr}&widx=${CGI_wlanid}"
	[ "${CGI_pwd}"    != ""  ] && cmdstr="${cmdstr}&key=${CGI_pwd}"

    . ./ajax_sac_object_lib
	sac_task_commit "${cmdstr}"
}


edit_ssid()
{
	[ "${CGI_vlan}"  = "" ] && retjson 1 "NO_VLAN"
	[ "${CGI_mode}"  = "" ] && retjson 1 "NO_SSID_MODE"
    [ "${CGI_ssidname}"  = "" ] && retjson 1 "NO_SSID_NAME"

	if [ "${CGI_mode}" != "0" ]; then
		[ "${CGI_pwd}" = "" ] && retjson 1 "NO_PASSWORD"
		cmdargs="amode=${CGI_mode} pwd=${CGI_pwd}"
	else
		cmdargs="amode=0"
	fi

	CGI_ssidvlan=`echo "${CGI_ssidvlan}" | sed 's/ /#/g'`
	CGI_ssidvlan=`gb2312toutf8 ${CGI_ssidvlan}`

    cmdargs="vlan=${CGI_vlan} hide=${CGI_hide} maxsta=${CGI_maxonline} isolate=${CGI_isolate} ${cmdargs}"
	cmdargs="${cmdargs} kick=-${CGI_kick} reject=-${CGI_reject} uprate=${CGI_intf_tx} downrate=${CGI_intf_rx}"
	errmsg=`${FLOWEYE} sacssid set name=${CGI_ssidvlan} ${cmdargs}`

	[ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
	[ "${CGI_apcnt}" = "0" ] && retjson 0 "ɹ"

	if [ "${CGI_radio0}" != "" -a "${CGI_radio1}" != "" ]; then
		rdidx=2
	elif [ "${CGI_radio0}" != "" ]; then
		rdidx=0
	else
		rdidx=1
	fi
	
	cmdstr="amode=${CGI_mode}&ridx=${rdidx}&vlan=${CGI_vlan}&hide=${CGI_hide}&isolate=${CGI_isolate}&maxsta=${CGI_maxonline}"
	cmdstr="${cmdstr}&kick=-${CGI_kick}&reject=-${CGI_reject}&uprate=${CGI_intf_tx}&downrate=${CGI_intf_rx}"
	[ "${CGI_wlanid}" != "0" ] && cmdstr="${cmdstr}&widx=${CGI_wlanid}"
	[ "${CGI_pwd}"    != ""  ] && cmdstr="${cmdstr}&key=${CGI_pwd}"

	cmdstr="op=modwlan&${cmdstr}&ssid=${CGI_ssidvlan}"
	

    . ./ajax_sac_object_lib
	sac_task_commit "${cmdstr}"
}


bind_ssid()
{
	[ "${CGI_vlan}"  = "" ] && retjson 1 "NO_VLAN"
	[ "${CGI_mode}"  = "" ] && retjson 1 "NO_SSID_MODE"
    [ "${CGI_ssidname}"  = "" ] && retjson 1 "NO_SSID_NAME"

	[ "${CGI_radio0}" = "" -a "${CGI_radio1}" = "" ] && retjson 1 "NO_RADIO"
	[ "${CGI_mode}" -gt 0 -a "${CGI_pwd}" = "" ] && retjson 1 "NO_PASSWORD"

	CGI_ssidname=`echo "${CGI_ssidname}" | sed 's/ /#/g'`
	CGI_ssidname=`gb2312toutf8 ${CGI_ssidname}`
	
	if [ "${CGI_radio0}" != "" -a "${CGI_radio1}" != "" ]; then
		rdidx=2
	elif [ "${CGI_radio0}" != "" ]; then
		rdidx=0
	else
		rdidx=1
	fi

	cmdstr="op=addwlan&ridx=${rdidx}&ssid=${CGI_ssidname}&amode=${CGI_mode}&vlan=${CGI_vlan}&hide=${CGI_hide}&isolate=${isolate}&maxsta=${CGI_maxonline}"
	
	[ "${CGI_wlanid}" != "0" ] && cmdstr="${cmdstr}&widx=${CGI_wlanid}"
	[ "${CGI_pwd}"    != ""  ] && cmdstr="${cmdstr}&key=${CGI_pwd}"
	
	. ./ajax_sac_object_lib
	sac_task_commit "${cmdstr}"
}


remove_ssid()
{
	for id in ${CGI_emptyssid}
	do
		${FLOWEYE} sacssid remove id=${id}
	done
	
	[ "${CGI_ssidstr}" = "" ] && retjson 2 "ɹ"

	. ./ajax_sac_object_lib

	CGI_ssidstr=`echo "${CGI_ssidstr}" | sed 's/ /#/g'`
	CGI_ssidstr=`gb2312toutf8 ${CGI_ssidstr}`

	cmdstr="op=delwlan&ssidstr=${CGI_ssidstr}"
	sac_task_commit "${cmdstr}"
}


hide_ssid()
{
	CGI_ssidvlan=`echo "${CGI_ssidvlan}" | sed 's/ /#/g'`
	CGI_ssidvlan=`gb2312toutf8 ${CGI_ssidvlan}`
	errmsg=`${FLOWEYE} sacssid set name=${CGI_ssidvlan} hide=${CGI_hide}`
	
	[ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
	[ "${CGI_apcnt}" = "0" ] && retjson 2 "ɹ"
	
	. ./ajax_sac_object_lib
	
	cmdstr="op=modwlan&ssid=${CGI_ssidvlan}&hide=${CGI_hide}&amode=${CGI_mode}&key=${CGI_pwd}&vlan=${CGI_vlan}"
	sac_task_commit "${cmdstr}"
}


set_ssid_click()
{
	CGI_ssidvlan=`echo "${CGI_ssidvlan}" | sed 's/ /#/g'`
	CGI_ssidvlan=`gb2312toutf8 ${CGI_ssidvlan}`
	cmd="${FLOWEYE} sac list listssid=1 ssid=${CGI_ssidvlan}"
	
	widx=`${cmd} | tail -1 | cut -d" " -f4 | cut -d"/" -f1`
	apid=`${cmd} | awk '{printf ",%s", $2;}'`
	
	args="widx=${widx}&kickrssi=-${CGI_kickrssi}&rejectrssi=-${CGI_rejectrssi}"
	cmdstr="pappiw&modwlan&ridx=0&${args};pappiw&modwlan&ridx=1&${args};pappiw&reload"

	. ./ajax_sac_object_lib
	sac_debug_commit "${cmdstr}"
}


load_rmvssid_conf()
{
	AutoDelSSID="no"
	[ -f ${APPCONF} ] && . ${APPCONF}
	printf "{\"auto_rmv\":\"${AutoDelSSID}\"}"
}


set_rmvssid_conf()
{
	mkdir -p ${CONFDIR}
	touch ${APPCONF}

	grep -v "AutoDelSSID=" ${APPCONF}.bak
	echo "AutoDelSSID=${CGI_auto_rmv_ssid}" >> ${APPCONF}.bak
	mv ${APPCONF}.bak ${APPCONF}
	retjson 0 "OK"
}


load_acl_list()
{
	dot=","
	result=`${FLOWEYE} sac_acl list | grep "${CGI_keyword}"`

	if [ -z "$result" ]; then
		printf "[]"
		return
	fi

	printf "[{}"

	while read apid ridx widx mode mac
	do
		mode="${mode#*=}"
		mac="${mac#*=}"
		ridx="${ridx#*=}"
		widx="${widx#*=}"
		apid="${apid#*=}"
		
		${FLOWEYE} sac list listssid=1 apid=${apid} rdid=${ridx} | while read vlan wlanid mac1 ssid other
		do
			[ "${ssid}" != "${CGI_ssidname}" ] && continue
			[ "${widx}" != "${wlanid}" ] && continue

			apname=$(${FLOWEYE} sac list listssid=1 ssid=${CGI_ssidname}_${vlan} | awk "\$2 == ${apid} {print \$1}")
			apname=`utf8togb2312 ${apname}`

			printf "%s{" "$dot"
			printf "\"apname\":\"%s\"," "$(printf "%s" "$apname" | sed 's/["\\]/\\&/g')"
			printf "\"mode\":\"%s\"," "$mode"
			printf "\"mac\":\"%s\"," "$(printf "%s" "$mac" | sed 's/:/-/g')"
			printf "\"apid\":\"%s\"," "$apid"
			printf "\"ridx\":\"%s\"," "$ridx"
			printf "\"widx\":\"%s\"" "$widx"
			printf "}"
		done
	done << EOF
${result}
EOF

	printf "]"
}


add_acl()
{
	mac=${CGI_mac//-/:}

	if echo "$CGI_ridx" | grep -q "/"; then
			ridx_2g=$(echo "$CGI_ridx" | cut -d'/' -f1)
			widx_2g=$(echo "$CGI_widx" | cut -d'/' -f1)
			ridx_5g=$(echo "$CGI_ridx" | cut -d'/' -f2)
			widx_5g=$(echo "$CGI_widx" | cut -d'/' -f2)
			${FLOWEYE} sactask add apidstr=,${CGI_apidstr} debugstr="pappiw&modwlan&ridx=${ridx_2g}&widx=${widx_2g}&action=add&stamac="${mac} >>/dev/null 2>&1
			${FLOWEYE} sactask add apidstr=,${CGI_apidstr} debugstr="pappiw&modwlan&ridx=${ridx_5g}&widx=${widx_5g}&action=add&stamac="${mac}";pappiw&reload;" >>/dev/null 2>&1
	else
		errmsg=`${FLOWEYE} sactask add apidstr=,${CGI_apidstr} debugstr="pappiw&modwlan&ridx=${CGI_ridx}&widx=${CGI_widx}&action=add&stamac="${mac}";pappiw&reload;" >>/dev/null 2>&1`
	fi

	if [ $? -eq 0 ]; then
		retjson 0 "ɹ"
	else
		WEB_LOGGER "SSIDڰ" "apidstr=${CGI_apidstr} mac=${mac} ridx=${CGI_ridx}"
		retjson 1 "ʧ:${errmsg}"
	fi
}


del_acl()
{
	mac=${CGI_mac//-/:}
	errmsg=`${FLOWEYE} sactask add apidstr=,${CGI_apidstr} debugstr="pappiw&modwlan&ridx=${CGI_ridx}&widx=${CGI_widx}&action=del&stamac="${mac}";pappiw&reload;" >>/dev/null 2>&1`

	if [ $? -eq 0 ]; then
		retjson 0 "ɹ"
	else
		WEB_LOGGER "ɾSSIDڰ" "apidstr=${CGI_apidstr} mac=${mac} ridx=${CGI_ridx}"
		retjson 1 "ʧ:${errmsg}"
	fi
}


mode_acl()
{

	if echo "$CGI_ridx" | grep -q "/"; then
			ridx_2g=$(echo "$CGI_ridx" | cut -d'/' -f1)
			widx_2g=$(echo "$CGI_widx" | cut -d'/' -f1)
			ridx_5g=$(echo "$CGI_ridx" | cut -d'/' -f2)
			widx_5g=$(echo "$CGI_widx" | cut -d'/' -f2)
			${FLOWEYE} sactask add apidstr=,${CGI_apidstr} debugstr="pappiw&modwlan&ridx=${ridx_2g}&widx=${widx_2g}&aclmode=${CGI_mode}" >>/dev/null 2>&1
			${FLOWEYE} sactask add apidstr=,${CGI_apidstr} debugstr="pappiw&modwlan&ridx=${ridx_5g}&widx=${widx_5g}&aclmode=${CGI_mode}" >>/dev/null 2>&1
	else
		errmsg=`${FLOWEYE} sactask add apidstr=,${CGI_apidstr} debugstr="pappiw&modwlan&ridx=${CGI_ridx}&widx=${CGI_widx}&aclmode=${CGI_mode}" >>/dev/null 2>&1`
	fi

	if [ $? -eq 0 ]; then
		retjson 0 "ɹ"
	else
		WEB_LOGGER "SSIDڰģʽ" "apidstr=${CGI_apidstr} aclmode=${CGI_mode}"
		retjson 1 "ʧ:${errmsg}"
	fi
}


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

	"show_ssid_bindap")
		retjson 0 "OK" "`show_ssid_bindap`"
		;;

    "add_ssid")
        action_check
        add_ssid
        ;;

	"edit_ssid")
		action_check
		edit_ssid
		;;

	"bind_ssid")
		action_check
		bind_ssid
		;;

	"remove_ssid")
		action_check
		remove_ssid
		;;

	"hide_ssid")
		action_check
		hide_ssid
		;;

	"set_ssid_click")
		action_check
		set_ssid_click
		;;

	"load_rmvssid_conf")
		retjson 0 "OK" "`load_rmvssid_conf`"
		;;

	"set_rmvssid_conf")
		action_check
		set_rmvssid_conf
		;;

	"load_acl_list")
		retjson 0 "OK" "`load_acl_list`"
		;;

	"add_acl")
		action_check
		add_acl
		;;

	"del_acl")
		action_check
		del_acl
		;;

	"mode_acl")
		action_check
		mode_acl
		;;
    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
