<?php
//-----------本文件是omc php模块的函数库文件，包含一些常用的自定义函数----------
require("../../inc/config.inc");

function sftp_connect($remote_ip)
{
    $connection = ssh2_connect($remote_ip, 22);
    if (!$connection)
        echo "sftp_connect:Connect server[$remote_ip] failed<br>";
    return $connection;
}

function sftp_login($connection, $logName, $logPass)
{
    ssh2_auth_password($connection, $logName, $logPass);
    $sftp_key = ssh2_sftp($connection);
    return $sftp_key;
}

define("SFTP_ASCII", 0);
define("SFTP_BINARY", 1);
function sftp_put($key, $remote_file, $local_file, $mode)
{
    $read_mode = "r";
    $write_mode = "w";
    if ($mode == SFTP_BINARY) {
        $open_mode .= "b";
        $write_mode .= "b";
    }
    $handle = fopen($local_file, $read_mode);
    if (!$handle) {
        echo "sftp_put:Can not open $local_file<br>";
        return FALSE;
    }


	$stream=fopen("ssh2.sftp://".intval($key).$remote_file, $write_mode);
    if (!$stream) {
        echo "sftp_put:Can not open $remote_file<br>";
        return FALSE;
    }

    $content = stream_get_contents($handle);
    fwrite($stream, $content);

    fclose($handle);
    fclose($stream);

    return TRUE;
}

function sftp_get($key, $local_file, $remote_file, $mode)
{
	$read_mode="r";
	$write_mode="w";
	if($mode == SFTP_BINARY)
	{
		$open_mode.="b";
		$write_mode.="b";
	}
	$handle=fopen($local_file,$write_mode);
	if(!$handle)
	{
		echo "sftp_put:Can not open $local_file<br>";
		return FALSE;
	}
	echo "$remote_file=$read_mode";

	$stream=fopen("ssh2.sftp://$key"."$remote_file", $read_mode);
	if(!$stream)
	{
		echo "sftp_put:Can not open $remote_file<br>";
		return FALSE;
	}
	
	$content=stream_get_contents($stream);
	fwrite($handle,$content);
	
	fclose($handle);
	fclose($stream);
	
	return TRUE;
}

function checkWritePrivilege()
{
    global $userInfo;
    // echo "<pre>";
    // print_r($userInfo);
    // echo "<pre>";

    if ($userInfo['privilege'] > 2)
        return 1;
    else
        return 0;
}


function debug_log($file, $info)
{
    $handle = fopen($file, "a+");
    if ($handle == false)
        return;
    fwrite($handle, $info . "\n");
    fclose($handle);
}

//****************************************************************
//函数4 pageAccessChk($pageURL)
//函数说明:
//	检测用户是否可进入本页, 
//	如果用户的权限包含$page_url所对应的权限，则可进入该页面。
//
//变量说明: 
//	$page_url                                       
//                                                             
//返回值： 1 = 可 -1 = 否
//
//作者： 
//      
//修改注释：      
//NO.1      
//姓名：           
//时间：2002-03-26      
//修改说明：修改了权限定义方式，该函数改变了大部分
//NO.2      
//姓名：          
//时间：2002-04-09      
//修改说明：修改了权限定义方式，该函数改变了大部分
//----------------------------------------------------------

function pageAccessChk($pageURL)
{
    global $userInfo, $privilegeDefArr, $dirAccessDefArr;
    $funcDebug = 0;
    $privilege = $userInfo['privilege'];

    if ($funcDebug) echo "<br>privilege:$privilege";
    if (strlen($privilege) > 0) { //判断已登录的用户
        //检查该权限编号是否存在
        $isPass = 0;
        for ($k = 0; $k < sizeof($privilegeDefArr); $k++) {
            if ($privilege == $privilegeDefArr[$k]['permit']) {
                $isPass = 1;
                break;
            }
        }
        if ($isPass == 0) {
            if ($funcDebug) echo "<br>Invailed privilege";
            return (-1);
        }


        //对比判断该编号对应的权限能否访问该目录
        for ($p = 0; $p < sizeof($dirAccessDefArr[$privilege]); $p++) {
            $tmpDir = $dirAccessDefArr[$privilege][$p];
            $tmpLen = strlen($tmpDir);
            if ($funcDebug) echo "<br>$tmpDir == substr($pageURL,0,$tmpLen)";

            if ($tmpDir == substr($pageURL, 0, $tmpLen) && $tmpLen > 0) {
                return (1);
            }
        }
        //找不到,清掉这个无效权限代码
        if ($funcDebug) echo "1:$userInfo[privilege]";
        $userInfo['privilege'] = 0;
        if ($funcDebug) echo "2:$userInfo[privilege]";
        return (-1);
    } else {//判断未登录的用户
        for ($p = 0; $p < sizeof($dirAccessDefArr[0]); $p++) {
            $tmpDir = $dirAccessDefArr[0][$p];
            $tmpLen = strlen($tmpDir);
            $thisDir = substr($pageURL, 0, $tmpLen);
            if ($funcDebug) echo "<br>$tmpDir == $thisDir";
            if ($tmpDir == $thisDir && $tmpLen > 0) {
                return (1);
            }
        }
    }
    if ($funcDebug) echo "<br>??";

    return (1);
}


//****************************************************************
//函数5
//	function insertLog($operDesc,$operTypeNo='0',$OperNo='0')
//函数说明:
//	该函数用于记录日志                                                
//
//变量说明: 
//	$operDesc  ->操作说明
//	$operTypeNo  ->操作类型编号
//	$OperNo		->操作代码
//                                                             
//返回值：
//	无
//
//作者：     
//      
//修改注释：      
//NO.1      
//姓名：         
//时间：2003-04-09      
//修改说明：简化输入参数，缺省操作类型为unknown    
//----------------------------------------------------------

function insertLog($operDesc, $operTypeNo = '0', $OperNo = '0')
{
    global $userInfo, $link, $pubConn;
    $userName = $userInfo['ID'];
    $operDesc = isset($operDesc) ? stripslashes($operDesc) : '';  //解码
    $operDesc = addslashes($operDesc);                //解码

    $insertLogSql = "INSERT INTO sysOperLog 
		(operTypeNo,OperNo,remark,userName,datetime)
		VALUES
		('$operTypeNo','$OperNo','$operDesc','$userName',CURRENT_TIMESTAMP)
		";
    //echo "$insertLogSql";
    $result = mysqli_query($pubConn, $insertLogSql);
    //echo  mysqli_error($pubConn);
    if ($result < 0) {
        echo "Error:Insert the log failed";
    }
}


//****************************************************************
//函数6
//	change_percent($num)
//函数说明:
//	该函数用于把两个数相除后得到的小数变为精确到百分位的百分数                                           
//	如把0.91233456790变为91.23%                                             
//
//变量说明: 
//	$num  <= 1
//                                                             
//返回值：
//	一个带百分号的字符
//
//作者：    
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function change_percent($num)
{
    if ($num >= 1) {
        $num = 100;
    } else {
        $num = $num * 1000000;
        $num = round($num);
        $num = $num / 10000;
    }
    $string = $num . '%';
    return ($string);
}

//****************************************************************
//函数7
//	omc_keep_length($value,$add_value='0',$length='2',$position='1')
//函数说明:
//	该函数用于在字符串$value左边添$add_value,直到补足所需位数$length                                          
//	例如:omc_keep_length(2,0,2)变为02
//      常用于获取系统编号。                                       
//
//变量说明: 
//	$value		需要补足位数的字符
//	$add_value	需要用什么去补足位数，默认为'0'
//	$length		需要补足到多少位，默认为'2'系统编号的长度
//                                                             
//返回值：
//	被补足位数的字符串
//
//     
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function omc_keep_length($value, $add_value = '0', $length = '2', $position = '1')
{

    while (strlen($value) < $length) {
        if ($position)
            $value = $add_value . $value;
        else
            $value = $value . $add_value;

    }
    //echo "<br>value=$value";
    return ($value);
}


//****************************************************************
//函数8
//	omc_table_name($sys_type,$table_type)
//函数说明:
//	该函数通过$sys_type,$table_type生成该系统的各种相关表名
//	根据$sys_id确定系统编号
//	根据$table_type确定要返回的表名：
//	1、csta-h  -->(每小时)话务统计表
//	2、csta-q  -->(每15分钟)话务统计表
//
//
//
//变量说明: 
//	$sys_type	需要获取表名的系统
//	$table_type	该表名的类型字符串
//                                                             
//返回值：
//	所需的表名字符串
//
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function omc_table_name($sys_type, $table_type)
{
    switch ($table_type) {
        case 'csta-h':
            $sys_type = strtolower($sys_type);
            $table_name = $sys_type . 'Sum';
            break;
        case 'csta-q':
            $sys_type = strtolower($sys_type);
            $table_name = $sys_type . 'Detail';
            break;
        default:
            echo "<br>database's table name can not be maked,please check table type";
            break;
    }
    if ($table_name) {
        return ($table_name);
    } else {
        exit();
    }
}


//****************************************************************
//函数9
//	omc_sys_type2code($sys_type)
//函数说明:
//	由系统简写获取系统编号，通过查询系统定义表$sysconf_table获取
//	例:omc_sys_type2code(TS) = '04'
//
//变量说明: 
//	$sys_type
//                                                             
//返回值：
//	成功: 系统编号
//	失败: -1
//
//   
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function omc_sys_type2code($sysTypeName)
{
    global $omc_db, $sysconf_table;
    $sel_sql = "SELECT sysTypeNo FROM $sysconf_table  WHERE sysTypeName = '$sysTypeName'";
    //echo "<br>$sel_sql";
    $result = mysqli_query($pubConn, $sel_sql);
    if ($result > 0) {
        $result_rows = mysqli_fetch_array($result);
        //echo "<br>result_rows[sysTypeNo]=$result_rows[sysTypeNo]";
        return ($result_rows[sysTypeNo]);
    } else {
        echo mysqli_error($pubConn);
        echo "<br>Can not get this device's code by omc_sys_type2code($sysTypeName)";
        return (-1);
    }
}


//****************************************************************
//函数10
//	omc_sys_code2type($sys_code)
//函数说明:
//	由系统编号获取系统简写，通过查询系统定义表$sysconf_table获取
//
//
//变量说明: 
//	$sys_code
//                                                             
//返回值：
//	成功: 系统简写
//	失败: -1
//
//     
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function omc_sys_code2type($sysTypeNo)
{
    global $omc_db, $sysconf_table;
    $sel_sql = "SELECT sysTypeName FROM $sysconf_table  WHERE sysTypeNo = '$sysTypeNo'";
    //echo "$sel_sql";
    $result = mysqli_query($pubConn, $sel_sql);
    if ($result > 0) {
        $result_rows = mysqli_fetch_array($result);
        return ($result_rows[sysTypeName]);
    } else {
        echo mysqli_error($pubConn);
        echo "<br>Can not get this device's code by omc_sys_code2type($sysTypeNo)";
        return (-1);
    }

}


//****************************************************************
//函数11
//	get_subsys_total($sys_type)
//函数说明:
//	由系统类型获取系统每个系统的子系统总数，通过查询系统定义表$sysconf_table获取
//
//
//变量说明: 
//	$sys_type:系统类型
//                                                             
//返回值：
//	成功: 系统子系统个数
//	失败: -1
//
//
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function get_subsys_total($sys_type)
{
    global $omc_db, $sysconf_table;
    $sel_sql = "SELECT maxSubsysNum FROM $sysconf_table  WHERE sysTypeName = '$sys_type'";
    $result = mysqli_query($pubConn, $sel_sql);
    if ($result > 0) {
        $result_rows = mysqli_fetch_array($result);
        return ($result_rows[maxSubsysNum]);
    } else {
        echo mysqli_error($pubConn);
        echo "<br>Can not get this system's subsystem quantity by get_subsys_total($sys_type)";
        return (-1);
    }
}

//****************************************************************
//函数12
//	get_e1_total($sys_type)
//函数说明:
//	由系统类型获取系统每个系统的e1总数，通过查询系统定义表$sysconf_table获取
//
//
//变量说明: 
//	$sys_type:系统类型
//                                                             
//返回值：
//	成功: 系统子系统个数
//	失败: -1
//
//作者：      
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function get_e1_total($sys_type)
{
    global $omc_db, $sysconf_table;
    $sel_sql = "SELECT maxE1Num FROM $sysconf_table  WHERE sysTypeName = '$sys_type'";
    //echo "<br>sel_sql=$sel_sql";
    $result = mysqli_query($pubConn, $sel_sql);
    if ($result > 0) {
        $result_rows = mysqli_fetch_array($result);
        return ($result_rows[maxE1Num]);
    } else {
        echo mysqli_error($pubConn);
        echo "<br>Can not get this system's e1 quantity by get_e1_total($sys_type)";
        return (-1);
    }
}

//****************************************************************
//函数13
//	get_sys_total($sys_type)
//函数说明:
//	由系统类型获取系统系统总数，通过查询系统定义表$sysconf_table获取
//
//
//变量说明: 
//	$sys_type:系统类型
//                                                             
//返回值：
//	成功: 系统总数
//	失败: -1
//
//     
//      
//修改注释：      
//NO.1      
//姓名：       
//时间： 2002-5-14   
//修改说明： 表名、字段有变   
//----------------------------------------------------------
function get_sys_total($sys_type)
{
    global $omc_db, $sysconf_table;
    $sel_sql = "SELECT maxSysNum FROM $sysconf_table  WHERE sysTypeName = '$sys_type'";
    //echo "<br>sel_sql=$sel_sql";
    $result = mysqli_query($pubConn, $sel_sql);
    if ($result > 0) {
        $result_rows = mysqli_fetch_array($result);
        return ($result_rows[maxSysNum]);
    } else {
        echo mysqli_error($pubConn);
        echo "<br>Can not get this system's system quantity by get_sys_total($sys_type)";
        return (-1);
    }
}

//****************************************************************
//函数14
//	get_sys_offset($sys_type)
//函数说明:
//	由系统类型获取该类系统的顺序编号offset，通过查询系统定义表$sysconf_table获取
//
//
//变量说明: 
//	$sys_type :系统类型
//                                                             
//返回值：
//	成功: 系统顺序编号
//	失败: -1
//
//
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function get_sys_offset($sys_type)
{
    global $omc_db, $sysconf_table;
    $sel_sql = "SELECT ledOffset FROM $sysconf_table  WHERE sysTypeName = '$sys_type'";
    $result = mysqli_query($pubConn, $sel_sql);
    if ($result > 0) {
        $result_rows = mysqli_fetch_array($result);
        return ($result_rows[0]);
    } else {
        echo mysqli_error($pubConn);
        echo "<br>Can not get this system's offset by get_sys_offset($sys_type)";
        return (-1);
    }

}

//****************************************************************
//函数15
//	omc_sys_id_info($sys_id,$info_type)
//函数说明:
//	该函数通过$sys_id的分析，取得相应信息
//	例：由 "TS00_01" 可得系统编号为"00"
//
//变量说明: 
//	参数$info_type对应要取得的信息
//	sys_type     -->系统类型
//	sys_num      -->系统编号
//	subsys_num   -->子系统编号
//	led_num	     -->led offset
//                                                             
//返回值：
//	成功: 要取得的信息
//	失败: -1
//
//作者：    
//      
//修改注释：      
//NO.1      
//姓名：
//时间：
//修改说明：
//---------------------------------------------------------- 

function omc_sys_id_info($sys_id, $info_type)
{
    global $sysconf_table, $omc_db;
    $db = $omc_db;
    $pieces = explode('_', $sys_id);
    $sys_type = $pieces[0];
    $sys_num = $pieces[1];
    $subsys_num = $pieces[2];
    //echo "<br>sys_type=$sys_type";

    switch ($info_type) {
        case 'sys_type':
            $info = $sys_type;
            break;
        case 'sys_num':
            $info = $sys_num;
            break;
        case 'subsys_num':
            $info = $subsys_num;
            break;
        case 'led_num':
            $sysTypeNo = omc_sys_type2code($sys_type);
            $ledSql = " SELECT ledOffset FROM $sysconf_table WHERE sysTypeNo = '$sysTypeNo' ";
            $ledRes = @mysqli_query($pubConn, $ledSql);
            $ledRow = @mysqli_fetch_array($ledRes);
            $info = $ledRow[0];
            break;
        default:
            echo "<br>the $sys_id information can not be get,please check  parameter: info_typ!";
            break;
    }
    if ($info) {
        return ($info);
    } else {
        return (0);
    }
}

//****************************************************************
//函数16
//	omc_uninstall_device($sys_id)
//函数说明:
//	删除系统
//
//变量说明: 
//	删除系统的系统编号$sys_id
//                                                             
//返回值：
//	成功: 1
//	失败: -1
//
//   
//      
//修改注释：      
//NO.1      
//姓名：           
//时间：2002-5-17      
//修改说明：
//----------------------------------------------------------------------------------

function omc_uninstall_device($sys_id)
{
    global $omc_db, $sysStat_table;
    //echo "<br>sys_id=$sys_id";
    $sysType = omc_sys_id_info($sys_id, sys_type);
    $sysTypeNo = omc_sys_type2code($sysType);
    $sysNo = omc_sys_id_info($sys_id, sys_num);
    $subSysNo = omc_sys_id_info($sys_id, subsys_num);
    $del_sql = "DELETE FROM $sysStat_table  WHERE 
			  sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND subSysNo='$subSysNo'";

    if ($sys_id = 'all') {
        $del_sql = " DELETE FROM $sysStat_table ";
    }
    //echo "<br>del_sql=$del_sql";
    $result = mysqli_query($pubConn, $del_sql);
    //echo "<br>result=$result";
    if ($result) {
        return (1);
    } else {
        return (-1);
    }

}

//****************************************************************
//函数17
//	omc_chk_load_in($file_info,$remark_style)
//函数说明:
//      导入文件检查函数
//      根据需导入文件的信息（包括文件名，需导入的服务器，数据库，表），导入文件的注释信息（指出该文的字段名列表所在行）
//      方法：一一比较数据库中表的字段和注释中列出的字段，如果互相匹配，返回成功
//
//变量说明: 
//	$file_info	所需导入文件的信息数组,包括:
//	$file_info[db]		所需导入的数据库名
//	$file_info[table]	所需导入的数据库中的表名
//	$file_info[name]	所需导入数据库的文件名
//	$remark_style	数据文件注释风格
//	$remark_style[fields_seek]	文件开头，需要忽略的行数，一般为1
//	$remark_style[sign]		注释符，一般为'#'
//	$remark_style[fields_by]	字段分割符，一般为';'

//                                                             
//返回值：
//	成功: 1
//	失败: -1
//
//     
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function omc_chk_load_in($file_info, $remark_style)
{
    //-------------1.get database's information---------------
    global $link;
    $field_sql = "SELECT * FROM {$file_info[table]} LIMIT 0,1";
    //echo "<br>field_sql=$field_sql";
    $field_result = mysqli_query($pubConn, $field_sql);

    //-------------check the data file's fields-------------
    //echo "<br>name={$file_info[name]}";
    $fp = fopen($file_info[name], r);
    for ($i = 1; $i <= $remark_style[fields_seek]; $i++) {
        $field_line = fgets($fp, 4096);  //4096 is a constant
    }
    //echo "<br>field_line=$field_line";
    if (substr($field_line, 0, 1) != $remark_style[sign]) {
        echo "the remark sign not exist"; //注释符错误
        return (-1);
    } else {
        //echo "<br>field_line=$field_line";
        $file_fields_line = substr($field_line, 1);
        //echo "<br>file_fields_line=$file_fields_line";
        $file_fields_rows = explode($remark_style[fields_by], $file_fields_line);
        $fields_num = mysql_num_fields($field_result);
        //echo "<br>fields_num=$fields_num";
        for ($i = 0; $i < $fields_num; $i++) {
            $this_field = mysql_field_name($field_result, $i);
            //echo "<br>$file_fields_rows[$i] -- $this_field";
            if ($file_fields_rows[$i] != $this_field && $this_field != '') {
                echo "<br>error: fields is not matching.";//字段名不匹配
                return (-1);
            }
        }
        fclose($fp);
        return (1);
    }
}


//*******************************************************************
//函数18
// Get the content of $table as a CSV output.
// $sep contains the separation string.
// After every row, a custom callback function $handler gets called.
// $handler must accept one parameter ($sql_insert);

function get_table_csv($server_link, $db_name, $table, $where, $handler)
{
    global $separator;

    $sep = $separator;
    if (strlen($where) > 1) {
        $sel_sql = "SELECT * FROM $table $where";
        $sel_sql = stripslashes($sel_sql);
    } else {
        $sel_sql = "SELECT * FROM $table ";
    }
    //echo "<br>sel_sql=$sel_sql<br>";
    $result = mysqli_query($pubConn, $sel_sql, $server_link);
    if ($result < 0) {
        echo "$sel_sql /n";
        echo mysqli_error($pubConn);
        exit();
    }

    /*
    //输出字段名
    $schema_insert = "#";
    for($j=0; $j<mysql_num_fields($result);$j++){
        $schema_insert .= mysql_field_name($result,$j).$sep;
        //echo "<br>schema_insert=$schema_insert<br>";
    }
    $handler(trim($schema_insert));//按某种方式处理后,输出一行
    */

    $i = 0;
    while ($row = mysqli_fetch_row($result)) {
        set_time_limit(60); // HaRa
        $schema_insert = "";
        for ($j = 0; $j < mysql_num_fields($result); $j++) {
            if (!isset($row[$j]))
                $schema_insert .= "NULL" . $sep;
            elseif ($row[$j] != "")
                $schema_insert .= "$row[$j]" . $sep;
            else
                $schema_insert .= "" . $sep;
        }
        $schema_insert = str_replace($sep . "$", "", $schema_insert);
        $handler(trim($schema_insert));
        $i++;
    }
    return (true);
}


//****************************************************************
//函数19
//	omc_die($warning_words)
//函数说明:
//	输出错误信息,退出
//
//变量说明: 
//	$warning_words 警告说明
//                                                             
//返回值：
//	无
//
//     
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function omc_die($warning_words)
{
    echo "<br><font color=\"#FF0000\" size=\"3\"><b>$warning_words</b></font>";
    exit();
}


//****************************************************************
//函数20
//	change2point($number)
//函数说明:
//	将该数字按保留小数点后两位的规则转换，常用于货币的统一标示
//
//变量说明: 
//	$number ->所需转换的数字
//                                                             
//返回值：
//	成功: 返回转换后的字符
//	失败: -1
//
//     
//      
//修改注释：      
//NO.1      
//姓名：        
//时间：2003-07-10      
//修改说明：干了蠢事，一句话的事，用了一个函数。    
//----------------------------------------------------------

function change2point($number)
{
    /*
    $num	= explode(".",$number);
    $num_1	= $num[0];
    $num_2	= substr($num[1],0,2);
    if(strlen($num_2) < 2){
        if(strlen($num_2) == 0){
            $num_2 = '00';
        }else{
            $num_2 = $num_2.'0';
        }
    }
    if($num_1==0 && $num_2 == 0){
        $number = 0;
    }else{
        $number = $num_1.'.'.$num_2;
    }
    */
    $number = sprintf("%01.2f", $number);
    return ($number);
}

//****************************************************************
//函数21
//	Second2format($number)
//函数说明:
//	把秒数变成标准时间显示如：17秒->00:00:17
//
//变量说明: 
//	$number 秒数
//                                                             
//返回值：
//	成功: 返回标准时间
//	失败: 返回0
//
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
//
function Second2format($number)
{
    if ($number > 0) {
        $number_h = floor($number / 3600);
        if (strlen($number_h) < 2) {
            $number_h = "0" . $number_h;
        }
        $number_ms = $number % 3600;
        $number_m = floor($number_ms / 60);
        if (strlen($number_m) < 2) {
            $number_m = "0" . $number_m;
        }
        $number_s = $number_ms % 60;
        if (strlen($number_s) < 2) {
            $number_s = "0" . $number_s;
        }
        $format = $number_h . ":" . $number_m . ":" . $number_s;
        return ($format);
    } else {
        return (0);
    }
}

//****************************************************************
//函数22
//	getDetailedStatus($sysTypeNo,$sysNo,$subSysNo,$getType)
//	得到详细状态信息或刷新时间
//
//变量说明: 
//	$sysTypeNo系统类型编号
//	$sysNo系统编号
//	$subSysNo子系统编号
//	$getType详细状态(status)或刷新时间(time)
//                                                             
//返回值：
//	成功: 返回状态或时间
//	失败: 返回-1
//
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
//
function getDetailedStatus($sysTypeNo, $sysNo, $subSysNo, $getType = 'time')
{
    global $omc_db, $sysStat_table;
    $db = $omc_db;
    $DEBUG = 0;
    $sel_mes_sql = "SELECT detailStat,updateTime FROM $sysStat_table 
			  WHERE 
			  sysTypeNo='$sysTypeNo' AND sysNo='$sysNo'
			 ";
    if ($DEBUG) echo "<br>sel_mes_sql=$sel_mes_sql";
    $sel_mes_res = mysqli_query($pubConn, $sel_mes_sql);
    $sel_mes_row = @mysqli_fetch_array($sel_mes_res);
    if ($getType == 'Time') {
        $value = $sel_mes_row[detailStat];
    } else {
        $value = $sel_mes_row[updateTime];
    }
    if ($DEBUG) echo "<br>value=$value";
    if (!$sel_mes_row) {
        return (-1);
    } else {
        return ($value);
    }
}


//****************************************************************
//函数23 omc_get_sysAlias($sysTypeNo,$sysNo,$subSysNo)
//
//功能说明:根据指定系统编号获取系统别名
//
//变量说明: 
//                                                             
//返回值：
//	成功: 系统别名字符串
//	失败:-1
//
//
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function omc_get_sysAlias($sysTypeNo, $sysNo, $subSysNo)
{
    $sysAliasSql = "SELECT sysAlias 
				FROM sysInfo 
				WHERE sysTypeNo='$sysTypeNo' AND sysNo='$sysNo'AND subSysNo='$subSysNo'
				ORDER BY sysTypeNo,sysNo,subSysNo
				";
    $sysAliasResult = mysqli_query($pubConn, $sysAliasSql);
    echo mysqli_error($pubConn);
    $sysAliasRows = @mysqli_fetch_array($sysAliasResult);
    $sysAlias = $sysAliasRows[sysAlias];
    if (strlen($sysAlias) > 0) {
        return ($sysAlias);
    } else {
        return (-1);
    }
}


//****************************************************************
//函 数 名:	omcProgressBar($allNum,$doneNum)
//功能说明:	显示进度条
//变量说明: $allNum总数,$doneNum已完成的数目                
//返 回 值: 无
//
//  
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function omcProgressBar($allNum, $doneNum)
{
    if ($allNum > 0) {
        if ($doneNum >= $allNum) {
            $quoNum = 100;
        } else {
            $quoNum = $doneNum / $allNum * 100;
            $quoNum = round($quoNum);
        }
    } else {
        echo "omcProgressBar() Error:allNum cannot < 0!";
        exit();
    }
    $big_width = 320;
    $border = 10;
    $lit_width = $big_width - 2 * $border;
    $height = 10;
    echo "<br>";
    echo "\n";
    echo "<table width=$big_width border=0 cellpadding=0 cellspacing=0 ><tr><td>";
    echo "<font color=000000>$strProgressIsShowedAsFollowing</font>";
    echo "</td></tr></table>";
    echo "\n<table border=0 cellpadding=0 cellspacing=0 width=$big_width bgcolor=#0000FF>";
    echo "\n<tr><td width=$big_width height=$height colspan=3><p align=center><font color=#FFFFFF>";
    echo "\n$quoNum%";
    echo "\n</font></td></tr>";
    echo "\n<tr><td width=$border></td><td width=$lit_width height=$height bgcolor=#FFFFFF>";
    $lineNum = $quoNum * 3;
    echo "\n<table border=1 cellpadding=0 cellspacing=0 width=$lineNum height=$height bordercolor=#FF0000 bgcolor=#FFCC00>";
    echo "\n<tr><td></td></tr></table>  </td><td width=$border></td></tr>";
    echo "\n<tr><td width=$big_width height=$height  colspan=3></td></tr></table> ";
    //echo  "\n<br>";
}


//****************************************************************
//函 数 名:	sysCommSend($sysId,$commType,$objIdStr,$selectedSysId=-1)
//功能说明:	系统参数维护发送命令
//变量说明: 略                 
//返 回 值:
//	成功: 命令编号字符串
//	失败: -1
//
// 
//      
//修改注释：      
//NO.1      
//姓名：cuiticun            
//时间：2005-7-13      
//修改说明：check the exist command num in the buffer    
//----------------------------------------------------------
function sysCommSend($sysId, $commType, $objIdStr, $selectedSysId = -1, $selectedDPC = 0)
{
    global $OMC_server,$pubConn;
    $DEBUG = 0;
    $pubDb = 'OMC_PUB';
    $commTable = 'parameterComm';
    $nGetComm = 0;
    $nSetComm = 3;

    $nSubSysParam = 0;
    $nGlobalSubSysParam = 1;
    $nGlobalSysParam = 2;

    $sysIdArr = explode('_', $sysId);
    $sysTypeNo = $sysIdArr[0];
    $sysNo = $sysIdArr[1];
    $subSysNo = $sysIdArr[2];

    $objConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], 'OBJ_'.$sysTypeNo);

    //insert the operation log into the database
    $logStr = "";
    if ($commType == 0)
        $logStr .= "Get ";
    else if ($commType == 3)
        $logStr .= "Set ";
    //=====get the LevelWhere
    $levelArr = explode('.', $objIdStr);
    for ($j = 0; $j < sizeof($levelArr); $j++) {
        $levelNo = $j + 1;
        ${"level_$levelNo"} = $levelArr[$j];
        if ($DEBUG) echo "<BR>levelArr[$j]={$levelArr[$j]}";
        if ($j == 0) {
            $levelWhere = "level_$levelNo='${"level_$levelNo"}'";
        } else {
            $levelWhere .= " AND level_$levelNo='${"level_$levelNo"}'";
        }
    }
    $temp = $levelWhere;

    $database = "OBJ_" . $sysTypeNo;
    $ssql = "select name_2 from paramConf where $temp ";
    $resultt = @mysqli_query($objConn, $ssql);
    $rowss = @mysqli_fetch_array($resultt);
    //==get the parameter name
    $p_name = $rowss[name_2];

    $p_name = str_replace("Entry", "", $p_name);
    $sysName = getSystemNameBySysTypeNo($sysTypeNo);
    $logStr .= "$sysName $p_name's parameter";

    $logStr .= $p_name;

    insertLog($logStr);
    //end insert operation log

    if ($sysNo == '99') {
        $paramType = $nGlobalSysParam;
    } else if ($subSysNo == '99') {
        $paramType = $nGlobalSubSysParam;
    } else {
        $paramType = $nSubSysParam;
    }

    if ($selectedSysId != -1) {
        $selectedSysIdArr = explode('_', $selectedSysId);
        $selectedSysTypeNo = $selectedSysIdArr[0];
        $selectedSysNo = $selectedSysIdArr[1];
        $selectedSubSysNo = $selectedSysIdArr[2];
    }
    if ($DEBUG) echo "<br>selectedSysId=$selectedSysId";
    if ($DEBUG) echo "<br>selectedSysNo=$selectedSysNo";
    if ($DEBUG) echo "<br>selectedSubSysNo=$selectedSubSysNo";

    //step 1: delete overtime and imcompetent command , check the exist command num
    $overTime = date("Y-m-d H:i:s", mktime(date("H"), date("i") - 1, date("s"), date("m"), date("d"), date("Y")));
    $delSql = "DELETE FROM $commTable 
				WHERE commState ='0' OR commState ='3' OR sendTime < '$overTime'
				";
    if ($DEBUG) echo "<br>delete command sql = $delSql";
    mysqli_query($pubConn, $delSql);
    echo mysqli_error($pubConn);

    //check the exist command num
    $chkSql = "SELECT commId FROM $commTable";
    if ($DEBUG) echo "<br>check exist commands sql = $chkSql";
    $chkResult = mysqli_query($pubConn, $chkSql);
    echo mysqli_error($pubConn);
    $existNum = @mysqli_num_rows($chkResult);

    //step 2: check the command(不能同时操作同一个子系统，也不能同时读同一个系统的不同子系统的公共参数)
    if (($paramType != $nSubSysParam) && ($commType == $nGetComm)) { //0=Get,2=response,3=Set
        if ($selectedSysId == -1) {
            $chkSql = "SELECT commId FROM $commTable
						WHERE sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND commType ='$nGetComm'
						";
        } else {
            //选择指定子系统
            $chkSql = "SELECT commId FROM $commTable
						WHERE sysTypeNo='$selectedSysTypeNo' AND sysNo='$selectedSysNo' 
						AND subSysNo='$selectedSubSysNo' AND commType ='$nGetComm'
						";
        }
    } else {
        $chkSql = "SELECT commId FROM $commTable
					WHERE sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND subSysNo='$subSysNo'
					";
    }
    if ($DEBUG) echo "<br>check command sql = $chkSql";
    $chkResult = mysqli_query($pubConn, $chkSql);
    echo mysqli_error($pubConn);
    $rowsNum = @mysqli_num_rows($chkResult);
    if ($rowsNum > 0) {
        echo "<script language=\"javascript\"> ";
        echo "alert('Command object is being operated by others,please try it later!'); ";
        echo "</script>";
        return (-1);
    }

    //step 3:insert commands into command table
    if (($paramType != $nSubSysParam) && ($commType == $nSetComm)) { //写公共参数
        //get maxSubSysNum
        //$selSql="SELECT maxSysNum, maxSubSysNum FROM sysConf WHERE sysTypeNo = '$sysTypeNo'";
        //$selResult=mysqli_query($pubConn,$selSql);
        //$selRows=mysqli_fetch_array($selResult);
        //echo mysqli_error($pubConn);
        //$maxSysNum = $selRows[maxSysNum];
        //$maxSubSysNum = $selRows[maxSubSysNum];
        if ($selectedSysId == -1) {
            if ($paramType == $nGlobalSysParam) {
                if ($sysTypeNo[0] == '2') {
                    if ($sysTypeNo == '222' || $sysTypeNo == '223') {//MTP3,SCCP
                        $selSql = "SELECT sysNo,subSysNo FROM sysInfo 
							WHERE sysTypeNo = '$sysTypeNo' AND sysNo!='8' AND sysNo!='11' 
								AND DPC = '$selectedDPC'
							ORDER BY sysNo,subSysNo,baseStat DESC
						";
                    } else {
                        $selSql = "SELECT sysNo,subSysNo FROM sysInfo 
							WHERE sysTypeNo = '$sysTypeNo' AND sysNo='9' 
								AND DPC = '$selectedDPC'
							ORDER BY sysNo,subSysNo,baseStat DESC
						";
                    }

                } else {
                    $selSql = "SELECT sysNo,subSysNo FROM sysInfo 
						WHERE sysTypeNo = '$sysTypeNo' 
						ORDER BY sysNo,subSysNo,baseStat DESC
						";
                }
            } else {
                $selSql = "SELECT sysNo,subSysNo FROM sysInfo 
					WHERE sysTypeNo = '$sysTypeNo' 
						AND sysNo='$sysNo' 
					ORDER BY sysNo,subSysNo,baseStat DESC
					";
            }
        } else {
            //选择指定子系统
            $selSql = "SELECT sysNo,subSysNo FROM sysInfo 
					WHERE sysTypeNo = '$selectedSysTypeNo' 
						AND sysNo	='$selectedSysNo' 
						AND subSysNo='$selectedSubSysNo'
					ORDER BY baseStat DESC
					";
        }
        if ($DEBUG) {
            echo "<br>selSql=$selSql";
            exit();
        }
        $selResult = mysqli_query($pubConn, $selSql);
        $selRowsNum = mysqli_num_rows($selResult);

        if ($selRowsNum + $existNum > 64) {
            echo "<script language=\"javascript\"> ";
            echo "alert('The command buffer is full,please try it later!'); ";
            echo "</script>";
            return (-1);
        }

        if ($selRowsNum > 0) {
            while ($selRows = mysqli_fetch_array($selResult)) {
                $sysNo = $selRows[sysNo];
                $subSysNo = $selRows[subSysNo];
                $sendSql = "INSERT INTO $commTable 
					(commType,sysTypeNo,sysNo,subSysNo,paramType,objId,InstanceNo,commState,sendTime)
					VALUES
					('$commType','$sysTypeNo','$sysNo','$subSysNo','$paramType','$objIdStr','','1',CURRENT_TIMESTAMP)
					";
                if ($DEBUG) echo "<br>send command sql = $sendSql";
                $sendResult = mysqli_query($pubConn, $sendSql);
                echo mysqli_error($pubConn);
            }
        } else {
            echo "<br><font size=3 color=red><b>Error:This canot get this system's IP</b></font>";
            return (-1);
        }
        //写VPS 或PCR 参数
        /*
        }elseif(($sysTypeNo == 120 || $sysTypeNo == 130 || $sysTypeNo == 140) && ($commType == $nSetComm)){
            $tmpLevelOne = $sysNo + 1;
            $tmpLevelTwo = $subSysNo + 1;
            switch($sysTypeNo){
                case 120:
                        $sysParamOidPrefix=$tmpLevelOne.'.'.$tmpLevelTwo.'.7.2.1';
                        break;
                case 130:
                        $sysParamOidPrefix=$tmpLevelOne.'.'.$tmpLevelTwo.'.6.2.1';
                        break;
                case 140:
                        $sysParamOidPrefix=$tmpLevelOne.'.'.$tmpLevelTwo.'.8.2.1';
                        break;
                default:	echo "error sysTypeNo";
                        exit();
            }
            if($DEBUG) echo"<br>strstr($objIdStr, $sysParamOidPrefix)";

            if(strstr($objIdStr, $sysParamOidPrefix)){//写VPS 或PCR的公共参数
                //get maxSubSysNum
                $selSql="SELECT maxSubSysNum FROM sysConf WHERE sysTypeNo = '$sysTypeNo'";
                $selResult=mysqli_query($pubConn,$selSql);
                $selRows=mysqli_fetch_array($selResult);
                echo mysqli_error($pubConn);
                $maxSubSysNum = $selRows[maxSubSysNum];
                //get alive system
                $selSql="SELECT DISTINCT subSysNo FROM sysInfo
                        WHERE sysTypeNo = '$sysTypeNo' AND sysNo='$sysNo'
                        ORDER BY sysNo,subSysNo,baseStat DESC
                        ";
                $selResult = mysqli_query($pubConn,$selSql);
                $selRowsNum  = mysqli_num_rows($selResult);
                if($selRowsNum > 0){
                    while($selRows  = mysqli_fetch_array($selResult)){
                        $subSysNo = $selRows[subSysNo];
                        $tmpLevelOne = $sysNo + 1;
                        $tmpLevelTwo = $subSysNo + 1;
                        $tmpOidLen = strlen($objIdStr);
                        $tmpSubfixOid = substr($objIdStr,3,$tmpOidLen);
                        $objIdStr = $tmpLevelOne.'.'.$tmpLevelTwo.$tmpSubfixOid;
                        if($DEBUG) echo"<br>VPS OR PCR'S system parameter Oid:$objIdStr";
                        $sendSql="INSERT INTO $commTable
                            (commType,sysTypeNo,sysNo,subSysNo,paramType,objId,InstanceNo,commState,sendTime)
                            VALUES
                            ('$commType','$sysTypeNo','$sysNo','$subSysNo','$paramType','$objIdStr','0','1',CURRENT_TIMESTAMP)
                            ";
                        if($DEBUG) echo"<br>send command sql = $sendSql";
                        $sendResult=mysqli_query($pubConn,$sendSql);
                        echo mysqli_error($pubConn);
                    }
                }else{
                    echo "<br><font size=3 color=red><b>Error:This canot get this system's IP</b></font>";
                    return(-1);
                }
            }else{//写VPS，PCR子系统参数
                $sendSql="INSERT INTO $commTable
                    (commType,sysTypeNo,sysNo,subSysNo,paramType,objId,InstanceNo,commState,sendTime)
                    VALUES
                    ('$commType','$sysTypeNo','$sysNo','$subSysNo','$paramType','$objIdStr','0','1',CURRENT_TIMESTAMP)
                    ";
                if($DEBUG) echo"<br>send command sql = $sendSql";
                $sendResult=mysqli_query($pubConn,$sendSql);
                echo mysqli_error($pubConn);
            }
        */
    } else {//读系统/子系统参数或写子系统参数
        if ($paramType != $nSubSysParam) {//读公共参数
            if ($selectedSysId == -1) {
                //get baseStat 0=uninstalled, 1=nomal, 2=warning, 3=error
                //先按状态排序，再按系统编号,子系统编号排序，同种状态优先选择编号小的
                if ($paramType == $nGlobalSysParam) {
                    /*if($sysTypeNo == '222' || $sysTypeNo == '223'){//MTP3,SCCP
                        $selSql="SELECT sysNo,subSysNo FROM sysInfo
                            WHERE sysTypeNo = '$sysTypeNo' AND sysNo='9'
                                AND DPC = '$selectedDPC'
                            ORDER BY baseStat DESC,updateTime DESC,sysNo DESC,subSysNo
                            ";
                    }else{
                        $selSql="SELECT sysNo,subSysNo FROM sysInfo
                            WHERE sysTypeNo = '$sysTypeNo' AND sysNo='9'
                            ORDER BY baseStat DESC,updateTime DESC,sysNo,subSysNo
                            ";
                    }*/

                    if ($sysTypeNo[0] == '2') {//Platform
                        $selSql = "SELECT sysNo,subSysNo FROM sysInfo 
								WHERE sysTypeNo = '$sysTypeNo' AND sysNo='9' 
								ORDER BY baseStat DESC,sysNo DESC,subSysNo,updateTime DESC
							";
                    } else {
                        $selSql = "SELECT sysNo,subSysNo FROM sysInfo 
							WHERE sysTypeNo = '$sysTypeNo' 
							ORDER BY baseStat DESC,updateTime DESC,sysNo,subSysNo
							";
                    }
                } else {
                    $selSql = "SELECT sysNo,subSysNo FROM sysInfo 
							WHERE sysTypeNo = '$sysTypeNo' AND sysNo='$sysNo'
							ORDER BY baseStat DESC,updateTime DESC,sysNo,subSysNo
							";
                }
            } else {
                //选择指定子系统
                $selSql = "SELECT sysNo,subSysNo FROM sysInfo 
						WHERE sysTypeNo = '$sysTypeNo' AND sysNo='$selectedSysNo' AND subSysNo='$selectedSubSysNo'
						ORDER BY baseStat DESC,updateTime DESC
						";
            }

            $selResult = mysqli_query($pubConn, $selSql);
            if ($DEBUG) {
                echo "<br>get system info sql = $selSql";
            }
            echo mysqli_error($pubConn);
            if (@mysqli_num_rows($selResult) > 0) {
                $selRows = @mysqli_fetch_array($selResult);
                $sysNo = $selRows[sysNo];
                $subSysNo = $selRows[subSysNo];
            } else {
                echo "<script language=\"javascript\"> ";
                echo "alert('This system have not been installed'); ";
                echo "</script>";
                return (-1);
            }
        }
        // special for sccp and mtp3 add by lilong 2004-6-11
        if ($selectedSysId == -1) {
            $DEBUG = 0;
            if ($DEBUG) echo "sysTypeNo = $sysTypeNo, sysNo = $sysNo<br>";
            if (($sysTypeNo == '222' || $sysTypeNo == '223') && ('8' != $sysNo) && ('11' != $sysNo)) {//MTP3,SCCP
                $selSql_1 = "SELECT sysNo,subsysNo FROM sysInfo 
						WHERE baseStat=1 AND sysTypeNo=320";
                if ($DEBUG) echo "selSql_1 = $selSql_1<br>";
                $selRes_1 = mysqli_query($pubConn, $selSql_1);
                if (mysqli_num_rows($selRes_1) > 0) {
                    $selRows_1 = @mysqli_fetch_array($selRes_1);
                    $sysNo = 9;
                    $subSysNo = $selRows_1[sysNo];
                } else {
                    $selSql_2 = "SELECT sysNo,subsysNo FROM sysInfo 
							WHERE baseStat=1 AND sysTypeNo=0";
                    if ($DEBUG) echo "selSql_2 = $selSql_2<br>";
                    $selRes_2 = mysqli_query($pubConn, $selSql_2);
                    if (mysqli_num_rows($selRes_2) > 0) {
                        $selRows_2 = @mysqli_fetch_array($selRes_2);
                        $sysNo = 0;
                        $subSysNo = $selRows_2[subsysNo];
                    }
                }
            }
            if ($DEBUG) {
                echo "sysNo=$sysNo, subSysNo=$subSysNo<br>";
                exit();
            }
            $DEBUG = 0;
            //add end by lilong 2004-6-11
        }

        if (1 + $existNum > 64) {
            echo "<script language=\"javascript\"> ";
            echo "alert('The command buffer is full,please try it later!'); ";
            echo "</script>";
            return (-1);
        }

        $sendSql = "INSERT INTO $commTable 
			(commType,sysTypeNo,sysNo,subSysNo,paramType,objId,InstanceNo,commState,sendTime)
			VALUES
			('$commType','$sysTypeNo','$sysNo','$subSysNo','$paramType','$objIdStr','','1',CURRENT_TIMESTAMP)
			";
        if ($DEBUG) echo "<br>send command sql = $sendSql";
        $sendResult = mysqli_query($pubConn, $sendSql);
        echo mysqli_error($pubConn);
    }

    //step 4:Return commIdStr
    if ($DEBUG) echo "<br>if(($paramType != $nSubSysParam) && ($commType == $nSetComm))";
    if (($paramType != $nSubSysParam) && ($commType == $nSetComm)) {
        if ($paramType == $nGlobalSysParam) {
            $selSql = "SELECT commId FROM $commTable 
				WHERE paramType='$paramType' AND sysTypeNo='$sysTypeNo'  
				ORDER BY sysTypeNo,sysNo,subSysNo";
        } else {
            $selSql = "SELECT commId FROM $commTable 
				WHERE paramType='$paramType' AND sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' 
				ORDER BY sysTypeNo,sysNo,subSysNo";
        }
        /*
            //写VPS 或PCR 参数
        }elseif(($sysTypeNo == 120 || $sysTypeNo == 130 || $sysTypeNo == 140) && ($commType == $nSetComm)){
            $selSql = "SELECT commId FROM $commTable
                WHERE sysTypeNo='$sysTypeNo' AND sysNo='$sysNo'
                ORDER BY sysTypeNo,sysNo,subSysNo";
        */
    } else {
        $selSql = "SELECT commId FROM $commTable 
			WHERE sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND subSysNo='$subSysNo'";
    }
    if ($DEBUG) echo "<br>get command id sql = $selSql";
    $selResult = mysqli_query($pubConn, $selSql);
    echo mysqli_error($pubConn);
    $commIdStr = '';
    while ($selRows = mysqli_fetch_array($selResult)) {
        $commIdStr .= $selRows[commId] . '-';
    }
    if ($DEBUG) echo "<br>commIdStr = $commIdStr";
    if ($DEBUG) exit();
    return ($commIdStr);
}//end of function sysSendComm()

//****************************************************************
//函 数 名:	setSysParamValue($sysId,$objIdStr,$instanceNo,$newValue)
//功能说明:	修改设备参数的initValue,为发送命令做准备
//变量说明: 略
//返 回 值:
//	成功: 命令编号字符串
//	失败: -1
//
// 
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function setSysParamValue($sysId, $objIdStr, $instanceNo, $newValue)
{
    global $paramConf_table;
    $DEBUG = 0;

    if ($DEBUG) echo "<br>sysId=$sysId";
    $tmpSysIdArr = explode('_', $sysId);
    $sysTypeNo = $tmpSysIdArr[0];
    $sysNo = $tmpSysIdArr[1];
    $subSysNo = $tmpSysIdArr[2];

    $paramDb = 'OBJ_' . $sysTypeNo;
    if ($DEBUG) echo "<br>paramDb=$paramDb";
    //获取参数定义表的表名，根据oid取出参数定义
    $tmpObjArr = explode('.', $objIdStr);
    $objWhereStr = '';
    for ($i = 0; $i < sizeof($tmpObjArr); $i++) {
        $j = $i + 1;
        $objWhereStr .= ' level_' . $j . "='{$tmpObjArr[$i]}' AND ";
    }
    $objWhereStr = substr($objWhereStr, 0, -5); //最后多了一个' AND ',将其去掉
    $getParamDefSql = "SELECT paramType FROM $paramConf_table WHERE $objWhereStr";
    if ($DEBUG) echo "<br>$getParamDefSql";
    $getParamDefResult = mysqli_query($pubConn, $getParamDefSql);
    echo mysqli_error($pubConn);
    if (mysqli_num_rows($getParamDefResult) <= 0) {
        echo "Cannot find this objId from paramConf";
        return (-1);
    }
    $getParamDefRows = mysqli_fetch_array($getParamDefResult);
    $paramType = $getParamDefRows[paramType];

    //根据参数类型，推出参数表的表名
    if ($paramType == 2) {
        $paramTable = 'param_99';
    } else {
        $paramTable = 'param_' . $sysNo;
    }
    if ($paramType != 0) {
        $subSysNo = '99';
    }

    //修改对应的参数值
    $setParamValueSql = "UPDATE $paramTable 
			SET initValue='$newValue',readValue='' 
			WHERE subSysNo = '$subSysNo' AND instanceNo = '$instanceNo'
				AND $objWhereStr
			";
    if ($DEBUG) echo "<br> $setParamValueSql <br>";
    $setResult = mysqli_query($pubConn, $setParamValueSql);
    echo mysqli_error($pubConn);
    if ($setResult) {
        return (1);
    } else {
        return (-1);
    }
}//end of function setSysParamValue

//****************************************************************
//函 数 名:	getSysParamValue($sysId,$objIdStr,$instanceNo,$valueName)
//功能说明:	获取指定的设备参数
//变量说明: 略
//返 回 值:
//	成功: 参数值
//	失败: exit()退出
//
// 
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function getSysParamValue($sysId, $objIdStr, $instanceNo, $valueName)
{
    global $paramConf_table;
    $DEBUG = 0;

    $tmpSysIdArr = explode('_', $sysId);
    $sysTypeNo = $tmpSysIdArr[0];
    $sysNo = $tmpSysIdArr[1];
    $subSysNo = $tmpSysIdArr[2];

    $paramDb = 'OBJ_' . $sysTypeNo;
    //获取参数定义表的表名，根据oid取出参数定义
    $tmpObjArr = explode('.', $objIdStr);
    $objWhereStr = '';
    for ($i = 0; $i < sizeof($tmpObjArr); $i++) {
        $j = $i + 1;
        $objWhereStr .= ' level_' . $j . "='{$tmpObjArr[$i]}' AND ";
    }
    $objWhereStr = substr($objWhereStr, 0, -5); //最后多了一个' AND ',将其去掉
    $getParamDefSql = "SELECT paramType FROM $paramConf_table WHERE $objWhereStr";
    if ($DEBUG) echo "<br>$getParamDefSql";
    $getParamDefResult = mysqli_query($pubConn, $getParamDefSql);
    echo mysqli_error($pubConn);
    if (@mysqli_num_rows($getParamDefResult) <= 0) {
        echo "Cannot find this objId from paramConf";
        return (-1);
    }
    $getParamDefRows = mysqli_fetch_array($getParamDefResult);
    $paramType = $getParamDefRows[paramType];

    //根据参数类型，推出参数表的表名
    if ($paramType == 2) {
        $paramTable = 'param_99';
    } else {
        $paramTable = 'param_' . $sysNo;
    }
    if ($paramType != 0) {
        $subSysNo = '99';
    }

    //修改对应的参数值
    $getParamValueSql = "SELECT $valueName FROM $paramTable 
			WHERE subSysNo = '$subSysNo' AND instanceNo = '$instanceNo'
				AND $objWhereStr
			";
    if ($DEBUG) echo "<br> $getParamValueSql <br>";
    $getResult = mysqli_query($pubConn, $getParamValueSql);
    echo mysqli_error($pubConn);
    if (mysqli_num_rows($getResult) > 0) {
        $paramRows = mysqli_fetch_array($getResult);
        $paramValue = $paramRows[0];
        return ($paramValue);
    } else {
        return (-1);
    }
}//end of function getSysParamValue

//****************************************************************
//函数8
//	getMoneyPara()
//函数说明:
//	该函数通过查询PPS系统配置得到显示的货币单位是最小货币单位的倍数
//	
//
//
//
//变量说明: 
//	无
//	
//                                                             
//返回值：
//	显示的货币单位是最小货币单位的倍数
//
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function getMoneyPara()
{
    global $pubConn;
    $getSql = " SELECT pubVarValue FROM omcPubVarConf WHERE pubVarName='pssCardMinCount'";
    $getRes = @mysqli_query($pubConn, $getSql);
    $getRow = @mysqli_fetch_array($getRes);
    echo mysqli_error($pubConn);
    $multiple = $getRow[pubVarValue];
    /*
    if($multiple < 0.1)
    {
        $multiple = 100;
    }
    elseif($multiple < 1)
    {
        $multiple = 10;
    }
    */
    return ($multiple);
}

function getMoneyPara_forPpsReport()
{
    global $omc_db;
    $getSql = " SELECT pubVarValue FROM omcPubVarConf WHERE pubVarName='currency_decimal'";
    $getRes = @mysqli_query($pubConn, $getSql);
    $getRow = @mysqli_fetch_array($getRes);
    echo mysqli_error($pubConn);
    $multiple = $getRow[pubVarValue];
    /*
    if($multiple < 0.1)
    {
        $multiple = 100;
    }
    elseif($multiple < 1)
    {
        $multiple = 10;
    }
    */
    return ($multiple);
}

//****************************************************************
//函数:
//	getBitValue($hexStr,$offset)
//功能说明:
//从一个十六进制字符串$hexStr中取出指定位置$offset的二进制字符
//例:	getBitValue('F1','0.1')=1
//		getBitValue('F1','1.0')=1
//		getBitValue('F1','1.1')=0
//参数说明: 
//                                                             
//返回值：
//	取出的二进制字符
//
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function getBitValue($hexStr, $offset)
{
    $DEBUG = 0;
    if ($DEBUG) echo "<br>hexStr=$hexStr";
    if ($DEBUG) echo "<br>offset=$offset";
    $tmpArr = explode('-', $offset);
    $tmpBgnArr = explode('.', $tmpArr[0]);
    $bgnByteOffset = $tmpBgnArr[0];
    $bgnBitOffset = $tmpBgnArr[1];
    if ($DEBUG) echo "<br>bgnByteOffset=$bgnByteOffset";
    if ($DEBUG) echo "<br>bgnBitOffset=$bgnBitOffset";
    if (sizeof($tmpArr) > 1 && $tmpArr[0] != $tmpArr[1]) {
        $tmpEndArr = explode('.', $tmpArr[1]);
        $endByteOffset = $tmpEndArr[0];
        $endBitOffset = $tmpEndArr[1];
        if ($bgnBitOffset == 0 && $endBitOffset == 7) {
            $byteOffset = $endByteOffset - $bgnByteOffset + 1;
            $selByte = substr($hexStr, $bgnByteOffset * 2, $byteOffset * 2);
            return ($selByte);
        } else {
            $tmpByte_1 = substr($hexStr, 0, $bgnByteOffset * 2);
            $tmpByte_2 = substr($hexStr, ($endByteOffset + 1) * 2);
            $selByte = substr($hexStr, $bgnByteOffset * 2, ($endByteOffset - $bgnByteOffset + 1) * 2);
            if (strlen($selByte) > 2) {//不能跨byte
                return (-1);
            }
            if ($DEBUG) echo "<br>selByte=$selByte";
            $tmpDec = HexDec($selByte);
            $binStr = DecBin($tmpDec);
            for ($i = strlen($binStr); $i < strlen($selByte) * 4; $i++) {
                $binStr = '0' . $binStr;
            }
            if ($DEBUG) echo "<br>binStr=$binStr";
            if ($bgnBitOffset == 0) {
                $selBit = substr($binStr, -($endBitOffset + 1));
            } else {
                $selBit = substr($binStr, -($endBitOffset + 1), ($endBitOffset - $bgnBitOffset + 1));
            }

            return ($selBit);
        }
    } else {
        if ($bgnByteOffset >= ceil(strlen($hexStr) / 2)) {
            return (0);
        } else {
            $selByte = substr($hexStr, $bgnByteOffset * 2, 2);
            if ($DEBUG) echo "<br>selByte=$selByte";
            $tmpDec = HexDec($selByte);
            $binStr = DecBin($tmpDec);
            if ($DEBUG) echo "<br>binStr=$binStr";
            $binStrLen = strlen($binStr);
            if ($bgnBitOffset >= $binStrLen) {
                return (0);
            } else {
                $tmpLen = 0 - $bgnBitOffset - 1;
                $selBit = substr($binStr, $tmpLen, 1);
                if ($DEBUG) echo "<br>selBit=$selBit=substr($binStr,$tmpLen,1)";
                return ($selBit);
            }
        }
    }
}

//****************************************************************
//函数:
//	setBitValue($hexStr,$offset,$bitValue)
//功能说明:
//将一个十六进制字符串$hexStr中指定位置$offset的二进制字符，设成指定的值$bitValue
//例:	setBitValue('F1','1.1','1')=F3
//		setBitValue('F1','0.1','0')=E1
//参数说明: 
//                                                             
//返回值：
//	被改过的十六进制字符串$hexStr
//
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function setBitValue($hexStr, $offset, $bitValue)
{
    $DEBUG = 0;
    if ($DEBUG) echo "<br>====================";
    if ($DEBUG) echo "<br>hexStr=$hexStr";
    if ($DEBUG) echo "<br>offset=$offset";
    $tmpArr = explode('-', $offset);
    $tmpBgnArr = explode('.', $tmpArr[0]);
    $bgnByteOffset = $tmpBgnArr[0];
    $bgnBitOffset = $tmpBgnArr[1];
    if ($DEBUG) echo "<br>bgnByteOffset=$bgnByteOffset";
    if ($DEBUG) echo "<br>bgnBitOffset=$bgnBitOffset";
    if (sizeof($tmpArr) > 1) {
        $tmpEndArr = explode('.', $tmpArr[1]);
        $endByteOffset = $tmpEndArr[0];
        $endBitOffset = $tmpEndArr[1];
        if ($DEBUG) echo "<br>endByteOffset=$endByteOffset";
        if ($DEBUG) echo "<br>endBitOffset=$endBitOffset";
        if ($bgnBitOffset == 0 && $endBitOffset == 7) {
            //这时，$bitValue是16进值字符串，其他情况下，都是二进制字符串
            $tmpByte_1 = substr($hexStr, 0, $bgnByteOffset * 2);
            if ($DEBUG) echo "<br>tmpByte_1=$tmpByte_1";
            $tmpByte_2 = substr($hexStr, ($endByteOffset + 1) * 2);
            if ($DEBUG) echo "<br>tmpByte_2=$tmpByte_2";
            $tmpHexStr = $tmpByte_1 . $bitValue . $tmpByte_2;
            if ($DEBUG) echo "<br>tmpHexStr=$tmpHexStr";
            return ($tmpHexStr);
        } else {
            $tmpByte_1 = substr($hexStr, 0, $bgnByteOffset * 2);
            $tmpByte_2 = substr($hexStr, ($endByteOffset + 1) * 2);
            $selByte = substr($hexStr, $bgnByteOffset * 2, ($endByteOffset - $bgnByteOffset + 1) * 2);
            if (strlen($selByte) > 2) {//不能跨byte
                return (-1);
            }
            if ($DEBUG) echo "<br>selByte=$selByte";
            $tmpDec = HexDec($selByte);
            $binStr = DecBin($tmpDec);
            for ($i = strlen($binStr); $i < strlen($selByte) * 4; $i++) {
                $binStr = '0' . $binStr;
            }
            if ($DEBUG) echo "<br>binStr=$binStr";

            $tmpBit_1 = substr($binStr, 0, 8 - $endBitOffset - 1);
            if ($DEBUG) echo "<br>tmpBit_1=$tmpBit_1";
            if ($bgnBitOffset == 0) {
                $tmpBit_2 = '';
            } else {
                $tmpBit_2 = substr($binStr, -($bgnBitOffset));
            }
            if ($DEBUG) echo "<br>tmpBit_2=$tmpBit_2";
            $tmpBit = $tmpBit_1 . $bitValue . $tmpBit_2;
            if ($DEBUG) echo "<br>tmpBit=$tmpBit";
            $tmpByte = dechex(BinDec($tmpBit));
            if ($DEBUG) echo "<br>tmpByte=$tmpByte";
            for ($i = strlen($tmpByte); $i < strlen($selByte); $i++) {
                $tmpByte = '0' . $tmpByte;
            }
            $tmpHexStr = $tmpByte_1 . $tmpByte . $tmpByte_2;
            return ($tmpHexStr);
        }
    } else {
        if ($bgnByteOffset >= ceil(strlen($hexStr) / 2)) {
            return (-1);
        } else {
            $selByte = substr($hexStr, $bgnByteOffset * 2, 2);
            if ($DEBUG) echo "<br>selByte=$selByte";
            $tmpDec = HexDec($selByte);
            $binStr = DecBin($tmpDec);
            $binStr = omc_keep_length($binStr, '0', '8', '1');
            if ($DEBUG) echo "<br>binStr=$binStr";
            $tmpLen = 7 - $bgnBitOffset;
            $tmpBit_1 = substr($binStr, 0, $tmpLen);
            if ($DEBUG) echo "<br>tmpBit_1=$tmpBit_1=substr($binStr,0,$tmpLen)";
            $tmpLen = 8 - $bgnBitOffset;
            $tmpBit_2 = substr($binStr, $tmpLen);
            if ($DEBUG) echo "<br>tmpBit_2=$tmpBit_2=substr($binStr,$tmpLen) ";
            $newBitStr = $tmpBit_1 . $bitValue . $tmpBit_2;
            if ($DEBUG) echo "<br>newBitStr=$newBitStr";
            $tmpDec = BinDec($newBitStr);
            $selByte = DecHex($tmpDec);
            $selByte = omc_keep_length($selByte, '0', '2', '1');
            if ($DEBUG) echo "<br>selByte=$selByte";
            $tmpHexStr_1 = substr($hexStr, 0, $bgnByteOffset * 2);
            $tmpHexStr_2 = substr($hexStr, $bgnByteOffset * 2 + 2);
            if ($DEBUG) echo "<br>tmpHexStr_1=$tmpHexStr_1 / tmpHexStr_2=$tmpHexStr_2 ";
            $tmpHexStr = $tmpHexStr_1 . $selByte . $tmpHexStr_2;
            if ($DEBUG) echo "<br>tmpHexStr=$tmpHexStr";
            return ($tmpHexStr);
        }
    }
}

//****************************************************************
//函数:
//	getPubVar($pubVarName)
//功能说明:
//从表OMC_PUB.omcPubVarConf中取出对应变量名的值
//例:	getPubVar('pssCardMinCount')=100
//参数说明: 
//                                                             
//返回值：
//	成功：取出变量名的值，失败：-1
//
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function getPubVar($pubVarName)
{
    $DEBUG = 0;
    global $pubConn;
    $db = 'OMC_PUB';
    $tableName = 'omcPubVarConf';
    $sql = " SELECT pubVarValue
			   FROM $tableName
			   WHERE pubVarName='$pubVarName'
			 ";
    if ($DEBUG) echo "<br>sql=$sql";
    //$res = @mysqli_query($pubConn,$sql);
    $res = mysqli_query($pubConn, $sql);
    if (NULL == $res) return (-1);
    if (mysqli_num_rows($res) > 0) {
        $row = mysqli_fetch_array($res);
        return ($row['pubVarValue']);
    } else {
        return (-1);
    }
}


//****************************************************************
//函数:
//	showComma($value)
//功能说明:如果值大于3位,加逗号显示
//
//参数说明: 
//                                                             
//返回值：
//	成功：转换的值，失败：-1
//
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function showComma($value)
{
    $sValue = explode("-", $value);
    if (isset($sValue[1]))
        $pValue = explode(".", $sValue[1]);
    else
        $pValue = explode(".", $sValue[0]);

    $intValue = $pValue[0];
    $len = strlen($intValue);
    $temValue = '';
    while ($len > 3) {
        $value_1 = substr($intValue, -3);
        $intValue = substr($intValue, 0, -3);
        $temValue = "," . $value_1 . $temValue;
        $len = strlen($intValue);
    }
    $temValue = $intValue . $temValue;
    if (isset($pValue[1])) {
        $temValue = $temValue . "." . $pValue[1];
    }
    if (isset($sValue[1]))
        $temValue = "-" . $temValue;

    return $temValue;
}

//****************************************************************
//函数:
//	showHelp($helpId)
//功能说明:显示帮组文件
//
//参数说明: 
//                                                             
//返回值：
//	成功：
//
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function showHelp($helpId)
{
    echo "&nbsp;";
    echo "<a href=\"javascript:openHelp('$helpId');\"><img id=\"helpId\" align=absBottom border=0 src=../../images/help.gif></a>";
    //echo "<div onClick=\"openHelp('$helpId');\" style=\"cursor:hand\"><img align=absBottom border=0 src=../../images/help.gif></div>";
}


//****************************************************************
//函数:
//	getSerial()
//功能说明:得到各模块的系列号,以及总的系列号.
//
//参数说明: 
//                                                             
//返回值：
//	成功：
//
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
function getSerial()
{
    $DEBUG = 0;
    $dbName = 'OMC_PUB';
    $tbName = 'sysStat';
    global $ttlSerial;
    global $srlArr;
    global $noSrlStt, $ttlSrlClr;
    global $pubConn;

    $selSql = " SELECT * FROM $tbName WHERE sysTypeNo = 0";
    if ($DEBUG) echo "<br>selSql=$selSql";
    $selRes = mysqli_query($pubConn, $selSql);
    $selRow = mysqli_fetch_array($selRes);
    $detailStat = $selRow['detailStat'];
    $ttlSerial = substr($detailStat, 39 * 2, 8);
    $ttlSrlClr = "#000000";
    if ($DEBUG) echo "<br>ttlSerial=$ttlSerial<br>";

    $selSql = " SELECT * FROM $tbName WHERE sysTypeNo=320 OR sysTypeNo = 330 OR sysTypeNo = 340 OR sysTypeNo = 360 OR sysTypeNo = 380 OR sysTypeNo = 390 OR sysTypeNo = 350 ";

    if ($DEBUG) echo "<br>selSql=$selSql";
    $selRes = mysqli_query($pubConn, $selSql);
    $selRow = mysqli_fetch_array($selRes);

    do {
        if (!$selRow)
            break;
        $sysTypeNo = $selRow['sysTypeNo'];
        $sysNo = $selRow['sysNo'];
        if ($DEBUG) echo "<br>sysTypeNo=$sysTypeNo";
        if ($DEBUG) echo "<br>sysNo=$sysNo";
        $detailStat = $selRow['detailStat'];
        if ($DEBUG) echo "<br>detailStat=$detailStat";
        switch ($sysTypeNo) {
            case '320':    //MSC
                $arrNum = 0;
                $subBgn = 39;
                break;
            case '330':    //HLR
                $arrNum = 1;
                $subBgn = 39;
                break;
            case '340':    //AUC
                $arrNum = 2;
                $subBgn = 39;
                break;
            case '360':    //PPS
                $arrNum = 3;
                $subBgn = 39;
                break;
            case '380':    //EIR
                $arrNum = 4;
                $subBgn = 39;
                break;
            case '390':    //VSS
                $arrNum = 5;
                $subBgn = 39;
                break;
            case '350':    //SMSC
                $arrNum = 6;
                $subBgn = 39;
                break;
            default:
                break;
        }

        $srlArr[$arrNum][$sysNo]['number'] = substr($detailStat, $subBgn * 2, 8);
        $srlArr[$arrNum][$sysNo]['color'] = "#000000";

        if ($DEBUG) echo "<br>srlArr[$arrNum][$sysNo] = {$srlArr[$arrNum][$sysNo]}<br> ";
        if ($srlArr[$arrNum][$sysNo]['number'] != $ttlSerial) {
            $srlArr[$arrNum][$sysNo]['color'] = "#FF0000";
            $noSrlStt = 1;
        }
        if ($DEBUG) echo "<br>noSrlStt=$noSrlStt";

    } while ($selRow = mysqli_fetch_array($selRes));


    if ($DEBUG) echo "<br>ttlSerial=$ttlSerial";
    if ($noSrlStt)
        $ttlSrlClr = "#FF0000";
    else
        $ttlSrlClr = "#000000";
    if ($DEBUG) echo "<br>ttlSrlClr=$ttlSrlClr<br>";


    return 1;
}

function byte2ip($byteStr)
{
    for ($i = 0; $i < 4; $i++) {
        $unitIp = substr($byteStr, $i * 2, 2);
        $unitIp = hexdec($unitIp);
        if ($i == 0) {
            $ipStr = $unitIp;
        } else {
            $ipStr .= ".$unitIp";
        }
    }
    return ($ipStr);
}

function changeByteOrder($hex_byte)
{
    while (strlen($hex_byte) % 2 == 1)
        $hex_byte = "0" . $hex_byte;
    $byte_len = strlen($hex_byte) / 2;

    $res = "";
    for ($i = 0; $i < $byte_len; $i++) {
        $tmpstr = substr($hex_byte, $i * 2, 2);
        $res = $tmpstr . $res;
    }
    return $res;
}

function getSystemNameBySysTypeNo($sysTypeNo)
{
    global $pubConn;

    $sqlstr = "select sysTypeName from OMC_PUB.sysConf where sysTypeNo = $sysTypeNo ";
    $res = @mysqli_query($pubConn, $sqlstr);
    $row = @mysqli_fetch_array($res);
    return $row['sysTypeName'];
}

function getSystemState($sysTypeNo, $byteStart, $length)
{
    global $pubConn;
    $sql = "select detailStat from OMC_PUB.sysStat where sysTypeNo=$sysTypeNo order by updateTime DESC ";
    if ($DEBUG) echo "$sql<br>";
    $result = @mysqli_query($pubConn, $sql);
    $record_num = 0;
    $record_num = @mysqli_num_rows($result);

    if ($record_num == 0)
        return "";

    $rows = @mysqli_fetch_array($result);
    $allbin = $rows['detailStat'];

    if (strlen($allbin) < $length)
        return "";

    $resbin = substr($allbin, $byteStart, $length);

    return $resbin;
}

//****************************************************************
//函数:
//	getParamOptionArray
//功能说明:返回参数选项值的数组
//
//参数说明: 
//				sysTypeNo: sysTypeNo
//         value: parameter oid or name
//				 mode:  PARAM_OID PARAM_NAME                                
//返回值：
//	成功：
//
//作者：zhengjianhui
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------
define("PARAM_OID", 0);
define("PARAM_NAME", 1);
function getParamOptionArray($sysTypeNo, $value, $mode)
{
    $db = "OBJ_" . $sysTypeNo;
    global $objDbConn;
    if ($mode == PARAM_NAME) {
        $sql = "select * from OBJ_$sysTypeNo.paramConf where name_2 like '%$value%' ";
    } else {
        $levelArr = explode('.', $value);
        for ($j = 0; $j < sizeof($levelArr); $j++) {
            $levelNo = $j + 1;
            ${"level_$levelNo"} = $levelArr[$j];
            if ($DEBUG) echo "<BR>levelArr[$j]={$levelArr[$j]}";
            if ($j == 0) {
                $levelWhere = "level_$levelNo='${"level_$levelNo"}'";
            } else {
                $levelWhere .= " AND level_$levelNo='${"level_$levelNo"}'";
            }
        }

        $sql = "select * from OBJ_$sysTypeNo.paramConf where $levelWhere ";
    }

    $res = @mysqli_query($objDbConn, $sql);
    $row = @mysqli_fetch_array($res);
    $template = $row['setTemplate_2'];
    $options = explode(";", $template);
    for ($i = 0; $i < sizeof($options); $i++) {
        $tmp = explode("=", $options[$i]);
        $n = $tmp[0];
        $v = $tmp[1] + 0;
        //echo "value=$v,name=$n<br>";
        $arr[$v] = $n;

    }
    return $arr;
}


function getMSSFtpAccount()
{
    global $pubConn;

    $default_user_name = "subsManager";
    $default_password = "iammanager";

    $sql = "select * from OMC_PUB.omcPubVarConf where pubVarName='globalFTPUserName' ";
//	$res=@mysqli_query($pubConn,$sql);
//	$num=@mysqli_num_rows($res);
    $res = @mysqli_query($pubConn, $sql);
    $num = @mysqli_fetch_array($res);
    if ($num == 0) {
        $isql = "insert into OMC_PUB.omcPubVarConf(instance,pubVarName,pubVarValue,remark)values(18,'globalFTPUserName','subsManager','For Import subscriber, download PPS Tarriff, backup MSS subs data and MSS Configuration Synchronization');";
        @mysqli_query($pubConn, $isql);
        $isql = "insert into OMC_PUB.omcPubVarConf(instance,pubVarName,pubVarValue,remark)values(18,'globalFTPPassword','iammanager','For Import subscriber, download PPS Tarriff, backup MSS subs data and MSS Configuration Synchronization');";
        @mysqli_query($pubConn, $isql);
    }

    $row = @mysqli_fetch_array($res);
    if (strlen(trim($row['pubVarValue'])) > 0)
        $ftpInfo['UserName'] = $row['pubVarValue'];
    else
        $ftpInfo['UserName'] = $default_user_name;

    $sql = "select * from OMC_PUB.omcPubVarConf where pubVarName='globalFTPPassword' ";
    $res = @mysqli_query($pubConn, $sql);
    $row = @mysqli_fetch_array($res);
    if (strlen(trim($row['pubVarValue'])) > 0)
        $ftpInfo['Password'] = $row['pubVarValue'];
    else
        $ftpInfo['Password'] = $default_password;

    return $ftpInfo;
}

//****************************************************************
//函数:
//	alertMsgAndGoLastl()
//功能说明:使用JS提示一些信息.
//
//参数说明: 
//                                                             
//返回值：
//	成功：
//
//作者：
//      
//修改注释：      
//NO.1      
//姓名：            
//时间：      
//修改说明：    
//----------------------------------------------------------

function alertMsgAndGoLast($msg)
{
    echo "<script LANGUAGE=\"JavaScript\">";
    echo "\n alert('$msg');";
    echo "\n window.history.go(-1);";
    echo "\n </script>";
    exit();
}

//注意：下面的"? >"后面不能有空行，否则cvs数据下载时，会出错。
?>

