<?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-title2'];
  $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(empty($_POST['dom_id'])) puterror2($TEXT['error-domain5']);
  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(empty($_POST['city_id'])) puterror2($TEXT['error-city5']);
  if(strlen($_POST['str_id'])>11) puterror2($TEXT['error-str6']);
  if(empty($_POST['str_id'])) puterror2($TEXT['error-str5']);
  if(strlen($_POST['house_id'])>11) puterror2($TEXT['error-house6']);
  if(empty($_POST['house_id'])) puterror2($TEXT['error-house5']);
  if(strlen($_POST['room'])>16) puterror2($TEXT['error-user22']);
  if(strlen($_POST['org_id'])>11) puterror2($TEXT['error-org1']);
  if(empty($_POST['org_id'])) puterror2($TEXT['error-org10']);
  if(strlen($_POST['upr_id'])>11) puterror2($TEXT['error-upr2']);
  if(empty($_POST['upr_id'])) puterror2($TEXT['error-upr9']);
  if(strlen($_POST['otd_id'])>11) puterror2($TEXT['error-otd9']);
  if(empty($_POST['otd_id'])) puterror2($TEXT['error-otd2']);
  if(strlen($_POST['dol_id'])>11) puterror2($TEXT['error-post9']);
  if(empty($_POST['dol_id'])) puterror2($TEXT['error-post8']);
  if(strlen($_POST['subscribe'])>8) puterror2($TEXT['error-user24']);
  if(strlen($_POST['visible'])>4) puterror2($TEXT['error-user23']);
  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['subscribe'] = rep($_POST['subscribe']);
  $_POST['visible'] = rep($_POST['visible']);
  $_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);

       if($_POST['visible'] == "on") $visible = "show";
          else $visible = "hide";

       if($_POST['subscribe'] == "on") $subscribe = "scribe";
          else $subscribe = "unscribe";

    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("../".$image)) unlink("../".$image);
            }
            if($_POST['photo_obr'] == '3') {
                 if(!resizeimg_simple($image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
                if(is_file("../".$image)) unlink("../".$image);
            }
             if($_POST['photo_obr'] == '4') {
                 if(!resizeimg($image, $smallimage, 130, 185))
                {
                  puterror2($TEXT['error-gdlib']);
                }
                if(is_file("../".$image)) unlink("../".$image);
            }
             if($_POST['photo_obr'] == '5') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
                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("../".$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("../".$image)) unlink("../".$image);
            }
             if($_POST['photo_obr'] == '8') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
                if(is_file("../".$image)) unlink("../".$image);
            }
             if($_POST['photo_obr'] == '9') {
                 if(!resizeimg($image, $smallimage, 185, 130))
                {
                  puterror2($TEXT['error-gdlib']);
                }
                if(is_file("../".$image)) unlink("../".$image);
	}
     }
      else $image = "";
  $mailbase = strtolower($_POST['mail']);
  if($mailbase == "") $mailbase = "no-mail";
     else $mailbase = $mailbase;

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

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

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

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

  $query_dol2 = "SELECT `dolzhnost` FROM `tbl_dolzhnost` WHERE `dol_id`= '".$_POST['dol_id']."' ";
   $result_dol2 = mysql_query($query_dol2);
     if(!$result_dol2) puterror2($TEXT['error-tbl6']);
      $result_dolzh2 = mysql_fetch_array($result_dol2,MYSQL_ASSOC);

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

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

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

  if(!empty($result_city2['city']) && $result_city2['city'] !== "-"){$cit = $TEXT['user-city']." ".$result_city2['city'].", ";}
   else $cit = " ";

  if(!empty($result_street2['street']) && $result_street2['street'] !== "-"){$str = $TEXT['user-str']." ".$result_street2['street'].", ";}
   else $str = " ";

  if(!empty($result_house2['house']) && $result_house2['house'] !== "-"){$hou = $TEXT['user-home']." ".$result_house2['house'].", ";}
   else $hou = " ";

  if(!empty($_POST['room']) && $_POST['room']!=="-"){$rum = $TEXT['user-room']." ".$_POST['room'];}
   else $rum = " ";

$name_tmp=substr($_POST['name'], 0,2);
$otch_tmp=substr($_POST['otchestvo'], 0,2);
$inits1 = strtoupper($name_tmp).".";
$inits2 = strtoupper($otch_tmp).".";
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'];

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-usr1'];
  $what = $_POST['familiya']." ".$_POST['name']." ".$_POST['otchestvo'];
  $who = $result2['fullname'];
  //*************************************
  $query = "INSERT INTO tbl_users ( `user_id` , `first_b`, `familiya`, `name`, `otchestvo`, `position`, `dom_id`, `mail`, `phone_mob`, `phone_int`, `phone_city`, `dol_id`, `otd_id`, `otd_pos`, `upr_id`, `upr_pos`, `org_id`, `org_pos`, `room`, `house_id`, `str_id`, `city_id`, `photo`, `subscribe`, `visible` )
            VALUES ( NULL , '$first_b', '".$_POST['familiya']."', '".$_POST['name']."', '".$_POST['otchestvo']."', '".$_POST['position']."', '".$_POST['dom_id']."', '$mailbase', '".$_POST['phone_mob']."', '".$_POST['phone_int']."', '".$_POST['phone_city']."', '".$_POST['dol_id']."', '".$_POST['otd_id']."', '$otd_pos', '".$_POST['upr_id']."', '$upr_pos', '".$_POST['org_id']."', '$org_pos', '".$_POST['room']."', '".$_POST['house_id']."', '".$_POST['str_id']."', '".$_POST['city_id']."', '$smallimage', '$subscribe', '$visible') ";

  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'].";add;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-\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_dolzh2['dolzhnost'].";".$_POST['phone_city'].";".$_POST['phone_mob'].";".$_POST['phone_int'].";".$_POST['mail'].";".$result_domain2['domain'].";".$_POST['position'].";add;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-\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'].";add;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-\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_dolzh2['dolzhnost'].";".$_POST['phone_city'].";".$_POST['phone_mob'].";".$_POST['phone_int'].";".$_POST['mail'].";".$result_domain2['domain'].";".$_POST['position'].";add;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-;-\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) {
           if($result_otdel2['otdel'] == "") $res_otd = $result_upravlenie2['upravlenie'];
            else $res_otd = $result_otdel2['otdel'];
           $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=".$_POST['familiya']." ".$_POST['name']." ".$_POST['otchestvo'].",".$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'] = $result_organizaciya2['organizaciya']; // организация
           $ldap_mass['l'] = $result_city2['city']; // город
           $ldap_mass['mail'] = $mailbase."@".$result_domain2['domain']; // почта
           $ldap_mass['title'] = $result_dolzh2['dolzhnost']; // должность
           $ldap_mass['ou'] = $res_otd; // отдел
           $ldap_mass['telephoneNumber'] = $res_phone." ".$res_phone_int; // телефон
           $ldap_mass['mobile'] = $res_phone_mob; // телефон мобильный
           $ldap_mass['street'] = $result_street2['street']; // улица
           $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."')");
           }
           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-user28']);
  require_once "../util/bottom.php";
?>