<?php
  ///////////////////////////////////////////////////
  //2011 (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['menu-btn37'];
  $helppage =  $TEXT['menu-btn37-tit'];

  require_once "../config.php";
  require_once "../util/top.php";
  require_once "../lang/".$lang."/trans.php";
  require_once "util.php";

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

  if(empty($_POST['org_id'])) puterror2($TEXT['error-org10']);
  if(strlen($_POST['org_id'])>11) puterror2($TEXT['error-org1']);
  $_POST['org_id'] = rep($_POST['org_id']);

  if($_POST['old_org'] == "on") $use_org = "tbl_users.org_id=".$_POST['org_id']."";
   else $use_org = "tbl_users.user_id>0";

  $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));

  $ldap_mass = array();

  $query1 = "SELECT
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_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 $use_org";
  $result = mysql_query($query1);
  if(!$result) puterror2($TEXT['error-tbl9']);
  $num_rows = mysql_num_rows($result);
   while($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'];
   if($result3['otdel'] == "") $res_otd = $result3['upravlenie'];
    else $res_otd = $result3['otdel'];
   $res_dol = $result3['dolzhnost'];
   $res_fam = $result3['familiya'];
   $res_name = $result3['name'];
   $res_otch = $result3['otchestvo'];
   if($result3['room'] == "") $res_room = "-";
    else $res_room = $result3['room'];
   if($result3['phone_city'] == "") $res_phone = "+";
     else $res_phone = $result3['phone_city'];
   if($result3['phone_int'] == "-")  $res_phone_int = "";
     else $res_phone_int = $result3['phone_int'];
   if($result3['phone_mob'] == "") $res_phone_mob = "+";
     else $res_phone_mob = $result3['phone_mob'];

   $name_tmp=substr($res_name, 0,2);
   $otch_tmp=substr($res_otch, 0,2);
   $inits1 = strtoupper($name_tmp).".";
   $inits2 = strtoupper($otch_tmp).".";
   $mailbase = strtolower($result3['mail']);
   if($mailbase == "") $mailbase = "no-mail";
     else $mailbase = $mailbase;

   if($res_cit == "" && $res_cit !== "-"){$cit = $TEXT['user-city']." ".$res_cit.", ";}
    else $cit = " ";
   if($res_str == "" && $res_str !== "-"){$str = $TEXT['user-str']." ".$res_str.", ";}
    else $str = " ";
   if($res_hou == "" && $res_hou !== "-"){$hou = $TEXT['user-home']." ".$res_hou.", ";}
    else $hou = " ";
   if($res_room == "" && $res_room !== "-"){$rum = $TEXT['user-room']." ".$res_room;}
    else $rum = " ";

   $dn = "cn=".$res_fam." ".$res_name." ".$res_otch.",".$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'] = $res_fam." ".$res_name." ".$res_otch; // полное имя, равно отображаемому имени
   $ldap_mass['givenName'] = $res_name." ".$res_otch; //имя
   $ldap_mass['sn'] = $res_fam; // фамилия
   $ldap_mass['displayName'] = $res_fam." ".$res_name." ".$res_otch; //отображаемое имя
   $ldap_mass['uid'] = $mailbase; //уникальное имя, я исп чать мыльника
   $ldap_mass['initials'] = $inits1.$inits2; // инициалы
   $ldap_mass['postalAddress'] = $cit.$str.$hou.$rum; // почтовый адрес
   $ldap_mass['o'] = $res_org; // организация
   $ldap_mass['l'] = $res_cit; // город
   $ldap_mass['mail'] = $mailbase."@".$res_dom; // почта
   $ldap_mass['title'] = $res_dol; // должность
   $ldap_mass['ou'] = $res_otd; // отдел
   $ldap_mass['telephoneNumber'] = $res_phone." ".$res_phone_int; // телефон
   $ldap_mass['mobile'] = $res_phone_mob; // телефон мобильный
   $ldap_mass['street'] = $res_str; // улица
   $ldap_mass['roomNumber'] = $res_room; // номер комнаты
   $ldap_mass['physicalDeliveryOfficeName'] = $res_room; // номер комнаты

   ldap_add($ds, $dn, $ldap_mass);
   if(ldap_error($ds) == "Already exists")
    {
      //*************************************
      $what_tmp = $TEXT['global-est']."<br />".$dn."<br />".$TEXT['ldap-export3'];
     //*************************************
     $query3 = mysql_query("INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '".$_SERVER['REMOTE_ADDR']."' , '".$result2['fullname']."' , '".$TEXT['menu-btn37']."' , '".$what_tmp."')");
    }
    $dn = "";
   }
  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['menu-btn37'];
  $what = $TEXT['ldap-export1']." ".$num_rows." ".$TEXT['ldap-export2'];
  $who = $result2['fullname'];
  //*************************************
    $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=../logs/index.php' />
                   </head></html>";
           
           ldap_unbind($ds) or die(ldap_error($ds));
           ldap_close($ds) or die(ldap_error($ds));

       } else puterror2($TEXT['error-log']);
  require_once "../util/bottom.php";
?>