<?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'];

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

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

if(empty($_POST['srv_id'])) {puterror($result2['skin'],$TEXT['error-srv21']); require_once "../utils/bottom_adm.php"; exit();}
if(strlen($_POST['srv_id'])>5) {puterror($result2['skin'],$TEXT['error-srv22']); require_once "../utils/bottom_adm.php"; exit();}
$_POST['srv_id'] = rep($_POST['srv_id']);
$query_srv_fl = "SELECT `name`, `descr` FROM `tbl_srv` WHERE `srv_id` = '".$_POST['srv_id']."'";
 $res_srv_fl = mysqli_query($dbcnx,$query_srv_fl);
  if(!$res_srv_fl) {puterror($result2['skin'],$TEXT['error-tbl1']); require_once "../utils/bottom_adm.php"; exit();}
   $result_srv_fl = mysqli_fetch_array($res_srv_fl,MYSQLI_ASSOC);
   $fl_name = $result_srv_fl['name'].".";
   $fl_name2 = translate($result_srv_fl['descr']).".";

$exp_path = "../temp/".$fl_name.$fl_name2."dhcp_".date("dMY").".csv";
if(is_file($exp_path)) {unlink($exp_path);}
$file = fopen($exp_path,"a");
$exp_head = "ip-address,name,mac,description,vkl\r\n";
fwrite($file, $exp_head, strlen($exp_head));

$query_srv = "SELECT
tbl_ip.*,
tbl_srv.name AS srv_name,
tbl_srv.descr AS srv_descr
FROM tbl_ip
JOIN tbl_srv ON tbl_ip.srv_id = tbl_srv.srv_id
WHERE tbl_ip.srv_id = '".$_POST['srv_id']."' ORDER BY tbl_ip.ip_addr ";
 $res_srv = mysqli_query($dbcnx,$query_srv);
  if(!$res_srv) {puterror($result2['skin'],$TEXT['error-tbl2']); require_once "../utils/bottom_adm.php"; exit();}
   while($result_srv = mysqli_fetch_array($res_srv,MYSQLI_ASSOC))
    {
    if(trim($result_srv['descr']) !== "") {$addr_descr = $result_srv['descr'];}
     else {$addr_descr = "";}

    $log_srv = $result_srv['srv_name'];
    $log_srv_descr = $result_srv['srv_descr'];

    $exp_addr = $result_srv['ip_addr'].",".translate($result_srv['name']).",".translate($result_srv['hw']).",".$addr_descr.",".$result_srv['vkl_ip']."\r\n";
    fwrite($file, $exp_addr, strlen($exp_addr));
    }
    fclose($file);
    chmod($exp_path,0644);

    echo "<table align='center' class='table' border='0' cellpadding='0' cellspacing='0' width='30%' bordercolordark='white' bordercolorlight='black'>
           <tr><td width='15%'><img align='right' src='../skins/".$result2['skin']."/ok.png'></td><td width='30%' class='bold_it' >&nbsp;".$TEXT['log-exp3']."</td></tr>
          </table>";
//*************************************
//Информация для лога
$ip = $_SERVER['REMOTE_ADDR'];
$doing = $TEXT['log-exp1'];
$what = $TEXT['log-exp2'].$log_srv." (".$log_srv_descr.")";
$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=transfer.php?trans=".$exp_path."' />
       </head></html>";
} else {puterror($result2['skin'],$TEXT['error-log']); require_once "../utils/bottom_adm.php"; exit();}
require_once "../utils/bottom_adm.php";
?>