<?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['upr-title-edit'];
  $button = $TEXT['btn_save'];
  $action = "edit.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['addr_id'])) {puterror($result2['skin'],$TEXT['error-upr5']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['addr_id'])>5) {puterror($result2['skin'],$TEXT['error-upr6']); require_once "../utils/bottom_adm.php"; exit();}
  if(empty($_POST['kfr'])) {puterror($result2['skin'],$TEXT['error-upr8']); require_once "../utils/bottom_adm.php"; exit();}
  if(strlen($_POST['kfr'])>255) {puterror($result2['skin'],$TEXT['error-upr2']); require_once "../utils/bottom_adm.php"; exit();}

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

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

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

  $query_iskh = "SELECT
     tbl_upr.addr_id,
     tbl_upr.srv_id,
     tbl_upr.usr,
     tbl_upr.comanda,
     tbl_upr.locpath,
     tbl_upr.vkl_upr,
     tbl_upr.pwd AS rawpwd,
     AES_DECRYPT(tbl_upr.pwd,'".$_POST['kfr']."') AS pwd,
     tbl_srv.srv_id AS server_id,
     tbl_srv.name AS srv_name,
     tbl_srv.descr AS srv_descr
     FROM tbl_upr
     JOIN tbl_srv ON tbl_upr.srv_id=tbl_srv.srv_id
     WHERE tbl_upr.addr_id='".$_POST['addr_id']."'";
  $result_iskh = mysqli_query($dbcnx,$query_iskh);
  if(!$result_iskh) {puterror($result2['skin'],$TEXT['error-tbl3']); require_once "../utils/bottom_adm.php"; exit();}
      $result_iskh2 = mysqli_fetch_array($result_iskh,MYSQLI_ASSOC);
      $old_addr_id = $_POST['addr_id'];
      $comanda = $result_iskh2['comanda'];

  if($result_iskh2['vkl_upr'] == 1) {$vkl = "checked";}
    else {$vkl = "";}

  if(empty($result_iskh2['pwd']) && $result_iskh2['rawpwd'] !== "") {puterror($result2['skin'],$TEXT['error-upr7']); require_once "../utils/bottom_adm.php"; exit();}
?>
<form enctype='multipart/form-data' action=<?php echo $action; ?> method=post>
<table align="center" border="0" width="50%">
<tr><td class="bold_it_page"><?php echo $TEXT['srv-name']; ?></td>
    <td><select name="srv_id">
    <?php
        $query1 = "SELECT srv_id, name, descr, autorit FROM `tbl_srv` WHERE vkl = '1' ORDER BY name";
        $srv_tmp = mysqli_query($dbcnx,$query1);
          if(!$srv_tmp) {puterror2($TEXT['error-tbl1']);}
              while($srv = mysqli_fetch_array($srv_tmp,MYSQLI_ASSOC))
                {
                 if($srv['srv_id'] == $result_iskh2['server_id'])
                   {
                   $chk = "<option selected value=".$srv['srv_id'].">".$srv['name']."(".$srv['descr'].")";
                   }
                 else {$chk = "<option value=".$srv['srv_id'].">".$srv['name']."(".$srv['descr'].")";}
                 echo $chk;
                }
    ?>
    </select></td></tr>
<tr><td class="bold_it_page"><?php echo $TEXT['upr-type']; ?>*</td>
    <td>
        <select name=comanda>
          <option value='scp'<?php if ($comanda =='scp') {echo "selected";} ?>><?php echo $TEXT['upr-vnesh']; ?>
          <option value='cp' <?php if ($comanda =='cp') {echo "selected";} ?>><?php echo $TEXT['upr-vnutr']; ?>
       </select>
    </td></tr>
<tr><td class="bold_it_page"><?php echo $TEXT['login-login']; ?>*</td><td><input size="30" maxlength="255" type="text" name="usr" value="<?php echo $result_iskh2['usr']; ?>" /></td></tr>
<tr><td class="bold_it_page"><?php echo $TEXT['login-password']; ?>*</td><td><input size="30" maxlength="100" type="text" name="pwd" value="<?php echo $result_iskh2['pwd']; ?>" /></td></tr>
<tr><td class="bold_it_page"><?php echo $TEXT['login-locpath']; ?></td><td><input size="30" maxlength="255" type="text" name="locpath" value="<?php echo $result_iskh2['locpath']; ?>" /></td></tr>
<tr><td class="bold_it_page"><?php echo $TEXT['srv-vkl']; ?></td><td><input type="checkbox" name="vkl" <?php echo $vkl; ?>></td></tr>
<tr><td></td><td>
<input class="button2" type="submit" value='<?php echo $button; ?>'>
<input type="hidden" name="addr_id" value='<?php echo $old_addr_id; ?>' />
<input type="hidden" name="kfr" value='<?php echo $_POST['kfr']; ?>' />
</td></tr>
</table>
</form>
<?php
  require_once "../utils/bottom_adm.php";
?>