<?php
  ///////////////////////////////////////////////////
  //2007 (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=$titlepage = $TEXT['usr-title4'];
  $helppage = "";

  require_once "../config.php";
  require_once "../util/top.php";
  require_once "util.php";
  if(is_file ("../lang/".$lang."/trans.php")) require_once "../lang/".$lang."/trans.php";
    else require_once "../lang/ru/trans.php";

  if($result2['master'] == 1) puterror2($TEXT['error-loginform4']);
  if($result2['manager'] == 1) puterror2($TEXT['error-loginform4']);

  if(empty($_POST['familiya'])) puterror2($TEXT['error-user11']);
  if(strlen($_POST['familiya'])>200) puterror2($TEXT['error-user12']);
  if(empty($_POST['name'])) puterror2($TEXT['error-user13']);
  if(strlen($_POST['name'])>200) puterror2($TEXT['error-user14']);
  if(strlen($_POST['otchestvo'])>200) puterror2($TEXT['error-user15']);
  if(empty($_POST['position'])) puterror2($TEXT['error-user16']);
  if(strlen($_POST['position'])>5) puterror2($TEXT['error-user17']);
  if(strlen($_POST['mail'])>220) puterror2($TEXT['error-user18']);
  if(strlen($_POST['dom_id'])>11) puterror2($TEXT['error-domain6']);
  if(strlen($_POST['phone_mob'])>30) puterror2($TEXT['error-user19']);
  if(strlen($_POST['phone_city'])>30) puterror2($TEXT['error-user20']);
  if(strlen($_POST['phone_int'])>20) puterror2($TEXT['error-user21']);
  if(strlen($_POST['city_id'])>11) puterror2($TEXT['error-city6']);
  if(strlen($_POST['str_id'])>11) puterror2($TEXT['error-str6']);
  if(strlen($_POST['house_id'])>11) puterror2($TEXT['error-house6']);
  if(strlen($_POST['room'])>16) puterror2($TEXT['error-user22']);
  if(strlen($_POST['org_id'])>11) puterror2($TEXT['error-org1']);
  if(strlen($_POST['upr_id'])>11) puterror2($TEXT['error-upr2']);
  if(strlen($_POST['otd_id'])>11) puterror2($TEXT['error-otd9']);
  if(strlen($_POST['dol_id'])>11) puterror2($TEXT['error-post9']);
  if(strlen($_POST['old_dom_id'])>11) puterror2($TEXT['error-domain9']);
  if(strlen($_POST['old_city_id'])>11) puterror2($TEXT['error-city9']);
  if(strlen($_POST['old_str_id'])>11) puterror2($TEXT['error-str9']);
  if(strlen($_POST['old_house_id'])>11) puterror2($TEXT['error-house9']);
  if(strlen($_POST['old_org_id'])>11) puterror2($TEXT['error-org2']);
  if(strlen($_POST['old_upr_id'])>11) puterror2($TEXT['error-upr1']);
  if(strlen($_POST['old_otd_id'])>11) puterror2($TEXT['error-otd12']);
  if(strlen($_POST['old_dol_id'])>11) puterror2($TEXT['error-post11']);
  if(strlen($_POST['user_id'])>11) puterror2($TEXT['error-user2']);
  if(strlen($_POST['clear_picture'])>5) puterror2($TEXT['error-new11']);
  if(strlen($_POST['photo_obr'])>1) puterror2($TEXT['error-new11']);

  if(isset($_POST['familiya'])) $familiya=trim($_POST['familiya']);
    else $familiya="";
     if (empty($familiya)) puterror2($TEXT['error-user25']);

  if(isset($_POST['name'])) $name=trim($_POST['name']);
    else $name="";
     if (empty($name)) puterror2($TEXT['error-user26']);

  if(isset($_POST['position'])) $position=trim($_POST['position']);
    else $position="";
     if (empty($position)) puterror2($TEXT['error-user27']);

  $_POST['familiya'] = rep($_POST['familiya']);
  $_POST['name'] = rep($_POST['name']);
  $_POST['otchestvo'] = rep($_POST['otchestvo']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['mail'] = rep($_POST['mail']);
  $_POST['dom_id'] = rep($_POST['dom_id']);
  $_POST['phone_mob'] = rep($_POST['phone_mob']);
  $_POST['phone_city'] = rep($_POST['phone_city']);
  $_POST['phone_int'] = rep($_POST['phone_int']);
  $_POST['city_id'] = rep($_POST['city_id']);
  $_POST['str_id'] = rep($_POST['str_id']);
  $_POST['house_id'] = rep($_POST['house_id']);
  $_POST['room'] = rep($_POST['room']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_POST['upr_id'] = rep($_POST['upr_id']);
  $_POST['otd_id'] = rep($_POST['otd_id']);
  $_POST['dol_id'] = rep($_POST['dol_id']);
  $_POST['old_dom_id'] = rep($_POST['old_dom_id']);
  $_POST['old_photo_id'] = rep($_POST['old_photo_id']);
  $_POST['old_city_id'] = rep($_POST['old_city_id']);
  $_POST['old_str_id'] = rep($_POST['old_str_id']);
  $_POST['old_house_id'] = rep($_POST['old_house_id']);
  $_POST['old_org_id'] = rep($_POST['old_org_id']);
  $_POST['old_upr_id'] = rep($_POST['old_upr_id']);
  $_POST['old_otd_id'] = rep($_POST['old_otd_id']);
  $_POST['old_dol_id'] = rep($_POST['old_dol_id']);
  $_POST['user_id'] = rep($_POST['user_id']);
  $_POST['clear_picture'] = rep($_POST['clear_picture']);
  $_POST['photo_obr'] = rep($_POST['photo_obr']);

  $first_bt = substr($_POST['familiya'], 0,1);
    if(preg_match("/[A-Z]/i", $first_bt)) $first_b = substr($_POST['familiya'], 0,1);
   //if(preg_match("/[A-Z]/i", $first_bt)) $first_b = substr($_POST['familiya'], 0,3);
       else $first_b = substr($_POST['familiya'], 0,2);

  $new_dom = $_POST['dom_id'];
  $new_city = $_POST['city_id'];
  $new_str = $_POST['str_id'];
  $new_house = $_POST['house_id'];
  $new_org = $_POST['org_id'];
  $new_upr = $_POST['upr_id'];
  $new_otd = $_POST['otd_id'];
  $new_dol = $_POST['dol_id'];

  if($_POST['clear_picture'] == "on")
  {
  $image = "";
      unlink("../".$_POST['old_pho']);
  }
    else
    {
  if($_FILES['photo']['size'] > 1024*2*1024) puterror2($TEXT['error-pic3']);
    if (!empty($_FILES['photo']['tmp_name']))
      {
       $ext = strrchr($_FILES['photo']['name'], ".");
       $name = $_POST['familiya'];
       $mail = translate($name)."_";
       $mail1 = translate($name)."1_";
       $image = "../photo/$mail1".date("YmdHis",time())."$ext";
       $smallimage = "../photo/$mail".date("YmdHis",time())."$ext";

          if (copy($_FILES['photo']['tmp_name'], "../".$image))
            {
            unlink($_FILES['photo']['tmp_name']);
            chmod("../".$image, 0644);
            }
            //if(!resizeimg($image, $smallimage, 130, 185))
           //{
            //puterror2($TEXT['error-gdlib']);
           //}
            if($_POST['photo_obr'] == '1') {
                 if(is_file("../".$_POST['old_pho'])) unlink("../".$_POST['old_pho']);
                 $smallimage = $image;
            }
            if($_POST['photo_obr'] == '2') {
                 if(!resizeimg_simple($image, $smallimage, 130, 185))
                {
                  puterror2($TEXT['error-gdlib']);
                }
                if(is_file("../".$_POST['old_pho'])) unlink("../".$_POST['old_pho']);
                if(is_file("../".$image)) unlink("../".$image);
            }
            if($_POST['photo_obr'] == '3') {
                 if(!resizeimg_simple($image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
                if(is_file("../".$_POST['old_pho'])) unlink("../".$_POST['old_pho']);
                if(is_file("../".$image)) unlink("../".$image);
            }
             if($_POST['photo_obr'] == '4') {
                 if(!resizeimg($image, $smallimage, 130, 185))
                {
                  puterror2($TEXT['error-gdlib']);
                }
                if(is_file("../".$_POST['old_pho'])) unlink("../".$_POST['old_pho']);
                if(is_file("../".$image)) unlink("../".$image);
            }
             if($_POST['photo_obr'] == '5') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
                if(is_file("../".$_POST['old_pho'])) unlink("../".$_POST['old_pho']);
                if(is_file("../".$image)) unlink("../".$image);
            }
             if($_POST['photo_obr'] == '6') {
                 $maskfile = "../img/vert.png";
                 if(!resizeimg_round($maskfile, $image, $smallimage, 130, 185))
                {
                  puterror2($TEXT['error-gdlib']);
                }
                if(is_file("../".$_POST['old_pho'])) unlink("../".$_POST['old_pho']);
                if(is_file("../".$image)) unlink("../".$image);
            }
             if($_POST['photo_obr'] == '7') {
                 $maskfile = "../img/horiz.png";
                 if(!resizeimg_round($maskfile, $image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
                if(is_file("../".$_POST['old_pho'])) unlink("../".$_POST['old_pho']);
                if(is_file("../".$image)) unlink("../".$image);
            }
             if($_POST['photo_obr'] == '8') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
                if(is_file("../".$_POST['old_pho'])) unlink("../".$_POST['old_pho']);
                if(is_file("../".$image)) unlink("../".$image);
            }
             if($_POST['photo_obr'] == '9') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
           if(is_file("../".$_POST['old_pho'])) unlink("../".$_POST['old_pho']);
           if(is_file("../".$image)) unlink("../".$image);
	}
     }
      else $smallimage = $_POST['old_pho'];
  }
  //*************************************
  $query1 = "SELECT
tbl_users.user_id,
tbl_users.room,
tbl_users.familiya,
tbl_users.name,
tbl_users.otchestvo,
tbl_users.mail,
tbl_users.phone_mob,
tbl_users.phone_int,
tbl_users.phone_city,
tbl_users.photo,
tbl_users.position,
tbl_dolzhnost.dolzhnost,
tbl_domain.domain,
tbl_city.city,
tbl_street.street,
tbl_house.house,
tbl_organizaciya.organizaciya,
tbl_upravlenie.upravlenie,
tbl_otdel.otdel 
FROM tbl_users 
JOIN tbl_dolzhnost ON tbl_users.dol_id=tbl_dolzhnost.dol_id 
JOIN tbl_domain ON tbl_users.dom_id=tbl_domain.dom_id 
JOIN tbl_city ON tbl_users.city_id=tbl_city.city_id 
JOIN tbl_street ON tbl_users.str_id=tbl_street.str_id 
JOIN tbl_house ON tbl_users.house_id=tbl_house.house_id 
JOIN tbl_organizaciya ON tbl_users.org_id=tbl_organizaciya.org_id 
JOIN tbl_upravlenie ON tbl_users.upr_id=tbl_upravlenie.upr_id 
JOIN tbl_otdel ON tbl_users.otd_id=tbl_otdel.otd_id 
WHERE user_id='".$_POST['user_id']."' ";
  $result = mysql_query($query1);
  if(!$result) puterror2($TEXT['error-tbl9']);
   $result3 = mysql_fetch_array($result,MYSQL_ASSOC);

   $res_dom = $result3['domain'];
   $res_cit = $result3['city'];
   $res_str = $result3['street'];
   $res_hou = $result3['house'];
   $res_org = $result3['organizaciya'];
   $res_upr = $result3['upravlenie'];
   $res_otd = $result3['otdel'];
   $res_dol = $result3['dolzhnost'];
   $res_fam = $result3['familiya'];
   $res_name = $result3['name'];
   $res_otch = $result3['otchestvo'];

                 $query_dom2 = "SELECT * FROM `tbl_domain` WHERE `dom_id`= '$new_dom' ";
                 $result_dom2 = mysql_query($query_dom2);
                   if(!$result_dom2) puterror2($TEXT['error-tbl5']);
                       $result_domain2 = mysql_fetch_array($result_dom2,MYSQL_ASSOC);
                       $res_dom2 = $result_domain2['domain'];

                 $query_city2 = "SELECT * FROM `tbl_city` WHERE `city_id`= '$new_city' ";
                 $result_cit2 = mysql_query($query_city2);
                   if(!$result_cit2) puterror2($TEXT['error-tbl1']);
                       $result_city2 = mysql_fetch_array($result_cit2,MYSQL_ASSOC);
                       $res_cit2 = $result_city2['city'];

                 $query_street2 = "SELECT * FROM `tbl_street` WHERE `str_id`= '$new_str' ";
                 $result_str2 = mysql_query($query_street2);
                   if(!$result_str2) puterror2($TEXT['error-tbl3']);
                       $result_street2 = mysql_fetch_array($result_str2,MYSQL_ASSOC);
                       $res_str2 = $result_street2['street'];

                 $query_house2 = "SELECT * FROM `tbl_house` WHERE `house_id`= '$new_house' ";
                 $result_hou2 = mysql_query($query_house2);
                   if(!$result_hou2) puterror2($TEXT['error-tbl4']);
                       $result_house2 = mysql_fetch_array($result_hou2,MYSQL_ASSOC);
                       $res_hou2 = $result_house2['house'];

                 $query_organizaciya2 = "SELECT * FROM `tbl_organizaciya` WHERE `org_id`= '$new_org' ";
                 $result_org2 = mysql_query($query_organizaciya2);
                   if(!$result_org2) puterror2($TEXT['error-tbl7']);
                       $result_organizaciya2 = mysql_fetch_array($result_org2,MYSQL_ASSOC);
                       $res_org2 = $result_organizaciya2['organizaciya'];
                       $org_pos = $result_organizaciya2['position'];

                 $query_upravlenie2 = "SELECT * FROM `tbl_upravlenie` WHERE `upr_id`= '$new_upr' ";
                 $result_upr2 = mysql_query($query_upravlenie2);
                   if(!$result_upr2) puterror2($TEXT['error-tbl9']);
                       $result_upravlenie2 = mysql_fetch_array($result_upr2,MYSQL_ASSOC);
                       $res_upr2 = $result_upravlenie2['upravlenie'];
                       $upr_pos = $result_upravlenie2['position'];

                 $query_otdel2 = "SELECT * FROM `tbl_otdel` WHERE `otd_id`= '$new_otd' ";
                 $result_otd2 = mysql_query($query_otdel2);
                   if(!$result_otd2) puterror2($TEXT['error-tbl8']);
                       $result_otdel2 = mysql_fetch_array($result_otd2,MYSQL_ASSOC);
                       $res_otd2 = $result_otdel2['otdel'];
                       $otd_pos = $result_otdel2['position'];

                 $query_dolzhnost2 = "SELECT * FROM `tbl_dolzhnost` WHERE `dol_id`= '$new_dol' ";
                 $result_dol2 = mysql_query($query_dolzhnost2);
                   if(!$result_dol2) puterror2($TEXT['error-tbl6']);
                       $result_dolzhnost2 = mysql_fetch_array($result_dol2,MYSQL_ASSOC);
                       $res_dol2 = $result_dolzhnost2['dolzhnost'];

  if($result3['familiya'] !== $_POST['familiya']) $log_famil = $TEXT['log-usr4']."&laquo;".$result3['familiya']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['familiya']."&raquo;<br />";
    else $log_famil = "";

  if($result3['familiya'] !== $_POST['familiya']) $log_famil_visible = $_POST['familiya'];
    else $log_famil_visible = $result3['familiya'];

  if($result3['name'] !== $_POST['name']) $log_name = $TEXT['log-usr5'].$log_famil_visible.$TEXT['log-usr6']."&laquo;".$result3['name']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['name']."&raquo;<br />";
    else $log_name = "";

  if($result3['name'] !== $_POST['name']) $log_name_visible = $_POST['name'];
    else $log_name_visible = $result3['name'];

  if($result3['otchestvo'] !== $_POST['otchestvo']) $log_otch = $TEXT['log-usr5'].$log_famil_visible.$TEXT['log-usr7']."&laquo;".$result3['otchestvo']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['otchestvo']."&raquo;<br />";
    else $log_otch = "";

  if($result3['otchestvo'] !== $_POST['otchestvo']) $log_otch_visible = $_POST['otchestvo'];
    else $log_otch_visible = $result3['otchestvo'];

  if(empty($_POST['position'])) $_POST['position'] = "";
  if($result3['position'] !== $_POST['position']) $log_pos = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr8']."&laquo;".$result3['position']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['position']."&raquo;<br />";
    else $log_pos = "";

  if($res_dom !== $res_dom2) $log_dom = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr9']."&laquo;".$res_dom."&raquo;".$TEXT['log-nd8']."&laquo;".$res_dom2."&raquo;<br />";
    else $log_dom = "";

  if($result3['mail'] !== $_POST['mail']) $log_mail = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr10']."&laquo;".$result3['mail']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['mail']."&raquo;<br />";
    else $log_mail = "";

  if(empty($_POST['phone_mob'])) $_POST['phone_mob'] = "";
  if($result3['phone_mob'] !== $_POST['phone_mob']) $log_phm = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr11']."&laquo;".$result3['phone_mob']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['phone_mob']."&raquo;<br />";
    else $log_phm = "";

  if(empty($_POST['phone_int'])) $_POST['phone_int'] = "";
  if($result3['phone_int'] !== $_POST['phone_int']) $log_phi = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr12']." &laquo;".$result3['phone_int']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['phone_int']."&raquo;<br />";
    else $log_phi = "";

  if(empty($_POST['phone_city'])) $_POST['phone_city'] = "";
  if($result3['phone_city'] !== $_POST['phone_city']) $log_phc = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr13']."&laquo;".$result3['phone_city']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['phone_city']."&raquo;<br />";
    else $log_phc = "";

  if($res_dol !== $res_dol2) $log_dol = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr14']."&laquo;".$res_dol."&raquo;".$TEXT['log-nd8']."&laquo;".$res_dol2."&raquo;<br />";
    else $log_dol = "";

  if($res_otd !== $res_otd2) $log_otd = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr15']."&laquo;".$res_otd."&raquo;".$TEXT['log-nd8']."&laquo;".$res_otd2."&raquo;<br />";
    else $log_otd = "";

  if($res_upr !== $res_upr2) $log_upr = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr16']."&laquo;".$res_upr."&raquo;".$TEXT['log-nd8']."&laquo;".$res_upr2."&raquo;<br />";
    else $log_upr = "";

  if($res_org !== $res_org2) $log_org = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr17']."&laquo;".$res_org."&raquo;".$TEXT['log-nd8']."&laquo;".$res_org2."&raquo;<br />";
    else $log_org = "";

  if(empty($_POST['room'])) $_POST['room'] = "";
  if($result3['room'] !== $_POST['room']) $log_room = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr18']."&laquo;".$result3['room']."&raquo;".$TEXT['log-nd8']."&laquo;".$_POST['room']."&raquo;<br />";
    else $log_room = "";

  if($res_hou !== $res_hou2) $log_hou = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr19']."&laquo;".$res_hou."&raquo;".$TEXT['log-nd8']."&laquo;".$res_hou2."&raquo;<br />";
    else $log_hou = "";

  if($res_str !== $res_str2) $log_str = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr20']."&laquo;".$res_str."&raquo;".$TEXT['log-nd8']."&laquo;".$res_str2."&raquo;<br />";
    else $log_str = "";

  if($res_cit !== $res_cit2) $log_cit = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr21']."&laquo;".$res_cit."&raquo;".$TEXT['log-nd8']."&laquo;".$res_cit2."&raquo;<br />";
    else $log_cit = "";

  if(empty($_FILES['photo']['tmp_name'])) $log_photo = "";
   else 
   {
    if($result3['photo'] !== $image) $log_photo = $TEXT['log-usr5'].$log_famil_visible." ".$log_name_visible." ".$log_otch_visible." ".$TEXT['log-usr22']."&laquo;".$result3['photo']."&raquo;".$TEXT['log-nd8']."&laquo;".$image."&raquo;<br />";
    else $log_photo = "";
   }

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-usr3'];
  $what = $log_famil." ".$log_name." ".$log_otch." ".$log_photo." ".$log_pos." ".$log_mail." ".$log_dom." ".$log_phm." ".$log_phc." ".$log_phi." ".$log_cit." ".$log_str." ".$log_hou." ".$log_room." ".$log_org." ".$log_upr." ".$log_otd." ".$log_dol.$TEXT['log-id'].$_POST['user_id'];
  $who = $result2['fullname'];
  //*************************************
  $query = "UPDATE tbl_users SET  `first_b` =  '$first_b', `familiya` = '".$_POST['familiya']."', `name` = '".$_POST['name']."', `otchestvo` = '".$_POST['otchestvo']."', `position` = '".$_POST['position']."', `dom_id` = '$new_dom', `mail` = '".$_POST['mail']."', `phone_mob` = '".$_POST['phone_mob']."', `phone_int` = '".$_POST['phone_int']."', `phone_city` = '".$_POST['phone_city']."', `dol_id` = '$new_dol', `otd_id` = '$new_otd', `otd_pos` = '$otd_pos', `upr_id` = '$new_upr', `upr_pos` = '$upr_pos', `org_id` = '$new_org', `org_pos` = '$org_pos', `room` = '".$_POST['room']."', `house_id` = '$new_house', `str_id` = '$new_str', `city_id` = '$new_city', `photo` = '$smallimage' WHERE `user_id`='".$_POST['user_id']."' ";
  if(mysql_query($query))
  {
   if($result_srv_set['repl'] == 1 && $_POST['org_id'] == $result_srv_set['rep_org_id'])
   {
    $exp_path = "../../replica/main-organization";
    if(is_file($exp_path))
     {
     $file = fopen($exp_path,"a");
     if($result_srv_set['repl_mail'] == "yes") $exp_param = $result_organizaciya2['organizaciya'].";---;---;---;---;---;---;".$_POST['familiya'].";".$_POST['name'].";".$_POST['otchestvo'].";---;---;---;---;".$_POST['mail'].";".$result_domain2['domain'].";".$_POST['position'].";edt;".$result3['organizaciya'].";---;---;---;---;---;---;".$result3['familiya'].";".$result3['name'].";".$result3['otchestvo'].";---;---;---;---;".$result3['mail'].";".$result3['domain'].";".$result3['position']." \r\n";
      else $exp_param = $result_organizaciya2['organizaciya'].";".$result_upravlenie2['upravlenie'].";".$result_otdel2['otdel'].";".$result_city2['city'].";".$result_street2['street'].";".$result_house2['house'].";".$_POST['room'].";".$_POST['familiya'].";".$_POST['name'].";".$_POST['otchestvo'].";".$result_dolzhnost2['dolzhnost'].";".$_POST['phone_city'].";".$_POST['phone_mob'].";".$_POST['phone_int'].";".$_POST['mail'].";".$result_domain2['domain'].";".$_POST['position'].";edt;".$result3['organizaciya'].";".$result3['upravlenie'].";".$result3['otdel'].";".$result3['city'].";".$result3['street'].";".$result3['house'].";".$result3['room'].";".$result3['familiya'].";".$result3['name'].";".$result3['otchestvo'].";".$result3['dolzhnost'].";".$result3['phone_city'].";".$result3['phone_mob'].";".$result3['phone_int'].";".$result3['mail'].";".$result3['domain'].";".$result3['position']." \r\n";
     fwrite($file, $exp_param, strlen($exp_param));
     fclose($file);
     }
     else
      {
      $file = fopen($exp_path,"a");
      $exp_head = "организация;управление;отдел;город;улица;дом;комната;Фамилия;Имя;Отчество;должность;город.тел;моб.тел;внутр.тел;почта;домен;позиция пользователя;действие;Ст.организация;Ст.управление;Ст.отдел;Ст.город;Ст.улица;Ст.дом;Ст.комната;Ст.Фамилия;Ст.Имя;Ст.Отчество;Ст.должность;Ст.город.тел;Ст.моб.тел;Ст.внутр.тел;Ст.почта;Ст.домен;Ст.позиция пользователя\r\n";
      fwrite($file, $exp_head, strlen($exp_head));
      if($result_srv_set['repl_mail'] == "yes") $exp_param = $result_organizaciya2['organizaciya'].";---;---;---;---;---;---;".$_POST['familiya'].";".$_POST['name'].";".$_POST['otchestvo'].";---;---;---;---;".$_POST['mail'].";".$result_domain2['domain'].";".$_POST['position'].";edt;".$result3['organizaciya'].";---;---;---;---;---;---;".$result3['familiya'].";".$result3['name'].";".$result3['otchestvo'].";---;---;---;---;".$result3['mail'].";".$result3['domain'].";".$result3['position']." \r\n";
       else $exp_param = $result_organizaciya2['organizaciya'].";".$result_upravlenie2['upravlenie'].";".$result_otdel2['otdel'].";".$result_city2['city'].";".$result_street2['street'].";".$result_house2['house'].";".$_POST['room'].";".$_POST['familiya'].";".$_POST['name'].";".$_POST['otchestvo'].";".$result_dolzhnost2['dolzhnost'].";".$_POST['phone_city'].";".$_POST['phone_mob'].";".$_POST['phone_int'].";".$_POST['mail'].";".$result_domain2['domain'].";".$_POST['position'].";edt;".$result3['organizaciya'].";".$result3['upravlenie'].";".$result3['otdel'].";".$result3['city'].";".$result3['street'].";".$result3['house'].";".$result3['room'].";".$result3['familiya'].";".$result3['name'].";".$result3['otchestvo'].";".$result3['dolzhnost'].";".$result3['phone_city'].";".$result3['phone_mob'].";".$result3['phone_int'].";".$result3['mail'].";".$result3['domain'].";".$result3['position']." \r\n";
      fwrite($file, $exp_param, strlen($exp_param));
      fclose($file);
      chmod($exp_path,0644);
      }
   }
    $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
        if(mysql_query($query2))
           {
             echo "<html><head>
                   <meta HTTP-EQUIV='Refresh' CONTENT='0; URL=index.php' />
                   </head></html>";

          if($ldp == 1) {

           $name_tmp=substr($_POST['name'], 0,2);
           $otch_tmp=substr($_POST['otchestvo'], 0,2);
           $inits1 = strtoupper($name_tmp).".";
           $inits2 = strtoupper($otch_tmp).".";
           $mailbase = strtolower($_POST['mail']);
           if($mailbase == "") $mailbase = "no-mail";
            else $mailbase = $mailbase;
           if($res_otd2 == "") $res_otd2 = $res_upr2;
            else $res_otd2 = $res_otd2;

           if(!empty($res_cit2) && $res_cit2 !== "-"){$cit = $TEXT['user-city']." ".$res_cit2.", ";}
            else $cit = " ";
           if(!empty($res_str2) && $res_str2 !== "-"){$str = $TEXT['user-str']." ".$res_str2.", ";}
            else $str = " ";
           if(!empty($res_hou2) && $res_hou2 !== "-"){$hou = $TEXT['user-home']." ".$res_hou2.", ";}
             else $hou = " ";
           if(!empty($_POST['room']) && $_POST['room']!=="-"){$rum = $TEXT['user-room']." ".$_POST['room'];}
            else $rum = " ";
           if($_POST['room'] == "") $res_room = "-";
            else $res_room = $_POST['room'];
           if($_POST['phone_city'] == "") $res_phone = "+";
            else $res_phone = $_POST['phone_city'];
           if($_POST['phone_int'] == "")  $res_phone_int = "";
            else $res_phone_int = $_POST['phone_int'];
           if($_POST['phone_mob'] == "") $res_phone_mob = "+";
            else $res_phone_mob = $_POST['phone_mob'];

           $ds=ldap_connect($dlp_srv, $ldp_port) or die("Could not connect to {$ldp_srv}");
           ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
           $usr_bnd=ldap_bind($ds, $ldp_user, $ldp_pass) or die(ldap_error($ds));
           $dn_tmp1 = $_POST['familiya']." ".$_POST['name']." ".$_POST['otchestvo'];
           $dn_tmp2 = $res_fam." ".$res_name." ".$res_otch;
           if($dn_tmp1 !== $dn_tmp2)
             {
             $dn = "cn=".$dn_tmp2.",".$ldp_dn; // dn: uid=user,ou=people,dc=atomstroyexport,dc=local
             ldap_delete($ds, $dn) or die(ldap_error($ds));
             $dn = "cn=".$dn_tmp1.",".$ldp_dn; // dn: uid=user,ou=people,dc=atomstroyexport,dc=local

           $ldap_mass['objectClass'] [0] = "top";
           $ldap_mass['objectClass'] [1] = "person";
           $ldap_mass['objectClass'] [2] = "organizationalPerson";
           $ldap_mass['objectClass'] [3] = "inetOrgPerson";
           $ldap_mass['cn'] = $_POST['familiya']." ".$_POST['name']." ".$_POST['otchestvo']; // полное имя, равно отображаемому имени
           $ldap_mass['givenName'] = $_POST['name']." ".$_POST['otchestvo']; //имя
           $ldap_mass['sn'] = $_POST['familiya']; // фамилия
           $ldap_mass['displayName'] = $_POST['familiya']." ".$_POST['name']." ".$_POST['otchestvo']; //отображаемое имя
           $ldap_mass['uid'] = $mailbase; //уникальное имя, я исп чать мыльника
           $ldap_mass['initials'] = $inits1.$inits2; // инициалы
           $ldap_mass['postalAddress'] = $cit."".$str."".$hou."".$rum; // почтовый адрес
           $ldap_mass['o'] = $res_org2; // организация
           $ldap_mass['l'] = $res_cit2; // город
           $ldap_mass['mail'] = $mailbase."@".$res_dom2; // почта
           $ldap_mass['title'] = $res_dol2; // должность
           $ldap_mass['ou'] = $res_otd2; // отдел
           $ldap_mass['telephoneNumber'] = $res_phone." ".$res_phone_int; // телефон
           $ldap_mass['mobile'] = $res_phone_mob; // телефон мобильный
           $ldap_mass['street'] = $res_str2; // улица
           $ldap_mass['roomNumber'] = $res_room; // номер комнаты
           $ldap_mass['physicalDeliveryOfficeName'] = $res_room; // номер комнаты

           ldap_add($ds, $dn, $ldap_mass) or die(ldap_error($ds));
           }
           else {
           $dn = "cn=".$res_fam." ".$res_name." ".$res_otch.",".$ldp_dn; // dn: uid=user,ou=people,dc=atomstroyexport,dc=local

           //$ldap_mass['cn'] = $_POST['familiya']." ".$_POST['name']." ".$_POST['otchestvo']; // полное имя, равно отображаемому имени
           $ldap_mass['givenName'] = $_POST['name']." ".$_POST['otchestvo']; //имя
           $ldap_mass['sn'] = $_POST['familiya']; // фамилия
           $ldap_mass['displayName'] = $_POST['familiya']." ".$_POST['name']." ".$_POST['otchestvo']; //отображаемое имя
           $ldap_mass['uid'] = $mailbase; //уникальное имя, я исп чать мыльника
           $ldap_mass['initials'] = $inits1.$inits2; // инициалы
           $ldap_mass['postalAddress'] = $cit."".$str."".$hou."".$rum; // почтовый адрес
           $ldap_mass['o'] = $res_org2; // организация
           $ldap_mass['l'] = $res_cit2; // город
           $ldap_mass['mail'] = $mailbase."@".$res_dom2; // почта
           $ldap_mass['title'] = $res_dol2; // должность
           $ldap_mass['ou'] = $res_otd2; // отдел
           $ldap_mass['telephoneNumber'] = $res_phone." ".$res_phone_int; // телефон
           $ldap_mass['mobile'] = $res_phone_mob; // телефон мобильный
           $ldap_mass['street'] = $res_str2; // улица
           $ldap_mass['roomNumber'] = $res_room; // номер комнаты
           $ldap_mass['physicalDeliveryOfficeName'] = $res_room; // номер комнаты

           ldap_modify($ds, $dn, $ldap_mass);
           if(ldap_error($ds) == "No such object")
           {
           $dn = "cn=".$dn_tmp1.",".$ldp_dn; // dn: uid=user,ou=people,dc=atomstroyexport,dc=local

           $ldap_mass['objectClass'] [0] = "top";
           $ldap_mass['objectClass'] [1] = "person";
           $ldap_mass['objectClass'] [2] = "organizationalPerson";
           $ldap_mass['objectClass'] [3] = "inetOrgPerson";
           $ldap_mass['cn'] = $_POST['familiya']." ".$_POST['name']." ".$_POST['otchestvo']; // полное имя, равно отображаемому имени
           $ldap_mass['givenName'] = $_POST['name']." ".$_POST['otchestvo']; //имя
           $ldap_mass['sn'] = $_POST['familiya']; // фамилия
           $ldap_mass['displayName'] = $_POST['familiya']." ".$_POST['name']." ".$_POST['otchestvo']; //отображаемое имя
           $ldap_mass['uid'] = $mailbase; //уникальное имя, я исп чать мыльника
           $ldap_mass['initials'] = $inits1.$inits2; // инициалы
           $ldap_mass['postalAddress'] = $cit."".$str."".$hou."".$rum; // почтовый адрес
           $ldap_mass['o'] = $res_org2; // организация
           $ldap_mass['l'] = $res_cit2; // город
           $ldap_mass['mail'] = $mailbase."@".$res_dom2; // почта
           $ldap_mass['title'] = $res_dol2; // должность
           $ldap_mass['ou'] = $res_otd2; // отдел
           $ldap_mass['telephoneNumber'] = $res_phone." ".$res_phone_int; // телефон
           $ldap_mass['mobile'] = $res_phone_mob; // телефон мобильный
           $ldap_mass['street'] = $res_str2; // улица
           $ldap_mass['roomNumber'] = $res_room; // номер комнаты
           $ldap_mass['physicalDeliveryOfficeName'] = $res_room; // номер комнаты

           ldap_add($ds, $dn, $ldap_mass) or die(ldap_error($ds));
           }
           ldap_unbind($ds) or die(ldap_error($ds));
           ldap_close($ds) or die(ldap_error($ds));
           }
          }
       } else puterror2($TEXT['error-log']);
  } else puterror2($TEXT['error-user30']);
  require_once "../util/bottom.php";
?>