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

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

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

  if(empty($_POST['header'])) puterror2($TEXT['error-new1']);
  if(strlen($_POST['header'])>300) puterror2($TEXT['error-new2']);
  if(empty($_POST['message'])) puterror2($TEXT['error-new4']);
  if(strlen($_POST['message'])>60000) puterror2($TEXT['error-new5']);
  if(empty($_POST['position'])) puterror2($TEXT['error-new7']);
  if(strlen($_POST['position'])>11) puterror2($TEXT['error-new8']);
  if(strlen($_POST['description'])>300) puterror2($TEXT['error-new10']);
  if(strlen($_POST['org_id'])>5) puterror2($TEXT['error-org1']);
  if(strlen($_POST['visible'])>4) puterror2($TEXT['error-new11']);
  if(strlen($_POST['url'])>500) puterror2($TEXT['error-new12']);
  if(strlen($_POST['url_text'])>300) puterror2($TEXT['error-new13']);

  if(isset($_POST['header'])) $header=trim($_POST['header']);
    else $header="";
     if (empty($header)) puterror2($TEXT['error-new3']);

  if(isset($_POST['message'])) $new=trim($_POST['message']);
    else $new="";
     if (empty($new)) puterror2($TEXT['error-new6']);

  if(isset($_POST['position'])) $position=trim($_POST['position']);
    else $position="";
     if(empty($position)) puterror2($TEXT['error-new9']);

  $_POST['header'] = rep($_POST['header']);
  $_POST['position'] = rep($_POST['position']);
  $_POST['description'] = rep($_POST['description']);
  $_POST['org_id'] = rep($_POST['org_id']);
  $_POST['visible'] = rep($_POST['visible']);
  $_POST['url'] = rep($_POST['url']);
  $_POST['url_text'] = rep($_POST['url_text']);
  $_POST['message'] = rep2($_POST['message']);

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

            ////////////////////////////////////////////////////////
            // mail
            ////////////////////////////////////////////////////////
                $query5 = "SELECT * FROM `tbl_organizaciya` WHERE `org_id` = '".$_POST['org_id']."'";
                  $result_query5 = mysql_query($query5);
                     if(!$result_query5) puterror2($TEXT['error-tbl7']);
                      {
                       $result_organizaciya = mysql_fetch_array($result_query5);
                       $user_org = $result_organizaciya['organizaciya'];
                      }
                 if($user_org=="-")
                  {
                    $query7 = "SELECT * FROM `tbl_users` WHERE `subscribe` = 'scribe'";
                      $result_query7 = mysql_query($query7);
                       if(!$result_query7) puterror2($TEXT['error-tbl2']);
                       {
                        while($result_mail2 = mysql_fetch_array($result_query7))
                         {
                          $user_mail2 = $result_mail2['mail'];

                          $query8 = "SELECT * FROM `tbl_domain` WHERE `dom_id` = '".$result_mail2['dom_id']."'";
                            $result_query8 = mysql_query($query8);
                             if(!$result_query8) puterror2($TEXT['error-tbl5']);
                             {
                              $result_domain2 = mysql_fetch_array($result_query8);
                              $user_dom2 = $result_domain2['domain'];

                              if ($result_news['url'] == "") $url = "";
                                else $url = "<a href=".$_POST['url']." target=_blank>".$_POST['url_text']."</a>";

                              $organizaciya = $TEXT['new-glob1'];
                              $address = $user_mail2."@".$user_dom2;
                              $podpis = $organizaciya." ".$data_podp;
                              $tema = $TEXT['new-tema'];
                              $msg = "<b>".$_POST['header']."</b><br /><br />".$_POST['new']."<br />".$url."<br /><hr />".$podpis."";

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

                              if($visible == "show")
                              @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"
                              )));
                             }
                         }
                       }
                  }
                  else
                   {
                    $query3 = "SELECT * FROM `tbl_users` WHERE `org_id` = '".$_POST['org_id']."' AND `subscribe` = 'scribe'";
                     $result_query3 = mysql_query($query3);
                       if(!$result_query3) puterror2($TEXT['error-tbl2']);
                        {
                         while($result_mail = mysql_fetch_array($result_query3))
                         {
                          $user_mail = $result_mail['mail'];

                          $query4 = "SELECT * FROM `tbl_domain` WHERE `dom_id` = '".$result_mail['dom_id']."'";
                            $result_query4 = mysql_query($query4);
                              if(!$result_query4) puterror2($TEXT['error-tbl5']);
                               {
                                $result_domain = mysql_fetch_array($result_query4);
                                $user_dom = $result_domain['domain'];

                                if ($result_news['url'] == "") $url = "";
                                else $url = "<a href=".$_POST['url']." target=_blank>".$_POST['url_text']."</a>";

                                $organizaciya = $TEXT['new-glob21'].$user_org.$TEXT['new-glob22'];
                                $address = $user_mail."@".$user_dom;
                                $podpis = $organizaciya." ".$data_podp;
                                $tema = $TEXT['new-tema'];
                                $msg = "<b>".$_POST['header']."</b><br /><br />".$_POST['new']."<br />".$url."<br /><hr />".$podpis."";

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

                                if($visible == "show")
                                @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
            ////////////////////////////////////////////////////////

  //*************************************
  $ip = $_SERVER['REMOTE_ADDR'];
  $doing = $TEXT['log-new1'];
  $what = $_POST['header'];
  $who = $result2['fullname'];
  //*************************************
  $query = "INSERT INTO `tbl_news` ( `new_id` , `org_id`, `header`, `news`, `position`, `description`, `url`, `url_text`, `picture`, `picture_big`, `data`, `visible` ) VALUES ( NULL , '".$_POST['org_id']."', '".$_POST['header']."', '".$_POST['message']."', '".$_POST['position']."', '".$_POST['description']."', '$url', '".$_POST['url_text']."', '$smallimage', '$image', NULL , '$visible') ";
  if(mysql_query($query))
  {
    $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']);
  } else puterror2($TEXT['error-new15']);
  require_once "../util/bottom.php";
?>