<?php
require("./total.inc");

session_start();
require("./ninsyou.inc");
if(0 > ninsyou_check(2)){
   exit;
   }


echo "<!DOCTYP:pserver:cvs@dhcvs.ms-n.com:/usr/home/cvsE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>";
echo "<HTML>";
echo "<HEAD>";
echo "<META name='GENERATOR' content='AmBuilder'>";
echo "<META http-equiv='Content-Type' content='text/html; charset=euc-jp'>";
	echo "<title>zither monitorring save info</title>";

echo "<SCRIPT LANGUAGE='JavaScript'>";
echo "<!--

function WinCls(){
        window.close()
                       }
// window.onload=WinCls();
//-->";
echo "</SCRIPT>";

echo "</head>";
echo "<body alink='#ff0000' bgcolor='#ffffff' link='#0000ff' text='#000000' vlink='#000080'>";

echo "<pre>";


$C_NAME = enco($_GET["C_NAME"]);
$CIP = enco($_GET["CIP"]); 
$C_INFO = enco($_GET["C_INFO"]);
$SOCK_IPADDR = enco($_GET["SOCK_IPADDR"]); 
$SOCK2_IPADDR = enco($_GET["SOCK2_IPADDR"]);
$SOCK_PORT = enco($_GET["SOCK_PORT"]);
$SOCK2_PORT = enco($_GET["SOCK2_PORT"]);
$BAT_FILE = enco($_GET["BAT_FILE"]);
$BAT2_FILE = enco($_GET["BAT2_FILE"]);
$COMMAND_PATH = enco($_GET["COMMAND_PATH"]);
$BAT_INT = enco($_GET["BAT_INT"]);
$BAT2_INT = enco($_GET["BAT2_INT"]);
$LIFE_INT = enco($_GET["LIFE_INT"]);
$LIFE_LOS =  enco($_GET["LIFE_LOS"]);
$MAIL_CC = enco($_GET["MAIL_CC"]); 
$UPDATE_COM = enco($_GET["UPDATE_COM"]); 
$GEN = enco($_GET["GEN"]);

$COM_MONITOR = enco($_GET["COM_MONITOR"]); 

$C_BLOCK_NAME = $C_NAME . "-default";

$UP_TIME = date("Y m d g i s A");   



print($font_size);
print($version_name);
print(date("Y m d g i s A"));
print("\n");
print("監視クライアント $CNAME の起動ファイルを編集できます\n");


if(0 == strlen($C_NAME)){
   print("NOT hostname");
   exit;
   }
if(0 == strlen($CIP)){
   print("NOT client ip");
   exit;
   }


$conn = db_connect();
if(!$conn){
echo "NG DBに接続出来ません<br>";
   exit;
   }


$result = db_query($conn, "select * from connection where cname='$C_NAME';");
if(!$result){
   print("NG 不正な問い合わせです\n");
   exit;
   }
$num = db_num_rows($result);
if($num > 0){
   print("NG すでにデータが存在します\n");
   exit;
   }


$result = db_query($conn, "select * from config where c_name='$C_NAME';");
if(!$result){
   print("NG 不正な問い合わせです 1\n");
   exit;
   }
$num = db_num_rows($result);
if($num > 0){
   $result = db_query($conn, "update config set
    c_name='$C_NAME',
    c_info='$C_INFO',
    sock_ipaddr='$SOCK_IPADDR',
    sock_port='$SOCK_PORT',
    sock2_ipaddr='$SOCK2_IPADDR',
    sock2_port='$SOCK2_PORT',
    bat_file='$BAT_FILE',
    bat2_file='$BAT2_FILE',
    command_path='$COMMAND_PATH',
    bat_int=$BAT_INT,
    bat2_int=$BAT2_INT,
    life_int=$LIFE_INT,
    life_los=$LIFE_LOS,
    mail_cc='$MAIL_CC',
    update_com='$UPDATE_COM',
    up_time='$UP_TIME'
    where c_name='$C_NAME'");

   if(!$result){
      print("NG データは空っぽです\n");
      exit;
      }
   }
else{
   $result = db_query($conn, "insert into config
    (
    c_name,
    c_info,
    sock_ipaddr,
    sock_port,
    sock2_ipaddr,
    sock2_port,
    bat_file,
    bat2_file,
    command_path,
    bat_int,
    bat2_int,
    life_int,
    life_los,
    mail_cc,
    update_com,
    up_time
     ) values (
    '$C_NAME',
    '$C_INFO',
    '$SOCK_IPADDR',
    '$SOCK_PORT',
    '$SOCK2_IPADDR',
    '$SOCK2_PORT',
    '$BAT_FILE',
    '$BAT2_FILE',
    '$COMMAND_PATH',
    $BAT_INT,
    $BAT2_INT,
    $LIFE_INT,
    $LIFE_LOS,
    '$MAIL_CC',
    '$UPDATE_COM',
    '$UP_TIME'
    )");

   if(!$result){
      print("NG 間違ったクエリーです1\n");
      exit;
      }
   }


$result = db_query($conn, "select * from connection where cname='$C_NAME';");
if(!$result){
   print("NG 不正な問い合わせです 2\n");
   exit;
   }
$num = db_num_rows($result);
if($num > 0){
   $result = db_query($conn, "update connection set
    cname='$C_NAME',
    cip='$CIP'
    where cname='$C_NAME'");

   if(!$result){
      print("NG 間違ったクエリーです2\n");
      exit;
      }
   print("connection.sqlにデータが存在しますが、上書きします\n");
   }

else{
   $result = db_query($conn, "insert into connection
    (
    uct,
    ye,
    mo,
    da,
    ha,
    mi,
    se,
    cname,
    cip,
    server,
    mode,
    stat
    ) values (
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    '$C_NAME',
    '$CIP',
    'NOT',
    'NOT',
    'FREE')
    ");
   if(!$result){
      print("NG 間違ったクエリーです3\n");
      exit;
      }
   print("コネクションDBに $C_NAME がないので新規追加\n");
   }



$start_file = "/usr/local/zither/conf/$C_NAME.conf";
$result = db_query($conn, "select * from config where c_name='$C_NAME';");
if(!$result){
   print("NG 間違ったクエリーです4\n");
   exit;
   }
$num = db_num_rows($result);
if($num > 0){
   if(($fp = fopen($start_file, "w+"))==NULL){
      print("NG file not open $start_file\n");
      exit;
      }
   $s = 0;
   while($s < $num){
      $DATA = enco_rtrim(db_fetch_Result($result, $s, c_name));
      if(0 != strlen($DATA)){ fwrite($fp, "C_NAME:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, c_info));
      if(0 != strlen($DATA)){ fwrite($fp, "C_INFO:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, sock_ipaddr));
      if(0 != strlen($DATA)){ fwrite($fp, "SOCK_IPADDR:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, sock2_ipaddr));
      if(0 != strlen($DATA)){ fwrite($fp, "SOCK2_IPADDR:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, sock_port));
      if(0 != strlen($DATA)){ fwrite($fp, "SOCK_PORT:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, sock2_port));
      if(0 != strlen($DATA)){ fwrite($fp, "SOCK2_PORT:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, bat_file));
      if(0 != strlen($DATA)){ fwrite($fp, "BAT_FILE:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, bat2_file));
      if(0 != strlen($DATA)){ fwrite($fp, "BAT2_FILE:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, command_path));
      if(0 != strlen($DATA)){ fwrite($fp, "COMMAND_PATH:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, bat_int));
      if(0 != strlen($DATA)){ fwrite($fp, "BAT_INT:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, bat2_int));
      if(0 != strlen($DATA)){ fwrite($fp, "BAT2_INT:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, life_int));
      if(0 != strlen($DATA)){ fwrite($fp, "LIFE_INT:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, life_los));
      if(0 != strlen($DATA)){ fwrite($fp, "LIFE_LOS:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, mail_cc));
      if(0 != strlen($DATA)){ fwrite($fp, "MAIL_CC:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, update_com));
      if(0 != strlen($DATA)){ fwrite($fp, "UPDATE_COM:$DATA\n"); }
      $DATA = enco_rtrim(db_fetch_Result($result, $s, up_time));

      $s++;
      }

   fclose($fp);

   echo "$C_NAME.conf file update ok\n";
   echo "成功 \n";
   }
else{
   echo "$C_NAME.conf file not update!!\n";
   echo "失敗ですぅ \n";
   }














$result = db_query($conn, "select * from monitor where cname='$C_NAME';");
if(!$result){
   print("bud query set(select)\n");
   exit;
   }
$num = db_num_rows($result);
if($num < 1){

   if(0 == strcmp($COM_MONITOR, "ON")){   

      $result = db_query($conn, "insert into monitor
         (
         come,
         cname,
         monitor,
         sign,
         proto,
         com,
         cip,
         port,
         wtime,
         pinfo,
         prino,
         free1,
         free2,
         free3,
         free4
         ) values (
         'ON',
         '$C_NAME',
         'COMAND',
         0,
         'NOT',
         'dfc.sh none 90',
         '$CIP',
         'NOT',
         8,
         '$C_NAME($CIP)',
         2,
         'NOT',
         'NOT',
         'NOT',
         '$C_BLOCK_NAME') ");
      if(!$result){
         print("bud query set\n");
         exit;
         }

      $result = db_query($conn, "insert into monitor
         (
         come,
         cname,
         monitor,
         sign,
         proto,
         com,
         cip,
         port,
         wtime,
         pinfo,
         prino,
         free1,
         free2,
         free3,
         free4
         ) values (
         'ON',
         '$C_NAME',
         'COMAND',
         0,
         'NOT',
         'inode.sh none 40',
         '$CIP',
         'NOT',
         8,
         '$C_NAME($CIP)',
         2,
         'NOT',
         'NOT',
         'NOT',
         '$C_BLOCK_NAME') ");
      if(!$result){
         print("bud query set\n");
         exit;
         }

      $result = db_query($conn, "insert into monitor
         (
         come,
         cname,
         monitor,
         sign,
         proto,
         com,
         cip,
         port,
         wtime,
         pinfo,
         prino,
         free1,
         free2,
         free3,
         free4
         ) values (
         'ON',
         '$C_NAME',
         'COMAND',
         0,
         'NOT',
         'uptime.sh 40',
         '$CIP',
         'NOT',
         8,
         '$C_NAME($CIP)',
         2,
         'NOT',
         'NOT',
         'NOT',
         '$C_BLOCK_NAME') ");
      if(!$result){
         print("bud query set\n");
         exit;
         }

      $result = db_query($conn, "insert into monitor
         (
         come,
         cname,
         monitor,
         sign,
         proto,
         com,
         cip,
         port,
         wtime,
         pinfo,
         prino,
         free1,
         free2,
         free3,
         free4
         ) values (
         'ON',
         '$C_NAME',
         'COMAND',
         0,
         'NOT',
         'freemem.sh 1000',
         '$CIP',
         'NOT',
          8,
          '$C_NAME($CIP)',
          2,
          'NOT',
          'NOT',
          'NOT',
          '$C_BLOCK_NAME') ");
      if(!$result){
         print("bud query set\n");
         exit;
         }        

      print("OK 監視ブロックを作成します\n");
      $result = db_query($conn, "insert into userblock
        (
        youname,
        bname,
        cname,
        mailsend,
        stat,
        plustime,
        starttime,
        stopsettime,
        mailto,
        mailcc,
        free1,
        free2,
        free3
        ) values (
        'test',
        '$C_BLOCK_NAME',
        '$C_NAME',
        'ON',
        'NON',
        60,
        0,
        0,
        'test@localhost',
        'test@localhost',
        'It corrects it. ',
        'NOT',
        'NOT'
      )");
      if(!$result){
         print("NG 監視ブロックを作成出来ませんでした\n");
         exit;
         }
      }

   db_free_result($result);

   echo "OK Inser data\n";
   echo "$C_NAME\n";
   echo "$CIP\n";
   }






$conf_file = "/usr/local/zither/conf/$C_NAME.stat";
if(($fp = fopen($conf_file, "w+"))==NULL){
   print("stat file not open\n");
   exit;
   }
$result = db_query($conn, "select * from connection where cname='$C_NAME'");
if(!$result){
   print("bud query\n");
   exit;
   }
$num = db_num_rows($result);
if($num <= 0){
   print("no data\n");
   fwrite($fp, "START");
   echo "stat file update ok(START)\n";
   echo "成功 \n";
   }
else{
   $s = 0;
   while($s < $num){
      $MODE = enco_rtrim(db_fetch_Result($result, $s, mode));
      fwrite($fp, $MODE);
      echo "stat file update ok\n";
      echo "成功 \n";
      $s++;
      }
   }

fclose($fp);




$conf_file = "/usr/local/zither/conf/$C_NAME.command";
if(($fp = fopen($conf_file, "w+"))==NULL){
   print("$conf_file file not open\n");
   exit;
   }
else{
   $conf_file2 = "/usr/local/zither/conf/test.command";
   if(($fp2 = fopen($conf_file2, "r+"))!=NULL){
      print("write open file name $conf_file \n");
      print("read open file name $conf_file2 \n");
      while(1){
         $file_data = fgets($fp2, 2048);
         fwrite($fp, "$file_data");
         if(0 == strcmp($file_data, NULL)){
            break;
            }
         }
      fclose($fp2);
      }
   else{
      fwrite($fp, "##\n");
      fwrite($fp, "##\n");
      fclose($fp);
      }
   }


echo "command file update ok\n";
echo "成功 \n";


$conf_file = "/usr/local/zither/conf/neg.conf";
$result = db_query($conn, "select * from connection");
if(!$result){
   print("bud query conection\n");
   exit;
   }$num = db_num_rows($result);
if($num <= 0){
   print("no data conection\n");
   exit;
   }
if(($fp = fopen($conf_file, "w+"))==NULL){
   print("file not open neg.conf\n");
   exit;   }  
$s = 0;
while($s < $num){
   $CNAME = enco_rtrim(db_fetch_Result($result, $s, cname));
   $CIP = enco_rtrim(db_fetch_Result($result, $s, cip));
   fwrite($fp, "$CNAME:$CIP\n");
   print("$CNAME:$CIP\n");
   $s++;
   }
fclose($fp);
echo "neg.conf file update ok\n";
echo "成功 \n";




list_file_upload($C_NAME, $GEN);




db_free_result($result);
db_Close($conn);

print("</font>");

?>

</pre>
</body>
</html>


