<?php
///////////////////////////////////////////////////
//2010 (C)
//Карпухин Павел
///////////////////////////////////////////////////

$lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0,2);
if(is_file("lang/".$lang."/".$lang.".php")) {include "lang/".$lang."/".$lang.".php";}
 else {include "lang/ru/ru.php";}

$title = $TEXT['addr-sost-tit2'];

require_once "config.php";
require_once "utils/functions.php";

//require_once "lang/".$lang."/trans.php";
if(is_file("lang/".$lang."/trans.php")) {require_once "lang/".$lang."/trans.php";}
 else {require_once "lang/ru/trans.php";}

require_once "utils/top.php";

if($result2['administrator'] == 0 && $result2['user'] == 0) {puterror2($result2['skin'],$TEXT['error-loginform2']); require_once "utils/bottom.php"; exit();}

if(empty($_POST['srv_used'])) {puterror2($result2['skin'],$TEXT['error-srv27']); require_once "utils/bottom.php"; exit();}
if(empty($_POST['srv_id'])) {puterror2($result2['skin'],$TEXT['error-srv27']); require_once "utils/bottom.php"; exit();}
if(strlen($_POST['srv_id'])>255) {puterror2($result2['skin'],$TEXT['error-srv27']); require_once "utils/bottom.php"; exit();}
if(strlen($_POST['kfr'])>255) {puterror2($result2['skin'],$TEXT['error-upr2']); require_once "utils/bottom.php"; exit();}
if(empty($_POST['kfr'])) {puterror2($result2['skin'],$TEXT['error-upr8']); require_once "utils/bottom.php"; exit();}

if(isset($_POST['kfr'])) {$kfr=trim($_POST['kfr']);}
else {$kfr="";}
if(empty($kfr)) {puterror2($result2['skin'],$TEXT['error-upr3']); require_once "utils/bottom.php"; exit();}

$_POST['kfr'] = trim(rep($_POST['kfr']));

$query_log1 = "SELECT
     AES_DECRYPT(pwd,'".$_POST['kfr']."') AS pwd
     FROM tbl_upr
     WHERE `pwd` !='' LIMIT 1";
$result_log1 = mysqli_query($dbcnx,$query_log1);
if(!$result_log1) {puterror2($result2['skin'],$TEXT['error-tbl3']); require_once "utils/bottom.php"; exit();}
$result_upr1 = mysqli_fetch_array($result_log1,MYSQLI_ASSOC);

if(empty($result_upr1['pwd'])) {puterror2($result2['skin'],$TEXT['error-upr7']); require_once "utils/bottom.php"; exit();}

$delimiter = ".";

//function disabled by php > 5.5.3
////import_request_variables("gP", "in_");
// before using only ->  while(list($id,$value) = @each($in_srv))

//while(list($id2,$value2) = each($_POST)) {
//  if($id2 == "srv") {
//   while(list($id,$value) = each($value2)) {
    if(!empty($_POST['srv_id']) && $_POST['srv_used'] == 'on') {
        $query_srv = "SELECT
        tbl_srv.*,
        tbl_srv.name AS srv_name,
        tbl_upr.*,
        AES_DECRYPT(tbl_upr.pwd,'".$_POST['kfr']."') AS pwd
        FROM tbl_upr
        JOIN tbl_srv ON tbl_upr.srv_id = tbl_srv.srv_id
        WHERE tbl_upr.srv_id = '".$_POST['srv_id']."'";
        $res_srv = mysqli_query($dbcnx,$query_srv);
        if(!$res_srv) {puterror2($result2['skin'],mysqli_error($dbcnx)); require_once "utils/bottom.php"; exit();}
        while($result_srv = mysqli_fetch_array($res_srv,MYSQLI_ASSOC))
        {
            if(trim($result_srv['descr']) !== "") {$srv_descr = $result_srv['descr'];}
             else {$srv_descr = $TEXT['search-descr'];}

            if($result_srv['autorit'] == 1) {$autorit = "authoritative;\r\n";}
             else {$authorit = "";}

            if(trim($result_srv['log_facility']) !== "") {$logfac = "log-facility ".$result_srv['log_facility'].";\r\n";}
             else {$logfac = "";}

            if(trim($result_srv['end_opt_ons']) !== "") {$endopt = $result_srv['end_opt_ons'];}
             else {$endopt = "";}
             
            //$exp_path = "temp/dhcp.conf_".$result_srv['srv_name'];
            $path_to_srv_file = strstr($result_srv['locpath'], 'temp/');
            if(file_exists($result_srv['locpath']) && !empty($result_srv['locpath'])) {$exp_path = $path_to_srv_file."/dhcp.conf_".$result_srv['srv_name'];}
              else {$exp_path = "temp/dhcp.conf_".$result_srv['srv_name'];}

            if(is_file($exp_path)) {unlink($exp_path);}
            $file = fopen($exp_path,"a");
            putwait2($result2['skin'],$TEXT['global-create-file']);
            $exp_head = "######## ".translate($srv_descr)." ################\r\n# IP: ".$result_srv['srv_name']."\r\n# Created by ManyDHCP ".translate($TEXT['global-vertion'])."\r\n#######################################\r\n\r\n######## server parameters ########\r\n";
            fwrite($file, $exp_head, strlen($exp_head));
            $exp_param = "default-lease-time ".$result_srv['def_lease'].";\r\nmax-lease-time ".$result_srv['max_lease'].";\r\n".$autorit.$logfac."\r\n".htmlspecialchars_decode($result_srv['opt_ons'], ENT_COMPAT)."\r\n";
            fwrite($file, $exp_param, strlen($exp_param));

            if($result_srv['ddns_upd_style'] == "none") {
             $exp_param2 = "ddns-update-style ".$result_srv['ddns_upd_style'].";\r\n";
            fwrite($file, $exp_param2, strlen($exp_param2));
            } else {
             $exp_param2 = "ddns-update-style ".$result_srv['ddns_upd_style'].";\r\nddns-updates on;\r\nallow client-updates;\r\n";
            fwrite($file, $exp_param2, strlen($exp_param2));
            }

        $query_pods1 = "SELECT `podset`, `begin_subn`, `end_subn`, `opthstnm` FROM `tbl_podset` WHERE `srv_id` = '".$_POST['srv_id']."' AND `begin_subn` !='' ORDER BY podset ";
        $res_addr1 = mysqli_query($dbcnx,$query_pods1);
         if(!$res_addr1) {puterror2($result2['skin'],mysqli_error($dbcnx)); require_once "utils/bottom.php"; exit();}
         if(mysqli_num_rows($res_addr1)>0)
         {
          while($result_pods = mysqli_fetch_array($res_addr1,MYSQLI_ASSOC))
          {
            $opt_hst_nm = $result_pods['opthstnm'];

      $addr_iskh = explode($delimiter,$result_pods['podset'],4);
       $oct_iskh1 = substr($addr_iskh['0'],0,1);
        if($oct_iskh1 == '0')
         {
          $oct_res_iskh1 = substr($addr_iskh['0'],1,1); exit();
          if($oct_res_iskh1 == '0')
           {
         $oct_result_iskh1 = substr($addr_iskh['0'],2,3);
           } else {$oct_result_iskh1 = substr($addr_iskh['0'],1,3);}
         } else {$oct_result_iskh1 = $addr_iskh['0'];}

       $oct_iskh2 = substr($addr_iskh['1'],0,1);
        if($oct_iskh2 == '0')
         {
          $oct_res_iskh2 = substr($addr_iskh['1'],1,1);
           if($oct_res_iskh2 == '0')
            {
            $oct_result_iskh2 = substr($addr_iskh['1'],2,3);
            } else {$oct_result_iskh2 = substr($addr_iskh['1'],1,3);}
         } else {$oct_result_iskh2 = $addr_iskh['1'];}

       $oct_iskh3 = substr($addr_iskh['2'],0,1);
        if($oct_iskh3 == '0')
         {
         $oct_res_iskh3 = substr($addr_iskh['2'],1,1);
          if($oct_res_iskh3 == '0')
           {
           $oct_result_iskh3 = substr($addr_iskh['2'],2,3);
           } else {$oct_result_iskh3 = substr($addr_iskh['2'],1,3);}
         } else {$oct_result_iskh3 = $addr_iskh['2'];}

        $oct_iskh4 = substr($addr_iskh['3'],0,1);
         if($oct_iskh4 == '0')
          {
          $oct_res_iskh4 = substr($addr_iskh['3'],1,1);
           if($oct_res_iskh4 == '0')
            {
            $oct_result_iskh4 = substr($addr_iskh['3'],2,3);
            } else {$oct_result_iskh4 = substr($addr_iskh['3'],1,3);}
          } else {$oct_result_iskh4 = $addr_iskh['3'];}
        $pods_num = $oct_result_iskh1.".".$oct_result_iskh2.".".$oct_result_iskh3.".".$oct_result_iskh4;

           //$pods_num = $result_pods['podset'];
           $pods_num_end = $result_pods['end_subn'];

           //пишем начало подсети
           $bgn_sub1 = "\r\n######## Begin subnetwork ".$pods_num." ########\r\n";
           fwrite($file, $bgn_sub1, strlen($bgn_sub1));
           $bgn_sub2 = $result_pods['begin_subn']."\r\n\r\n";
           fwrite($file, $bgn_sub2, strlen($bgn_sub2));

            $query_addr = "SELECT * FROM tbl_ip WHERE srv_id = '".$_POST['srv_id']."' AND vkl_ip = '1' AND ip_addr REGEXP '".$result_pods['podset']."' ORDER BY ip_addr ";
            $res_addr = mysqli_query($dbcnx,$query_addr);
            if(!$res_addr) {puterror2($result2['skin'],mysqli_error($dbcnx)); require_once "utils/bottom.php"; exit();}
            while($result_addr = mysqli_fetch_array($res_addr,MYSQLI_ASSOC))
            {
             if(trim($result_addr['descr']) !== "") {$addr_descr = $result_addr['descr'];}
              else {$addr_descr = $TEXT['search-descr'];}

             $addr = explode($delimiter,$result_addr['ip_addr'],4);
              $oct1 = substr($addr['0'],0,1);
               if($oct1 == '0')
                { 
                 $oct_res1 = substr($addr['0'],1,1);
                 if($oct_res1 == '0') 
                  {
                  $oct_result1 = substr($addr['0'],2,3);
                  } else {$oct_result1 = substr($addr['0'],1,3);}
                 } else {$oct_result1 = $addr['0'];}

              $oct2 = substr($addr['1'],0,1);
               if($oct2 == '0')
                { 
                 $oct_res2 = substr($addr['1'],1,1);
                 if($oct_res2 == '0') 
                  {
                  $oct_result2 = substr($addr['1'],2,3);
                  } else {$oct_result2 = substr($addr['1'],1,3);}
                 } else {$oct_result2 = $addr['1'];}

              $oct3 = substr($addr['2'],0,1);
               if($oct3 == '0')
                { 
                 $oct_res3 = substr($addr['2'],1,1);
                 if($oct_res3 == '0') 
                  {
                  $oct_result3 = substr($addr['2'],2,3);
                  } else {$oct_result3 = substr($addr['2'],1,3);}
                 } else {$oct_result3 = $addr['2'];}

              $oct4 = substr($addr['3'],0,1);
               if($oct4 == '0')
                { 
                 $oct_res4 = substr($addr['3'],1,1);
                 if($oct_res4 == '0') 
                  {
                  $oct_result4 = substr($addr['3'],2,3);
                  } else {$oct_result4 = substr($addr['3'],1,3);}
                 } else {$oct_result4 = $addr['3'];}
              $ip_addres = $oct_result1.".".$oct_result2.".".$oct_result3.".".$oct_result4;

              if($opt_hst_nm == "1") {$opt_hst_nm_res = "     option host-name \"".translate($result_addr['name'])."\";\r\n";}
               else {$opt_hst_nm_res = "";}

             $exp_addr = "# ".translate($addr_descr)."\r\n"."     host ".translate($result_addr['name'])."{\r\n".$opt_hst_nm_res."     hardware ethernet ".substr(chunk_split(translate($result_addr['hw']),2,":"),0,-1).";\r\n"."     fixed-address ".$ip_addres.";\r\n     }\r\n";
             fwrite($file, $exp_addr, strlen($exp_addr));

            }
            //Пишем закрытие подсети
            $end_sub1 = "\r\n".$pods_num_end;
            fwrite($file, $end_sub1, strlen($end_sub1));
            $end_sub2 = "\r\n######## End subnetwork ".$pods_num." ########\r\n";
            fwrite($file, $end_sub2, strlen($end_sub2));
          }
          //пишем хвост конфигурации сервера
          $rwendopt = "\r\n".$endopt."\r\n";
           fwrite($file, $rwendopt, strlen($rwendopt));
         }
         else
          {
           //пишем хвост конфигурации сервера
           $khvost = "\r\n\r\n".$result_srv['subnts']."\r\n######## end server parameters ########\r\n\r\n########## Hosts parameters ###########\r\n";
           fwrite($file, $khvost, strlen($khvost));

            $query_addr = "SELECT * FROM tbl_ip WHERE srv_id = '".$_POST['srv_id']."' AND vkl_ip = '1' ORDER BY ip_addr ";
            $res_addr = mysqli_query($dbcnx,$query_addr);
            if(!$res_addr) {puterror2($result2['skin'],mysqli_error($dbcnx)); require_once "utils/bottom.php"; exit();}
            while($result_addr = mysqli_fetch_array($res_addr,MYSQLI_ASSOC))
            {
             if(trim($result_addr['descr']) !== "") {$addr_descr = $result_addr['descr'];}
              else {$addr_descr = $TEXT['search-descr'];}

             $addr = explode($delimiter,$result_addr['ip_addr'],4);
              $oct1 = substr($addr['0'],0,1);
               if($oct1 == '0')
                {
                 $oct_res1 = substr($addr['0'],1,1);
                 if($oct_res1 == '0')
                  {
                  $oct_result1 = substr($addr['0'],2,3);
                  } else {$oct_result1 = substr($addr['0'],1,3);}
                 } else {$oct_result1 = $addr['0'];}

              $oct2 = substr($addr['1'],0,1);
               if($oct2 == '0')
                {
                 $oct_res2 = substr($addr['1'],1,1);
                 if($oct_res2 == '0')
                  {
                  $oct_result2 = substr($addr['1'],2,3);
                  } else {$oct_result2 = substr($addr['1'],1,3);}
                 } else {$oct_result2 = $addr['1'];}

              $oct3 = substr($addr['2'],0,1);
               if($oct3 == '0')
                {
                 $oct_res3 = substr($addr['2'],1,1);
                 if($oct_res3 == '0')
                  {
                  $oct_result3 = substr($addr['2'],2,3);
                  } else {$oct_result3 = substr($addr['2'],1,3);}
                 } else {$oct_result3 = $addr['2'];}

              $oct4 = substr($addr['3'],0,1);
               if($oct4 == '0')
                {
                 $oct_res4 = substr($addr['3'],1,1);
                 if($oct_res4 == '0')
                  {
                  $oct_result4 = substr($addr['3'],2,3);
                  } else {$oct_result4 = substr($addr['3'],1,3);}
                 } else {$oct_result4 = $addr['3'];}
              $ip_addres = $oct_result1.".".$oct_result2.".".$oct_result3.".".$oct_result4;

             $exp_addr = "# ".translate($addr_descr)."\r\n"."     host ".translate($result_addr['name'])."{\r\n"."     option host-name \"".translate($result_addr['name'])."\";\r\n"."     hardware ethernet ".substr(chunk_split(translate($result_addr['hw']),2,":"),0,-1).";\r\n"."     fixed-address ".$ip_addres.";\r\n     }\r\n";
             fwrite($file, $exp_addr, strlen($exp_addr));

            }
            $khvost2 = "########### end hosts parameters ###########\r\n";
            fwrite($file, $khvost2, strlen($khvost2));
         }
         fclose($file);
         chmod($exp_path,0644);
         if($result_srv['comanda'] == 'scp')
         {
         $connection = ssh2_connect($result_srv['srv_name'], $result_srv['sshport']);
         $usr = $result_srv['usr'];
         $password = $result_srv['pwd'];
         ssh2_auth_password($connection, $usr, $password);
         ssh2_scp_send($connection, $exp_path, 'dhcpd.conf', 0666);
         }
          else
          {
           $connention = copy($exp_path,$filep);
           chmod($filep,0644);
          }
         $log_serv = $result_srv['srv_name'];
        }
    }
    $log_servers .= $log_serv.", ";
//  }
// }
//}

//*************************************
//Информация для лога
$ip = $_SERVER['REMOTE_ADDR'];
$doing = $TEXT['log-import7'];
$what = $TEXT['log-import8'].substr($log_servers,0,-2);
$who = $result2['familiya']." ".$result2['name'];
//*************************************

$query_log = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
if(mysqli_query($dbcnx,$query_log)) {
 echo "<html><head>
       <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=main.php'>
       </head></html>";
} else {puterror2($result2['skin'],$TEXT['error-log']); require_once "utils/bottom.php"; exit();}
require_once "utils/bottom.php";
?>