<?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['pochta-title'];

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

 if($result2['administrator'] == 0) puterror2($TEXT['error-loginform4']);

 if(empty($_POST['tema'])) puterror2($TEXT['pochta-err1']);
 if(strlen($_POST['tema'])>100) puterror2($TEXT['pochta-err2']);
 if(empty($_POST['message'])) puterror2($TEXT['pochta-err3']);
 if(strlen($_POST['message'])>1000) puterror2($TEXT['pochta-err4']);
 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(isset($_POST['tema'])) $tema=trim($_POST['tema']);
    else $tema="";
     if (empty($tema)) puterror2($TEXT['pochta-err1']);

 if(isset($_POST['message'])) $message=trim($_POST['message']);
    else $message="";
     if (empty($message)) puterror2($TEXT['pochta-err3']);

  $_POST['tema'] = rep($_POST['tema']);
  $_POST['message'] = rep($_POST['message']);
  $_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']);

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

  if($_POST['old_upr'] == "on") $upr = "AND tbl_users.upr_id='".$_POST['upr_id']."' ";
    else $upr = "";

  if($_POST['old_otd'] == "on") $otd = "AND tbl_users.otd_id='".$_POST['otd_id']."' ";
    else $otd = "";

  if($_POST['old_dol'] == "on") $dol = "AND tbl_users.dol_id='".$_POST['dol_id']."' ";
    else $dol = "";

  ////////////////////////////////////////////////////////
  // Блок рассылки писем
  ////////////////////////////////////////////////////////
     $query_m = "SELECT 
     tbl_users.mail, 
     tbl_organizaciya.organizaciya, 
     tbl_upravlenie.upravlenie, 
     tbl_otdel.otdel, 
     tbl_domain.domain  
     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_otdel ON tbl_users.otd_id=tbl_otdel.otd_id 
     JOIN tbl_domain ON tbl_users.dom_id=tbl_domain.dom_id 
     WHERE tbl_users.user_id != '' ".$org." ".$upr." ".$otd." ".$dol." ";
     $result_m = mysql_query($query_m);
     if(!$result_m) puterror2($TEXT['error-tbl2']);
	 $num_rows = mysql_num_rows($result_m);
	 if($num_rows>0) {
	  while($result_mail = mysql_fetch_array($result_m))
	    {
             $user_mail = $result_mail['mail'];
             $user_dom = $result_mail['domain'];
             $organizaciya = $result_mail['organizaciya'];
             $upravlenie = $result_mail['upravlenie'];
             $otdel = $result_mail['otdel'];
             $address = $user_mail."@".$user_dom;
             $dat1=date('j');
             $dat2=date('m');
             $dat3=date('Y G:i:s');
             $podpis = $organizaciya.", ".$dat1." ".$month[$dat2]." ".$dat3;
             $tema = $_POST['tema'];
             $msg = "<b>".$_POST['tema']."</b><br /><br />".$_POST['message']."<br /><hr />".$podpis."";

             //Обрабатываем данные
             $podpis = stripslashes($podpis);
             $tema = stripslashes($tema);
             $msg = nl2br(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, version 2.3.5"
             )));
             $addr_log .= $address.", ";
            }
          }
 ///////////////////////////////
 // Конец блока рассылки писем
 ///////////////////////////////

  //*************************************
  if(!empty($org)) $log_org = $TEXT['log-upr5'].": ".$organizaciya;
  if(!empty($upr)) $log_upr = $TEXT['otd-now2'].$upravlenie;
  if(!empty($otd)) $log_otd = $TEXT['otd-now3'].$otdel;
  if(!empty($addr_log)) $log_addresses = $TEXT['bllst-text1']." &rArr; ".$addr_log;

  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-mail1'];
  $what = $TEXT['log-mail2'].$num_rows.$log_org.$log_upr.$log_otd.$log_addresses;
  $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=index.php' />
                   </head></html>";
           } else puterror2($TEXT['error-log']);
  require_once "../util/bottom.php";
?>