<?php

// ----- block name -> client name ----- //

function cname_return($FREE4)
{

$conn = db_connect();
if(!$conn){
   echo "not connection!<br>";
   exit;
   }
$result = db_query($conn, "select * from userblock where bname='$FREE4'");
if(!$result){
   print("bud query set\n");
   exit;
   }
$num = db_num_rows($result);
if($num > 0){
   while($s < $num){
      $CNAME = enco_rtrim(db_fetch_Result($result, $s, cname));
      echo "---------- $CNAME -------------\n";
      $s++;
      }
   }
db_free_result($result);
return($CNAME);
}




// --- list file upload --- // 

function list_file_upload($CNAME, $GEN){

require("./total.inc");

$conf_file = "/usr/local/zither/conf/$CNAME.list";
$lock_file = $conf_file . "_lock";



// total.inc で設定されていない場合のみ実行
if(0 != strcmp($file_write_on, "OFF")){

echo "conf file name = $conf_file\n";
echo "conf lock file name = $lock_file\n";


for($countx = 0; $countx < 15; $countx++){
   if(($lfp = fopen($lock_file, "r"))==NULL){                   // lock ファイルが無い場合
      echo "$lock_file ロックファイルが無いことを確認 \n";
      if(($lfp = fopen($lock_file, "w+"))!=NULL){               // lock ファイルが作れた場合
         echo "$lock_file ロックファイルを作成出来たことを確認 \n";
         if(($fp = fopen($conf_file, "w+"))!=NULL){                      // list ファイルが開けた場合
            echo "リストファイルが作成出来たことを確認 \n";
            fwrite($fp, "## zither moniterring server list file ##\n");

            $conn = db_connect();
            if(!$conn){
               echo "$lock_file ロックファイルを削除します \n";
               unlink($lock_file);
               print("NG DBに接続出来ませんでした\n");
               exit;
               }

            $result = db_query($conn, "select distinct(bname) from userblock where cname='$CNAME';");
            if(!$result){
               echo "$lock_file ロックファイルを削除します \n";
               unlink($lock_file);
               print("bud query\n");
               exit;
               }
            $num = db_num_rows($result);
            if($num <= 0){
               echo "$lock_file ロックファイルを削除します \n";
               unlink($lock_file);
               print("no data\n");
               exit;
               }

            $s = 0;

            while($s < $num){

               $BNAME = enco_rtrim(db_fetch_Result($result, $s, bname));

               $result2 = db_query($conn, "select * from userblock where bname='$BNAME'");
               if(!$result2){
                  print("bud query\n");
                  echo "$lock_file ロックファイルを削除します \n";
                  unlink($lock_file);
                  exit;
                  }
               $s2 = 0;
               $num2 = db_num_rows($result2);
               if($num2 > 0){
                  $YOUNAME = enco_rtrim(db_fetch_Result($result2, $s2, youname));
//                $CNAME = enco_rtrim(db_fetch_Result($result2, $s2, cname));
                  $BNAME = enco_rtrim(db_fetch_Result($result2, $s2, bname));
                  $MAILSEND = enco_rtrim(db_fetch_Result($result2, $s2, mailsend));
                  $STAT = enco_rtrim(db_fetch_Result($result2, $s2, stat));
                  $PLUSTIME = enco_rtrim(db_fetch_Result($result2, $s2, plustime));
                  $STARTTIME = enco_rtrim(db_fetch_Result($result2, $s2, starttime));
                  $STOPSETTIME = enco_rtrim(db_fetch_Result($result2, $s2, stopsettime));
                  $MAILTO = enco_rtrim(db_fetch_Result($result2, $s2, mailto));
                  $MAILCC = enco_rtrim(db_fetch_Result($result2, $s2, mailcc));
                  $FREE1 = enco_rtrim(db_fetch_Result($result2, $s2, free1));
                  $FREE2 = enco_rtrim(db_fetch_Result($result2, $s2, free2));
                  $FREE3 = enco_rtrim(db_fetch_Result($result2, $s2, free3));

                  $YOUNMAE = str_replace(",", "|", $YOUNAME);
                  $BNAME = str_replace(",", "|", $BNAME);
                  $MAILSEND = str_replace(",", "|", $MAILSEND);
                  $STAT = str_replace(",", "|", $STAT);
                  $PLUSTIME = str_replace(",", "|", $PLUSTIME);
                  $STARTTIME = str_replace(",", "|", $STARTTIME);
                  $STOPSETTIME = str_replace(",", "|", $STOPSETTIME);
                  $MAILTO = str_replace(",", "|", $MAILTO);
                  $MAILCC = str_replace(",", "|", $MAILCC);
                  $FREE1 = str_replace(",", "|", $FREE1);
                  $FREE2 = str_replace(",", "|", $FREE2);
                  $FREE3 = str_replace(",", "|", $FREE3);

//  SET,0,FOR,NOT,NOT,4,6,NOT,120,ON,taro@localhost|edge@localhost,masashi@localhost,hana4-6,

                  fwrite($fp, "************\n");
//                  fwrite($fp, "SET,0,$STAT,$YOUNAME,$CNAME,$STARTTIME,$STOPSETTIME,NOT,$PLUSTIME,$MAILSEND,$MAILTO,$MAILCC,$BNAME,\n");
                  fwrite($fp, "SET,0,$STAT,$YOUNAME,$CNAME,$STARTTIME,$STOPSETTIME,$FREE2,$PLUSTIME,$MAILSEND,$MAILTO,$MAILCC,$BNAME,\n");

                  db_free_result($result2);
                  }


               $result2 = db_query($conn, "select * from monitor where cname='$CNAME' and free4='$BNAME'");
               if(!$result2){
                  print("bud query\n");
                  echo "$lock_file ロックファイルを削除します \n";
                  unlink($lock_file);
                  exit;
                  }
               $s2 = 0;
               $num2 = db_num_rows($result2);
               if($num2 >= 0){
                  while($s2 < $num2){
                     $COME = enco_rtrim(db_fetch_Result($result2, $s2, come));
                     $CNAME = enco_rtrim(db_fetch_Result($result2, $s2, cname));
                     $MONITOR = enco_rtrim(db_fetch_Result($result2, $s2, monitor));
                     $SIGN = enco_rtrim(db_fetch_Result($result2, $s2, sign));
                     $PROTO = enco_rtrim(db_fetch_Result($result2, $s2, proto));
                     $COM = enco_rtrim(db_fetch_Result($result2, $s2, com));
                     $CIP = enco_rtrim(db_fetch_Result($result2, $s2, cip));
                     $PORT = enco_rtrim(db_fetch_Result($result2, $s2, port));
                     $WTIME = enco_rtrim(db_fetch_Result($result2, $s2, wtime));
                     $PINFO = enco_rtrim(db_fetch_Result($result2, $s2, pinfo));
                     $PRINO = enco_rtrim(db_fetch_Result($result2, $s2, prino));
                     $FREE1 = enco_rtrim(db_fetch_Result($result2, $s2, free1));
                     $FREE2 = enco_rtrim(db_fetch_Result($result2, $s2, free2));
                     $FREE3 = enco_rtrim(db_fetch_Result($result2, $s2, free3));
                     $FREE4 = enco_rtrim(db_fetch_Result($result2, $s2, free4));
                     $UNO = enco_rtrim(db_fetch_Result($result2, $s2, uno));

                     $COME = str_replace(",", "|", $COME);
                     $CNAME = str_replace(",", "|", $CNAME);
                     $MONITOR = str_replace(",", "|", $MONITOR);
                     $SIGN = str_replace(",", "|", $SIGN);
                     $PROTO = str_replace(",", "|", $PROTO);
                     $COM = str_replace(",", "|", $COM);
                     $CIP = str_replace(",", "|", $CIP);
                     $PORT = str_replace(",", "|", $PORT);
                     $WTIME = str_replace(",", "|", $WTIME);
                     $PINFO = str_replace(",", "|", $PINFO);
                     $PRINO = str_replace(",", "|", $PRINO);
                     $FREE1 = str_replace(",", "|", $FREE1);
                     $FREE2 = str_replace(",", "|", $FREE2);
                     $FREE3 = str_replace(",", "|", $FREE3);
                     $FREE4 = str_replace(",", "|", $FREE4);

                     if(0 == strcmp($COME, "OFF")){
                        fwrite($fp, "# ");
                        }
                     else{
                        }
//       fwrite($fp, "$MONITOR,$SIGN,$PROTO,$COM,$CIP,$PORT,$WTIME,$PINFO,$PRINO,$FREE1,$FREE2,$FREE3,$FREE4,\n");
//  FREE3を監視のユニーク番号に変更
                     fwrite($fp, "$MONITOR,$SIGN,$PROTO,$COM,$CIP,$PORT,$WTIME,$PINFO,$PRINO,$FREE1,$FREE2,$UNO,$FREE4,\n");
                     $s2++;
                     }
                  db_free_result($result2);
                  }
               $s++;
               }
            echo "リストファイルの仕事が終わったのでストリームの開放です \n";
            fclose($fp);
            echo "file update ok\n";
            echo "成功 \n";
            }
         else{
            echo "$lock_file ロックファイルを削除します \n";
            unlink($lock_file);
            print("NG ファイル名 $conf_file が開けません\n");
            exit;
            }
         echo "$lock_file ロックファイルを削除します \n";
         unlink($lock_file);
         break;
         }
      else{    // lock file が作れない場合
         print("NG ファイル名 $lock_file が開けません\n");
         break;
         }
      }
   else{     // lock file があった場合
      echo "ループしてロックファイルが消えるのを待ちます。\n";
      fclose($flp);
      sleep(1);
      }
   echo "ループしてロックファイルが消えるのを待っています$count\n";
   }     // for()

}
else{
   echo "書き込みOFFに設定されてます\n";
   }

db_free_result($result);






// ---- 他のサーバに反映 ---- //

if(0 >= strlen($GEN)){


   $s = 0;
   $result = db_query($conn, "select distinct(server) from connection;");
   if(!$result){
      print("bud query\n");
      exit;
      }
   $num = db_num_rows($result);
   if($num <= 0){
      print("no data\n");
      exit;
      }
   else{

      while($s < $num){
         $SERVER = enco_rtrim(db_fetch_Result($result, $s, server));

         print("$SERVER $my_web_ip ");

         if(0 != strcmp($SERVER, $my_web_ip)){

            $T_1 = "/monitor_upnew.php" . "?";
            $T_2 = "COME=" . urlencode($COME) . "&"; 
            $T_3 = "CNAME=" . urlencode($CNAME) . "&";
            $T_4 = "MONITOR=" . urlencode($MONITOR) . "&";
            $T_5 = "SIGN=" . urlencode($SIGN) . "&";
            $T_6 = "PROTO=" . urlencode($PROTO) . "&";
            $T_7 = "COM=" . urlencode($COM) . "&";
            $T_8 = "CIP=" . urlencode($CIP) . "&";
            $T_9 = "PORT=" . urlencode($PORT) . "&";
            $T_10 = "WTIME=" . urlencode($WTIME) . "&";
            $T_11 = "PINFO=" . urlencode($PINFO) . "&";
            $T_12 = "PRINO=" . urlencode($PRINO) . "&";
            $T_13 = "FREE1=" . urlencode($FREE1) . "&";
            $T_14 = "FREE2=" . urlencode($FREE2) . "&";
            $T_15 = "FREE3=" . urlencode($FREE3) . "&";
            $T_16 = "FREE4=" . urlencode($FREE4) . "&";
            $T_17 = "GEN=" . urlencode("GEN_taro") . "&";
            $T_18 = "UNO=" . urlencode($UNO);
            $T_ALL = $T_1 . $T_2 . $T_3 . $T_4 . $T_5 . $T_6 . $T_7 . $T_8 . $T_9 . $T_10 . $T_11 . $T_12 . $T_13 . $T_14 . $T_15 . $T_16 . $T_17 . $T_18;

            print("<br>");
            print("$SERVER$T_ALL");
            http_open($SERVER, $T_ALL, $my_web_ip);

            }

         $s++;
         }
      }
   db_free_result($result);
   db_Close($conn);

   }

}

?>

