#!/bin/sh

if [ "${PALANG}" = "en" ]; then
	LANG_001="NO_PASSWORD_FILE"
	LANG_002="PASSWORD_ERROR"
	LANG_003="Permission denied"
fi


RAMDISK=/usr/ramdisk

. ${RAMDISK}/PG.conf
. ${RAMDISK}/etc/panabit.inf

TMPDIR=${RAMDISK}/tmp
WEB_DOWNLOAD=${TMPDIR}/web_download

EVENTFILE=${PGETC}/log/pending_events
CURL="/usr/bin/curl"
FLOWEYE="${RAMDISK}/bin/floweye"
NTMEYE="${RAMDISK}/bin/ntmeye"
ESCTOOL="${RAMDISK}/bin/esctool"
URLENCODE="${RAMDISK}/bin/urlencode"
WEBUICTL="${RAMDISK}/app/webui/appctrl"
USER_DIR="${RAMDISK}/etc/webuser"


print_json_head()
{
	printf "Content-Type:application/json;charset=gb2312\r\n"
	printf "\r\n"
}


retjson()
{
	print_json_head
	
	if [ "${3}" = "" ]; then
		echo -n "{ \"code\": ${1}, \"msg\": \"${2}\", \"data\": \"\"}"
	else
		echo -n "{ \"code\": ${1}, \"msg\": \"${2}\", \"data\": ${3}}"
	fi

	exit 0
}


print_json_file()
{
	awk '{gsub("\\t", "", $0); printf "%s ", $0}' $1
}


admin_check()
{
	level=1
	aes_name=`${ESCTOOL} -e ${PANABIT_USER}`
	ufile=${USER_DIR}/${aes_name}

	#  ⲿ˺Ų
	[ ! -f ${ufile} ] && retjson 1 "${LANG_003:=Ȩ޲}!"

	. ${ufile}
	[ "${level}" != 0 ] && retjson 1 "${LANG_003:=Ȩ޲}!"
	[ "${CGI_password}" != "" -a "${CGI_password}" != "${password}" ] && retjson 1 "${LANG_002:=}!"
}


action_check()
{
	level=1
	aes_name=`${ESCTOOL} -e ${PANABIT_USER}`
	ufile=${USER_DIR}/${aes_name}

	# ܶԽⲿ˺ţradius, ldap
	[ -f ${ufile} ] && . ${ufile}

	[ "${level}" -gt 1 ] && retjson 1 "${LANG_003:=Ȩ޲}!"
}


sync_floweye()
{
	enable=0
	SYNC_CONF="${PGETC}/web/config_sync.conf"
	[ -f "${SYNC_CONF}" ] && . ${SYNC_CONF}

	[ "${enable}" != "1" ] && return

	sync_file="${RAMDISK}/tmp/ipe_syncd/`date +%s`_$$.syncd"
	mkdir -p ${RAMDISK}/tmp/ipe_syncd
	${ESCTOOL} -e "floweye ${*}" > ${sync_file}
}


WEB_LOGGER()
{
	local logfile="${DATAPATH}/web_`date +%Y.%m.%d`.log"
	local curtime=`date +%Y.%m.%d/%H:%M:%S`

	echo "${curtime} ${REMOTE_ADDR} ${PANABIT_USER} $1 $2" >> ${logfile}
	sync
	sync
}


chart_dump()
{
	[ $# -ne 3 ] && retjson 1 "INV_ARGS"

	hours="${1}"
	tmend="${2}"
	tag="${3}"
	
	${FLOWEYE} chart dump json=1 hours=${hours} tmend=${tmend} ${tag}
}


XSS_FILTER()
{
	xss_verify_enable="off"
	xss_verify_mode="black"
	xss_verify_rule="<>/()"

	[ -f "/usr/ramdisk/web_secure.conf" ] && . /usr/ramdisk/web_secure.conf
	
	[ "${xss_verify_enable}" = "off" ] && return

	file_upload=`echo "${CONTENT_TYPE}" | grep "multipart/form-data"`
	[ "${file_upload}" != "" ] && return

	[ "${URLENCODE}" = "" -a "${QUERY_STRING}" = "" ] && return

	get_str=`${URLENCODE} -d ${QUERY_STRING}`
	post_str=`${URLENCODE} -d  ${POST_STRING}`

	for line in "${get_str}" "${post_str}"
	do
		for str in `echo "${line}" | tr "&" " "`
		do
			exist=`echo ${str} | grep "=" `
			[ "${exist}" = "" ] && retjson 1 "INV_STR!"

			awk -v str="${str}" -v mode="${xss_verify_mode}" -v rule=${xss_verify_rule} \
			'BEGIN{
				for(i = 1; i <= length(str); i++){
					char = substr(str, i, 1);

					if (char == "`") exit 1;
					if (char == " " || char == "-" || char == "=" || char == "_" || char == ".") continue;

					if (mode == "black") {
						for(x = 1; x < length(rule); x++){
							r = substr(rule, x, 1);
							if(char == r) exit 1;
						}
					} else {
						r = sprintf("[%s]", rule);
						if (char !~ r) exit 1;
					}
				}
			}'

			[ $? -ne 0 ] && retjson 1 "INV_STR!"
		done
	done
}


XSS_FILTER
