<?php

require("./total.inc");

session_start();
require("./ninsyou.inc");
if(0 > ninsyou_check(5)){
   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>";
/* date */

$ncol1 = "bgcolor=#f0f0d2";
$ncol2 = "bgcolor=#d2f0f0";

$COME = enco($_GET["COME"]);
$CNAME = enco($_GET["CNAME"]);
$MONITOR = enco($_GET["MONITOR"]);
$SIGN = enco($_GET["SIGN"]);
$PROTO = enco($_GET["PROTO"]);
$COM = enco($_GET["COM"]);
$CIP = enco($_GET["CIP"]);
$PORT = enco($_GET["PORT"]);
$WTIME = enco($_GET["WTIME"]);
$PINFO = enco($_GET["PINFO"]);
$PRINO = enco($_GET["PRINO"]);
$FREE1 = enco($_GET["FREE1"]);
$FREE2 = enco($_GET["FREE2"]);
$FREE3 = enco($_GET["FREE3"]);
$FREE4 = enco($_GET["FREE4"]);
$UNO = enco($_GET["UNO"]);
$GEN = enco($_GET["GEN"]);

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

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

print($font_size);
print($version_name);
print(date("Y m d g i s A"));
print("\n");



// 引数が足りているかの確認

if(0 >= strlen($CNAME)){
   print("NOT CNAME ERR \n");
   }

if(0 >= strlen($FREE4)){
   print("NOT FREE4 ERR \n");
   }

if(0 >= strlen($UNO)){
   print("NOT UNO ERR \n");
   }

if(0 >= strlen($GEN)){
   print("NOT GEN ERR \n");
   }




//  新ブロックが実際にあるかの確認 新しい監視クライアント名を記憶します
$CNAME_NEW = cname_return($FREE4);
if(0 >= strlen($CNAME_NEW)){
   echo "not cname return!<br>";
   exit;
   }




// DBに接続
$conn = db_connect();
if(!$conn){
   echo "not connection!<br>";
   exit;
   }

if(0 < $MAX){
   $count = 0;
   $result=db_query($conn,"select cname from monitor where free4='$FREE4';");
   if(!$result){
      print("canem select , bud query\n");
      exit;
      }
   $maxnum = db_num_rows($result);
   if($maxnum >= $MAX){
      print("MAX $MAX \n");
      exit;
      }

   db_free_result($result);
   }




// 変更前のブロック名とクライアント名を記憶します
$result = db_query($conn, "select * from monitor where uno='$UNO'");
if(!$result){
   print("bud query set\n");
   exit;
   }
$num = db_num_rows($result);
if($num > 0){
   $FREE4_OLD = enco_rtrim(db_fetch_Result($result, $s, free4));
   $CNAME_OLD = enco_rtrim(db_fetch_Result($result, $s, cname));
   }
else{
   print("not data?\n");
   exit;
   }
db_free_result($result);



if(0 < strlen($GEN)){

   // UPDATE なのでアップデートします
   if($STATUS == "UPDATE"){
      $result = db_query($conn, "update monitor set
       come='$COME',
       cname='$CNAME_NEW',
       monitor='$MONITOR',
       sign=$SIGN,
       proto='$PROTO',
       com='$COM',
       cip='$CIP',
       port='$PORT',
       wtime=$WTIME,
       pinfo='$PINFO',
       prino=$PRINO,
       free1='$FREE1',
       free2='$FREE2',
       free3='$FREE3',
       free4='$FREE4'
       where uno=$UNO");

      if(!$result){
         print("bud query set\n");
         exit;
         }
      db_free_result($result);
      print("変更点はDBに反映されました\n");
      }

  // UPDATEではないので、新規に追加します。
   else{
      $result = db_query($conn, "insert into monitor
        (
        come,
        cname,
        monitor,
        sign,
        proto,
        com,
        cip,
        port,
        wtime,
        pinfo,
        prino,
        free1,
        free2,
        free3,
        free4
        ) values (
       '$COME',
       '$CNAME_NEW',
       '$MONITOR',
       $SIGN,
       '$PROTO',
       '$COM',
       '$CIP',
       '$PORT',
       $WTIME,
       '$PINFO',
       $PRINO,
       '$FREE1',
       '$FREE2',
       '$FREE3',
       '$FREE4')
       ");
      if(!$result){
         print("bud query set\n");
         exit;
         }
      }

   // 新旧監視クライアントのファイル変更
   print("監視ブロックが変更されています\n");
   // --- list file upload --- //
   if(0 != strcmp($CNAME_NEW, $CNAME_OLD)){
      list_file_upload($CNAME_NEW, $GEN);
      list_file_upload($CNAME_OLD, $GEN);
      }
   else{
      list_file_upload($CNAME_NEW, $GEN);
      }
   }


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

?>

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


