#!/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 nas radius_server radius_port radius_key
	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 "\"nas\":%d," ${nas}
		printf "\"radius_server\":\"%s\"," "${radius_server}"
		printf "\"radius_port\":%u," ${radius_port}
		printf "\"radius_key\":\"%s\"" "${radius_key}"
		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"

	CGI_radius_port=${CGI_radius_port:-1812}
	if [ "${CGI_mode}" != "0" ] && [ "${CGI_mode}" != "5" ]; then
		[ "${CGI_pwd}" = "" ] && retjson 1 "NO_PASSWORD"
		cmdargs="mode=${CGI_mode} pwd=${CGI_pwd}"
	elif [ "${CGI_mode}" = "5" ]; then
		cmdargs="mode=${CGI_mode}"
		[ -n "${CGI_radius_server}" ] && cmdargs="${cmdargs} radius_server=${CGI_radius_server}"
		[ -n "${CGI_radius_port}" ]   && cmdargs="${cmdargs} radius_port=${CGI_radius_port}"
		[ -n "${CGI_radius_key}" ]    && cmdargs="${cmdargs} radius_key=${CGI_radius_key}"
		[ -n "${CGI_nas}" ]           && cmdargs="${cmdargs} nas=${CGI_nas}"
	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}"

	if [ "${CGI_mode}" = "5" ]; then
		cmdstr="${cmdstr}&key="
		[ -n "${CGI_radius_server}" ] && cmdstr="${cmdstr}&radius_server=${CGI_radius_server}"
		[ -n "${CGI_radius_port}" ]   && cmdstr="${cmdstr}&radius_port=${CGI_radius_port}"
		[ -n "${CGI_radius_key}" ]    && cmdstr="${cmdstr}&radius_key=${CGI_radius_key}"
		[ -n "${CGI_nas}" ]           && cmdstr="${cmdstr}&nas=${CGI_nas}"
	else
		[ "${CGI_pwd}" != "" ] && cmdstr="${cmdstr}&key=${CGI_pwd}"
	fi

	. ./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"

	CGI_radius_port=${CGI_radius_port:-1812}
	if [ "${CGI_mode}" != "0" ] && [ "${CGI_mode}" != "5" ]; then
		[ "${CGI_pwd}" = "" ] && retjson 1 "NO_PASSWORD"
		cmdargs="amode=${CGI_mode} pwd=${CGI_pwd}"
	elif [ "${CGI_mode}" = "5" ]; then
		cmdargs="amode=${CGI_mode}"
		[ -n "${CGI_radius_server}" ] && cmdargs="${cmdargs} radius_server=${CGI_radius_server}"
		[ -n "${CGI_radius_port}" ]   && cmdargs="${cmdargs} radius_port=${CGI_radius_port}"
		[ -n "${CGI_radius_key}" ]    && cmdargs="${cmdargs} radius_key=${CGI_radius_key}"
		[ -n "${CGI_nas}" ]           && cmdargs="${cmdargs} nas=${CGI_nas}"
	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}"
	
	if [ "${CGI_mode}" = "5" ]; then
		cmdstr="${cmdstr}&key="
		[ -n "${CGI_radius_server}" ] && cmdstr="${cmdstr}&radius_server=${CGI_radius_server}"
		[ -n "${CGI_radius_port}" ]   && cmdstr="${cmdstr}&radius_port=${CGI_radius_port}"
		[ -n "${CGI_radius_key}" ]    && cmdstr="${cmdstr}&radius_key=${CGI_radius_key}"
		[ -n "${CGI_nas}" ]           && cmdstr="${cmdstr}&nas=${CGI_nas}"
	else
		[ "${CGI_pwd}" != "" ] && cmdstr="${cmdstr}&key=${CGI_pwd}"
	fi

	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"

	if [ "${CGI_mode}" != "0" ] && [ "${CGI_mode}" != "5" ]; then
		[ "${CGI_pwd}" = "" ] && retjson 1 "NO_PASSWORD"
	fi

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

	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_radius_port=${CGI_radius_port:-1812}
	if [ "${CGI_mode}" = "5" ]; then
		cmdstr="${cmdstr}&key="
		[ -n "${CGI_radius_server}" ] && cmdstr="${cmdstr}&radius_server=${CGI_radius_server}"
		[ -n "${CGI_radius_port}" ]   && cmdstr="${cmdstr}&radius_port=${CGI_radius_port}"
		[ -n "${CGI_radius_key}" ]    && cmdstr="${cmdstr}&radius_key=${CGI_radius_key}"
		[ -n "${CGI_nas}" ]           && cmdstr="${cmdstr}&nas=${CGI_nas}"
	else
		[ "${CGI_pwd}" != "" ] && cmdstr="${cmdstr}&key=${CGI_pwd}"
	fi
	
	. ./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}`

	WEB_LOGGER "ɾssid" "${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}&radius_port=${CGI_radius_port:-1812}"
	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=$(printf '%s' "$CGI_mac" | tr '-' ':')

	apid_cnt=$(printf '%s' "$CGI_apidstr" | tr -cd ',' | wc -c)
	ridx_cnt=$(printf '%s' "$CGI_ridx" | tr -cd ',' | wc -c)
	widx_cnt=$(printf '%s' "$CGI_widx" | tr -cd ',' | wc -c)
	apid_num=$((apid_cnt + 1))
	ridx_num=$((ridx_cnt + 1))
	widx_num=$((widx_cnt + 1))

	if [ $apid_num -ne $ridx_num ] || [ $apid_num -ne $widx_num ]; then
		retjson 1 "ƥ: apidstr=$apid_num ridx=$ridx_num widx=$widx_num"
		return
	fi

	all_success=true
	last_errmsg=""

	i=1
	while [ $i -le $apid_num ]; do
		apid=$(printf '%s' "$CGI_apidstr" | cut -d',' -f$i)
		ridx=$(printf '%s' "$CGI_ridx" | cut -d',' -f$i)
		widx=$(printf '%s' "$CGI_widx" | cut -d',' -f$i)

		if echo "$ridx" | grep -q "/"; then
			ridx_2g=$(echo "$ridx" | cut -d'/' -f1)
			widx_2g=$(echo "$widx" | cut -d'/' -f1)
			ridx_5g=$(echo "$ridx" | cut -d'/' -f2)
			widx_5g=$(echo "$widx" | cut -d'/' -f2)

			errmsg1=`${FLOWEYE} sactask add apidstr=,${apid} debugstr="pappiw&modwlan&ridx=${ridx_2g}&widx=${widx_2g}&action=add&stamac="${mac} >>/dev/null 2>&1`
			if [ $? -ne 0 ]; then
				all_success=false
				last_errmsg="$errmsg1"
				WEB_LOGGER "SSIDڰʧ" "apid=${apid} mac=${mac} ridx=${ridx_2g}"
			fi

			errmsg2=`${FLOWEYE} sactask add apidstr=,${apid} debugstr="pappiw&modwlan&ridx=${ridx_5g}&widx=${widx_5g}&action=add&stamac="${mac}";pappiw&reload;" >>/dev/null 2>&1`
			if [ $? -ne 0 ]; then
				all_success=false
				last_errmsg="$errmsg2"
				WEB_LOGGER "SSIDڰʧ" "apid=${apid} mac=${mac} ridx=${ridx_5g}"
			fi
		else
			errmsg=`${FLOWEYE} sactask add apidstr=,${apid} debugstr="pappiw&modwlan&ridx=${ridx}&widx=${widx}&action=add&stamac="${mac}";pappiw&reload;" >>/dev/null 2>&1`
			if [ $? -ne 0 ]; then
				all_success=false
				last_errmsg="$errmsg"
				WEB_LOGGER "SSIDڰʧ" "apid=${apid} mac=${mac} ridx=${ridx}"
			fi
		fi

		i=$((i + 1))
	done

	if [ "$all_success" = "true" ]; then
		retjson 0 "ɹ"
	else
		retjson 1 "ʧ: ${last_errmsg}"
	fi
}


del_acl()
{
	mac=$(printf '%s' "$CGI_mac" | tr '-' ':')

	apid_cnt=$(printf '%s' "$CGI_apidstr" | tr -cd ',' | wc -c)
	ridx_cnt=$(printf '%s' "$CGI_ridx" | tr -cd ',' | wc -c)
	widx_cnt=$(printf '%s' "$CGI_widx" | tr -cd ',' | wc -c)
	apid_num=$((apid_cnt + 1))
	ridx_num=$((ridx_cnt + 1))
	widx_num=$((widx_cnt + 1))

	if [ $apid_num -ne $ridx_num ] || [ $apid_num -ne $widx_num ]; then
		retjson 1 "ƥ: apidstr=$apid_num ridx=$ridx_num widx=$widx_num"
		return
	fi

	all_success=true
	last_errmsg=""

	i=1
	while [ $i -le $apid_num ]; do
		apid=$(printf '%s' "$CGI_apidstr" | cut -d',' -f$i)
		ridx=$(printf '%s' "$CGI_ridx" | cut -d',' -f$i)
		widx=$(printf '%s' "$CGI_widx" | cut -d',' -f$i)

		errmsg=`${FLOWEYE} sactask add apidstr=,${apid} debugstr="pappiw&modwlan&ridx=${ridx}&widx=${widx}&action=del&stamac="${mac}";pappiw&reload;" >>/dev/null 2>&1`
		if [ $? -ne 0 ]; then
			all_success=false
			last_errmsg="$errmsg"
			WEB_LOGGER "ɾSSIDڰʧ" "apid=${apid} mac=${mac} ridx=${ridx}"
		fi

		i=$((i + 1))
	done

	if [ "$all_success" = "true" ]; then
		retjson 0 "ɹ"
	else
		retjson 1 "ʧ: ${last_errmsg}"
	fi
}


mode_acl()
{
	apid_cnt=$(printf '%s' "$CGI_apidstr" | tr -cd ',' | wc -c)
	ridx_cnt=$(printf '%s' "$CGI_ridx" | tr -cd ',' | wc -c)
	widx_cnt=$(printf '%s' "$CGI_widx" | tr -cd ',' | wc -c)
	apid_num=$((apid_cnt + 1))
	ridx_num=$((ridx_cnt + 1))
	widx_num=$((widx_cnt + 1))

	if [ $apid_num -ne $ridx_num ] || [ $apid_num -ne $widx_num ]; then
		retjson 1 "ƥ: apidstr=$apid_num ridx=$ridx_num widx=$widx_num"
		return
	fi

	all_success=true
	last_errmsg=""

	i=1
	while [ $i -le $apid_num ]; do
		apid=$(printf '%s' "$CGI_apidstr" | cut -d',' -f$i)
		ridx=$(printf '%s' "$CGI_ridx" | cut -d',' -f$i)
		widx=$(printf '%s' "$CGI_widx" | cut -d',' -f$i)

		if echo "$ridx" | grep -q "/"; then
			ridx_2g=$(echo "$ridx" | cut -d'/' -f1)
			widx_2g=$(echo "$widx" | cut -d'/' -f1)
			ridx_5g=$(echo "$ridx" | cut -d'/' -f2)
			widx_5g=$(echo "$widx" | cut -d'/' -f2)

			errmsg1=`${FLOWEYE} sactask add apidstr=,${apid} debugstr="pappiw&modwlan&ridx=${ridx_2g}&widx=${widx_2g}&aclmode=${CGI_mode}" >>/dev/null 2>&1`
			if [ $? -ne 0 ]; then
				all_success=false
				last_errmsg="$errmsg1"
				WEB_LOGGER "SSIDڰģʽʧ" "apid=${apid} aclmode=${CGI_mode} ridx=${ridx_2g}"
			fi

			errmsg2=`${FLOWEYE} sactask add apidstr=,${apid} debugstr="pappiw&modwlan&ridx=${ridx_5g}&widx=${widx_5g}&aclmode=${CGI_mode}" >>/dev/null 2>&1`
			if [ $? -ne 0 ]; then
				all_success=false
				last_errmsg="$errmsg2"
				WEB_LOGGER "SSIDڰģʽʧ" "apid=${apid} aclmode=${CGI_mode} ridx=${ridx_5g}"
			fi
		else
			errmsg=`${FLOWEYE} sactask add apidstr=,${apid} debugstr="pappiw&modwlan&ridx=${ridx}&widx=${widx}&aclmode=${CGI_mode}" >>/dev/null 2>&1`
			if [ $? -ne 0 ]; then
				all_success=false
				last_errmsg="$errmsg"
				WEB_LOGGER "SSIDڰģʽʧ" "apid=${apid} aclmode=${CGI_mode} ridx=${ridx}"
			fi
		fi

		i=$((i + 1))
	done

	if [ "$all_success" = "true" ]; then
		retjson 0 "ɹ"
	else
		retjson 1 "ʧ: ${last_errmsg}"
	fi
}


load_scan_list()
{

	total=0 dot=","
	[ "${CGI_page}" = "" ] && CGI_page=1
	[ "${CGI_limit}" = "" ] && CGI_limit=100
	end=$((${CGI_page} * ${CGI_limit}))
	start=$((${end} - ${CGI_limit}))

	result=`${FLOWEYE} sac_scan list | grep "${CGI_keyword}"`

	if [ "${result}" = "" ]; then
		echo "{\"total\":0,\"data\":[]}"
		return 0
	fi

	printf "{\"data\":[{}"

	while read ap channel bssid ssid
	do
	    total=$((${total}+1))
		[ ${total} -le ${start} -o ${total} -gt ${end} ] && continue
		ap="${ap#*=}"
		channel="${channel#*=}"
		bssid="${bssid#*=}"
		ssid="${ssid#*=}"
		
		${FLOWEYE} sac list | while read ver devtype apid name other
		do
			[ "${ap}" != "${apid}" ] && continue

			apname=`${FLOWEYE} iconv utf8togb2312 ${name}`
			ssid=`${FLOWEYE} iconv utf8togb2312 ${ssid}`

			printf "%s{" "$dot"
			printf "\"apname\":\"%s\"," "$(printf "%s" "$apname" | sed 's/["\\]/\\&/g')"
			printf "\"ssid\":\"%s\"," "$(printf "%s" "$ssid" | tr -d '[:cntrl:]')"
			printf "\"bssid\":\"%s\"," "$bssid"
			printf "\"channel\":\"%s\"" "$channel"
			printf "}"
		done
	done << EOF
${result}
EOF

	printf "],\"total\":%d}" ${total};
}


set_scan_conf()
{
	apidstr="$1"
	apname="$2"
	ridx="$3"
	action="$4"
	msg="$5"

	conf_dir="${CONFDIR:-/usr/panaetc/App/sac}"
	conf_file="${conf_dir}/scan.conf"

	mkdir -p "$conf_dir"
	touch "$conf_file"

	ts="$(date '+%Y-%m-%d %H:%M:%S')"

	new_line="{\"apidstr\": \"$apidstr\", \"apname\": \"$apname\", \"ridx\": \"$ridx\", \"action\": \"$action\", \"time\": \"$ts\", \"msg\": \"$msg\"}"

	{ echo "$new_line"; head -n 255 "$conf_file"; } > "$conf_file.tmp"
	mv "$conf_file.tmp" "$conf_file"
}


load_scan_conf()
{
	local file="/usr/panaetc/App/sac/scan.conf"

	if [ -f "$file" ]; then
		printf "["
		awk 'NR>1{printf ","}1' "$file"
		printf "]"
	else
		retjson 1 "ûд洢"
	fi
}


start_scan()
{
	local ridx="$CGI_ridx"

	if [ "$ridx" = "-1" ]; then
		retjson 0 "ridx=-1"
		return
	fi

	set_scan_conf "$CGI_apidstr" "$CGI_apname" "$ridx" "start_scan" "autoscan&ridx=${ridx}"

	errmsg=""

	out=$(${FLOWEYE} sactask add apidstr=,${CGI_apidstr} \
			debugstr="pappiw&autoscan&ridx=${ridx}" \
			2>&1 >/dev/null)
	errmsg="${errmsg}${out}"

	if [ $? -eq 0 ]; then
		retjson 0 "ɹ"
	else
		WEB_LOGGER "ʼɨǷssid" "apidstr=${CGI_apidstr} ridx=${CGI_ridx}"
		retjson 1 "ʧ:${errmsg}"
	fi
}


stop_scan()
{
	local ridx="$CGI_ridx"

	if [ "$ridx" = "-1" ]; then
		retjson 0 "ridx=-1"
		return
	fi

	set_scan_conf "$CGI_apidstr" "$CGI_apname" "$ridx" "stop_scan" "stopscan&ridx=${ridx}"

	errmsg=""

	out=$(${FLOWEYE} sactask add apidstr=,${CGI_apidstr} \
			debugstr="pappiw&stopscan&ridx=${ridx}" \
			2>&1 >/dev/null)
	errmsg="${errmsg}${out}"

	if [ $? -eq 0 ]; then
		retjson 0 "ɹ"
	else
		WEB_LOGGER "ֹͣɨǷssid" "apidstr=${CGI_apidstr} ridx=${CGI_ridx}"
		retjson 1 "ʧ:${errmsg}"
	fi
}


start_wids()
{
	local ridx="${CGI_ridx}"

	if [ "$ridx" = "-1" ]; then
		retjson 0 "ridx=-1"
		return
	fi

	errmsg=""

	debugstr="startwids&ridx=${ridx}"
	[ -n "${CGI_ssid}" ]     && debugstr="${debugstr}&ssid=${CGI_ssid}"
	[ -n "${CGI_channel}" ]  && debugstr="${debugstr}&channel=${CGI_channel}"
	[ -n "${CGI_bssid}" ]    && debugstr="${debugstr}&bssid=${CGI_bssid}"

	set_scan_conf "$CGI_apidstr" "$CGI_apname" "$ridx" "start_wids" "${debugstr}"

	out=$(${FLOWEYE} sactask add apidstr=,${CGI_apidstr} \
		debugstr="pappiw&${debugstr}" \
		2>&1 >/dev/null)
	errmsg="${errmsg}${out}"

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


stop_wids()
{
	local ridx="${CGI_ridx}"

	if [ "$ridx" = "-1" ]; then
		retjson 0 "ridx=-1"
		return
	fi

	set_scan_conf "$CGI_apidstr" "$CGI_apname" "$ridx" "stop_wids" "stopwids&ridx=${ridx}"

	errmsg=""

	out=$(${FLOWEYE} sactask add apidstr=,${CGI_apidstr} \
			debugstr="pappiw&stopwids&ridx=${ridx}" \
			2>&1 >/dev/null)
	errmsg="${errmsg}${out}"

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


scan_ap_list()
{
	dot=""
	echo "["
	floweye sac list | while read -r line; do
		[ -z "$line" ] && continue

		local id=$(echo "$line" | awk '{print $3}')
		local apname=$(echo "$line" | awk '{print $4}')

		echo "${dot}"
		echo "{\"id\":\"$id\",\"apname\":\"$apname\"}"
		[ "${dot}" = "" ] && dot=","
	done
	echo "]"
}


load_wids_list()
{
	result=`${FLOWEYE} sac_scan list`

	if [ "${result}" = "" ]; then
		echo "{\"data\":"SSID":[],"BSSID":[],"Channel":[]}}"
		return 0
	fi

	ssid_list=""
	bssid_list=""
	channel_list=""

	while read apid channel bssid ssid
	do
		channel="${channel#*=}"
		bssid="${bssid#*=}"
		ssid="${ssid#*=}"

		ssid=`${FLOWEYE} iconv utf8togb2312 ${ssid}`
		ssid=$(printf "%s" "$ssid" | tr -d '[:cntrl:]' | cut -d '=' -f2)

		[ -n "$ssid_list" ] && ssid_list="$ssid_list,"
		ssid_list="${ssid_list}\"$ssid\""

		[ -n "$bssid_list" ] && bssid_list="$bssid_list,"
		bssid_list="${bssid_list}\"$bssid\""

		[ -n "$channel_list" ] && channel_list="$channel_list,"
		channel_list="${channel_list}\"$channel\""
	done << EOF
${result}
EOF

	ssid_array=$(echo "$ssid_list" | awk -F',' '
	{
		for(i=1; i<=NF; i++) {
			if($i != "" && !($i in seen)) {
				seen[$i]=1;
				if(sep) printf ",";
				printf "%s", $i;
				sep=1;
			}
		}
	}')

	bssid_array=$(echo "$bssid_list" | awk -F',' '
	{
		for(i=1; i<=NF; i++) {
			if($i != "" && !($i in seen)) {
				seen[$i]=1;
				if(sep) printf ",";
				printf "%s", $i;
				sep=1;
			}
		}
	}')

	channel_array=$(echo "$channel_list" | awk -F',' '
	{
		for(i=1; i<=NF; i++) {
			if($i != "" && !($i in seen)) {
				seen[$i]=1;
				if(sep) printf ",";
				printf "%s", $i;
				sep=1;
			}
		}
	}')

	printf '{"data":{"SSID":[%s],"BSSID":[%s],"Channel":[%s]}}' \
		"$ssid_array" "$bssid_array" "$channel_array"
}

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
		;;

	"load_scan_list")
		retjson 0 "OK" "`load_scan_list`"
		;;

	"scan_ap_list")
		retjson 0 "OK" "`scan_ap_list`"
		;;

	"load_scan_conf")
		retjson 0 "OK" "`load_scan_conf`"
		;;

	"start_scan")
		action_check
		start_scan
		;;

	"stop_scan")
		action_check
		stop_scan
		;;

	"start_wids")
		action_check
		start_wids
		;;

	"stop_wids")
		action_check
		stop_wids
		;;

	"load_wids_list")
		retjson 0 "OK" "`load_wids_list`"
		;;

	*)

	retjson 1 "UNKNOW_ACTION"
	;;
esac
