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

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

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

// $BNAME = enco($_GET["BNAME"]);
$CNAME = enco($_GET["CNAME"]);
$MAILTO = enco($_GET["MAILTO"]);
$MAILCC = enco($_GET["MAILCC"]);
$MAILSEND = enco($_GET["MAILSEND"]);
$PLUSTIME = enco($_GET["PLUSTIME"]);
$STAT = enco($_GET["STAT"]);
$STARTTIME = enco($_GET["STARTTIME"]);
$STOPSETTIME = enco($_GET["STOPSETTIME"]);
$FREE1 = enco($_GET["FREE1"]);
$select = enco($_GET["SELECT"]);



// 時間に間するエラーチェック
if(0 != strcmp("ASCII", mb_detect_encoding($STARTTIME))){
   print("NG NOT ASCII enc \n");
   $STARTTIME = "";
   }
if(0 != strcmp("ASCII", mb_detect_encoding($STOPSETTIME))){
   print("NG NOT ASCII enc \n");
   $STOPSETTIME = "";
   }
if(5 != strlen($STARTTIME)){
   print("NG NOT time format 00:00 \n");
   $STARTTIME = "";
   }
if( 5 != strlen($STOPSETTIME)){
   print("NG NOT time format 00:00 \n");
   $STOPSETTIME = "";
   }
if(!preg_match("/^[0-9]+$/", substr($STARTTIME, 0, 2))){
   print("NG NOT a int\n");
   $STARTTIME = "";
   }
if(!preg_match("/^[0-9]+$/", substr($STOPSETTIME, 3, 4))){
   print("NG NOT a int\n");
   $STOPSETTIME = "";
   }





$new_cname_ok = 0;
$count = 0;
$SE="LIKE";
$EE="=";

// ----- DB 接続 ----//

$conn = db_connect();
if(!$conn){
   echo "not connection!<br>";
   exit;
   }


// ------ 検索内容のあんでこーど -----//
if(0 != strlen($select)){
   $select_get = urldecode($select);
   print("検索内容のあんでこーど $select_get \n");
   $select_data = "update userblock set ";
   }
else{
   echo "NG NOT WHER DATA! \n";
   exit;
   }





// ------ 変更内容の作成 -----//
print("------ 情報を生理しています -----\n");
if(0 != strlen($MAILTO)){
   if($count > 0){
      $select_data = $select_data . ", ";
      }
   $count++; 
   $select_data = $select_data . " mailto='$MAILTO'";
   }

if(0 != strlen($MAILCC)){
   if($count > 0){
      $select_data = $select_data . ", ";
      }
   $count++; 
   $select_data = $select_data . " MAILCC='$MAILCC'";
   }

if(0 != strlen($MAILSEND)){
   if($count > 0){
      $select_data = $select_data . ", ";
      }
   $count++; 
   $select_data = $select_data . " MAILSEND='$MAILSEND'";
   }

if(0 != strlen($PLUSTIME)){
   if($count > 0){
      $select_data = $select_data . ", ";
      }
   $count++; 
   $select_data = $select_data . " plustime='$PLUSTIME'";
   }

if(0 != strlen($STAT)){
   if($count > 0){
      $select_data = $select_data . ", ";
      }
   $count++; 
   $select_data = $select_data . " stat='$STAT'";
   }

if(0 != strlen($STARTTIME)){
   if($count > 0){
      $select_data = $select_data . ", ";
      }
   $count++; 
   $select_data = $select_data . " starttime='$STARTTIME'";
   }

if(0 != strlen($STOPSETTIME)){
   if($count > 0){
      $select_data = $select_data . ", ";
      }
   $count++; 
   $select_data = $select_data . " stopsettime='$STOPSETTIME'";
   }

if(0 != strlen($FREE1)){
   if($count > 0){
      $select_data = $select_data . ", ";
      }
   $count++; 
   $select_data = $select_data . " free1='$FREE1'";
   }
print("へんこう内容の作成: $select_data ");





// CNAME に変更がある場合はチェック
if(0 != strlen($CNAME)){
   if($count > 0){
      $select_data = $select_data . ", ";
      }
   $count++; 
   $select_data = $select_data . " cname='$CNAME'";
   $new_cname_ok = 1;

   print("CNAME に変更がありました new_cname_ok = $new_cname_ok\n");
   print("検索条件から 監視ブロック名リスト作成 \n");

   $bname_count = 0;
   $result = db_query($conn, "select distinct(bname) from userblock $select_get;");
   if(!$result){
      print("banem select , bud query\n");
      exit;
      }
   $num = db_num_rows($result);
   if($num > 0){
      while($bname_count < $num){
         $bname_data[$bname_count] = enco_rtrim(db_fetch_Result($result, $bname_count, bname));
         print("番号 ($bname_count) 検索条件から検索したBNAME= $bname_data[$bname_count] をリストに追加\n");
         $bname_count++;
         }
      }
   else{
      print("NG 監視ブロックが無いです \n");
      }
   db_free_result($result);

   }



print("COUNT = $count DATA $select_data\n");
if(0 == $count){
   print("NG SELECT DATA ERR 変更点が無いので終了します\n");
   exit;
   }



$select_data = $select_data . " $select_get";

print("UPDATE COMMAND = $select_data\n");






print("変更前のCNAMEをリスト \n");

$result = db_query($conn, "select distinct(cname) from userblock $select_get");
if(!$result){
   print("NG bud query\n");
   exit;
   }
$num = db_num_rows($result);
if($num <= 0){
   print("no data\n");
   exit;
   }
else{
   $cname_count =0;
   while($cname_count < $num){
      $cname_data[$cname_count]  = enco_rtrim(db_fetch_Result($result, $cname_count, cname));
      print("番号 ($cname_count) 検索条件から変更前クライアントをリスト CNAME= $cname_data[$cname_count]");
      print("\n");
      $cname_count++;
      }
   }
if($cname_count < 0){
   print("NG:  変更前クライアントのリストが作れませんでした \n");
   exit;
   }



print("------ userblock DB変更します------- \n");


if(0 == strlen($select_data)){
   print("NG NOT select_data");
   exit;
   }
else{
   $result = db_query($conn, "$select_data");
   print("監視設定を変更ちゅうです select_data = $select_data\n");
   if(!$result){
      print("NG bud query $select_data\n");
      exit;
      }
   $num = db_num_rows($result);
   if($num < 0){
      print("NG 監視ブロックの設定が変更出来ていません\n");
      exit;
      }
   }
if($new_cname_ok == 1){

   print("------ monitor DB変更します------- \n");

   for($count = 0; $count < $bname_count; $count++){

      $qudata =  "update monitor set cname = " . "'" . $CNAME . "'" . " where free4 = " . "'" . $bname_data[$count] . "'";

      print("CNAME変更 BNAME= $bname_data[$count] を含む監視設定のクライアントを $CNAME に変更します$count $qudata");
      print("\n");

      $result = db_query($conn, $qudata);
      if(!$result){
         print("NG bud query $select_data\n");
         exit;
         }
      $num = db_num_rows($result);
      if($num < 0){
         print("NG monitor DB 監視設定が変更出来ていません\n");
         exit;
         }
      db_free_result($result);
      }
   }



print("OK DBの変更は完了しました\n");



//------ 設定ファイルへ反映（CNAMEのリストから） -----//

printf("------- ファイルへ反映しています --------\n");
if($cname_count < 0){
   exit;
   }
else{
   for($count = 0; $count < $cname_count; $count++){
      print("変更前のクライアント(1)  $cname_data[$count]  番号　 $count");
      print("\n");

      if($new_cname_ok == 1){
         print("変更後クライアント(2) $CNAME");
         print("\n");
         list_file_upload($CNAME, $GEN);
         }

      list_file_upload($cname_data[$count], $GEN);
      }
   }



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

?>

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

