<?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";
?>
<form action=index.php method=post>
<?php
  $title=$titlepage = $TEXT['ex-title'];
  $helppage = $TEXT['ex-help4']."<br>";

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

?>
  <input type=submit value='<?php echo $TEXT['btn_back']; ?>'>
<?php
  if($result2['user'] == 1) puterror3($TEXT['error-loginform4']);
  if($result2['master'] == 1) puterror3($TEXT['error-loginform4']);
  if($result2['manager'] == 1) puterror3($TEXT['error-loginform4']);

  if(empty($_POST['org_id'])) $_POST['org_id'] = "";
  if(empty($_POST['old_org'])) $_POST['old_org'] = "off";

  if(strlen($_POST['org_id'])>11) puterror3($TEXT['error-org1']);
  if(strlen($_POST['typepar'])>2) puterror3($TEXT['search-err3']);
  if(strlen($_POST['old_org'])>3) puterror3($TEXT['ex-err1']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_POST['typepar'] = rep($_POST['typepar']);
  $_POST['old_org'] = rep($_POST['old_org']);

  if($_POST['old_org'] !== "on") $new_org = "";
    else $new_org = "AND tbl_users.org_id = '".$_POST['org_id']."'";

  $action = "Thunderbird";
    switch($_POST['typepar'])
      {
      case 1:
      $action = "Kmail";
      break;
      case 2:
      $action = "Thunderbird";
      break;
      case 3:
      $action = "MSOutlook";
      break;
      case 4:
      $action = "OutlookExpress";
      break;
      case 5:
      $action = "MailWindows";
      break;
      case 6:
      $action = "TheBat";
      break;
      }

     $exp_path = "../../../NormDoc/".$action."-".date("YMd-H-i-s",time()).".csv";

      $file = fopen($exp_path,"a");

      if($action == "Kmail") 
      $exp_head = $TEXT['ex-kmail-ex1'].",".$TEXT['ex-kmail-ex2'].",".$TEXT['ex-kmail-ex3'].",".$TEXT['ex-kmail-ex10'].",".$TEXT['ex-kmail-ex4'].",".$TEXT['ex-kmail-ex5'].",".$TEXT['ex-kmail-ex11'].",".$TEXT['ex-kmail-ex12'].",".$TEXT['ex-kmail-ex6'].",".$TEXT['ex-kmail-ex7'].",".$TEXT['ex-kmail-ex8'].",".$TEXT['ex-kmail-ex9'].",".$TEXT['ex-kmail-ex14']."\r\n";

      if($action == "Thunderbird") 
      $exp_head = $TEXT['ex-thun-ex10'].",".$TEXT['ex-thun-ex11'].",".$TEXT['ex-thun-ex1'].",".$TEXT['ex-thun-ex2'].",".$TEXT['ex-thun-ex3'].",".$TEXT['ex-thun-ex4'].",".$TEXT['ex-thun-ex5'].",".$TEXT['ex-thun-ex6'].",".$TEXT['ex-thun-ex7'].",".$TEXT['ex-thun-ex8'].",".$TEXT['ex-thun-ex9']."\r\n";

      if($action == "MSOutlook") 
      $exp_head = $TEXT['ex-outl-ex1'].",".$TEXT['ex-outl-ex2'].",".$TEXT['ex-outl-ex3'].",".$TEXT['ex-outl-ex4'].",".$TEXT['ex-outl-ex5'].",".$TEXT['ex-outl-ex6'].",".$TEXT['ex-outl-ex7'].",".$TEXT['ex-outl-ex8'].",".$TEXT['ex-outl-ex9'].",".$TEXT['ex-outl-ex10'].",".$TEXT['ex-outl-ex11'].",".$TEXT['ex-outl-ex12'].",".$TEXT['ex-outl-ex13']."\r\n";

      if($action == "OutlookExpress") 
      $exp_head = $TEXT['ex-ouexp-ex1'].";".$TEXT['ex-ouexp-ex2'].";".$TEXT['ex-ouexp-ex3'].";".$TEXT['ex-ouexp-ex4'].";".$TEXT['ex-ouexp-ex5'].";".$TEXT['ex-ouexp-ex6'].";".$TEXT['ex-ouexp-ex7'].";".$TEXT['ex-ouexp-ex8'].";".$TEXT['ex-ouexp-ex9'].";".$TEXT['ex-ouexp-ex10'].";".$TEXT['ex-ouexp-ex11'].";".$TEXT['ex-ouexp-ex12'].";".$TEXT['ex-ouexp-ex13']."\r\n";

      if($action == "MailWindows") 
      $exp_head = $TEXT['ex-mwin-ex1'].";".$TEXT['ex-mwin-ex2'].";".$TEXT['ex-mwin-ex3'].";".$TEXT['ex-mwin-ex4'].";".$TEXT['ex-mwin-ex5'].";".$TEXT['ex-mwin-ex6'].";".$TEXT['ex-mwin-ex7'].";".$TEXT['ex-mwin-ex8'].";".$TEXT['ex-mwin-ex9'].";".$TEXT['ex-mwin-ex10']."\r\n";

      if($action == "TheBat") 
      $exp_head =  $TEXT['ex-bat-ex1'].",".$TEXT['ex-bat-ex2'].",".$TEXT['ex-bat-ex3'].",".$TEXT['ex-bat-ex4'].",".$TEXT['ex-bat-ex5'].",".$TEXT['ex-bat-ex6'].",".$TEXT['ex-bat-ex7'].",".$TEXT['ex-bat-ex8'].",".$TEXT['ex-bat-ex9'].",".$TEXT['ex-bat-ex10'].",".$TEXT['ex-bat-ex11'].",".$TEXT['ex-bat-ex12'].",".$TEXT['ex-bat-ex13']."\r\n";

      fwrite($file, $exp_head, strlen($exp_head));

     $query = "SELECT tbl_users.name, tbl_users.familiya, tbl_users.otchestvo, tbl_users.phone_mob, tbl_users.phone_city,  tbl_users.phone_int, tbl_users.room, tbl_users.mail, tbl_users.upr_id, tbl_users.house_id, tbl_users.str_id, tbl_users.city_id, tbl_users.org_id, tbl_users.dom_id, tbl_users.dol_id, tbl_organizaciya.org_id, tbl_organizaciya.organizaciya, tbl_upravlenie.upr_id, tbl_upravlenie.upravlenie, tbl_city.city_id, tbl_city.city, tbl_street.str_id, tbl_street.street, tbl_house.house_id, tbl_house.house, tbl_domain.dom_id, tbl_domain.domain, tbl_dolzhnost.dol_id, tbl_dolzhnost.dolzhnost FROM tbl_users
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_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_domain ON tbl_users.dom_id=tbl_domain.dom_id 
JOIN tbl_dolzhnost ON tbl_users.dol_id=tbl_dolzhnost.dol_id 
WHERE tbl_users.visible = 'show' ".$new_org." ORDER BY tbl_users.familiya";
     $result = mysql_query($query);
     if(!$result) puterror3($TEXT['error-tbl10']);
       if(mysql_num_rows($result)>0);
         {
          while($result_exps = mysql_fetch_array($result,MYSQL_ASSOC))
               {

      if($result_exps['house'] == "") $house_tmp = "";
        else $house_tmp = $TEXT['tbl-house']." ".$result_exps['house'];

      if($action == "Kmail") 
	$exp_string = $result_exps['familiya']." ".$result_exps['name']." ".$result_exps['otchestvo'].",".$result_exps['familiya'].",".$result_exps['name'].",".$result_exps['otchestvo'].",".$result_exps['street']." ".$house_tmp.",".$result_exps['city'].",".$result_exps['phone_city']." ".$TEXT['ex-kmail-ex13']."".$result_exps['phone_int'].",".$result_exps['phone_mob'].",".$result_exps['mail']."@".$result_exps['domain'].",".$result_exps['dolzhnost'].",".$result_exps['organizaciya'].",".$result_exps['upravlenie'].",".$result_exps['room']."\r\n";

      if($action == "Thunderbird") 
	$exp_string = $result_exps['name'].",".$result_exps['familiya'].",".$result_exps['familiya']." ".$result_exps['name']." ".$result_exps['otchestvo'].",".$result_exps['mail']."@".$result_exps['domain'].",".$result_exps['phone_city']." ".$TEXT['ex-thun-ex12']."".$result_exps['phone_int'].",".$result_exps['phone_mob'].",".$result_exps['street']." ".$house_tmp." ".$TEXT['usr-field4']." ".$result_exps['room'].",".$result_exps['city'].",".$result_exps['dolzhnost'].", ".$result_exps['upravlenie'].",".$result_exps['organizaciya']."\r\n";

      if($action == "MSOutlook") 
	$exp_string = $result_exps['name'].",".$result_exps['otchestvo'].",".$result_exps['familiya'].",".$result_exps['organizaciya'].",".$result_exps['upravlenie'].",".$result_exps['dolzhnost'].",".$result_exps['street']." ".$house_tmp.",".$result_exps['city'].",".$result_exps['phone_city']." ".$TEXT['ex-outl-ex14']."".$result_exps['phone_int'].",".$result_exps['phone_mob'].",".$result_exps['room'].",".$result_exps['mail']."@".$result_exps['domain'].",".$result_exps['familiya']." ".$result_exps['name']." ".$result_exps['otchestvo']." (".$result_exps['mail']."@".$result_exps['domain'].")\r\n";

      if($action == "OutlookExpress") 
	$exp_string = $result_exps['name'].";".$result_exps['familiya'].";".$result_exps['otchestvo'].";".$result_exps['familiya']." ".$result_exps['name']." ".$result_exps['otchestvo'].";".$result_exps['mail']."@".$result_exps['domain'].";".$result_exps['phone_mob'].";".$result_exps['street']." ".$house_tmp.";".$result_exps['city'].";".$result_exps['phone_city']." ".$TEXT['ex-ouexp-ex14']."".$result_exps['phone_int'].";".$result_exps['organizaciya'].";".$result_exps['dolzhnost'].";".$result_exps['upravlenie'].";".$result_exps['room']."\r\n";

      if($action == "MailWindows") 
	$exp_string = $result_exps['name'].";".$result_exps['familiya'].";".$result_exps['otchestvo'].";".$result_exps['familiya']." ".$result_exps['name']." ".$result_exps['otchestvo'].";".$result_exps['mail']."@".$result_exps['domain'].";".$result_exps['street']." ".$house_tmp." ".$TEXT['usr-field4']." ".$result_exps['room'].";".$result_exps['city'].";".$result_exps['phone_city']." ".$TEXT['ex-mwin-ex11']."".$result_exps['phone_int'].";".$result_exps['organizaciya'].";".$result_exps['dolzhnost']."\r\n";

      if($action == "TheBat") 
	$exp_string =  $result_exps['familiya']." ".$result_exps['name']." ".$result_exps['otchestvo'].",".$result_exps['name'].",".$result_exps['familiya'].",".$result_exps['otchestvo'].",".$result_exps['mail']."@".$result_exps['domain'].",".$result_exps['organizaciya'].",".$result_exps['dolzhnost'].",".$result_exps['phone_mob'].",".$result_exps['phone_city']." ".$TEXT['ex-bat-ex14']."".$result_exps['phone_int'].",".$result_exps['upravlenie'].",".$result_exps['room'].",".$result_exps['street']." ".$house_tmp.",".$result_exps['city']."\r\n";

  	fwrite($file, $exp_string, strlen($exp_string));
 		}
         }
  	fclose($file);

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['ex-log1'];
  $what = $TEXT['ex-log2']."".$action;
  $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=transfer.php?trans=".$exp_path."' />
                   </head></html>";
           } else puterror3($TEXT['error-log']);
?>
</form>
<?php
  require_once "../../util/bottom_exp.php";
?>