#!/bin/sh

. ../common/ajax_common

notice_file="/usr/ramdisk/tmp/notice_ui.conf"


if [ "${PALANG}" = "en" ]; then
	LANG001="SSH Servere Off"	#SSHرճɹ
	LANG002="SSH Servere On"	#SSHɹ
	LANG003="No Such FILE"		#ļ
	LANG004="Error"				#ʧ
	LANG005="Del_DUMP_FILE"		#ɾdumpļ
	LANG006="Success"			#ɾɹ
	LANG007="SSH_Server_Off"	#ֶرSSH
	LANG008="SSH_Server_On"		#ֶSSH
fi


load_sys_runstat()
{
	curtime=`date +%s`
	if [ "`uname`" = "Linux" ]; then
		boottime=`cat /proc/stat | grep "^btime" | cut -d' ' -f2`
	else
		boottime=`sysctl -n kern.boottime | cut -d',' -f1 | cut -d' ' -f4`
	fi

	SYSNAME="Panabit"
	[ -f "${PGETC}/sysname.conf" ] && . ${PGETC}/sysname.conf

	if [ `uname` = "Linux" ]; then
		err=`ss -l4to | grep ":ssh"`
	else
		err=`sockstat -4l -P tcp | grep ssh`
	fi

	sshstat="on"
	[ "${err}" = "" ] && sshstat="off"

	crash="no"
	dump_file=`find /var/tmp/ -name "panaos.dump*"`
	[ "${dump_file}" != "" ] && crash="yes"

	notice="no"
	message_count=0
	if [ -f ${notice_file} ]; then
		message_count=$(grep -c " 0 " ${notice_file})
	fi
	[ ${message_count} -gt 0 ] && notice="yes"

	login_level=1
	ckfile=`get_cookie_file`
	[ -f "${ckfile}" ] && . ${ckfile}

	printf "{"
	printf "\"sysrun\":\"$((${curtime} - ${boottime}))\""
	printf ",\"sshstat\":\"${sshstat}\""
	printf ",\"crash\":\"${crash}\""
	printf ",\"notice\":\"${notice}\""
	printf ",\"login_user\":\"${PANABIT_USER}\""
	printf ",\"user_level\":\"${login_level}\""
	printf ",\"sysname\":\"${SYSNAME}\""
	printf "}"
	
	# ȫ״̬£Զˢʱ
	if [ -f "${PGETC}/webexit.conf" ]; then
		. ${PGETC}/webexit.conf
		if [ "${fullexit}" = "1" -a "${CGI_fullscreen}" = "1" ]; then
			now=`date +"%Y%m%d%H%M%S"`
			ckfile=`get_cookie_file`
			[ "${ckfile}" != "" ] && touch -m ${now} ${ckfile}
		fi
	fi
}


logout()
{
	ckfile=`get_cookie_file`

	[ -f ${ckfile} ] && rm ${ckfile}

	WEB_LOGGER "˳webҳ"
	retjson 0 "OK"
}


sshenable()
{
	os=`uname`
	
	if [ "${os}" = "Linux" ]; then
		if [ "${CGI_stat}" = "off" ]; then
			service sshd stop >/dev/null 2>&1 &
			/etc/rc.d/init.d/sshd stop >/dev/null 2>&1 &
			WEB_LOGGER "${LANG007:=ֶرSSH}"
			retjson 0 "${LANG001:=SSHرճɹ}"
		else
			service sshd restart >/dev/null 2>&1 &
			/etc/rc.d/init.d/sshd restart  >/dev/null 2>&1 &
			WEB_LOGGER "${LANG008:=ֶSSH}"
			retjson 0 "${LANG002:=SSHɹ}"
		fi
	elif [ "${os}" = "FreeBSD" ]; then
		if [ "${CGI_stat}" = "off" ]; then
			/etc/rc.d/sshd onestop >/dev/null 2>&1
			WEB_LOGGER "${LANG007:=ֶرSSH}"
			retjson 0 "${LANG001:=SSHرճɹ}"
		else
			/etc/rc.d/sshd onerestart >/dev/null 2>&1
			WEB_LOGGER "${LANG008:=ֶSSH}"
			retjson 0 "${LANG002:=SSHɹ}"
		fi
	fi
}


load_event()
{
	if [ ! -f ${notice_file} ]; then
		printf "[]"
		return
	fi
	
	sort -nrk 4 ${notice_file} | awk  -F" " \
	'BEGIN{
		dot="";
		printf "[";
	} {
		printf "%s", dot;
		printf "{";
		printf "\"module\":\"%s\",", $1;
		printf "\"num\":\"%s\",", $2;
		printf "\"read\":\"%s\",", $3;
		printf "\"time\":\"%s\"", $4;
		printf "}";
		if(dot == "") dot=",";

	}END{
		printf "]";
	}'
}


set_read()
{
	notice_file_new="/usr/ramdisk/tmp/notice_ui_new.conf"
	if [ ! -f ${notice_file} ]; then
		return
	fi
	awk -v time=${CGI_time} \
	'{ 
		if ($4 == time) { 
			$3 = 1
		} 
		print $0 
	}' ${notice_file} > ${notice_file_new} && mv ${notice_file_new} ${notice_file}
	
	retjson 0 "OK"
}


clear_event()
{
	[ -f "${notice_file}" ] && rm -rf ${notice_file}

	retjson 0 "OK"
}


load_dump_file_list()
{
	ls -lh /var/tmp/panaos.dump* | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		split($9, name, "/");

		printf "%s", dot;
		printf "{";
		printf "\"time\":\"%s %s %s\",", $6, $7, $8;
		printf "\"size\":\"%s\",", $5;
		printf "\"name\":\"%s\"", name[4];
		printf "}";
		if (dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


rmv_dump_file()
{
	[ "${CGI_name}" = "" ] && retjson 1 "NO_FILE"
	
	file_path="/var/tmp/${CGI_name}"
	
	[ ! -f ${file_path} ] && retjson 1 "${LANG003:=ļ}"
	
	errmsg=`rm ${file_path}`
	
	if [ $? -ne 0 ]; then
		retjson 1 "${LANG004:=ʧ}:${errmsg}"
	else
		WEB_LOGGER "${LANG005:=ɾdumpļ}", "name=${CGI_name}"
		retjson 0 "${LANG006:=ɾɹ}"
	fi
}


cat_dump_file()
{
	[ "${CGI_name}" = "" ] && retjson 1 "NO_FILE"
	
	file_path="/var/tmp/${CGI_name}"

	[ ! -f ${file_path} ] && retjson 1 "${LANG003:=ļ}"

	awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s\"%s\"", dot, $0;
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}' ${file_path} 
}


down_dump_file()
{
	[ "${CGI_name}" = "" ] && retjson 1 "NO_FILE"
	
	file_path="/var/tmp/${CGI_name}"
	mkdir -p ${WEB_DOWNLOAD}
	cp "${file_path}" "${WEB_DOWNLOAD}/${CGI_name}.txt"
	
	retjson 0 "OK" "{\"file_name\":\"${CGI_name}.txt\"}"
}


load_pcap_file_list()
{
	ls -lh /usr/ramdisk/var/*.pcap | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		split($9, name, "/");

		printf "%s", dot;
		printf "{";
		printf "\"time\":\"%s %s %s\",", $6, $7, $8;
		printf "\"size\":\"%s\",", $5;
		printf "\"name\":\"%s\"", name[5];
		printf "}";
		if (dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


down_pcap_file()
{
	[ "${CGI_name}" = "" ] && retjson 1 "NO_FILE"

	file_path="/usr/ramdisk/var/${CGI_name}"
	mkdir -p ${WEB_DOWNLOAD}
	cp "${file_path}" "${WEB_DOWNLOAD}/${CGI_name}"

	retjson 0 "OK" "{\"file_name\":\"${CGI_name}\"}"
}


rmv_pcap_file()
{
	[ "${CGI_name}" = "" ] && retjson 1 "NO_FILE"
	
	file_path="/usr/ramdisk/var/${CGI_name}"
	
	[ ! -f ${file_path} ] && retjson 1 "${LANG003:=ļ}"
	
	errmsg=`rm ${file_path}`
	
	if [ $? -ne 0 ]; then
		retjson 1 "${LANG004:=ʧ}:${errmsg}"
	else
		WEB_LOGGER "${LANG005:=ɾ쳣ݰ}", "name=${CGI_name}"
		retjson 0 "${LANG006:=ɾɹ}"
	fi
}


switch_lang()
{
	if [ "${CGI_lang}" != "ch" -a "${CGI_lang}" != "en" ]; then
		retjson 1 "INV_LANG"
	fi

	echo "PALANG=${CGI_lang}" > ${PGETC}/palang.conf

	retjson 0 "ɹ"
}


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

	"logout")
		logout
		;;
	
	"sshenable")
		action_check
		sshenable
		;;
		
	"load_event")
		print_json_head
		load_event
		;;

	"set_read")
		action_check
		set_read
		;;

	"clear_event")
		action_check
		clear_event
		;;
	
	"load_dump_file_list")
		retjson 0 "OK" "`load_dump_file_list`"
		;;
		
	"rmv_dump_file")
		action_check
		rmv_dump_file
		;;

	"cat_dump_file")
		retjson 0 "OK" "`cat_dump_file`"		
		;;
	
	"down_dump_file")
		action_check
		down_dump_file
		;;

	"load_pcap_file_list")
		retjson 0 "OK" "`load_pcap_file_list`"
		;;

	"down_pcap_file")
		action_check
		down_pcap_file
		;;

	"rmv_pcap_file")
		action_check
		rmv_pcap_file
		;;

	"switch_lang")
		switch_lang
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;

esac
