<?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-title3'];
  $helppage = "";

  require_once "../config.php";
  require_once "../util/top.php";

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

  if(empty($_POST['user_id'])) puterror2($TEXT['error-user6']);
  if(strlen($_POST['user_id'])>11) puterror2($TEXT['error-user2']);
  $_POST['user_id'] = rep($_POST['user_id']);
  $_POST['del_key'] = rep($_POST['del_key']);

  if($_POST['del_key'] == "on") {
      $del_key = ", `korzina`= 1 ";
      $del_pass = $TEXT['pass-del2']."<br />";
      }
   else {
       $del_key = ", `korzina`= 0 ";
       $del_pass = "";
   }

  //*************************************
  $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.org_id,
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']."'";
  $result3 = mysql_query($query1);
  if(!$result3) puterror2($TEXT['error-tbl2']);
    $result4 = mysql_fetch_array($result3,MYSQL_ASSOC);
     if(is_file("../".$result4['photo'])) unlink("../".$result4['photo']);
     $uid_usr = $result4['mail'];

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-usr2'];
  $what = $result4['familiya']." ".$result4['name']." ".$result4['otchestvo'].$TEXT['log-id'].$_POST['user_id'];
  $who = $result2['fullname'];
  $usr_cn = $result4['familiya']." ".$result4['name']." ".$result4['otchestvo'];

  $query_clr = mysql_query("UPDATE `tbl_pass_keep` SET `usr_id` = '', `data_edt` = NOW(), `login_edt_id` = '".$result2['login_id']."' $del_key WHERE `usr_id` = '".$_POST['user_id']."' ");
  ////////////////////////////////////////////////////////
  // mail
  ////////////////////////////////////////////////////////

  $address = $TEXT['admin-mail'];
  $podpis = $TEXT['admin-podpis']."<br />".$data_podp;
  $tema = $TEXT['log-usr2'];
  $msg = "<b>".$TEXT['log-usr2']."</b>&nbsp;(".$TEXT['search-org']." ".$result4['organizaciya'].")<br /><br />".$what."<br />e-mail: ".$result4['mail']."@".$result4['domain']."<br />".$del_pass."<hr />".$podpis."";

  $podpis = stripslashes($podpis);
  $tema = stripslashes($tema);
  $msg = stripslashes($msg);

  @mail($address, $tema, $msg, join("\r\n", array(
  "From: phonebook@".$_SERVER['SERVER_NAME']."",
  "MIME-Version: 1.0",
  "Content-type: text/html; charset=utf-8",
  "Content-Transfer-Encoding: 8bit",
  "Date: ".date("D, d-M-Y H:i:s", time())."",
  "X-Mailer: Phone Book"
  )));

  ////////////////////////////////////////////////////////
  // end mail
  ////////////////////////////////////////////////////////

  //*************************************
  $query = "DELETE FROM `tbl_users` WHERE `user_id`='".$_POST['user_id']."' LIMIT 1";
  if(mysql_query($query))
  {
   if($result_srv_set['repl'] == 1 && $result4['org_id'] == $result_srv_set['rep_org_id'])
   {
    $exp_path = "../../replica/main-organization";
    if(is_file($exp_path))
     {
     $file = fopen($exp_path,"a");
     $exp_param = $result4['organizaciya'].";".$result4['upravlenie'].";".$result4['otdel'].";".$result4['city'].";".$result4['street'].";".$result4['house'].";".$result4['room'].";".$result4['familiya'].";".$result4['name'].";".$result4['otchestvo'].";".$result4['dolzhnost'].";".$result4['phone_city'].";".$result4['phone_mob'].";".$result4['phone_int'].";".$result4['mail'].";".$result4['domain'].";".$result4['position'].";del;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-\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));
      $exp_param = $result4['organizaciya'].";".$result4['upravlenie'].";".$result4['otdel'].";".$result4['city'].";".$result4['street'].";".$result4['house'].";".$result4['room'].";".$result4['familiya'].";".$result4['name'].";".$result4['otchestvo'].";".$result4['dolzhnost'].";".$result4['phone_city'].";".$result4['phone_mob'].";".$result4['phone_int'].";".$result4['mail'].";".$result4['domain'].";".$result4['position'].";del;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-\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) {
             $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 = "cn=".$usr_cn.",".$ldp_dn; // dn: uid=user,ou=people,dc=atomstroyexport,dc=local

             ldap_delete($ds, $dn) or die(ldap_error($ds));
             if(ldap_error($ds) == "Success")
             {
             //*************************************
             $what_tmp = $TEXT['log-tit6']."<br />".$dn;
             //*************************************
            $query3 = mysql_query("INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '".$_SERVER['REMOTE_ADDR']."' , '".$result2['fullname']."' , '".$TEXT['ldap-delete']."' , '".$what_tmp."')");
            }
             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-user29']);
  require_once "../util/bottom.php";
?>