#!/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}`

	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}"
	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//-/:}"
	IFS=',' read -ra apid_array <<< "$CGI_apidstr"
	IFS=',' read -ra ridx_array <<< "$CGI_ridx"
	IFS=',' read -ra widx_array <<< "$CGI_widx"

	if [ ${#apid_array[@]} -ne ${#ridx_array[@]} ] || [ ${#apid_array[@]} -ne ${#widx_array[@]} ]; then
		retjson 1 "ƥ: apidstr=${#apid_array[@]} ridx=${#ridx_array[@]} widx=${#widx_array[@]}"
		return
	fi

	all_success=true
	last_errmsg=""

	i=0
	while [ $i -lt ${#apid_array[@]} ]; do
		apid="${apid_array[$i]}"
		ridx="${ridx_array[$i]}"
		widx="${widx_array[$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="${CGI_mac//-/:}"
	IFS=',' read -ra apid_array <<< "$CGI_apidstr"
	IFS=',' read -ra ridx_array <<< "$CGI_ridx"
	IFS=',' read -ra widx_array <<< "$CGI_widx"

	if [ ${#apid_array[@]} -ne ${#ridx_array[@]} ] || [ ${#apid_array[@]} -ne ${#widx_array[@]} ]; then
		retjson 1 "ƥ: apidstr=${#apid_array[@]} ridx=${#ridx_array[@]} widx=${#widx_array[@]}"
		return
	fi

	all_success=true
	last_errmsg=""

	i=0
	while [ $i -lt ${#apid_array[@]} ]; do
		apid="${apid_array[$i]}"
		ridx="${ridx_array[$i]}"
		widx="${widx_array[$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()
{
	IFS=',' read -ra apid_array <<< "$CGI_apidstr"
	IFS=',' read -ra ridx_array <<< "$CGI_ridx"
	IFS=',' read -ra widx_array <<< "$CGI_widx"

	if [ ${#apid_array[@]} -ne ${#ridx_array[@]} ] || [ ${#apid_array[@]} -ne ${#widx_array[@]} ]; then
		retjson 1 "ƥ: apidstr=${#apid_array[@]} ridx=${#ridx_array[@]} widx=${#widx_array[@]}"
		return
	fi

	all_success=true
	last_errmsg=""

	i=0
	while [ $i -lt ${#apid_array[@]} ]; do
		apid="${apid_array[$i]}"
		ridx="${ridx_array[$i]}"
		widx="${widx_array[$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()
{
	local apidstr="$1"
	local apname="$2"
	local ridx="$3"

	mkdir -p ${CONFDIR}
	touch /usr/panaetc/App/sac/scan.conf

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

	echo "{\"apidstr\": \"$apidstr\", \"apname\": \"$apname\", \"ridx\": \"$ridx\", \"time\": \"$ts\"}" \
		>> "/usr/panaetc/App/sac/scan.conf"
}


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 ridx1=$(echo "$CGI_ridx" | cut -d'/' -f1)
	local ridx2=$(echo "$CGI_ridx" | cut -d'/' -f2)

	set_scan_conf "$CGI_apidstr" "$CGI_apname" "$CGI_ridx"

	if [ "$ridx1" = "-1" ] && [ "$ridx2" = "-1" ]; then
		retjson 0 "ɹ"
		return
	fi

	errmsg=""

	for ridx in $ridx1 $ridx2; do
		if [ "$ridx" != "-1" ]; then
			out=$(${FLOWEYE} sactask add apidstr=,${CGI_apidstr} \
				debugstr="pappiw&autoscan&ridx=${ridx};pappiw&reload" \
				2>&1 >/dev/null)
			errmsg="${errmsg}${out}"
		fi
	done

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

stop_scan()
{
	local ridx1=$(echo "$CGI_ridx" | cut -d'/' -f1)
	local ridx2=$(echo "$CGI_ridx" | cut -d'/' -f2)

	sed -i "${CGI_id}d" "/usr/panaetc/App/sac/scan.conf"

	if [ "$ridx1" = "-1" ] && [ "$ridx2" = "-1" ]; then
		retjson 0 "ɹ"
		return
	fi

	errmsg=""

	for ridx in $ridx1 $ridx2; do
		if [ "$ridx" != "-1" ]; then
			out=$(${FLOWEYE} sactask add apidstr=,${CGI_apidstr} \
				debugstr="pappiw&stopscan&ridx=${ridx};pappiw&reload" \
				2>&1 >/dev/null)
			errmsg="${errmsg}${out}"
		fi
	done

	if [ $? -eq 0 ]; then
		retjson 0 "ɹ"
	else
		WEB_LOGGER "ʼɨǷssid" "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 "]"
}


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
		;;
    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
