<?
include("_switch.php");
if(show_lang()) require("config_file/lang_config");
else require("config_file/lang_eng");

function fn_DateTime($value){
//	if (ereg ("([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})", $value, $regs))
//		$value = $regs[1]."-".$regs[2]."-".$regs[3]." ".$regs[4].":".$regs[5].":".$regs[6];
//	else
//		$value = "0000-00-00 00:00:00";
	return $value;
}

function SubString($Column){
	if ((strpos($Column,"&lt;"))&&(strpos($Column,"&gt;"))){
		return substr($Column,strpos($Column,"&lt;")+4,((strrpos($Column,"&gt;")-4)-strpos($Column,"&gt;")));
	}
	return $Column;
}

function convert($value){
	if ($value == '') return $value;

    $value = str_replace("'", "&#039",$value);
	$value = str_replace("\"", "&quot",$value);
	$value = str_replace("\"", "'",$value);
	$value = str_replace(">", "&gt;",$value);
	$value = str_replace("<", "&lt;",$value);

	return $value;

}

function reconvert($value){
	if ($value == '') return $value;

    $value = str_replace("&#039","'", $value);
	$value = str_replace("&quot","\"",$value);
    //$value = str_replace("，",",",$value);
	$value = str_replace("&gt;",">",$value);
	$value = str_replace("&lt","<",$value);
	return $value;

}


function fn_NotShowIP(){
	$not_show_ip = "" ;
	$strSQL="Select ip from NOTSHOW";
	$intResult=mysql_query($strSQL);
	$row_num = mysql_num_rows($intResult);
	$index = 0;
	while($row=mysql_fetch_array($intResult)){
		   
	   if ($index == ($row_num-1)){ 
		   $ip = "'".$row["ip"]."'";
	   }
	   else{
		   $ip = "'".$row["ip"]."',";
	   }	 
	   $not_show_ip=$not_show_ip.$ip;
	   $index++;
	}
	if ($not_show_ip=="") $not_show_ip = "''";
	return $not_show_ip;
}

function fn_NotShowMAC(){
	$not_show_mac = "";
	if (!file_exists("/opt/local/apache/htdocs/sh/mac"))
		system(" echo '' > /opt/local/apache/htdocs/sh/mac");
	else{
		$arr = file("/opt/local/apache/htdocs/sh/mac");
		$index = 0;
		for ($i=0;$i<count($arr);$i++){
		   if ($index == (count($arr)-1)){ 
			   $mac = "'".trim($arr[$i])."'";
		   }
		   else{
			   $mac = "'".trim($arr[$i])."',";
		   }	 
		   $not_show_mac=$not_show_mac.$mac;
		   $index++;
		}
	}
	if ($not_show_mac=="") $not_show_mac = "''";
	return $not_show_mac;
}

//--所屬群組的IP
function fn_IPcontrol($groupno){

	$ip_control_ip = "" ;
	$strSQL="Select ip from IPCONTROL where groupno='$groupno'";

	$intResult=mysql_query($strSQL);
	$row_num = mysql_num_rows($intResult);

	$index = 0;
	while($row=mysql_fetch_array($intResult)){
		   
	   if ($index == ($row_num-1)){ 
		   $ip = "'".$row["ip"]."'";
	   }
	   else{
		   $ip = "'".$row["ip"]."',";
	   }	 
	   $ip_control_ip = $ip_control_ip.$ip;
	   $index += 1;
	}
	if ($ip_control_ip=="") $ip_control_ip = "''";
	return $ip_control_ip;
}

//--時
function fn_H($h){
	$strOpt = $strOpt."<option value=\"\" selected></option>";
	for ($i=0; $i<=23; $i++){
		if (strlen($i) == 1) $strI = "0".$i; else $strI = $i;
		if ($strI == $h){
			$strOpt = $strOpt."<option value=\"".$strI."\" selected>".$strI."</option>";
		}
		else{
			$strOpt = $strOpt."<option value=\"".$strI."\">".$strI."</option>";
		}
	}
	return $strOpt;
}

//--分鐘
function fn_M($m){
	$strOpt = $strOpt."<option value=\"\" selected></option>";
	for ($i=0; $i<=59; $i++){
		if (strlen($i) == 1) $strI = "0".$i; else $strI = $i;
		if ($strI == $m){
			$strOpt = $strOpt."<option value=\"".$strI."\" selected>".$strI."</option>";
		}
		else{
			$strOpt = $strOpt."<option value=\"".$strI."\">".$strI."</option>";
		}
	}
	return $strOpt;
}

//--每月下拉選項
function fn_optMonth($mon){
	global $mon;
	$arrMonth = array("","January","February","March","April","May","June","July","August","September","October","November","December");
	if (trim($mon) == "*"){ 
		$optMonth ="<option value =\"*\" selected>".$arrMonth[0]."</option>";
	}
	else{
		$optMonth ="<option value =\"*\">".$arrMonth[0]."</option>";
	}
	for ($i=1;$i<=12;$i++){
		if($mon == $i){
			$optMonth = $optMonth."<option value = \"".$i."\" //selected>".$arrMonth[$i]."</option>";
		}
		else{
			$optMonth = $optMonth."<option value = \"".$i."\">".$arrMonth[$i]."</option>";
		}
	}
	return $optMonth;
}

//--每天下拉選項
function fn_optDay(){
	global $day;
	if (trim($day) == "*"){ 
		$optDay ="<option value =\"*\" selected></option>";
	}
	else{
		$optDay ="<option value =\"*\"></option>";
	}
	for ($i=1;$i<=31;$i++){
		if ($day == $i){
			$optDay = $optDay."<option value = \"".$i."\" selected>".$i."</option>";
		}
		else{
			$optDay = $optDay."<option value = \"".$i."\">".$i."</option>";
		}
	}
	return $optDay;
}

//--每週下拉選項
function fn_optWeek(){
	global $week;
	$arrWeek = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	if (trim($week) == "*"){ 
		$optWeek ="<option value =\"*\" selected></option>";
	}
	else{
		$optWeek ="<option value =\"*\"></option>";
	}
	
	if (trim($week) == "*"){ 
		for ($i=0;$i<=6;$i++){
			$optWeek = $optWeek."<option value = \"".$i."\">".$arrWeek[$i]."</option>";
		}
	}
	elseif (trim($week) == ''){ 
		for ($i=0;$i<=6;$i++){
			$optWeek = $optWeek."<option value = \"".$i."\">".$arrWeek[$i]."</option>";
		}
	}
	else{
		for ($i=0;$i<=6;$i++){
			if (trim($week) == $i){
				$optWeek = $optWeek."<option value = \"".$i."\" selected>".$arrWeek[$i]."</option>";
			}
			else{
				$optWeek = $optWeek."<option value = \"".$i."\">".$arrWeek[$i]."</option>";
			}
		}
	}
	return $optWeek;
}


//--每天下拉選項
function fn_Day($day){
	$optDay = "<option value = \"\"></option>";
	for ($i=1;$i<=31;$i++){
		if ($day == $i){
			$optDay = $optDay."<option value = \"".$i."\" selected>".$i."</option>";
		}
		else{
			$optDay = $optDay."<option value = \"".$i."\">".$i."</option>";
		}
	}
	return $optDay;
}

//--每週下拉選項
function fn_Week($week){
	//if (empty($week)) $week = 7;
	$arrWeek = array("","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	$optWeek = "<option value = \"\"></option>";
	for ($i=1;$i<=7;$i++){
		if ($week == $i){
			$optWeek = $optWeek."<option value = \"".$i."\" selected>".$arrWeek[$i]."</option>";
		}
		else{
			$optWeek = $optWeek."<option value = \"".$i."\">".$arrWeek[$i]."</option>";
		}
	}
	return $optWeek;
}




//--WEB SERVER
function fn_WebServer($_WEB){
	$strSQL = "Select * from WEBSERVER";
	$result = mysql_query($strSQL);

	$optWebServer = "<option value=\"\"></option>";
	while($row=mysql_fetch_array($result)){

		if ($_WEB == $row['_VALUE']){
			$optWebServer=$optWebServer."<option value=\"".$row['_VALUE']."\" selected>".$row['_NAME']."</option>";
		}
		else{
			$optWebServer=$optWebServer."<option value=\"".$row['_VALUE']."\">".$row['_NAME']."</option>";
		}
	}
	return $optWebServer;
}


//--產生IP下拉選單資料
function fn_SelectIP($_IP,$_GroupSession){
	if ($_GroupSession == '0'){
		$strSQL = "Select distinct ip,username From IPCONTROL Where ip <>''";
		$result=mysql_query($strSQL);	
	}
	else{
		$strSQL = "Select distinct ip,username From IPCONTROL Where groupno='$_GroupSession]'";
		$result=mysql_query($strSQL);	
    }

	$optIP = "<option value=\"\"></option>";
	while ($row=mysql_fetch_array($result)){
		if ($_IP == $row['ip']){
			$optIP=$optIP."<option value=\"$row[ip]\">".$row['ip']." -- ".$row['username']."</option>";
		}else{
			$optIP=$optIP."<option value=\"$row[ip]\">".$row['ip']." -- ".$row['username']."</option>";
		}
	}

	return $optIP;
}


//--組成群組下拉式選項資料
function Fn_listGroups($GroupNo){
	$strSQL = "Select GroupNo,GroupName From OPERATEGROUPS";

	$intResult = mysql_query($strSQL);

	while ($row=mysql_fetch_array($intResult)){
		if ($GroupNo == $row['GroupNo']){
			$listGroups = $listGroups."<option value=\"$row[GroupNo]\" selected>$row[GroupName]</option>";
		}
		else{
			$listGroups = $listGroups."<option value=\"$row[GroupNo]\">$row[GroupName]</option>";
		}
	}
	return $listGroups ;
}

//--Del 9大mode
function Fn_ListMode($DEL_MODE){
	$strSQL = "Select name From TABLETYPE where name not in('VOIP','P2P')";
	$result=mysql_query($strSQL);	

	$optMode = "<option value=\"\"></option>";
	while ($row=mysql_fetch_array($result)){

		//--for soho modify
		if (fn_soho()){
			if (($row['name'] == 'DYNAMIC')||($row['name'] == 'TELNET')) continue;
		}
        //--for soho modify
		
		$strModeName = ($row['name'] == 'DYNAMIC') ? "URL Content" : $row['name'];
		if ($DEL_MODE == $row['name']){
			$optMode=$optMode."<option value=\"$row[name]\" selected>".$strModeName."</option>";
		}
		else{
			$optMode=$optMode."<option value=\"$row[name]\">".$strModeName."</option>";
		}
	}
	return $optMode;
}

//--OpenRaw 是否 start (Yes:1 , No:0)
function fn_OpenRaw(){
	$i = 0;
	$fp=popen("ps aux|grep OpenRaw","r");
	while($line=fgets($fp,1024)){
		$i++;
	}
	pclose($fp);

	$rawFlag = ($i>2) ? 1 : 0;
	return $rawFlag;
}
?>
